Пример #1
0
        private OptVector[] BuildMatrixA(
            List <Func <double[], double> > equalityConstraints,
            List <InequalityConstraintProperties> inequalityConstraints,
            OptVector[] lagrangianHessian,
            OptVector x)
        {
            List <OptVector> constraintDerivative = new List <OptVector>();

            for (int i = 0; i < equalityConstraints.Count; i++)
            {
                constraintDerivative.Add(new OptVector(numericalDerivative.EvaluatePartialDerivative(equalityConstraints[i], x.MinArray, 1)));
            }

            for (int i = 0; i < inequalityConstraints.Count; i++)
            {
                if (inequalityConstraints[i].IsActive)
                {
                    constraintDerivative.Add(new OptVector(numericalDerivative.EvaluatePartialDerivative(inequalityConstraints[i].Function, x.MinArray, 1)));
                }
            }

            OptVector[] bufHessian = new OptVector[lagrangianHessian.Length];

            OptVector[] modifiedLagrangian = OptVector.Sum(lagrangianHessian, epsilonNw);

            Array.Copy(modifiedLagrangian, bufHessian, lagrangianHessian.Length);

            for (int i = 0; i < bufHessian.Length; i++)
            {
                OptVector buf = new OptVector(constraintDerivative.Count);

                for (int j = 0; j < constraintDerivative.Count; j++)
                {
                    buf[j] = constraintDerivative[j][i];
                }

                bufHessian[i].Add(buf);
            }

            var lagrangianList = bufHessian.ToList();

            foreach (var item in constraintDerivative)
            {
                var res = OptVector.Add(item, new OptVector(constraintDerivative.Count, -epsilonSe));
                lagrangianList.Add(res);
            }

            return(lagrangianList.ToArray());
        }
Пример #2
0
        static void TestFunc()
        {
            OptVector a = new OptVector(new double[] { 1, 2 });
            OptVector b = new OptVector(new double[] { 3, 4 });

            OptVector c = new OptVector(new double[] { 1, 3 });
            OptVector d = new OptVector(new double[] { 4, 7 });

            var res = OptVector.Mult(a, b);

            var res1 = OptVector.SubtractFromIdentity(res);

            var res2 = OptVector.Div(res, 2);

            OptVector[] aa = new OptVector[] { a, b };
            OptVector[] bb = new OptVector[] { c, d };

            var res3 = OptVector.Mult(res, bb);
            var res4 = OptVector.Sum(res, bb);

            var res5 = OptVector.Mult(res, c);
        }
Пример #3
0
        private OptVector[] CalculateLagrangianHessian(
            Func <double[], double> lagrangian,
            OptVector[] lagrangianHessian,
            OptVector xNew,
            OptVector xOld,
            OptVector step)
        {
            OptVector s = step;
            OptVector y = new OptVector(numericalDerivative.EvaluatePartialDerivative(lagrangian, xNew.MinArray, 1)) -
                          new OptVector(numericalDerivative.EvaluatePartialDerivative(lagrangian, xOld.MinArray, 1));

            OptVector[] yy = OptVector.Mult(y, y);
            double      ys = y * s;

            OptVector partialDenom = OptVector.Mult(lagrangianHessian, s);

            OptVector[] num   = OptVector.Mult(partialDenom, OptVector.Mult(s, lagrangianHessian));
            double      denom = -1.0 * s * partialDenom;

            #region Positive definiteness

            if (ys < 1E-15)
            {
                double theta = 0.999999;

                OptVector yNew  = new OptVector(y);
                double    ysNew = ys;

                while (ysNew < lambda * s * partialDenom &&
                       theta >= 0.0)
                {
                    yNew = y * theta + (1.0 - theta) * partialDenom;

                    ysNew = yNew * s;

                    theta = theta - 1E-5;
                }

                y  = yNew;
                ys = ysNew;

                yy = OptVector.Mult(y, y);
            }

            #endregion

            if (ys == 0.0)
            {
                if (step.Length() > 0)
                {
                    return(OptVector.GetIdentity(xNew.Count, step));
                }

                return(OptVector.GetIdentity(xNew.Count));
            }

            OptVector[] addParam1 = OptVector.Div(yy, ys);
            OptVector[] addParam2 = OptVector.Div(num, denom);

            return(OptVector.Sum(OptVector.Sum(lagrangianHessian, addParam1), addParam2));
        }