コード例 #1
0
ファイル: DifferentialEvolution.cs プロジェクト: igitur/qlnet
        public override EndCriteria.Type minimize(Problem P, EndCriteria endCriteria)
        {
            EndCriteria.Type ecType = EndCriteria.Type.None;

            upperBound_         = P.constraint().upperBound(P.currentValue());
            lowerBound_         = P.constraint().lowerBound(P.currentValue());
            currGenSizeWeights_ = new Vector(configuration().populationMembers,
                                             configuration().stepsizeWeight);
            currGenCrossover_ = new Vector(configuration().populationMembers,
                                           configuration().crossoverProbability);

            List <Candidate> population = new InitializedList <Candidate>(configuration().populationMembers);

            population.ForEach((ii, vv) => population[ii] = new Candidate(P.currentValue().size()));

            fillInitialPopulation(population, P);

            //original quantlib use partial_sort as only first elements is needed
            double fxOld = population.Min(x => x.cost);

            bestMemberEver_ = (Candidate)population.First(x => x.cost.IsEqual(fxOld)).Clone();
            int iteration = 0, stationaryPointIteration = 0;

            // main loop - calculate consecutive emerging populations
            while (!endCriteria.checkMaxIterations(iteration++, ref ecType))
            {
                calculateNextGeneration(population, P.costFunction());

                double    fxNew = population.Min(x => x.cost);
                Candidate tmp   = (Candidate)population.First(x => x.cost.IsEqual(fxNew)).Clone();

                if (fxNew < bestMemberEver_.cost)
                {
                    bestMemberEver_ = tmp;
                }

                if (endCriteria.checkStationaryFunctionValue(fxOld, fxNew, ref stationaryPointIteration,
                                                             ref ecType))
                {
                    break;
                }
                fxOld = fxNew;
            }

            P.setCurrentValue(bestMemberEver_.values);
            P.setFunctionValue(bestMemberEver_.cost);
            return(ecType);
        }
コード例 #2
0
ファイル: ConjugateGradient.cs プロジェクト: ariesy/QLNet
        //! solve the optimization problem P
        public override EndCriteria.Type minimize(Problem P, EndCriteria endCriteria)
        {
            // Initializations
            double ftol = endCriteria.functionEpsilon();
            int maxStationaryStateIterations_ = endCriteria.maxStationaryStateIterations();
            EndCriteria.Type ecType = EndCriteria.Type.None; // reset end criteria
            P.reset(); // reset problem
            Vector x_ = P.currentValue(); // store the starting point
            int iterationNumber_ =0; // stationaryStateIterationNumber_=0
            lineSearch_.searchDirection = new Vector(x_.Count); // dimension line search
            bool done = false;

            // function and squared norm of gradient values;
            double fnew;
            double fold;
            double gold2;
            double c;
            double fdiff;
            double normdiff;
            // classical initial value for line-search step
            double t = 1.0;
            // Set gradient g at the size of the optimization problem search direction
            int sz = lineSearch_.searchDirection.Count;
            Vector g = new Vector(sz);
            Vector d = new Vector(sz);
            Vector sddiff = new Vector(sz);
            // Initialize cost function, gradient g and search direction
            P.setFunctionValue(P.valueAndGradient(g, x_));
            P.setGradientNormValue(Vector.DotProduct(g, g));
            lineSearch_.searchDirection = g * -1.0;
            // Loop over iterations
            do
            {
                // Linesearch
                t = lineSearch_.value(P, ref ecType, endCriteria, t);
                // don't throw: it can fail just because maxIterations exceeded
                //QL_REQUIRE(lineSearch_->succeed(), "line-search failed!");
                if (lineSearch_.succeed())
                {
                    // Updates
                    d = lineSearch_.searchDirection;
                    // New point
                    x_ = lineSearch_.lastX();
                    // New function value
                    fold = P.functionValue();
                    P.setFunctionValue(lineSearch_.lastFunctionValue());
                    // New gradient and search direction vectors
                    g = lineSearch_.lastGradient();
                    // orthogonalization coef
                    gold2 = P.gradientNormValue();
                    P.setGradientNormValue(lineSearch_.lastGradientNorm2());
                    c = P.gradientNormValue() / gold2;
                    // conjugate gradient search direction
                    sddiff = ((g*-1.0) + c * d) - lineSearch_.searchDirection;
                    normdiff = Math.Sqrt(Vector.DotProduct(sddiff, sddiff));
                    lineSearch_.searchDirection = (g*-1.0) + c * d;
                    // Now compute accuracy and check end criteria
                    // Numerical Recipes exit strategy on fx (see NR in C++, p.423)
                    fnew = P.functionValue();
                    fdiff = 2.0 *Math.Abs(fnew-fold) / (Math.Abs(fnew) + Math.Abs(fold) + Double.Epsilon);
                    if (fdiff < ftol || endCriteria.checkMaxIterations(iterationNumber_, ref ecType))
                    {
                        endCriteria.checkStationaryFunctionValue(0.0, 0.0, ref maxStationaryStateIterations_, ref ecType);
                        endCriteria.checkMaxIterations(iterationNumber_, ref ecType);
                        return ecType;
                    }
                    //done = endCriteria(iterationNumber_,
                    //                   stationaryStateIterationNumber_,
                    //                   true,  //FIXME: it should be in the problem
                    //                   fold,
                    //                   std::sqrt(gold2),
                    //                   P.functionValue(),
                    //                   std::sqrt(P.gradientNormValue()),
                    //                   ecType);
                    P.setCurrentValue(x_); // update problem current value
                    ++iterationNumber_; // Increase iteration number
                    }
                else
                {
                    done =true;
                }
            } while (!done);
            P.setCurrentValue(x_);
            return ecType;
        }
