Beispiel #1
0
        private LabeledData[] collect_samples(Func <double[], double[]>[] functions, Task task)
        {
            List <LabeledData> ldata = new List <LabeledData>();

            foreach (Func <double[], double[]> func in functions)
            {
                int j = 0;
                while (j < 1)
                {
                    List <double[]> points = get_start_points(j, task, func);

                    for (int k = 0; k < 1; k++)
                    {
                        points.AddRange(testDefWayUntilGood(points.ToArray(), func).ToList <double[]>());
                    }

                    ShepardApprox def_model = new ShepardApprox(config.FunctionDimension, points.ToArray());
                    int[]         count     = new int[config.FunctionDimension];
                    for (int i = 0; i < config.FunctionDimension; i++)
                    {
                        count[i] = (def_model.Min[i] == def_model.Max[i]) ? 1 : NGRID;
                    }
                    Grid grid = new Grid(def_model.N, def_model.M, def_model.Min, def_model.Max, count);
                    Solver.dist = update_path_to_knowing_points(grid, points.ToArray(), config.FunctionDimension);

                    int n = grid.Node.Length;
                    for (int i = 0; i < grid.Node.Length; i++)
                    {
                        double[] cuurentNode = (double[])grid.Node[i].Clone();
                        def_model.Calculate(cuurentNode);

                        double[] approxFunctionVal = new double[def_model.M];
                        for (int k = 0; k < def_model.M; ++k)
                        {
                            approxFunctionVal[k] = cuurentNode[def_model.N + k];
                        }

                        var realFunctionVal = func(grid.Node[i]);

                        double[] diffs = realFunctionVal.Zip(approxFunctionVal, (d1, d2) => Math.Abs(d1 - d2)).ToArray();

                        double err = (diffs.Sum() / diffs.Length);

                        //int pointClass = 0;
                        //if (err > config.Approximation)
                        //{
                        //    pointClass = 1;
                        //}
                        double pointClass = err;

                        double[] features = build_features(grid.Node[i], def_model, grid, Solver.dist, points.ToArray(), i);
                        ldata.Add(new LabeledData(features, pointClass));
                        featureCount = features.Length;
                    }

                    j++;
                }
            }
            return(ldata.ToArray());
        }
Beispiel #2
0
        protected double[][] testDefWayUntilGood(double[][] points, Func <double[], double[]> func)
        {
            int pointAmount = config.PointAmount;

            int    i      = 0;
            double maxErr = 10;
            int    goodPr = 0;

            while (i < 100000000 && maxErr > config.Approximation && goodPr < 50)
            {
                ShepardApprox model    = new ShepardApprox(config.FunctionDimension, points);
                Analyzer      analyzer = new Analyzer(model, points);
                analyzer.do_default_analyse();
                goodPr = analyzer.getGoodPr(func, config.Approximation);
                Console.WriteLine("Good pr " + goodPr);

                double[][] xx = analyzer.Result;
                int        newPointsAmount = Math.Min(config.PredictionPointAmount, xx.Length);
                pointAmount = pointAmount + newPointsAmount;
                points      = getNewPoints(points, analyzer.Result, newPointsAmount, config.FunctionDimension, func);


                double[][] new_points = new double[newPointsAmount][];
                for (int j = 0; j < newPointsAmount; j++)
                {
                    new_points[j] = new double[xx[j].Length];
                    Array.Copy(xx[j], new_points[j], xx[j].Length);
                    model.Calculate(new_points[j]);
                }

                double tempErr = 0;
                for (int k = 0; k < new_points.Length; k++)
                {
                    double err = Math.Abs(points[pointAmount - newPointsAmount + k][config.FunctionDimension] - new_points[k][config.FunctionDimension]);
                    if (err > tempErr)
                    {
                        tempErr = err;
                    }
                }
                maxErr = tempErr;
                i++;
            }
            ShepardApprox model1    = new ShepardApprox(config.FunctionDimension, points);
            Analyzer      analyzer1 = new Analyzer(model1, points);

            analyzer1.do_default_analyse();


            //return analyzer1.getGoodSamples(func, parser.Approximation, goodPr);
            return(points);
        }
