예제 #1
0
        static void Main(string[] args)
        {
            if (args.Length != 3)
            {
                Console.WriteLine("Missing input parameters.");
                Console.WriteLine("Command line should be in format: Solver.exe <config file> <file with calculated points> <file for output points>");
                //Console.ReadKey();
                return;
            }

            Parser parser = new Parser(args[0], args[1]);
            int    res    = parser.doParse();

            if (res != 0)
            {
                // Console.ReadKey();
                return;
            }


            /*double[][] xf = new double[][]
             * {
             * new double[] { 0, 0, 0 },
             * new double[] { 0, 1, 1 },
             * new double[] { 0, 2, 2 },
             * new double[] { 1, 0, 1 },
             * new double[] { 1, 1, 2 },
             * new double[] { 1, 2, 3 },
             * new double[] { 2, 0, 2 },
             * new double[] { 2, 1, 2 },
             * new double[] { 2, 2, 4 }
             * };*/
            Shepard model = new Shepard(parser.FunctionDimension, parser.Points);

            //double[] x = new double[] { 0.5, 0.5, 0 };
            //model.Calculate(x);
            //Console.WriteLine("f({0}, {1}) = {2}", x[0], x[1], x[2]);

            Analyzer analyzer = new Analyzer(model, parser.Points);

            double[][] xx         = analyzer.Result;
            double[][] new_points = new double[parser.PredictionPointAmount][];
            for (int i = 0; i < parser.PredictionPointAmount; i++)
            {
                new_points[i] = new double[xx[i].Length];
                Array.Copy(xx[i], new_points[i], xx[i].Length);
                model.Calculate(new_points[i]);
                // Console.WriteLine(String.Join(", ", xx[i]));
            }

            Parser.keepSolution(args[2], new_points);

            //Console.ReadKey();
        }
예제 #2
0
        private int testDefWay(string configFile, string pointFile, Func <double[], double> func)
        {
            Parser parser      = new Parser(configFile, pointFile);
            int    pointAmount = parser.PointAmount;

            double[][] points = new double[parser.PointAmount][];
            for (int j = 0; j < parser.PointAmount; j++)
            {
                points[j] = (double[])parser.Points[j].Clone();
            }

            int    i      = 0;
            double maxErr = 10;

            while (i < 100000000 && maxErr > parser.Approximation)
            {
                Shepard model = new Shepard(parser.FunctionDimension, points);
                Console.WriteLine("Max " + String.Join(", ", model.Max) + " Min " + String.Join(", ", model.Min));
                Analyzer analyzer = new Analyzer(model, points);
                analyzer.do_default_analyse();

                double[][] xx = analyzer.Result;
                pointAmount = pointAmount + parser.PredictionPointAmount;
                points      = getNewPoints(points, analyzer.Result, parser.PredictionPointAmount, parser.FunctionDimension, func);


                double[][] new_points = new double[parser.PredictionPointAmount][];
                for (int j = 0; j < parser.PredictionPointAmount; 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 - parser.PredictionPointAmount + k][parser.FunctionDimension] - new_points[k][parser.FunctionDimension]);
                    Console.WriteLine(" \n " + (points[pointAmount - parser.PredictionPointAmount + k][parser.FunctionDimension] - new_points[k][parser.FunctionDimension]) + " " + points[pointAmount - parser.PredictionPointAmount + k][parser.FunctionDimension] + " " + new_points[k][parser.FunctionDimension] + " \n ");
                    if (err > tempErr)
                    {
                        tempErr = err;
                    }
                    Console.WriteLine("f({0}) real val {1} predict val {2} err {3}", String.Join(", ", xx[k]), points[pointAmount - parser.PredictionPointAmount + k][parser.FunctionDimension], new_points[k][parser.FunctionDimension], err);
                }
                maxErr = tempErr;
                i++;
            }
            testResult(parser.FunctionDimension, points, func);
            return(i);
        }
예제 #3
0
        private void testResult(int functionDimension, double[][] points, Func <double[], double> func)
        {
            Console.WriteLine("\n\nTest result approximation");
            Shepard model = new Shepard(functionDimension, points);

            for (int i = 1; i < 10; i += 2)
            {
                double[] a = new double[functionDimension + 1];
                double[] b = new double[functionDimension + 1];
                for (int j = 0; j < functionDimension; j++)
                {
                    a[j] = model.Min[j] + ((model.Max[j] - model.Min[j]) * i / 10);
                    b[j] = a[j];
                }
                model.Calculate(a);
                Console.WriteLine("Vector " + String.Join(", ", b) + " Approximation result " + a[functionDimension] + " real " + func(b) + " error " + Math.Abs(a[functionDimension] - func(b)));
            }
        }
