alpaqa pantr
Nonconvex constrained optimization
Loading...
Searching...
No Matches
ipopt-driver.cpp
Go to the documentation of this file.
3#include <IpIpoptApplication.hpp>
4
5#include <stdexcept>
6#include <string>
7
8#include "results.hpp"
9#include "solver-driver.hpp"
10
11namespace {
12
13SolverResults run_ipopt_solver(LoadedProblem &problem,
14 Ipopt::SmartPtr<Ipopt::IpoptApplication> &solver,
15 std::ostream &os, unsigned N_exp) {
16 // Ipopt problem adapter
17 using Problem = alpaqa::IpoptAdapter;
18 Ipopt::SmartPtr<Ipopt::TNLP> nlp = new Problem(problem.problem);
19 auto *my_nlp = dynamic_cast<Problem *>(GetRawPtr(nlp));
20
21 // Initial guess
22 if (auto sz = problem.initial_guess_x.size(); sz != problem.problem.get_n())
23 throw std::invalid_argument(
24 "Invalid size for initial_guess_x (got " + std::to_string(sz) +
25 ", expected " + std::to_string(problem.problem.get_n()) + ")");
26 if (auto sz = problem.initial_guess_y.size(); sz != problem.problem.get_m())
27 throw std::invalid_argument(
28 "Invalid size for initial_guess_y (got " + std::to_string(sz) +
29 ", expected " + std::to_string(problem.problem.get_m()) + ")");
30 my_nlp->initial_guess = problem.initial_guess_x;
31 my_nlp->initial_guess_multipliers = problem.initial_guess_y;
32 if (auto sz = problem.initial_guess_w.size(); sz > 0) {
33 if (sz != problem.problem.get_n() * 2)
34 throw std::invalid_argument(
35 "Invalid size for initial_guess_w (got " + std::to_string(sz) +
36 ", expected " + std::to_string(problem.problem.get_n() * 2) +
37 ")");
38 my_nlp->initial_guess_bounds_multipliers_l =
39 problem.initial_guess_w.bottomRows(problem.problem.get_n());
40 my_nlp->initial_guess_bounds_multipliers_u =
41 problem.initial_guess_w.topRows(problem.problem.get_n());
42 }
43
44 // Solve the problem
45 auto t0 = std::chrono::steady_clock::now();
46 auto status = solver->OptimizeTNLP(nlp);
47 auto t1 = std::chrono::steady_clock::now();
48 auto evals = *problem.evaluations;
49
50 // Solve the problems again to average runtimes
51 using ns = std::chrono::nanoseconds;
52 auto avg_duration = duration_cast<ns>(t1 - t0);
53 os.setstate(std::ios_base::badbit);
54 for (unsigned i = 0; i < N_exp; ++i) {
55 my_nlp->initial_guess = problem.initial_guess_x;
56 my_nlp->initial_guess_multipliers = problem.initial_guess_y;
57
58 auto t0 = std::chrono::steady_clock::now();
59 solver->OptimizeTNLP(nlp);
60 auto t1 = std::chrono::steady_clock::now();
61 avg_duration += duration_cast<ns>(t1 - t0);
62 }
63 os.clear();
64 avg_duration /= (N_exp + 1);
65
66 // Results
67 auto &nlp_res = my_nlp->results;
68 SolverResults results{
69 .status = enum_name(status),
70 .success = status == Ipopt::Solve_Succeeded,
71 .evals = evals,
72 .duration = avg_duration,
73 .solver = "Ipopt",
74 .h = 0,
75 .δ = nlp_res.infeasibility,
76 .ε = nlp_res.nlp_error,
77 .γ = 0,
78 .Σ = 0,
79 .solution = nlp_res.solution_x,
80 .multipliers = nlp_res.solution_y,
81 .multipliers_bounds = Problem::vec(problem.problem.get_n() * 2),
82 .outer_iter = nlp_res.iter_count,
83 .inner_iter = nlp_res.iter_count,
84 .extra = {},
85 };
86 results.multipliers_bounds << nlp_res.solution_z_L, nlp_res.solution_z_U;
87 return results;
88}
89
90auto make_ipopt_solver(Options &opts) {
91 using namespace Ipopt;
92
93 // We are using the factory, since this allows us to compile this
94 // example with an Ipopt Windows DLL
95 SmartPtr<IpoptApplication> app = IpoptApplicationFactory();
96 app->RethrowNonIpoptException(true);
97
98 app->Options()->SetNumericValue("tol", 1e-8);
99 app->Options()->SetNumericValue("constr_viol_tol", 1e-8);
100 app->Options()->SetStringValue("linear_solver", "mumps");
101 // app->Options()->SetStringValue("print_timing_statistics", "yes");
102 // app->Options()->SetStringValue("timing_statistics", "yes");
103 app->Options()->SetStringValue("hessian_approximation", "exact");
104
105 set_params(*app, "solver", opts);
106
107 // Initialize the IpoptApplication and process the options
108 ApplicationReturnStatus status = app->Initialize();
109 if (status != Solve_Succeeded)
110 throw std::runtime_error("Error during Ipopt initialization: " +
111 std::string(enum_name(status)));
112
113 return app;
114}
115
116} // namespace
117
118solver_func_t make_ipopt_driver(std::string_view direction, Options &opts) {
119 if (!direction.empty())
120 throw std::invalid_argument(
121 "Ipopt solver does not support any directions");
122 auto solver = make_ipopt_solver(opts);
123 unsigned N_exp = 0;
124 set_params(N_exp, "num_exp", opts);
125 return [solver{std::move(solver)},
126 N_exp](LoadedProblem &problem,
127 std::ostream &os) mutable -> SolverResults {
128 return run_ipopt_solver(problem, solver, os, N_exp);
129 };
130}
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 direction, Options &opts)
std::string_view enum_name(Ipopt::ApplicationReturnStatus s)
Definition: ipopt-enums.hpp:6
@ 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)
Definition: options.hpp:29
vec initial_guess_y
Unknowns.
Definition: problem.hpp:20
vec initial_guess_x
Definition: problem.hpp:19
vec initial_guess_w
Multipliers g.
Definition: problem.hpp:21
alpaqa::TypeErasedProblem< config_t > problem
Definition: problem.hpp:15
std::shared_ptr< alpaqa::EvalCounter > evaluations
Definition: problem.hpp:18
std::function< solver_free_func_t > solver_func_t
std::string_view status
Definition: results.hpp:24