Beispiel #3
0
        private double[] build_features(double[] point, IFunction model, Grid grid, double[] distToKnownPoints, double[][] knownPoints = null, int index = -1)
        {
            // на сколько образующая домен точка близка
            // сколько до и после монотонно
            // расстояние до известной точки
            Analyzer analyzer = new Analyzer(model, knownPoints);

            analyzer.do_some_analyse();
            // min, max in locality
            double maxNeighbours = double.MinValue;
            double minNeighbours = double.MaxValue;

            foreach (var neighbour in grid.Neighbours(index))
            {
                double[] calcNeighbour = (double[])grid.Node[neighbour].Clone();
                model.Calculate(calcNeighbour);
                double calcNeighbourVal = calcVal(model.M, model.N, calcNeighbour);

                if (calcNeighbour[calcNeighbour.Length - 1] < minNeighbours)
                {
                    minNeighbours = calcNeighbourVal;
                }
                if (calcNeighbour[calcNeighbour.Length - 1] > maxNeighbours)
                {
                    maxNeighbours = calcNeighbourVal;
                }
            }
            // current val
            double[] curentNode = (double[])grid.Node[index].Clone();
            model.Calculate(curentNode);
            double curentNodeVal = calcVal(model.M, model.N, curentNode);

            if (curentNodeVal < minNeighbours)
            {
                minNeighbours = curentNodeVal;
            }
            if (curentNodeVal > maxNeighbours)
            {
                maxNeighbours = curentNodeVal;
            }

            List <double[]> temp_points = new List <double[]>();

            temp_points = knownPoints.ToList();
            temp_points.RemoveAt(analyzer.Domain(grid.Node[index]));
            ShepardApprox new_model = new ShepardApprox(model.N, temp_points.ToArray());

            double[] old_model_point = (double[])grid.Node[index].Clone();
            model.Calculate(old_model_point);
            double[] new_model_point = (double[])grid.Node[index].Clone();
            new_model.Calculate(new_model_point);

            double err = 0;

            for (int k = 0; k < model.M; ++k)
            {
                double newErr = Math.Abs(old_model_point[model.N + k] - new_model_point[model.N + k]);
                if (newErr > err)
                {
                    err = newErr;
                }
            }
            this.featureCount = 4;
            double[] features = new double[featureCount];
            features[0] = maxNeighbours - curentNodeVal;
            features[1] = curentNodeVal - minNeighbours;
            features[2] = distToKnownPoints[index];
            features[3] = err;

            return(features);
        }
Beispiel #4
0
        private void analyse_all_error()
        {
            //вычисляю экстраполянты
            //Shepard[] sh = new Shepard[xf.Length];
            //MeasuredPoint[] xfMeasured = MeasuredPoint.getArrayFromDouble(xf, N);
            ShepardApprox[] shepardApprox = new ShepardApprox[xf.Length];
            for (int i = 0; i < xf.Length; i++)
            {
                shepardApprox[i] = new ShepardApprox(N, xf, graph[i]);
            }

            //пересчитываю значения в узлах решетки только на границах доменов
            for (int i = 0; i < grid.Node.Length; i++)
            {
                shepardApprox[domain[i]].Calculate(grid.Node[i]);
            }

            //вычисляю ошибку на границах доменов
            error = new double[grid.Node.Length];
            for (int i = 0; i < grid.Node.Length; i++)
            {
                error[i] = 0;
                foreach (var adj in grid.Neighbours(i))
                {
                    double d = distanceF(grid.Node[i], grid.Node[adj]);
                    if (error[i] < d)
                    {
                        error[i] = d;
                    }
                }
            }

            //интерполирую ошибку
            double max = 0;

            for (int i = 0; i < grid.Node.Length; i++)
            {
                int    brd = bordernear[i];
                double err = error[brd];
                error[i] = err * (1 - borderdist[i]);
                if (max < error[i])
                {
                    max = error[i];
                }
            }

            //нормирую ошибку
            if (max > 0)
            {
                for (int i = 0; i < grid.Node.Length; i++)
                {
                    error[i] = error[i] / max;
                }
            }

            int maxcandidates = Math.Min(candidates.Length, 1000);

            //сортировка потенциальных кандидатов
            for (int i = 0; i < maxcandidates - 1; i++)
            {
                for (int j = i + 1; j < candidates.Length; j++)
                {
                    if (error[candidates[i]] < error[candidates[j]])
                    {
                        int temp = candidates[i];
                        candidates[i] = candidates[j];
                        candidates[j] = temp;
                    }
                }
            }

            candidates   = Tools.Sub(candidates, 0, maxcandidates);
            xfcandidates = Tools.Sub(grid.Node, candidates);
            for (int i = 0; i < xfcandidates.Length; i++)
            {
                for (int j = N; j < N + M; j++)
                {
                    xfcandidates[i][j] = 0;
                }
            }
            Console.WriteLine("Calculation process completed successfully");
        }
