public double[] Solve(
            Func <double[], double> f,
            double[] startValue,
            int nIter)
        {
            OptVector xOld          = new OptVector(startValue);
            OptVector xNew          = new OptVector();
            OptVector derivativeOld = new OptVector(numericalDerivative.EvaluatePartialDerivative(f, xOld.MinArray, 1));

            OptVector direction = -1.0 * derivativeOld;

            OptVector derivativeNew = new OptVector();

            for (int i = 0; i < nIter; i++)
            {
                StepSize = strongWolfeLineSearch.GetStepLength(f, direction, xOld, 20, MaxIterLineSearch);

                xNew = xOld + StepSize * direction;

                if (CheckEarlyExit(xNew, xOld))
                {
                    break;
                }

                derivativeNew = new OptVector(numericalDerivative.EvaluatePartialDerivative(f, xNew.MinArray, 1));

                double beta = PolakRibiere(derivativeNew, derivativeOld);

                direction = beta * direction - derivativeNew;

                xOld          = xNew;
                derivativeOld = derivativeNew;
            }
            return(xNew.MinArray);
        }
        private double[] GetAndSetPenaltyParam(
            Func <double[], double> f,
            List <Func <double[], double> > eqConstraints,
            ref List <InequalityConstraintProperties> inequalityConstraintsProp,
            OptVector x)
        {
            double[] eqPenaltyParams = new double[eqConstraints.Count];

            //Start penalty params
            double deltaF = (new OptVector(numericalDerivative.EvaluatePartialDerivative(f, x.MinArray, 1))).Length();

            for (int i = 0; i < eqConstraints.Count; i++)
            {
                double denom = (new OptVector(numericalDerivative.EvaluatePartialDerivative(eqConstraints[i], x.MinArray, 1))).Length();
                if (Math.Abs(denom) > 0.0)
                {
                    eqPenaltyParams[i] = deltaF / denom;
                }
            }

            for (int i = 0; i < inequalityConstraintsProp.Count; i++)
            {
                double denom = (new OptVector(numericalDerivative.EvaluatePartialDerivative(inequalityConstraintsProp[i].Function, x.MinArray, 1))).Length();
                if (Math.Abs(denom) > 0.0)
                {
                    inequalityConstraintsProp[i].PenaltyParam = deltaF / denom;
                }
            }

            return(eqPenaltyParams);
        }
Exemple #3
0
        public double GetStepLength(
            Func <double[], double> f,
            OptVector d,
            OptVector x0,
            double alpham,
            int maxIter)
        {
            double alphap = 0;

            double c1 = 1E-7;
            double c2 = 0.5;

            double alphax = alpham;//rnd.NextDouble();

            double fx0 = f(x0.MinArray);
            double gx0 = new OptVector(numericalDerivative.EvaluatePartialDerivative(f, x0.MinArray, 1)) * d;

            double fxp = fx0;
            double gxp = gx0;

            for (int i = 1; i < maxIter; i++)
            {
                OptVector xx  = x0 + alphax * d;
                double    fxx = f(xx.MinArray);
                double    gxx = new OptVector(numericalDerivative.EvaluatePartialDerivative(f, xx.MinArray, 1)) * d;

                if (fxx > fx0 + c1 * alphax * gx0 ||
                    (i > 1 && fxx >= fxp))
                {
                    return(Zoom(f, x0, d, alphap, alphax, maxIter));
                }

                if (Math.Abs(gxx) <= -c2 * gx0)
                {
                    return(alphax);
                }

                if (gxx >= 0)
                {
                    return(Zoom(f, x0, d, alphax, alphap, maxIter));
                }

                alphap = alphax;
                fxp    = fxx;
                gxp    = gxx;

                double r = rnd.NextDouble();

                alphax = alphax + (alpham - alphax) * r;
            }

            return(alphax);
        }