11 template <
typename SCALAR>
13 : BASE(nlp, settings), settings_(BASE::settings_.ipoptSettings_)
18 ipoptApp_ = std::shared_ptr<Ipopt::IpoptApplication>(
new Ipopt::IpoptApplication(
true,
false));
19 configureDerived(settings);
22 template <
typename SCALAR>
23 IpoptSolver<SCALAR>::~IpoptSolver()
26 Ipopt::Referencer*
t = NULL;
31 template <
typename SCALAR>
34 std::cout <<
"calling Ipopt configure derived" << std::endl;
35 settings_ = settings.ipoptSettings_;
37 this->isInitialized_ =
true;
40 template <
typename SCALAR>
41 void IpoptSolver<SCALAR>::setSolverOptions()
43 ipoptApp_->Options()->SetNumericValue(
"tol", settings_.tol_);
44 ipoptApp_->Options()->SetNumericValue(
"constr_viol_tol", settings_.constr_viol_tol_);
45 ipoptApp_->Options()->SetIntegerValue(
"max_iter", settings_.max_iter_);
49 ipoptApp_->Options()->SetStringValueIfUnset(
"linear_scaling_on_demand", settings_.linear_scaling_on_demand_);
50 ipoptApp_->Options()->SetStringValueIfUnset(
"hessian_approximation", settings_.hessian_approximation_);
52 ipoptApp_->Options()->SetIntegerValue(
"print_level", settings_.printLevel_);
53 ipoptApp_->Options()->SetStringValueIfUnset(
"print_user_options", settings_.print_user_options_);
55 ipoptApp_->Options()->SetStringValueIfUnset(
"derivative_test", settings_.derivativeTest_);
56 ipoptApp_->Options()->SetIntegerValue(
"print_level", settings_.printLevel_);
57 ipoptApp_->Options()->SetNumericValue(
"derivative_test_tol", settings_.derivativeTestTol_);
58 ipoptApp_->Options()->SetNumericValue(
"derivative_test_perturbation", settings_.derivativeTestPerturbation_);
59 ipoptApp_->Options()->SetNumericValue(
"point_perturbation_radius", settings_.point_perturbation_radius_);
60 ipoptApp_->Options()->SetStringValueIfUnset(
"linear_system_scaling", settings_.linearSystemScaling_);
61 ipoptApp_->Options()->SetStringValueIfUnset(
"linear_solver", settings_.linear_solver_);
64 template <
typename SCALAR>
67 status_ = ipoptApp_->Initialize();
68 if (!(status_ == Ipopt::Solve_Succeeded) && !this->isInitialized_)
69 throw(std::runtime_error(
"NLP initialization failed"));
72 status_ = ipoptApp_->OptimizeTNLP(
this);
74 if (status_ == Ipopt::Solve_Succeeded || status_ == Ipopt::Solved_To_Acceptable_Level)
77 if (settings_.printLevel_ > 1)
79 Ipopt::Index iter_count = ipoptApp_->Statistics()->IterationCount();
80 std::cout << std::endl
82 <<
"*** The problem solved in " << iter_count <<
" iterations!" << std::endl;
84 SCALAR final_obj = ipoptApp_->Statistics()->FinalObjective();
85 std::cout << std::endl
87 <<
"*** The final value of the objective function is " << final_obj <<
'.' << std::endl;
93 if (settings_.printLevel_ > 1)
94 std::cout <<
" ipopt return value: " << status_ << std::endl;
99 template <
typename SCALAR>
102 ipoptApp_->Options()->SetStringValue(
"warm_start_init_point",
"yes");
103 ipoptApp_->Options()->SetNumericValue(
"warm_start_bound_push", 1e-9);
104 ipoptApp_->Options()->SetNumericValue(
"warm_start_bound_frac", 1e-9);
105 ipoptApp_->Options()->SetNumericValue(
"warm_start_slack_bound_frac", 1e-9);
106 ipoptApp_->Options()->SetNumericValue(
"warm_start_slack_bound_push", 1e-9);
107 ipoptApp_->Options()->SetNumericValue(
"warm_start_mult_bound_push", 1e-9);
108 ipoptApp_->Options()->SetIntegerValue(
"max_iter", (
int)maxIterations);
109 ipoptApp_->Options()->SetStringValue(
"derivative_test",
"none");
112 template <
typename SCALAR>
113 bool IpoptSolver<SCALAR>::get_nlp_info(Ipopt::Index& n,
115 Ipopt::Index& nnz_jac_g,
116 Ipopt::Index& nnz_h_lag,
117 IndexStyleEnum& index_style)
120 std::cout <<
"... entering get_nlp_info()" << std::endl;
122 n = this->nlp_->getVarCount();
125 m = this->nlp_->getConstraintsCount();
128 nnz_jac_g =
static_cast<Ipopt::Index
>(this->nlp_->getNonZeroJacobianCount());
129 assert(nnz_jac_g == nnz_jac_g);
131 if (settings_.hessian_approximation_ ==
"exact")
132 nnz_h_lag =
static_cast<Ipopt::Index
>(this->nlp_->getNonZeroHessianCount());
134 index_style = Ipopt::TNLP::C_STYLE;
137 std::cout <<
"... number of decision variables = " << n << std::endl;
138 std::cout <<
"... number of constraints = " << m << std::endl;
139 std::cout <<
"... nonzeros in jacobian = " << nnz_jac_g << std::endl;
145 template <
typename SCALAR>
146 bool IpoptSolver<SCALAR>::get_bounds_info(Ipopt::Index n,
154 std::cout <<
"... entering get_bounds_info()" << std::endl;
156 MapVecXs x_lVec(x_l, n);
157 MapVecXs x_uVec(x_u, n);
158 MapVecXs g_lVec(g_l, m);
159 MapVecXs g_uVec(g_u, m);
160 this->nlp_->getVariableBounds(x_lVec, x_uVec, n);
168 this->nlp_->getConstraintBounds(g_lVec, g_uVec, m);
173 std::cout <<
"... Leaving get_bounds_info()" << std::endl;
179 template <
typename SCALAR>
180 bool IpoptSolver<SCALAR>::get_starting_point(Ipopt::Index n,
191 std::cout <<
"... entering get_starting_point()" << std::endl;
197 this->nlp_->getInitialGuess(n, xVec);
202 MapVecXs z_lVec(z_L, n);
203 MapVecXs z_uVec(z_U, n);
204 this->nlp_->getBoundMultipliers(n, z_lVec, z_uVec);
209 MapVecXs lambdaVec(lambda, m);
210 this->nlp_->getLambdaVars(m, lambdaVec);
215 std::cout <<
"... entering get_starting_point()" << std::endl;
221 template <
typename SCALAR>
222 bool IpoptSolver<SCALAR>::eval_f(Ipopt::Index n,
const SCALAR* x,
bool new_x,
SCALAR& obj_value)
225 std::cout <<
"... entering eval_f()" << std::endl;
227 MapConstVecXs xVec(x, n);
228 this->nlp_->extractOptimizationVars(xVec, new_x);
229 obj_value = this->nlp_->evaluateCostFun();
230 assert(obj_value == obj_value);
233 std::cout <<
"... leaving eval_f()" << std::endl;
238 template <
typename SCALAR>
239 bool IpoptSolver<SCALAR>::eval_grad_f(Ipopt::Index n,
const SCALAR* x,
bool new_x,
SCALAR* grad_f)
242 std::cout <<
"... entering eval_grad_f()" << std::endl;
244 MapVecXs grad_fVec(grad_f, n);
245 MapConstVecXs xVec(x, n);
246 this->nlp_->extractOptimizationVars(xVec, new_x);
247 this->nlp_->evaluateCostGradient(n, grad_fVec);
250 std::cout <<
"... leaving eval_grad_f()" << std::endl;
255 template <
typename SCALAR>
256 bool IpoptSolver<SCALAR>::eval_g(Ipopt::Index n,
const SCALAR* x,
bool new_x, Ipopt::Index m,
SCALAR* g)
259 std::cout <<
"... entering eval_g()" << std::endl;
261 assert(m == static_cast<int>(this->nlp_->getConstraintsCount()));
262 MapConstVecXs xVec(x, n);
263 this->nlp_->extractOptimizationVars(xVec, new_x);
265 this->nlp_->evaluateConstraints(gVec);
269 std::cout <<
"gVec: " << gVec.transpose() << std::endl;
270 std::cout <<
"... leaving eval_g()" << std::endl;
276 template <
typename SCALAR>
277 bool IpoptSolver<SCALAR>::eval_jac_g(Ipopt::Index n,
281 Ipopt::Index nele_jac,
289 std::cout <<
"... entering eval_jac_g, values == NULL" << std::endl;
292 Eigen::Map<Eigen::VectorXi> iRowVec(iRow, nele_jac);
293 Eigen::Map<Eigen::VectorXi> jColVec(jCol, nele_jac);
294 this->nlp_->getSparsityPatternJacobian(nele_jac, iRowVec, jColVec);
298 std::cout <<
"... leaving eval_jac_g, values == NULL" << std::endl;
304 std::cout <<
"... entering eval_jac_g, values != NULL" << std::endl;
306 MapVecXs valVec(values, nele_jac);
307 MapConstVecXs xVec(x, n);
308 this->nlp_->extractOptimizationVars(xVec, new_x);
309 this->nlp_->evaluateConstraintJacobian(nele_jac, valVec);
313 std::cout <<
"... leaving eval_jac_g, values != NULL" << std::endl;
320 template <
typename SCALAR>
321 bool IpoptSolver<SCALAR>::eval_h(Ipopt::Index n,
328 Ipopt::Index nele_hess,
334 std::cout <<
"... entering eval_h()" << std::endl;
340 Eigen::Map<Eigen::VectorXi> iRowVec(iRow, nele_hess);
341 Eigen::Map<Eigen::VectorXi> jColVec(jCol, nele_hess);
342 this->nlp_->getSparsityPatternHessian(nele_hess, iRowVec, jColVec);
348 MapVecXs valVec(values, nele_hess);
349 MapConstVecXs xVec(x, n);
350 MapConstVecXs lambdaVec(lambda, m);
351 this->nlp_->extractOptimizationVars(xVec, new_x);
352 this->nlp_->evaluateHessian(nele_hess, valVec, obj_factor, lambdaVec);
358 std::cout <<
"... leaving eval_h()" << std::endl;
364 template <
typename SCALAR>
365 void IpoptSolver<SCALAR>::finalize_solution(Ipopt::SolverReturn status,
374 const Ipopt::IpoptData* ip_data,
375 Ipopt::IpoptCalculatedQuantities* ip_cq)
378 std::cout <<
"... entering finalize_solution() ..." << std::endl;
380 MapConstVecXs xVec(x, n);
381 MapConstVecXs zLVec(z_L, n);
382 MapConstVecXs zUVec(z_U, n);
383 MapConstVecXs lambdaVec(lambda, m);
385 this->nlp_->extractIpoptSolution(xVec, zLVec, zUVec, lambdaVec);
void prepareWarmStart(size_t maxIterations) override
Prepares the solver for a warmstarting scenario with available (good) initial guess.
Definition: IpoptSolver.h:192
clear all close all load ct GNMSLog0 mat reformat t
Definition: gnmsPlot.m:6
CppAD::AD< CppAD::cg::CG< double > > SCALAR
ct::core::StateVector< state_dim > x
Definition: LoadFromFileTest.cpp:20
tpl::IpoptSolver< double > IpoptSolver
Definition: IpoptSolver.h:200
bool solve() override
Solves the nlp.
Definition: IpoptSolver.h:191
void configureDerived(const NlpSolverSettings &settings) override
Forwards the settings to the corresponding nlp solver.
Definition: IpoptSolver.h:193