5#include <IpIpoptApplication.hpp>
16 Ipopt::SmartPtr<Ipopt::IpoptApplication> &solver,
17 std::ostream &os,
unsigned N_exp) {
20 Ipopt::SmartPtr<Ipopt::TNLP> nlp =
new Problem(problem.
problem);
21 auto *my_nlp =
dynamic_cast<Problem *
>(GetRawPtr(nlp));
25 throw std::invalid_argument(
26 "Invalid size for initial_guess_x (got " + std::to_string(sz) +
27 ", expected " + std::to_string(problem.
problem.
get_n()) +
")");
29 throw std::invalid_argument(
30 "Invalid size for initial_guess_y (got " + std::to_string(sz) +
31 ", expected " + std::to_string(problem.
problem.
get_m()) +
")");
36 throw std::invalid_argument(
37 "Invalid size for initial_guess_w (got " + std::to_string(sz) +
38 ", expected " + std::to_string(problem.
problem.
get_n() * 2) +
40 my_nlp->initial_guess_bounds_multipliers_l =
42 my_nlp->initial_guess_bounds_multipliers_u =
47 auto t0 = std::chrono::steady_clock::now();
48 auto status = solver->OptimizeTNLP(nlp);
49 auto t1 = std::chrono::steady_clock::now();
53 using ns = std::chrono::nanoseconds;
54 auto avg_duration = duration_cast<ns>(t1 - t0);
55 os.setstate(std::ios_base::badbit);
56 for (
unsigned i = 0; i < N_exp; ++i) {
60 auto t0 = std::chrono::steady_clock::now();
61 solver->OptimizeTNLP(nlp);
62 auto t1 = std::chrono::steady_clock::now();
63 avg_duration += duration_cast<ns>(t1 - t0);
66 avg_duration /= (N_exp + 1);
69 auto &nlp_res = my_nlp->results;
72 .success = status == Ipopt::Solve_Succeeded,
74 .duration = avg_duration,
77 .δ = nlp_res.infeasibility,
78 .ε = nlp_res.nlp_error,
81 .solution = nlp_res.solution_x,
82 .multipliers = nlp_res.solution_y,
83 .multipliers_bounds = Problem::vec(problem.
problem.
get_n() * 2),
84 .outer_iter = nlp_res.iter_count,
85 .inner_iter = nlp_res.iter_count,
88 results.multipliers_bounds << nlp_res.solution_z_L, nlp_res.solution_z_U;
92auto make_ipopt_solver(
Options &opts) {
93 using namespace Ipopt;
97 SmartPtr<IpoptApplication> app = IpoptApplicationFactory();
98 app->RethrowNonIpoptException(
true);
100 app->Options()->SetNumericValue(
"tol", 1e-8);
101 app->Options()->SetNumericValue(
"constr_viol_tol", 1e-8);
102 app->Options()->SetStringValue(
"linear_solver",
"mumps");
105 app->Options()->SetStringValue(
"hessian_approximation",
"exact");
110 ApplicationReturnStatus status = app->Initialize();
111 if (status != Solve_Succeeded)
112 throw std::runtime_error(
"Error during Ipopt initialization: " +
121 if (!direction.empty())
122 throw std::invalid_argument(
123 "Ipopt solver does not support any directions");
124 auto solver = make_ipopt_solver(opts);
127 return [solver{std::move(solver)},
130 return run_ipopt_solver(problem, solver, os, N_exp);
139 throw std::invalid_argument(
140 "This version of alpaqa was compiled without Ipopt support.");
Based on https://coin-or.github.io/Ipopt/INTERFACES.html.
length_t get_n() const
[Required] Number of decision variables.
length_t get_m() const
[Required] Number of constraints.
solver_func_t make_ipopt_driver(std::string_view, Options &)
std::string_view enum_name(Ipopt::ApplicationReturnStatus s)
@ Ipopt
The stopping criterion used by Ipopt, see https://link.springer.com/article/10.1007/s10107-004-0559-y...
decltype(auto) set_params(T &t, std::string_view prefix, Options &opts)
vec initial_guess_y
Unknowns.
vec initial_guess_w
Multipliers g.
alpaqa::TypeErasedProblem< config_t > problem
std::shared_ptr< alpaqa::EvalCounter > evaluations
std::function< solver_free_func_t > solver_func_t