Пример #1
0
        public void GoldenSection_Maximum_In_0(double a, double b, double eps)
        {
            const double expectedValue = 0;
            var          goldenSection = new GoldenSection(MaximizationFunction);
            var          max           = goldenSection.FindMax(a, b, eps);

            Assert.True(Math.Abs(expectedValue - (max.LeftBound - max.RightBound) / 2) <= eps);
        }
Пример #2
0
        /// <summary>
        /// Finds minimum of given function using the Gradient Descent algorithm.
        /// </summary>
        /// <param name="function">Function used in algorithm.</param>
        /// <param name="e">Precision.</param>
        /// <param name="startingPoint">Starting point of algorithm.</param>
        /// <param name="usingGoldenSection">Flag used for tracking whether user wants to use Golden Section algorithm or not.</param>
        /// <returns>Minimum of function.</returns>

        public static Matrica FindMinimum(Function function, double e, Matrica startingPoint, bool usingGoldenSection)
        {
            Matrica x = new Matrica();

            x.Equals(startingPoint);
            function.SetParametersSize(x.NoOfRows);
            Matrica v      = new Matrica();
            int     brojac = 0;
            Matrica xs     = new Matrica();

            if (usingGoldenSection == false)
            {
                do
                {
                    xs.Equals(x);
                    v = function.CalculateGradientValue(x.VectorToArray());
                    function.IncreaseCounterGradientValue(algorithmName);
                    v.MultiplyByScalar(-1);
                    x.AddValue(v);
                    brojac++;
                    function.IncreaseCounterValue(algorithmName);
                    function.IncreaseCounterValue(algorithmName);
                } while (brojac < 10000);
            }
            else
            {
                do
                {
                    xs.Equals(x);
                    v = function.CalculateGradientValue(x.VectorToArray());
                    function.IncreaseCounterGradientValue(algorithmName);
                    v.MultiplyByScalar(-1);
                    v.MultiplyByScalar(1.0 / function.GetGradientNorm());
                    for (int i = 0; i < x.NoOfRows; i++)
                    {
                        double     value       = x.LoadedMatrix[i][0];
                        double     vectorValue = v.LoadedMatrix[i][0];
                        Expression expression  = lambda => value + lambda * vectorValue;
                        function.SetParameters(i, expression);
                    }

                    UnimodalInterval interval       = UnimodalInterval.FindUnimodalInterval(function, 0, 1, algorithmName);
                    UnimodalInterval lambdaInterval = GoldenSection.FindMinimum(function, interval.Minimum, interval.Maximum, e, algorithmName);

                    double lambdaMin = (lambdaInterval.Minimum + lambdaInterval.Maximum) / 2;
                    v.MultiplyByScalar(lambdaMin);
                    x.AddValue(v);

                    function.IncreaseCounterValue(algorithmName);
                    function.IncreaseCounterValue(algorithmName);
                } while (function.GetGradientNorm() >= e);
            }

            function.DeleteParameters();
            return(x);
        }
Пример #3
0
        public bool Match(List <Vector2> positions, float threshold, float minimalScore, float minSize)
        {
            if (positions.Count < samplesCount)
            {
                return(false);
            }

            if (!positions.IsLargeEnough(minSize))
            {
                return(false);
            }

            List <Vector2> locals = GoldenSection.Pack(positions, samplesCount);

            float score = GoldenSection.Search(locals, points, -MathHelper.PiOver4, MathHelper.PiOver4, threshold);

            return(score > minimalScore);
        }
