void FluctuatingChargePropagator::initialize() { if (info_->usesFluctuatingCharges()) { if (info_->getNFluctuatingCharges() > 0) { hasFlucQ_ = true; fqConstraints_ = new FluctuatingChargeConstraints(info_); fqConstraints_->setConstrainRegions(fqParams_->getConstrainRegions()); } } if (!hasFlucQ_) { initialized_ = true; return; } // SimInfo::MoleculeIterator i; // Molecule::FluctuatingChargeIterator j; // Molecule* mol; // Atom* atom; // // For single-minima flucq, this ensures a net neutral system, but // for multiple minima, this is no longer the right thing to do: // // for (mol = info_->beginMolecule(i); mol != NULL; // mol = info_->nextMolecule(i)) { // for (atom = mol->beginFluctuatingCharge(j); atom != NULL; // atom = mol->nextFluctuatingCharge(j)) { // atom->setFlucQPos(0.0); // atom->setFlucQVel(0.0); // } // } FluctuatingChargeObjectiveFunction flucQobjf(info_, forceMan_, fqConstraints_); DynamicVector<RealType> initCoords = flucQobjf.setInitialCoords(); Problem problem(flucQobjf, *(new NoConstraint()), *(new NoStatus()), initCoords); EndCriteria endCriteria(1000, 100, 1e-5, 1e-5, 1e-5); OptimizationMethod* minim = OptimizationFactory::getInstance()->createOptimization("SD", info_); DumpStatusFunction dsf(info_); // we want a dump file written // every iteration minim->minimize(problem, endCriteria); cerr << "back from minim\n"; initialized_ = true; }
void CalibratedModel::calibrate( const std::vector<boost::shared_ptr<CalibrationHelper> >& instruments, OptimizationMethod& method, const EndCriteria& endCriteria, const Constraint& additionalConstraint, const std::vector<Real>& weights) { QL_REQUIRE(weights.empty() || weights.size() == instruments.size(), "mismatch between number of instruments and weights"); Constraint c; if (additionalConstraint.empty()) c = *constraint_; else c = CompositeConstraint(*constraint_,additionalConstraint); std::vector<Real> w = weights.empty() ? std::vector<Real>(instruments.size(), 1.0): weights; CalibrationFunction f(this, instruments, w); Problem prob(f, c, params()); shortRateEndCriteria_ = method.minimize(prob, endCriteria); Array result(prob.currentValue()); setParams(result); Array shortRateProblemValues_ = prob.values(result); notifyObservers(); }
void CalibratedModel::calibrate( const vector<shared_ptr<CalibrationHelper> >& instruments, OptimizationMethod& method, const EndCriteria& endCriteria, const Constraint& additionalConstraint, const vector<Real>& weights, const vector<bool>& fixParameters) { QL_REQUIRE(weights.empty() || weights.size() == instruments.size(), "mismatch between number of instruments (" << instruments.size() << ") and weights(" << weights.size() << ")"); Constraint c; if (additionalConstraint.empty()) c = *constraint_; else c = CompositeConstraint(*constraint_,additionalConstraint); vector<Real> w = weights.empty() ? vector<Real>(instruments.size(), 1.0): weights; Array prms = params(); vector<bool> all(prms.size(), false); Projection proj(prms,fixParameters.size()>0 ? fixParameters : all); CalibrationFunction f(this,instruments,w,proj); ProjectedConstraint pc(c,proj); Problem prob(f, pc, proj.project(prms)); shortRateEndCriteria_ = method.minimize(prob, endCriteria); Array result(prob.currentValue()); setParams(proj.include(result)); problemValues_ = prob.values(result); notifyObservers(); }
int test(OptimizationMethod& method, CostFunction& f, const EndCriteria& endCriteria, const Array& start, const Constraint& constraint = Constraint(), const Array& optimum = Array()) { QL_REQUIRE(start.size() > 0, "Input size needs to be at least 1"); std::cout << "Starting point: "; Constraint c; if (!constraint.empty()) c = constraint; Problem p(f, c, start); printFunction(p, start); method.minimize(p, endCriteria); std::cout << "End point: "; Real val = printFunction(p, p.currentValue()); if(!optimum.empty()) { std::cout << "Global optimium: "; Real optimVal = printFunction(p, optimum); if(std::abs(optimVal) < 1e-13) return std::abs(val-optimVal) < 1e-6; else return std::abs((val-optimVal)/optimVal) < 1e-6; } return 1; }