Beispiel #5
0
        private void analyse_error()
        {
            Console.WriteLine("Calculation of local extrapolants");
            //вычисляю экстраполянты
            //Shepard[] sh = new Shepard[xf.Length];
            //MeasuredPoint[] xfMeasured = MeasuredPoint.getArrayFromDouble(xf, N);
            ShepardApprox[] shepardApprox = new ShepardApprox[xf.Length];
            for (int i = 0; i < xf.Length; i++)
            {
                shepardApprox[i] = new ShepardApprox(N, xf, graph[i]);
            }

            Console.WriteLine("Approximation of the values of the initial function at the domain boundaries");
            //пересчитываю значения в узлах решетки только на границах доменов
            for (int i = 0; i < grid.Node.Length; i++)
            {
                if (borderdist[i] > 0)
                {
                    continue;
                }
                shepardApprox[domain[i]].Calculate(grid.Node[i]);
            }

            Console.WriteLine("Calculation of the approximation error of a function at domain boundaries");
            //вычисляю ошибку на границах доменов
            error = new double[grid.Node.Length];
            for (int i = 0; i < grid.Node.Length; i++)
            {
                if (borderdist[i] > 0)
                {
                    continue;
                }
                error[i] = 0;
                foreach (var adj in grid.Neighbours(i))
                {
                    double d = distanceF(grid.Node[i], grid.Node[adj]);
                    if (error[i] < d)
                    {
                        error[i] = d;
                    }
                }
            }

            Console.WriteLine("Interpolation of approximation error function on domain domains");
            //интерполирую ошибку
            double max = 0;

            for (int i = 0; i < grid.Node.Length; i++)
            {
                int    brd = bordernear[i];
                double err = error[brd];
                error[i] = err * (1 - borderdist[i]);
                if (max < error[i])
                {
                    max = error[i];
                }
            }

            Console.WriteLine("Error Function Normalization");
            //нормирую ошибку
            if (max > 0)
            {
                for (int i = 0; i < grid.Node.Length; i++)
                {
                    error[i] = error[i] / max;
                }
            }

            int maxcandidates = Math.Min(candidates.Length, 1000);

            if (candidates.Length > maxcandidates)
            {
                Console.WriteLine("Preliminary selection of {1} of {0} candidates", candidates.Length, maxcandidates);
            }

            Console.WriteLine("Ordering {0} potential candidates", maxcandidates);
            //сортировка потенциальных кандидатов
            for (int i = 0; i < maxcandidates - 1; i++)
            {
                for (int j = i + 1; j < candidates.Length; j++)
                {
                    if (error[candidates[i]] < error[candidates[j]])
                    {
                        int temp = candidates[i];
                        candidates[i] = candidates[j];
                        candidates[j] = temp;
                    }
                }
            }

            Console.WriteLine("Creating a list of candidates", maxcandidates);
            candidates   = Tools.Sub(candidates, 0, maxcandidates);
            xfcandidates = Tools.Sub(grid.Node, candidates);
            for (int i = 0; i < xfcandidates.Length; i++)
            {
                for (int j = N; j < N + M; j++)
                {
                    xfcandidates[i][j] = 0;
                }
            }
            Console.WriteLine("Calculation process completed successfully");
        }