Пример #4
0
        static void Main(string[] args)
        {
            const double eps = 0.001;

            Console.WriteLine("Установление первоначальных границ");
            var boundFoundary = new UnconditionalOptimization(MaximizationFunction);

            boundFoundary.OnIteration           += BoundFoundary_OnIteration;
            var(startLeftBound, startRightBound) = boundFoundary.GetBoundForMaximization(5, .5);
            DisplayResult((startLeftBound, startRightBound), boundFoundary.Iterations, eps);

            Console.WriteLine("Уточнение границ методом Золотого сечения");
            var goldenSection = new GoldenSection(MaximizationFunction);

            goldenSection.OnIteration += BoundFoundary_OnIteration;
            var goldenSectionBounds = goldenSection.FindMax(startLeftBound, startRightBound, eps);

            DisplayResult(goldenSectionBounds, goldenSection.Iterations, eps);

            Console.WriteLine("Квадратичная апроксимация");
            var quadraticApproximation = new QuadraticApproximation(MaximizationFunction);

            quadraticApproximation.OnIteration += BoundFoundary_OnIteration;
            var quadApproxBounds = quadraticApproximation.Calculate(startLeftBound, startRightBound, eps);

            DisplayResult(quadApproxBounds, quadraticApproximation.Iterations, eps);

            Console.WriteLine("Метод Ньютона");
            var newtonsMethod = new NewtonsMethod(MaximizationFunctionDerivative1, MaximizationFunctionDerivative2);
            var rootNewtoon   = newtonsMethod.Newton(-9, eps);

            Console.WriteLine($"Корень {rootNewtoon}");
            Console.WriteLine();

            Console.ReadLine();
        }
Пример #5
0
 public void CloseAndPrepare()
 {
     points = GoldenSection.Pack(points, samplesCount);
 }
