alpaqa 1.0.0a8
Nonconvex constrained optimization
Loading...
Searching...
No Matches
ipopt-adapter.cpp
Go to the documentation of this file.
2
3#include <IpIpoptCalculatedQuantities.hpp>
4
5namespace alpaqa {
6
7IpoptAdapter::IpoptAdapter(const Problem &problem) : problem(problem) {
8 const length_t n = problem.get_n();
10 if (sparsity_H.nnz >= 0) {
12 sparsity_H.outer_ptr.resize(n + 1);
13 mvec null{nullptr, 0};
15 sparsity_H.outer_ptr, null);
16 }
18 if (sparsity_J.nnz >= 0) {
20 sparsity_J.outer_ptr.resize(n + 1);
21 mvec null{nullptr, 0};
23 null);
24 }
25}
26
28 Index &nnz_h_lag, IndexStyleEnum &index_style) {
29 n = static_cast<Index>(problem.get_n());
30 m = static_cast<Index>(problem.get_m());
31 nnz_jac_g = static_cast<Index>(sparsity_J.nnz);
32 if (nnz_jac_g < 0)
33 nnz_jac_g = n * m;
34 nnz_h_lag = static_cast<Index>(sparsity_H.nnz);
35 if (nnz_h_lag < 0)
36 nnz_h_lag = n * n;
37 // use the C style indexing (0-based)
38 index_style = TNLP::C_STYLE;
39 return true;
40}
41
43 Number *g_l, Number *g_u) {
44 const auto &C = problem.get_box_C();
45 mvec{x_l, n} = C.lowerbound;
46 mvec{x_u, n} = C.upperbound;
47 const auto &D = problem.get_box_D();
48 mvec{g_l, m} = D.lowerbound;
49 mvec{g_u, m} = D.upperbound;
50 return true;
51}
52
54 bool init_z, Number *z_L, Number *z_U,
55 Index m, bool init_lambda,
56 Number *lambda) {
57 if (init_x) {
58 if (initial_guess.size() > 0)
59 mvec{x, n} = initial_guess;
60 else
61 mvec{x, n}.setZero();
62 }
63 if (init_z) {
66 else
67 mvec{z_L, n}.setZero();
70 else
71 mvec{z_U, n}.setZero();
72 }
73 if (init_lambda) {
74 if (initial_guess_multipliers.size() > 0)
76 else
77 mvec{lambda, m}.setZero();
78 }
79 return true;
80}
81
82bool IpoptAdapter::eval_f(Index n, const Number *x, [[maybe_unused]] bool new_x,
83 Number &obj_value) {
84 obj_value = problem.eval_f(cmvec{x, n});
85 return true;
86}
87
89 [[maybe_unused]] bool new_x, Number *grad_f) {
90 problem.eval_grad_f(cmvec{x, n}, mvec{grad_f, n});
91 return true;
92}
93
94bool IpoptAdapter::eval_g(Index n, const Number *x, [[maybe_unused]] bool new_x,
95 Index m, Number *g) {
96 problem.eval_g(cmvec{x, n}, mvec{g, m});
97 return true;
98}
99
101 [[maybe_unused]] bool new_x, Index m,
102 Index nele_jac, Index *iRow, Index *jCol,
103 Number *values) {
105 throw std::logic_error("Missing required function: eval_jac_g");
106 if (values == nullptr) // Initialize sparsity
107 set_sparsity(n, m, nele_jac, iRow, jCol, sparsity_J);
108 else // Evaluate values
110 sparsity_J.outer_ptr, mvec{values, nele_jac});
111 return true;
112}
113
114bool IpoptAdapter::eval_h(Index n, const Number *x, [[maybe_unused]] bool new_x,
115 Number obj_factor, Index m, const Number *lambda,
116 [[maybe_unused]] bool new_lambda, Index nele_hess,
117 Index *iRow, Index *jCol, Number *values) {
119 throw std::logic_error("Missing required function: eval_hess_L");
120 // Initialize sparsity
121 if (values == nullptr) {
122 set_sparsity(n, n, nele_hess, iRow, jCol, sparsity_H);
123 }
124 // Evaluate values
125 else {
126 problem.eval_hess_L(cmvec{x, n}, cmvec{lambda, m}, obj_factor,
128 mvec{values, nele_hess});
129 // For dense matrices, set lower triangle to zero
130 // TODO: make this more efficient in alpaqa problem interface
131 if (sparsity_H.nnz < 0) {
132 mmat H{values, n, n};
133 for (Index c = 0; c < n; ++c)
134 for (Index r = c + 1; r < n; ++r)
135 H(r, c) = 0;
136 }
137 }
138 return true;
139}
140void IpoptAdapter::finalize_solution(Ipopt::SolverReturn status, Index n,
141 const Number *x, const Number *z_L,
142 const Number *z_U, Index m,
143 const Number *g, const Number *lambda,
144 Number obj_value,
145 const Ipopt::IpoptData *ip_data,
146 Ipopt::IpoptCalculatedQuantities *ip_cq) {
147 results.status = status;
148 results.solution_x = cmvec{x, n};
149 results.solution_z_L = cmvec{z_L, n};
150 results.solution_z_U = cmvec{z_U, n};
151 results.solution_y = cmvec{lambda, m};
152 results.solution_g = cmvec{g, m};
153 results.solution_f = obj_value;
154 results.infeasibility = ip_cq->curr_constraint_violation();
155 results.nlp_error = ip_cq->unscaled_curr_nlp_error();
156 results.iter_count = ip_data->iter_count();
157}
158
159void IpoptAdapter::set_sparsity(Index n, Index m, [[maybe_unused]] Index nele,
160 Index *iRow, Index *jCol, const Sparsity &sp) {
161 // sparse
162 if (sp.nnz >= 0) {
163 Index l = 0; // column major, jacobian is m×n, hessian is n×n
164 for (Index c = 0; c < n; ++c) {
165 auto inner_start = static_cast<Index>(sp.outer_ptr(c));
166 auto inner_end = static_cast<Index>(sp.outer_ptr(c + 1));
167 for (Index i = inner_start; i < inner_end; ++i) {
168 assert(l < nele);
169 jCol[l] = c;
170 iRow[l] = static_cast<Index>(sp.inner_idx(i));
171 ++l;
172 }
173 }
174 assert(l == nele);
175 }
176 // dense
177 else {
178 Index l = 0; // column major, jacobian is m×n, hessian is n×n
179 for (Index c = 0; c < n; ++c) {
180 for (Index r = 0; r < m; ++r) {
181 assert(l < nele);
182 iRow[l] = r;
183 jCol[l] = c;
184 ++l;
185 }
186 }
187 assert(l == nele);
188 }
189}
190
191} // namespace alpaqa
IpoptAdapter(const Problem &problem)
Ipopt::Number Number
static void set_sparsity(Index n, Index m, Index nele, Index *iRow, Index *jCol, const Sparsity &sp)
const Problem & problem
bool eval_g(Index n, const Number *x, bool new_x, Index m, Number *g) override
bool eval_grad_f(Index n, const Number *x, bool new_x, Number *grad_f) override
bool eval_h(Index n, const Number *x, bool new_x, Number obj_factor, Index m, const Number *lambda, bool new_lambda, Index nele_hess, Index *iRow, Index *jCol, Number *values) override
bool get_starting_point(Index n, bool init_x, Number *x, bool init_z, Number *z_L, Number *z_U, Index m, bool init_lambda, Number *lambda) override
bool get_bounds_info(Index n, Number *x_l, Number *x_u, Index m, Number *g_l, Number *g_u) override
struct alpaqa::IpoptAdapter::Results results
struct alpaqa::IpoptAdapter::Sparsity sparsity_J
bool eval_f(Index n, const Number *x, bool new_x, Number &obj_value) override
bool get_nlp_info(Index &n, Index &m, Index &nnz_jac_g, Index &nnz_h_lag, IndexStyleEnum &index_style) override
bool eval_jac_g(Index n, const Number *x, bool new_x, Index m, Index nele_jac, Index *iRow, Index *jCol, Number *values) override
Ipopt::SolverReturn status
void finalize_solution(Ipopt::SolverReturn status, Index n, const Number *x, const Number *z_L, const Number *z_U, Index m, const Number *g, const Number *lambda, Number obj_value, const Ipopt::IpoptData *ip_data, Ipopt::IpoptCalculatedQuantities *ip_cq) override
struct alpaqa::IpoptAdapter::Sparsity sparsity_H
bool provides_eval_hess_L() const
Returns true if the problem provides an implementation of eval_hess_L.
const Box & get_box_D() const
[Optional] Get the rectangular constraint set of the general constraint function, .
bool provides_eval_jac_g() const
Returns true if the problem provides an implementation of eval_jac_g.
length_t get_n() const
[Required] Number of decision variables.
length_t get_m() const
[Required] Number of constraints.
void eval_hess_L(crvec x, crvec y, real_t scale, rindexvec inner_idx, rindexvec outer_ptr, rvec H_values) const
[Optional] Function that evaluates the Hessian of the Lagrangian as a sparse matrix,
void eval_grad_f(crvec x, rvec grad_fx) const
[Required] Function that evaluates the gradient of the cost,
real_t eval_f(crvec x) const
[Required] Function that evaluates the cost,
void eval_g(crvec x, rvec gx) const
[Required] Function that evaluates the constraints,
length_t get_jac_g_num_nonzeros() const
[Optional] Function that gets the number of nonzeros of the sparse Jacobian of the constraints.
const Box & get_box_C() const
[Optional] Get the rectangular constraint set of the decision variables, .
void eval_jac_g(crvec x, rindexvec inner_idx, rindexvec outer_ptr, rvec J_values) const
[Optional] Function that evaluates the Jacobian of the constraints as a sparse matrix,
length_t get_hess_L_num_nonzeros() const
[Optional] Function that gets the number of nonzeros of the sparse Hessian of the Lagrangian.
typename Conf::mvec mvec
Definition: config.hpp:53
typename Conf::mmat mmat
Definition: config.hpp:58
typename Conf::length_t length_t
Definition: config.hpp:62
typename Conf::cmvec cmvec
Definition: config.hpp:54
vec lowerbound
Definition: box.hpp:25