コード例 #3
0
        //! solve the optimization problem P
        public override EndCriteria.Type minimize(Problem P, EndCriteria endCriteria)
        {
            // Initializations
            double ftol = endCriteria.functionEpsilon();
            int    maxStationaryStateIterations_ = endCriteria.maxStationaryStateIterations();

            EndCriteria.Type ecType = EndCriteria.Type.None;    // reset end criteria
            P.reset();                                          // reset problem
            Vector x_ = P.currentValue();                       // store the starting point
            int    iterationNumber_ = 0;                        // stationaryStateIterationNumber_=0

            lineSearch_.searchDirection = new Vector(x_.Count); // dimension line search
            bool done = false;

            // function and squared norm of gradient values;
            double fnew;
            double fold;
            double gold2;
            double c;
            double fdiff;
            double normdiff;
            // classical initial value for line-search step
            double t = 1.0;
            // Set gradient g at the size of the optimization problem search direction
            int    sz     = lineSearch_.searchDirection.Count;
            Vector g      = new Vector(sz);
            Vector d      = new Vector(sz);
            Vector sddiff = new Vector(sz);

            // Initialize cost function, gradient g and search direction
            P.setFunctionValue(P.valueAndGradient(g, x_));
            P.setGradientNormValue(Vector.DotProduct(g, g));
            lineSearch_.searchDirection = g * -1.0;
            // Loop over iterations
            do
            {
                // Linesearch
                t = lineSearch_.value(P, ref ecType, endCriteria, t);
                // don't throw: it can fail just because maxIterations exceeded
                //QL_REQUIRE(lineSearch_->succeed(), "line-search failed!");
                if (lineSearch_.succeed())
                {
                    // Updates
                    d = lineSearch_.searchDirection;
                    // New point
                    x_ = lineSearch_.lastX();
                    // New function value
                    fold = P.functionValue();
                    P.setFunctionValue(lineSearch_.lastFunctionValue());
                    // New gradient and search direction vectors
                    g = lineSearch_.lastGradient();
                    // orthogonalization coef
                    gold2 = P.gradientNormValue();
                    P.setGradientNormValue(lineSearch_.lastGradientNorm2());
                    c = P.gradientNormValue() / gold2;
                    // conjugate gradient search direction
                    sddiff   = ((g * -1.0) + c * d) - lineSearch_.searchDirection;
                    normdiff = Math.Sqrt(Vector.DotProduct(sddiff, sddiff));
                    lineSearch_.searchDirection = (g * -1.0) + c * d;
                    // Now compute accuracy and check end criteria
                    // Numerical Recipes exit strategy on fx (see NR in C++, p.423)
                    fnew  = P.functionValue();
                    fdiff = 2.0 * Math.Abs(fnew - fold) / (Math.Abs(fnew) + Math.Abs(fold) + Double.Epsilon);
                    if (fdiff < ftol || endCriteria.checkMaxIterations(iterationNumber_, ref ecType))
                    {
                        endCriteria.checkStationaryFunctionValue(0.0, 0.0, ref maxStationaryStateIterations_, ref ecType);
                        endCriteria.checkMaxIterations(iterationNumber_, ref ecType);
                        return(ecType);
                    }
                    //done = endCriteria(iterationNumber_,
                    //                   stationaryStateIterationNumber_,
                    //                   true,  //FIXME: it should be in the problem
                    //                   fold,
                    //                   std::sqrt(gold2),
                    //                   P.functionValue(),
                    //                   std::sqrt(P.gradientNormValue()),
                    //                   ecType);
                    P.setCurrentValue(x_);                  // update problem current value
                    ++iterationNumber_;                     // Increase iteration number
                }
                else
                {
                    done = true;
                }
            } while (!done);
            P.setCurrentValue(x_);
            return(ecType);
        }