Пример #6
0
        public void ExecuteMethod(
            string nameMethod,
            double param1,
            double param2,
            double param3,
            double param4,
            string testFunction,
            int rangeArray,
            double[] LinSysMasA,
            double[,] LinSysMatrixB,
            double[] massX,
            double[] massF,
            double[] massW,
            double pointInterpolation,
            double[,]  MatrixAlgebraA,
            double pointPercentile,
            double pointGenerator)
        {
            TestFunction = testFunction;
            switch (nameMethod)
            {
            //*** Approximate decision of equalization f(x)=0 ***
            case "Bisection Method":
            {
                Bisection bisect = new Bisection(new FunctionOne(TestFunBisection), param1, param2, param3);
                result = "\n Result of the program: \n" +
                         "    x= " + string.Format("{0:f" + precision + "}", bisect.Result);
            }
            break;

            case "Chord Method":
            {
                Сhord chord = new Сhord(new FunctionOne(TestFunNewton), new FunctionOne(TestFunNewton2), param1, param2, param3, param4);
                result = "\n Result of the program: \n" +
                         "   x= " + string.Format("{0:f" + precision + "}", chord.GetSolution());
            }
            break;

            case "Iteration Method":
            {
                IterationMethod itermet = new IterationMethod(new FunctionOne(TestFunIteration), param1, param2, param3, param4);
                result = "\n Result of the program: \n" +
                         "   x= " + string.Format("{0:f" + precision + "}", itermet.GetSolution());
            }
            break;

            case "Newton Method":
            {
                Newton newton = new Newton(new FunctionOne(TestFunNewton), new FunctionOne(TestFunNewton2), param1, param2, param3, param4);
                result = "\n Result of the program: \n" +
                         "   x= " + string.Format("{0:f" + precision + "}", newton.GetSolution());
            }
            break;

            //*** Differential Equations ***
            case "Euler Simple":
            {
                EulerSimple eulersimpl        = new EulerSimple(new Function(TestFunDifferEquations), param1, param2, param3, Convert.ToInt32(param4));
                var         result_eulersimpl = eulersimpl.Result;
                for (int j = 0; j < Convert.ToInt32(param4); j++)
                {
                    result = result + string.Format("{0:f" + precision + "}", result_eulersimpl[0, j])
                             + " : "
                             + string.Format("{0:f" + precision + "}", result_eulersimpl[1, j]) + "\n";
                }
            }
            break;

            case "Euler Modified":
            {
                EulerModified eulerModif        = new EulerModified(new Function(TestFunDifferEquations), param1, param2, param3, Convert.ToInt32(param4));
                var           result_eulerModif = eulerModif.Result;
                for (int j = 0; j < Convert.ToInt32(param4); j++)
                {
                    result = result + string.Format("{0:f" + precision + "}", result_eulerModif[0, j])
                             + " : " + string.Format("{0:f" + precision + "}", result_eulerModif[1, j]) + "\n";
                    // result = result + "fun=\n";
                    // result = result +Convert.ToString( TestFunDifferEquations);
                }
            }
            break;

            case "Euler Corrected":
            {
                EulerCorrected eulerCorrect        = new EulerCorrected(new Function(TestFunDifferEquations), param1, param2, param3, Convert.ToInt32(param4));
                var            result_eulerCorrect = eulerCorrect.Result;
                for (int j = 0; j < Convert.ToInt32(param4); j++)
                {
                    result = result + string.Format("{0:f" + precision + "}", result_eulerCorrect[0, j])
                             + " : " + string.Format("{0:f" + precision + "}", result_eulerCorrect[1, j]) + "\n";
                }
            }
            break;

            case "Runge-Kutta4":
            {
                RungeKutta4 rungeKutta4        = new RungeKutta4(new Function(TestFunDifferEquations), param1, param2, param3, Convert.ToInt32(param4));
                var         result_rungeKutta4 = rungeKutta4.Result;
                for (int j = 0; j < Convert.ToInt32(param4); j++)
                {
                    result = result + string.Format("{0:f" + precision + "}", result_rungeKutta4[0, j])
                             + " : " + string.Format("{0:f" + precision + "}", result_rungeKutta4[1, j]) + "\n";
                }
            }
            break;

            //*** Integration ***
            case "Chebishev":
                Chebishev chebish        = new Chebishev(new FunctionOne(TestFunInteger), param1, param2, Convert.ToInt32(param3));
                var       result_chebish = chebish.GetSolution();

                for (int j = 0; j <= Convert.ToInt32(param3); j++)
                {
                    result = result + "\n   h = " + string.Format("{0:f" + precision + "}", result_chebish[1, j]) + "   \t   integral = "
                             + string.Format("{0:f" + precision + "}", result_chebish[0, j]);
                }
                break;

            case "Simpson":
                Simpson simps        = new Simpson(new FunctionOne(TestFunInteger), param1, param2, Convert.ToInt32(param3));
                var     result_simps = simps.GetSolution();
                for (int j = 0; j <= Convert.ToInt32(param3); j++)
                {
                    result = result + "\n   h = " + string.Format("{0:f" + precision + "}", result_simps[1, j]) +
                             " \t   integral = " + string.Format("{0:f" + precision + "}", result_simps[0, j]);
                }
                break;

            case "Simpson2":
                Simpson2 simps2        = new Simpson2(new FunctionOne(TestFunInteger), param1, param2, Convert.ToInt32(param3));
                var      result_simps2 = simps2.GetSolution();
                result = "\n\n  integral = " + string.Format("{0:f" + precision + "}", result_simps2);
                break;

            case "Trapezium":
                Trapezium trapez        = new Trapezium(new FunctionOne(TestFunInteger), param1, param2, Convert.ToInt32(param3));
                var       result_trapez = trapez.GetSolution();
                for (int j = 0; j <= Convert.ToInt32(param3); j++)
                {
                    result = result + "\n   h = " + string.Format("{0:f" + precision + "}", result_trapez[1, j])
                             + " \t   integral = " + string.Format("{0:f" + precision + "}", result_trapez[0, j]);
                }
                break;

            //*** Non Linear equalization ***
            case "Half Division":
                HalfDivision halfdiv = new HalfDivision(new FunctionOne(TestFunNonLinearEquations), param1, param2, param3);
                result = "\n    X = " + string.Format("{0:f" + precision + "}", halfdiv.GetSolution()[0, 0])
                         + "       Iterations = " + string.Format("{0:f" + precision + "}", halfdiv.GetSolution()[1, 0]);
                break;

            case "Hord Metod":
                HordMetod hormet = new HordMetod(new FunctionOne(TestFunNonLinearEquations), param1, param2, Convert.ToInt32(param3));
                result = "\n    X = " + string.Format("{0:f" + precision + "}", hormet.GetSolution()[0, 0])
                         + "       Iterations = " + string.Format("{0:f" + precision + "}", hormet.GetSolution()[1, 0]);
                break;

            case "Newton Metod":
                NewtonMethod newt = new NewtonMethod(new FunctionOne(TestFunNonLinearEquations), param1, param2);
                result = "\n     X = " + string.Format("{0:f" + precision + "}", newt.GetSolution()[0, 0])
                         + "       Iterations = " + string.Format("{0:f" + precision + "}", newt.GetSolution()[1, 0]);
                break;

            case "Secant Metod":
                Secant sec = new Secant(new FunctionOne(TestFunNonLinearEquations), param1, param2);
                result = "\n     X = " + string.Format("{0:f" + precision + "}", sec.GetSolution()[0, 0])
                         + "       Iterations = " + string.Format("{0:f" + precision + "}", sec.GetSolution()[1, 0]);
                break;

            default:
                result = "";
                break;

            //*** Linear Systems ***
            case "Gaus":
                double[,] LinSysMatrix;
                LinSysMatrix = new double[100, 100];
                for (int l = 0; l < rangeArray; l++)
                {
                    for (int j = 0; j < rangeArray; j++)
                    {
                        LinSysMatrix[l, j] = LinSysMatrixB[l, j];
                    }
                    LinSysMatrix[l, rangeArray] = LinSysMasA[l];
                }
                Gaus gaus        = new Gaus(4, LinSysMatrix);
                var  result_gaus = gaus.GetSolution();
                result = "";
                for (int j = 0; j < result_gaus.Length; j++)
                {
                    result = result + "\n         X" + j + "  =  "
                             + string.Format("{0:f" + precision + "}", result_gaus[j]) + ";";
                }
                break;

            case "Zeidel":
                Zeidel zeidel        = new Zeidel(4, LinSysMatrixB, LinSysMasA);
                var    result_zeidel = zeidel.GetSolution();
                result = "";
                for (int j = 0; j < result_zeidel.Length; j++)
                {
                    result = result + "\n         X" + j + "  =  "
                             + string.Format("{0:f" + precision + "}", result_zeidel[j]) + ";";
                }
                break;

            //*** Interpolation ***
            case "Lagrange Interpolator":

                LagrangeInterpolator lagran = new LagrangeInterpolator(massX, massF, 6, pointInterpolation);
                result = "\n    A value interpolation polynomial is in the point of interpolation.";
                result = result + "\n\n    P = " + string.Format("{0:f" + precision + "}", lagran.GetSolution());
                break;

            case "Newton Interpolator":

                NewtonInterpolator newinterpol = new NewtonInterpolator(massX, massF, 6, pointInterpolation);
                result = "\n    A value interpolation polynomial is in the point of interpolation.";
                result = result + "\n\n    P = " + string.Format("{0:f" + precision + "}", newinterpol.GetSolution());
                break;

            case "Neville Interpolator":

                NevilleInterpolator newill = new NevilleInterpolator(massX, massF, 6, pointInterpolation);
                result = "\n    A value interpolation polynomial is in the point of interpolation.";
                result = result + "\n\n    P = " + string.Format("{0:f" + precision + "}", newill.GetSolution());
                break;

            case "Spline Interpolator":
                SplineInterpolator spline = new SplineInterpolator(massX, massF, 6, pointInterpolation);
                result = "\n    A value interpolation polynomial is in the point of interpolation.";
                result = result + "\n\n    P = " + string.Format("{0:f" + precision + "}", spline.GetSolution());
                break;

            case "Barycentric Interpolator":

                BarycentricInterpolation barycen = new BarycentricInterpolation(massX, massF, massW, 6, pointInterpolation);
                result = "\n    A value interpolation polynomial is in the point of interpolation.";
                result = result + "\n\n    P = " + string.Format("{0:f" + precision + "}", barycen.GetSolution());
                break;

            //*** Matrix Algebra ***
            case "Matrix Determinant":
                MatrixDeterminant matrdet = new MatrixDeterminant();
                result = " \n\n\n  Determinant =  " + string.Format("{0:f" + precision + "}", matrdet.MatrixDet(MatrixAlgebraA, rangeArray)) + ";\n";
                break;

            case "RMatrix LU":

                MatrixLU matrlu         = new MatrixLU(MatrixAlgebraA, rangeArray, rangeArray);
                var      result_matrlu  = matrlu.GetSolution();
                var      result_matrlu2 = matrlu.GetSolution2();
                result = "A:\n";
                for (int ii = 0; ii < rangeArray; ii++)
                {
                    for (int j = 0; j < rangeArray; j++)
                    {
                        result = result + "  \t " + string.Format("{0:f" + precision + "}", result_matrlu[ii, j]);
                    }
                    result = result + " \n";
                }
                result = result + "\nL:\n";
                for (int ii = 0; ii < rangeArray; ii++)
                {
                    result = result + "      " + string.Format("{0:f" + precision + "}", result_matrlu2[ii]);
                }
                result = result + " \n ";
                break;

            case "Matrix Inverse LU":
                RMatrixLuInverse matrluinv = new RMatrixLuInverse();

                MatrixLU matrlu2 = new MatrixLU(MatrixAlgebraA, rangeArray, rangeArray);
                if (matrluinv.rmatrixluinverse(MatrixAlgebraA, rangeArray, matrlu2.GetSolution2()) == true)
                {
                    result = "\n             An inverse matrix exists \n\n ";
                    var result_matrluinv = matrluinv.GetSolution();
                    for (int ii = 0; ii < rangeArray; ii++)
                    {
                        for (int j = 0; j < rangeArray; j++)
                        {
                            result = result + "    \t" + string.Format("{0:f" + precision + "}", result_matrluinv[ii, j]);
                        }
                        result = result + "\n\n";
                    }
                }
                else
                {
                    result = "\n             An inverse matrix does not exist";
                }

                break;

            //*** Optimizing***
            case "Brentopt":
                Brentopt brent = new Brentopt(new FunctionOne(TestFunOptimizing), param1, param2, param3);
                result = "\n    Point of the found minimum :";
                result = result + "\n\n    XMin = " + string.Format("{0:f" + precision + "}", brent.GetSolution());
                result = result + "\n\n     A value of function is in the found minimum :";
                result = result + "\n\n    F(XMin) = " + string.Format("{0:f" + precision + "}", brent.GetSolutionFunction());
                break;

            case "Golden Section":
                GoldenSection godsection = new GoldenSection(new FunctionOne(TestFunOptimizing), param1, param2, Convert.ToInt32(param3));
                result = "\n    Scopes   of segment  which a decision of task is on .";
                result = result + "\n\n    a = " + string.Format("{0:f" + precision + "}", godsection.GetSolutionA());
                result = result + "\n\n    b = " + string.Format("{0:f" + precision + "}", godsection.GetSolutionB());
                break;

            case "Pijavsky":
                Pijavsky pijavsky = new Pijavsky(new FunctionOne(TestFunOptimizing), param1, param2, param3, Convert.ToInt32(param4));
                result = "\n    Abscissa of the best point from found..";
                result = result + "\n\n    F = " + string.Format("{0:f" + precision + "}", pijavsky.GetSolution());
                break;

            //*** Statistics ***
            case "Correlation Pearson":


                CorrelationPearson corelperson = new CorrelationPearson(massX, massF, 6);
                result = "\n    Pearson product-moment correlation coefficient.";
                result = result + "\n\n    K = " + string.Format("{0:f" + precision + "}", corelperson.GetSolution());
                break;

            case "Correlation Spearmans Rank":
                CorrelationSpearmansRank corelspear = new CorrelationSpearmansRank(massX, massF, 6);
                result = "\n    Pearson product-moment correlation coefficient.";
                result = result + "\n\n    K = " + string.Format("{0:f" + precision + "}", corelspear.GetSolution());
                break;

            case "Descriptive Statistics Median":
                DescriptiveStatisticsADevMedian desceripM = new DescriptiveStatisticsADevMedian(massX, 6);
                result = "\n    Output parameters:";
                result = result + "\n\n    M = " + string.Format("{0:f" + precision + "}", desceripM.GetSolution());
                break;

            case "Descriptive Statistics Moments":
                DescriptiveStatisticsMoments desceripMo = new DescriptiveStatisticsMoments(massX, 6);
                result = "\n    Output parameters:";
                result = result + "\n\n    M = " + string.Format("{0:f" + precision + "}", desceripMo.GetSolution());
                result = result + "\n\n    Variance = " + string.Format("{0:f" + precision + "}", desceripMo.variance);
                result = result + "\n\n    Skewness = " + string.Format("{0:f" + precision + "}", desceripMo.skewness) + " (if variance<>0; zero otherwise)";
                result = result + "\n\n    Kurtosis = " + string.Format("{0:f" + precision + "}", desceripMo.kurtosis) + " (if variance<>0; zero otherwise)";
                break;

            case "Descriptive Statistics Percentile":
                DescriptiveStatisticsPercentile desceripP = new DescriptiveStatisticsPercentile(massX, 6, pointPercentile);
                result = "\n    Output parameters:";
                result = result + "\n\n    V = " + string.Format("{0:f" + precision + "}", desceripP.GetSolution());
                break;

            case "Generator 1":
                RandomGeneratorsMethod1 random1 = new RandomGeneratorsMethod1();
                result = "\n    Output parameters:";
                result = result + "\n\n    Random = " + string.Format("{0:f" + precision + "}", random1.GetSolution());
                break;

            case "Generator 2":
                RandomGeneratorsMethod2 random2 = new RandomGeneratorsMethod2(Convert.ToInt32(pointGenerator));
                result = "\n    Output parameters:";
                result = result + "\n\n    Random = " + string.Format("{0:f" + precision + "}", random2.GetSolution());
                break;

            case "Generator 3":
                RandomGeneratorsMethod3 random3 = new RandomGeneratorsMethod3();
                result = "\n    Output parameters:";
                result = result + "\n\n    Random = " + string.Format("{0:f" + precision + "}", random3.GetSolution());
                break;

            case "Generator 4":
                RandomGeneratorsMethod4 random4 = new RandomGeneratorsMethod4();
                result = "\n    Output parameters:";
                result = result + "\n\n    Random = " + string.Format("{0:f" + precision + "}", random4.GetSolution());
                break;

            case "Generator 5":
                RandomGeneratorsMethod5 random5 = new RandomGeneratorsMethod5(pointGenerator);
                result = "\n    Output parameters:";
                result = result + "\n\n    Random = " + string.Format("{0:f" + precision + "}", random5.GetSolution());
                break;
            }
        }
        private static void Main()
        {
            Parameters.Verbosity = VerbosityLevels.AboveNormal;
            // this next line is to set the Debug statements from OOOT to the Console.
            Debug.Listeners.Add(new TextWriterTraceListener(Console.Out));

            /* In this example, we first present how the details of an optimzation
             * problem can be saved to an XML-file so that it can be read in
             * and solved as opposed to defining all the details in an imperative
             * (code line by code line) way. In the first function, the xml file
             * name "test1.xml" is created. */
            makeAndSaveProblemDefinition();

            /* now we create a series of different optimization methods and test
             * them on the problem. The problem is now opened from the file and
             * the details are stored in an object of class "Problem Definition".*/
            var stream = new FileStream(filename, FileMode.Open);


            ProblemDefinition probTest1 = ProblemDefinition.OpenprobFromXml(stream);
            abstractOptMethod opty;


            /***********Gradient Based Optimization with Steepest Descent****************/
            SearchIO.output("***********Gradient Based Optimization with Steepest Descent****************");
            opty = new GradientBasedOptimization();
            opty.Add(probTest1);
            abstractSearchDirection searchDirMethod = new SteepestDescent();

            opty.Add(searchDirMethod);
            //abstractLineSearch lineSearchMethod = new ArithmeticMean(0.0001, 1, 100);
            //abstractLineSearch lineSearchMethod = new DSCPowell(0.0001, 1, 100);
            abstractLineSearch lineSearchMethod = new GoldenSection(0.0001, 1);

            opty.Add(lineSearchMethod);
            opty.Add(new squaredExteriorPenalty(opty, 10));
            /* since this is not a population-based optimization method, we need to remove the MaxSpan criteria. */
            opty.ConvergenceMethods.RemoveAll(a => a is MaxSpanInPopulationConvergence);

            double[] xStar;
            var      timer = Stopwatch.StartNew();
            var      fStar = opty.Run(out xStar);

            printResults(opty, xStar, fStar, timer);

            /***********Gradient Based Optimization with Fletcher-Reeves****************/
            SearchIO.output("***********Gradient Based Optimization with Fletcher-Reeves****************");

            /* we don't need to reset (invoke the constructor) for GradientBasedOptimization since we are only
             * change the seaach direction method. */
            searchDirMethod = new FletcherReevesDirection();
            /* you could also try the remaining 3 search direction methods. */
            //searchDirMethod = new CyclicCoordinates();
            //searchDirMethod = new BFGSDirection();
            //searchDirMethod = new PowellMethod(0.001, 6);
            opty.Add(searchDirMethod);

            timer = Stopwatch.StartNew();
            opty.ResetFunctionEvaluationDatabase();
            fStar = opty.Run(out xStar);
            printResults(opty, xStar, fStar, timer);
            /******************Generalized Reduced Gradient***********************/
            SearchIO.output("******************Generalized Reduced Gradient***********************");
            opty = new GeneralizedReducedGradientActiveSet();
            opty.Add(probTest1);
            opty.Add(new squaredExteriorPenalty(opty, 10));
            opty.ConvergenceMethods.RemoveAll(a => a is MaxSpanInPopulationConvergence);

            timer = Stopwatch.StartNew();
            fStar = opty.Run(out xStar);
            printResults(opty, xStar, fStar, timer);

            /* GRG is the ONLY one here that handles constraints explicity. It find the
             * optimal very quickly and accurately. However, many of the other show a
             * better value of f*, this is because they are using an imperfect penalty
             * function (new squaredExteriorPenalty(opty, 10)). While it seems that GRG
             * includes it as well, it is only used in the the line search method. */


            /******************Random Hill Climbing ***********************/
            SearchIO.output("******************Random Hill Climbing ***********************");
            opty = new HillClimbing();
            opty.Add(probTest1);
            opty.Add(new squaredExteriorPenalty(opty, 8));
            opty.Add(new RandomNeighborGenerator(probTest1.SpaceDescriptor));
            opty.Add(new KeepSingleBest(optimize.minimize));
            opty.ConvergenceMethods.RemoveAll(a => a is MaxSpanInPopulationConvergence);

            /* the deltaX convergence needs to be removed as well, since RHC will end many iterations
             * at the same point it started. */
            opty.ConvergenceMethods.RemoveAll(a => a is DeltaXConvergence);

            timer = Stopwatch.StartNew();
            fStar = opty.Run(out xStar);
            printResults(opty, xStar, fStar, timer);



            /******************Exhaustive Hill Climbing ***********************/
            SearchIO.output("******************Exhaustive Hill Climbing ***********************");
            /* Everything else about the Random Hill Climbing stays the same. */
            opty.Add(new ExhaustiveNeighborGenerator(probTest1.SpaceDescriptor));

            timer = Stopwatch.StartNew();
            fStar = opty.Run(out xStar);
            printResults(opty, xStar, fStar, timer);



            /******************Simulated Annealing***********************/
            SearchIO.output("******************Simulated Annealing***********************");
            opty = new SimulatedAnnealing(optimize.minimize);
            opty.Add(probTest1);
            opty.Add(new squaredExteriorPenalty(opty, 10));
            opty.Add(new RandomNeighborGenerator(probTest1.SpaceDescriptor, 100));
            opty.Add(new SACoolingSangiovanniVincentelli(100));
            opty.ConvergenceMethods.RemoveAll(a => a is MaxSpanInPopulationConvergence);

            /* the deltaX convergence needs to be removed as well, since RHC will end many iterations
             * at the same point it started. */
            opty.ConvergenceMethods.RemoveAll(a => a is DeltaXConvergence);


            timer = Stopwatch.StartNew();
            fStar = opty.Run(out xStar);
            printResults(opty, xStar, fStar, timer);


            /******************Exhaustive Search ***********************/
            // SearchIO.output("******************Exhaustive Search ***********************");
            //opty = new ExhaustiveSearch(probTest1.SpaceDescriptor, optimize.minimize);
            //opty.Add(probTest1);

            /* No convergence criteria is needed as the process concludes when all
             * states have been visited but for this problem that is 4 trillion states.*/
            //opty.ConvergenceMethods.Clear();
            /* if you DID KNOW the best, you can include a criteria like...*/
            //opty.ConvergenceMethods.Add(new ToKnownBestXConvergence(new[] { 3.0, 3.0 }, 0.0000001));
            //timer = Stopwatch.StartNew();
            //fStar = opty.Run(out xStar);

            /* you probably will never see this process complete. Even with the added
             * convergence criteria (which is not factored into the estimated time of
             * completion), you are probably looking at 1 to 2 years. */
            //printResults(opty, xStar, fStar, timer);
        }
