functionEvaluation() public method

public functionEvaluation ( ) : int
return int
Exemplo n.º 1
0
            // curve optimization called here- adjust optimization parameters here
            internal void calculate()
            {
                FittingCost costFunction = costFunction_;
                Constraint  constraint   = new NoConstraint();

                // start with the guess solution, if it exists
                Vector x = new Vector(size(), 0.0);

                if (!curve_.guessSolution_.empty())
                {
                    x = curve_.guessSolution_;
                }

                if (curve_.maxEvaluations_ == 0)
                {
                    //Don't calculate, simply use given parameters to provide a fitted curve.
                    //This turns the fittedbonddiscountcurve into an evaluator of the parametric
                    //curve, for example allowing to use the parameters for a credit spread curve
                    //calculated with bonds in one currency to be coupled to a discount curve in
                    //another currency.
                    return;
                }

                //workaround for backwards compatibility
                OptimizationMethod optimization = optimizationMethod_;

                if (optimization == null)
                {
                    optimization = new Simplex(curve_.simplexLambda_);
                }

                Problem problem = new Problem(costFunction, constraint, x);

                double rootEpsilon         = curve_.accuracy_;
                double functionEpsilon     = curve_.accuracy_;
                double gradientNormEpsilon = curve_.accuracy_;

                EndCriteria endCriteria = new EndCriteria(curve_.maxEvaluations_,
                                                          curve_.maxStationaryStateIterations_,
                                                          rootEpsilon,
                                                          functionEpsilon,
                                                          gradientNormEpsilon);

                optimization.minimize(problem, endCriteria);
                solution_ = problem.currentValue();

                numberOfIterations_ = problem.functionEvaluation();
                costValue_          = problem.functionValue();

                // save the results as the guess solution, in case of recalculation
                curve_.guessSolution_ = solution_;
            }
Exemplo n.º 2
0
        public void OptimizersTest()
        {
            //("Testing optimizers...");

            setup();

            // Loop over problems (currently there is only 1 problem)
            for (int i=0; i<costFunctions_.Count; ++i) {
                Problem problem = new Problem(costFunctions_[i], constraints_[i], initialValues_[i]);
                Vector initialValues = problem.currentValue();
                // Loop over optimizers
                for (int j = 0; j < (optimizationMethods_[i]).Count; ++j) {
                    double rootEpsilon = endCriterias_[i].rootEpsilon();
                    int endCriteriaTests = 1;
                   // Loop over rootEpsilon
                    for(int k=0; k<endCriteriaTests; ++k) {
                        problem.setCurrentValue(initialValues);
                        EndCriteria endCriteria = new EndCriteria(endCriterias_[i].maxIterations(),
                                                                  endCriterias_[i].maxStationaryStateIterations(),
                                                                  rootEpsilon,
                                                                  endCriterias_[i].functionEpsilon(),
                                                                  endCriterias_[i].gradientNormEpsilon());
                        rootEpsilon *= .1;
                        EndCriteria.Type endCriteriaResult =
                            optimizationMethods_[i][j].optimizationMethod.minimize(problem, endCriteria);
                        Vector xMinCalculated = problem.currentValue();
                        Vector yMinCalculated = problem.values(xMinCalculated);
                        // Check optimization results vs known solution
                        if (endCriteriaResult==EndCriteria.Type.None ||
                            endCriteriaResult==EndCriteria.Type.MaxIterations ||
                            endCriteriaResult==EndCriteria.Type.Unknown)
                            Assert.Fail("function evaluations: " + problem.functionEvaluation()  +
                                      " gradient evaluations: " + problem.gradientEvaluation() +
                                      " x expected:           " + xMinExpected_[i] +
                                      " x calculated:         " + xMinCalculated +
                                      " x difference:         " + (xMinExpected_[i]- xMinCalculated) +
                                      " rootEpsilon:          " + endCriteria.rootEpsilon() +
                                      " y expected:           " + yMinExpected_[i] +
                                      " y calculated:         " + yMinCalculated +
                                      " y difference:         " + (yMinExpected_[i]- yMinCalculated) +
                                      " functionEpsilon:      " + endCriteria.functionEpsilon() +
                                      " endCriteriaResult:    " + endCriteriaResult);
                    }
                }
            }
        }