public void Update(LineSearchResult lsr)
            {
                var currPoint  = lsr.CurrPoint;
                var gradAtCurr = lsr.GradAtCurr;
                var nextPoint  = lsr.NextPoint;
                var gradAtNext = lsr.GradAtNext;

                // Inner product of S_k and Y_k
                var SYk = 0.0;

                // Add new ones.
                if (kCounter < m)
                {
                    for (var j = 0; j < dimension; j++)
                    {
                        S[kCounter][j] = nextPoint[j] - currPoint[j];
                        Y[kCounter][j] = gradAtNext[j] - gradAtCurr[j];
                        SYk           += S[kCounter][j] * Y[kCounter][j];
                    }

                    rho[kCounter] = 1.0 / SYk;
                }
                else
                {
                    // Discard oldest vectors and add new ones.
                    for (var i = 0; i < m - 1; i++)
                    {
                        S[i]   = S[i + 1];
                        Y[i]   = Y[i + 1];
                        rho[i] = rho[i + 1];
                    }

                    for (var j = 0; j < dimension; j++)
                    {
                        S[m - 1][j] = nextPoint[j] - currPoint[j];
                        Y[m - 1][j] = gradAtNext[j] - gradAtCurr[j];
                        SYk        += S[m - 1][j] * Y[m - 1][j];
                    }

                    rho[m - 1] = 1.0 / SYk;
                }

                if (kCounter < m)
                {
                    kCounter++;
                }
            }
示例#2
0
        public void TestLineSearchDeterminesSaneStepLength2()
        {
            var objectiveFunction = new QuadraticFunction2();
            // given
            var testX         = new double[] { -2 };
            var testValueX    = objectiveFunction.ValueAt(testX);
            var testGradX     = objectiveFunction.GradientAt(testX);
            var testDirection = new double[] { 1 };

            // when
            var lsr = LineSearchResult.GetInitialObject(testValueX, testGradX, testX);

            LineSearch.DoLineSearch(objectiveFunction, testDirection, lsr, 1.0);
            var stepSize = lsr.StepSize;

            // then
            Assert.True(Tolerance < stepSize && stepSize <= 1);
        }
示例#3
0
        public void TestLineSearchFailsAtMinimum1()
        {
            var objectiveFunction = new QuadraticFunction2();

            // given
            double[] testX      = { 0 };
            var      testValueX = objectiveFunction.ValueAt(testX);
            var      testGradX  = objectiveFunction.GradientAt(testX);

            double[] testDirection = { -1 };
            // when
            var lsr = LineSearchResult.GetInitialObject(testValueX, testGradX, testX);

            LineSearch.DoLineSearch(objectiveFunction, testDirection, lsr, 1.0);
            var stepSize = lsr.StepSize;

            // then
            Assert.False(Tolerance < stepSize && stepSize <= 1);

            Assert.AreEqual(0.0, stepSize, Tolerance);
        }
示例#4
0
        protected int DoBfgsUpdate(ref ExitCondition currentExitCondition, WolfeLineSearch lineSearcher, ref Matrix <double> inversePseudoHessian, ref Vector <double> lineSearchDirection, ref IObjectiveFunction previousPoint, ref LineSearchResult lineSearchResult, ref IObjectiveFunction candidate, ref Vector <double> step, ref int totalLineSearchSteps, ref int iterationsWithNontrivialLineSearch)
        {
            int iterations;

            for (iterations = 1; iterations < MaximumIterations; ++iterations)
            {
                double startingStepSize;
                double maxLineSearchStep;
                lineSearchDirection = CalculateSearchDirection(ref inversePseudoHessian, out maxLineSearchStep, out startingStepSize, previousPoint, candidate, step);

                try
                {
                    lineSearchResult = lineSearcher.FindConformingStep(candidate, lineSearchDirection, startingStepSize, maxLineSearchStep);
                }
                catch (Exception e)
                {
                    throw new InnerOptimizationException("Line search failed.", e);
                }

                iterationsWithNontrivialLineSearch += lineSearchResult.Iterations > 0 ? 1 : 0;
                totalLineSearchSteps += lineSearchResult.Iterations;

                step          = lineSearchResult.FunctionInfoAtMinimum.Point - candidate.Point;
                previousPoint = candidate;
                candidate     = lineSearchResult.FunctionInfoAtMinimum;

                currentExitCondition = ExitCriteriaSatisfied(candidate, previousPoint, iterations);
                if (currentExitCondition != ExitCondition.None)
                {
                    break;
                }
            }

            return(iterations);
        }