Пример #8
0
        /// <summary>
        /// Finds minimum of function using Newton-Raphson algorithm.
        /// </summary>
        /// <param name="function">Function used in algorithm.</param>
        /// <param name="e">Precision</param>
        /// <param name="startingPoint">Starting point of algorithm.</param>
        /// <param name="usingGoldenSection">Flag used for tracking whether user wants to use Golden Section algorithm or not.</param>
        /// <returns>Minimum of function.</returns>

        public static Matrica FindMinimum(Function function, double e, Matrica startingPoint, bool usingGoldenSection)
        {
            Matrica x = new Matrica();

            x.Equals(startingPoint);
            function.SetParametersSize(x.NoOfRows);
            Matrica gradient      = new Matrica();
            Matrica hessianMatrix = new Matrica();
            int     brojac        = 0;
            Matrica xs            = new Matrica();

            if (usingGoldenSection == false)
            {
                do
                {
                    xs.Equals(x);
                    gradient = function.CalculateGradientValue(x.VectorToArray());
                    function.IncreaseCounterGradientValue(algorithmName);
                    hessianMatrix = function.CalculateHessianMatrix(x.VectorToArray());
                    function.IncreaseCounterHessian(algorithmName);
                    Matrica LU     = hessianMatrix.LUPDecomposition(e, gradient);
                    Matrica y      = Matrica.ForwardSubstitution(LU, gradient);
                    Matrica deltaX = Matrica.BackwardSubstitution(LU, gradient, e);
                    x.AddValue(deltaX);
                    brojac++;

                    function.IncreaseCounterValue(algorithmName);
                    function.IncreaseCounterValue(algorithmName);
                } while (brojac < 100);
            }
            else
            {
                do
                {
                    xs.Equals(x);
                    gradient = function.CalculateGradientValue(x.VectorToArray());
                    function.IncreaseCounterGradientValue(algorithmName);
                    hessianMatrix = function.CalculateHessianMatrix(x.VectorToArray());
                    function.IncreaseCounterHessian(algorithmName);
                    Matrica LU     = hessianMatrix.LUPDecomposition(e, gradient);
                    Matrica y      = Matrica.ForwardSubstitution(LU, gradient);
                    Matrica deltaX = Matrica.BackwardSubstitution(LU, gradient, e);
                    x.AddValue(deltaX);

                    for (int i = 0; i < x.NoOfRows; i++)
                    {
                        double     value       = x.LoadedMatrix[i][0];
                        double     vectorValue = deltaX.LoadedMatrix[i][0];
                        Expression expression  = lambda => value + lambda * vectorValue;
                        function.SetParameters(i, expression);
                    }

                    UnimodalInterval interval       = UnimodalInterval.FindUnimodalInterval(function, 0, 1, algorithmName);
                    UnimodalInterval lambdaInterval = GoldenSection.FindMinimum(function, interval.Minimum, interval.Maximum, e, algorithmName);

                    double lambdaMin = (lambdaInterval.Minimum + lambdaInterval.Maximum) / 2;
                    deltaX.MultiplyByScalar(lambdaMin);
                    x.AddValue(deltaX);

                    function.IncreaseCounterValue(algorithmName);
                    function.IncreaseCounterValue(algorithmName);
                } while (function.GetGradientNorm() >= e);
            }

            function.DeleteParameters();
            return(x);
        }