예제 #4
0
        private void analyse_error()
        {
            Console.WriteLine("Вычисление локальных экстраполянтов");
            //вычисляю экстраполянты
            Shepard[] sh = new Shepard[xf.Length];
            for (int i = 0; i < xf.Length; i++)
            {
                sh[i] = new Shepard(N, xf, graph[i]);
            }

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

            Console.WriteLine("Вычисление ошибки аппроксимации функции на границах доменов");
            //вычисляю ошибку на границах доменов
            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("Интерполяция ошибки аппроксимации функции на области доменов");
            //интерполирую ошибку
            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("Нормировка функции ошибки");
            //нормирую ошибку
            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("Предварительный отбор {1} из {0} кандидатов", candidates.Length, maxcandidates);
            }

            Console.WriteLine("Упорядочение {0} потенциальных кандидатов", 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("Формирование списка кандидатов", maxcandidates);
            candidates   = Tools.Sub(candidates, 0, maxcandidates);
            xfcandidates = Tools.Sub(grid.Node, candidates);
            for (int i = 0; i < xfcandidates.Length; i++)
            {
                //func.Calculate(xfcandidates[i]);
                for (int j = N; j < N + M; j++)
                {
                    xfcandidates[i][j] = 0;
                }
            }
            Console.WriteLine("Процесс расчета закончен успешно");
        }
예제 #5
0
        private int testWithRandomForest(string configFileToLearn, string pointFileToLearn, string configFile, string pointFile, Func <double[], double> funcToLearn, Func <double[], double[]> derivativeFuncToLearn, Func <double[], double> func, Func <double[], double[]> derivativeFunc)
        {
            Parser parserToLearn      = new Parser(configFile, pointFile);
            int    pointAmountToLearn = parserToLearn.PointAmount;

            Shepard  model    = new Shepard(parserToLearn.FunctionDimension, parserToLearn.Points);
            Analyzer analyzer = new Analyzer(model, parserToLearn.Points);

            Classifiers.IClassifier cls = analyzer.learn_random_forest_on_grid(funcToLearn, derivativeFuncToLearn, parserToLearn.Approximation);

            Parser parser      = new Parser(configFile, pointFile);
            int    pointAmount = parser.PointAmount;

            double[][] points = new double[parser.PointAmount][];
            for (int j = 0; j < parser.PointAmount; j++)
            {
                points[j] = (double[])parser.Points[j].Clone();
            }

            int    i      = 0;
            double maxErr = 10;

            while (i < 10 && maxErr > parser.Approximation)
            {
                model    = new Shepard(parser.FunctionDimension, points);
                analyzer = new Analyzer(model, points);
                analyzer.do_random_forest_analyse(cls, parser.Approximation, func, derivativeFunc);

                double[][] xx = analyzer.Result;
                pointAmount = pointAmount + parser.PredictionPointAmount;
                double[][] newPoints = new double[pointAmount][];
                for (int j = 0; j < pointAmount; j++)
                {
                    if (j < pointAmount - parser.PredictionPointAmount)
                    {
                        newPoints[j] = (double[])points[j].Clone();
                    }
                    else
                    {
                        newPoints[j] = (double[])xx[j - pointAmount + parser.PredictionPointAmount].Clone();
                        newPoints[j][parser.FunctionDimension] = func(newPoints[j]);
                    }
                }
                points = newPoints;


                double[][] new_points = new double[parser.PredictionPointAmount][];
                for (int j = 0; j < parser.PredictionPointAmount; 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 - parser.PredictionPointAmount + k][parser.FunctionDimension] - new_points[k][parser.FunctionDimension]);
                    Console.WriteLine(" \n " + (points[pointAmount - parser.PredictionPointAmount + k][parser.FunctionDimension] - new_points[k][parser.FunctionDimension]) + " " + points[pointAmount - parser.PredictionPointAmount + k][parser.FunctionDimension] + " " + new_points[k][parser.FunctionDimension] + " \n ");
                    if (err > tempErr)
                    {
                        tempErr = err;
                    }
                    Console.WriteLine("f({0}) real val {1} predict val {2} err {3}", String.Join(", ", xx[k]), points[pointAmount - parser.PredictionPointAmount + k][parser.FunctionDimension], new_points[k][parser.FunctionDimension], err);
                }
                maxErr = tempErr;
                i++;
            }
            testResult(parser.FunctionDimension, points, func);
            return(i);
        }