public void ScalarFunctonSecondDerivativeTest()
 {
     // d2/dx2((2x)^2) = 8
     Func<double, double> f = x => Math.Pow(2 * x, 2);
     var J = new NumericalHessian();
     var jeval = J.Evaluate(f, 2);
     Assert.AreEqual((double)8, jeval[0], 1e-7);
 }
 public void OneEquationTwoVarVectorFunctionTest()
 {
     Func<double[], double> f = x => 4 * Math.Pow(x[0], 2) + 3 * Math.Pow(x[1], 3);
     var H = new NumericalHessian();
     var heval = H.Evaluate(f, new double[] { 1, 1 });
     var solution = new double[,] { { 8, 0 }, { 0, 18 } };
     Assert.AreEqual(solution, heval);
     Assert.AreEqual(12, H.FunctionEvaluations);
     H.ResetFunctionEvaluations();
     Assert.AreEqual(0, H.FunctionEvaluations);
 }
        public void RosenbrockFunctionHessianTest()
        {
            Func<double[], double> f = x => Math.Pow((1 - x[0]), 2) + 100 * Math.Pow(x[1] - Math.Pow(x[0], 2), 2);
            var H = new NumericalHessian(5, 2);
            var heval = H.Evaluate(f, new double[] { 2, 3 });
            var solution = new double[,] { { 3602, -800 }, { -800, 200 } };
            for (int row = 0; row < solution.Rank; row++)
            {
                for (int col = 0; col < solution.Rank; col++)
                {
                    Assert.AreEqual(solution[row, col], heval[row, col], 1e-7);
                }
            }

        }
Ejemplo n.º 4
0
        public override void Execute()
        {
            var varCount = Fh.Point.Count;

            //Подготовка
            _hessian = new DenseMatrix(varCount, varCount);
            //начальная точка
            var x1 = Fh.Point;
            Func<double[], double> castFunc = doubles => Fh.Y(new DenseVector(doubles));
            var hessianCalculator = new NumericalHessian();
            //Альфа метод
            if (AlphaMethod == null) AlphaMethod = new BoostedDavidon(Fh.AlphaFunction, Fh.AlphaDiffFunction, Eps);
            IterationCount = 0;

            //Основной этап
            do
            {
                if (!Kop1(Fh.Grad(Fh.Point))) break;
                //Находим ньютоновское направление - p TODO
                var temp = hessianCalculator.Evaluate(castFunc, x1.ToArray());
                for (var i = 0; i < varCount; i++)
                    for (var j = 0; j < varCount; j++)
                        _hessian[i, j] = temp[i, j];
                var p = -(_hessian.Inverse())*Fh.Grad(x1);
                Fh.Dir = p;

                //Альфа метод
                AlphaMethod.SetSvenInterval();
                AlphaMethod.Execute();
                var alpha1 = AlphaMethod.Answer;

                //Переходим на новую точку
                x1 = x1 + Fh.Dir*alpha1;

                Fh.Point = x1;
                IterationCount++;
            } while (Kop1(Fh.Dir) && Kop1(Fh.Grad(Fh.Point)));

            Answer = Fh.Point;
        }