コード例 #4
0
        public override EndCriteria.Type minimize(Problem P, EndCriteria endCriteria)
        {
            // Initializations
            double ftol = endCriteria.functionEpsilon();
            int    maxStationaryStateIterations_ = endCriteria.maxStationaryStateIterations();

            EndCriteria.Type ecType = EndCriteria.Type.None; // reset end criteria
            P.reset();                                       // reset problem
            Vector x_ = P.currentValue();                    // store the starting point
            int    iterationNumber_ = 0;

            // dimension line search
            lineSearch_.searchDirection = new Vector(x_.size());
            bool done = false;

            // function and squared norm of gradient values
            double fnew, fold, gold2;
            double fdiff;
            // classical initial value for line-search step
            double t = 1.0;
            // Set gradient g at the size of the optimization problem
            // search direction
            int    sz = lineSearch_.searchDirection.size();
            Vector prevGradient = new Vector(sz), d = new Vector(sz), sddiff = new Vector(sz), direction = new Vector(sz);

            // Initialize cost function, gradient prevGradient and search direction
            P.setFunctionValue(P.valueAndGradient(prevGradient, x_));
            P.setGradientNormValue(Vector.DotProduct(prevGradient, prevGradient));
            lineSearch_.searchDirection = prevGradient * -1;

            bool first_time = true;

            // Loop over iterations
            do
            {
                // Linesearch
                if (!first_time)
                {
                    prevGradient = lineSearch_.lastGradient();
                }
                t = (lineSearch_.value(P, ref ecType, endCriteria, t));
                // don't throw: it can fail just because maxIterations exceeded
                if (lineSearch_.succeed())
                {
                    // Updates

                    // New point
                    x_ = lineSearch_.lastX();
                    // New function value
                    fold = P.functionValue();
                    P.setFunctionValue(lineSearch_.lastFunctionValue());
                    // New gradient and search direction vectors

                    // orthogonalization coef
                    gold2 = P.gradientNormValue();
                    P.setGradientNormValue(lineSearch_.lastGradientNorm2());

                    // conjugate gradient search direction
                    direction = getUpdatedDirection(P, gold2, prevGradient);

                    sddiff = direction - lineSearch_.searchDirection;
                    lineSearch_.searchDirection = direction;
                    // Now compute accuracy and check end criteria
                    // Numerical Recipes exit strategy on fx (see NR in C++, p.423)
                    fnew  = P.functionValue();
                    fdiff = 2.0 * Math.Abs(fnew - fold) /
                            (Math.Abs(fnew) + Math.Abs(fold) + Const.QL_EPSILON);
                    if (fdiff < ftol ||
                        endCriteria.checkMaxIterations(iterationNumber_, ref ecType))
                    {
                        endCriteria.checkStationaryFunctionValue(0.0, 0.0, ref maxStationaryStateIterations_, ref ecType);
                        endCriteria.checkMaxIterations(iterationNumber_, ref ecType);
                        return(ecType);
                    }
                    P.setCurrentValue(x_); // update problem current value
                    ++iterationNumber_;    // Increase iteration number
                    first_time = false;
                }
                else
                {
                    done = true;
                }
            }while (!done);
            P.setCurrentValue(x_);
            return(ecType);
        }