5 namespace pt = boost::property_tree;
27 pt::ptree const& pt) :
Optimizer(cost, pt),
28 algorithm(NLOptAlgorithm(pt.
get<std::
string>("Algorithm"))),
29 minimize(pt.
get<
bool>("Minimize", true))
34 nlopt_algorithm NLoptOptimizer::NLOptAlgorithm(std::string
const& alg)
const {
35 if( alg.compare(
"DIRECT")==0 ) {
return NLOPT_GN_DIRECT; }
36 if( alg.compare(
"DIRECTL")==0 ) {
return NLOPT_GN_DIRECT_L; }
37 if( alg.compare(
"CRS")==0 ) {
return NLOPT_GN_CRS2_LM; }
38 if( alg.compare(
"MLSL")==0 ) {
return NLOPT_G_MLSL_LDS; }
39 if( alg.compare(
"ISRES")==0 ) {
return NLOPT_GN_ISRES; }
40 if( alg.compare(
"COBYLA")==0 ) {
return NLOPT_LN_COBYLA; }
41 if( alg.compare(
"BOBYQA")==0 ) {
return NLOPT_LN_BOBYQA; }
42 if( alg.compare(
"NEWUOA")==0 ) {
return NLOPT_LN_NEWUOA_BOUND; }
43 if( alg.compare(
"PRAXIS")==0 ) {
return NLOPT_LN_PRAXIS; }
44 if( alg.compare(
"NM")==0 ) {
return NLOPT_LN_NELDERMEAD; }
45 if( alg.compare(
"SBPLX")==0 ) {
return NLOPT_LN_SBPLX; }
46 if( alg.compare(
"MMA")==0 ) {
return NLOPT_LD_MMA; }
47 if( alg.compare(
"SLSQP")==0 ) {
return NLOPT_LD_SLSQP; }
48 if( alg.compare(
"LBFGS")==0 ) {
return NLOPT_LD_LBFGS; }
49 if( alg.compare(
"PreTN")==0 ) {
return NLOPT_LD_TNEWTON_PRECOND_RESTART; }
50 if( alg.compare(
"LMVM")==0 ) {
return NLOPT_LD_VAR2; }
52 std::stringstream msg;
53 msg <<
"\nERROR: Invalid algorithm (" << alg <<
") passed to NLOptAlgorithm.";
54 throw std::runtime_error(msg.str());
56 return NLOPT_LN_COBYLA;
62 auto solver = nlopt_create(algorithm, opt->inputSizes(0));
64 nlopt_set_min_objective(solver, Cost, &opt);
66 nlopt_set_max_objective(solver, Cost, &opt);
69 if (!ineqConstraints.empty()) {
71 for (
int i; i<ineqConstraints.size(); ++i) {
73 double ineqTol[ineqConstraints[i]->numOutputs];
74 Eigen::Map<const Eigen::VectorXd> ineqTolmap(ineqTol, ineqConstraints[i]->numOutputs);
75 const Eigen::VectorXd ineqTolEig = constraint_tol*Eigen::VectorXd::Ones(ineqConstraints[i]->numOutputs);
77 nlopt_add_inequality_mconstraint(solver,
78 ineqConstraints[i]->numOutputs,
85 if (!eqConstraints.empty()) {
87 for (
int i; i<eqConstraints.size(); ++i) {
89 double eqTol[eqConstraints[i]->numOutputs];
90 Eigen::Map<const Eigen::VectorXd> eqTolmap(eqTol, eqConstraints[i]->numOutputs);
91 const Eigen::VectorXd eqTolEig = constraint_tol*Eigen::VectorXd::Ones(eqConstraints[i]->numOutputs);
93 nlopt_add_equality_mconstraint(solver,
94 eqConstraints[i]->numOutputs,
102 nlopt_set_ftol_rel(solver, ftol_rel);
103 nlopt_set_ftol_abs(solver, ftol_abs);
104 nlopt_set_xtol_rel(solver, xtol_rel);
105 nlopt_set_xtol_abs1(solver, xtol_abs);
106 nlopt_set_maxeval(solver, maxEvals);
108 const Eigen::VectorXd& xinit = boost::any_cast<Eigen::VectorXd const&>(inputs.at(0));
112 Eigen::VectorXd& xopt = boost::any_cast<Eigen::VectorXd&>(outputs.at(0));
115 const nlopt_result check = nlopt_optimize(solver, xopt.data(), &minf);
124 std::pair<Eigen::VectorXd, double>
125 NLoptOptimizer::Solve(std::vector<Eigen::VectorXd>
const& input) {
127 std::vector<boost::any> ins;
133 return std::pair<Eigen::VectorXd, double>(boost::any_cast<Eigen::VectorXd const&>(outputs[0]),
134 boost::any_cast<double const>(outputs[1]));
138 double NLoptOptimizer::Cost(
unsigned int n,
144 std::shared_ptr<CostFunction> opt = *((std::shared_ptr<CostFunction>*) f_data);
146 Eigen::VectorXd xeig = Eigen::Map<const Eigen::VectorXd>(x,
n);
150 Eigen::Map<Eigen::VectorXd> gradmap(grad,
n);
151 gradmap = opt->Gradient();
158 void NLoptOptimizer::Constraint(
unsigned int m,
165 std::shared_ptr<ModPiece> opt = *((std::shared_ptr<ModPiece>*) f_data);
167 Eigen::Map<const Eigen::VectorXd> xmap(x,
n);
168 const Eigen::VectorXd& xeig = xmap;
172 Eigen::Map<Eigen::MatrixXd> gradmap(grad,
n, m);
173 const Eigen::MatrixXd& gradeig =
174 opt->Jacobian(0, 0, xeig);
180 Eigen::Map<Eigen::VectorXd> resultmap(result, m);
181 std::vector<Eigen::VectorXd> resulteig = opt->Evaluate(xeig);
182 resultmap = resulteig.at(0);
REGISTER_OPTIMIZER(DIRECT, NLoptOptimizer) REGISTER_OPTIMIZER(DIRECTL
A MUQ dependency has failed.
Provides an abstract interface for defining vector-valued model components.
Solve an optimization problem.
std::vector< std::reference_wrapper< const T > > ref_vector
A vector of references to something ...
auto get(const nlohmann::detail::iteration_proxy_value< IteratorType > &i) -> decltype(i.key())
NLOHMANN_BASIC_JSON_TPL_DECLARATION std::string to_string(const NLOHMANN_BASIC_JSON_TPL &j)
user-defined to_string function for JSON values