alpaqa 1.0.0a12
Nonconvex constrained optimization
Loading...
Searching...
No Matches
ipopt-adapter.cpp
Go to the documentation of this file.
2
3#include <IpIpoptCalculatedQuantities.hpp>
4#include <stdexcept>
5
6namespace alpaqa {
7
8IpoptAdapter::IpoptAdapter(const Problem &problem) : problem(problem) {
9 work_jac_g.resize(get_nnz(this->problem.get_jac_g_sparsity()));
10 work_hess_L.resize(get_nnz(this->problem.get_hess_L_sparsity()));
11}
12
15 n = static_cast<Index>(problem.get_n());
16 m = static_cast<Index>(problem.get_m());
17 nnz_jac_g = static_cast<Index>(cvt_sparsity_jac_g.get_sparsity().nnz());
18 nnz_h_lag = static_cast<Index>(cvt_sparsity_hess_L.get_sparsity().nnz());
19 auto jac_g_index = cvt_sparsity_jac_g.get_sparsity().first_index;
20 auto hess_L_index = cvt_sparsity_hess_L.get_sparsity().first_index;
22 throw std::invalid_argument(
23 "All problem matrices should use the same index convention");
24 if (jac_g_index != 0 && jac_g_index != 1)
25 throw std::invalid_argument(
26 "Sparse matrix indices should start at 0 or 1");
27 index_style = jac_g_index == 0 ? TNLP::C_STYLE : TNLP::FORTRAN_STYLE;
28 auto hess_L_sym = cvt_sparsity_hess_L.get_sparsity().symmetry;
30 if (hess_L_sym != Upper && hess_L_sym != Lower)
31 throw std::invalid_argument("Hessian matrix should be symmetric");
32 return true;
33}
34
36 Number *g_l, Number *g_u) {
37 const auto &C = problem.get_box_C();
38 mvec{x_l, n} = C.lowerbound;
39 mvec{x_u, n} = C.upperbound;
40 const auto &D = problem.get_box_D();
41 mvec{g_l, m} = D.lowerbound;
42 mvec{g_u, m} = D.upperbound;
43 return true;
44}
45
47 bool init_z, Number *z_L, Number *z_U,
48 Index m, bool init_lambda,
49 Number *lambda) {
50 if (init_x) {
51 if (initial_guess.size() > 0)
52 mvec{x, n} = initial_guess;
53 else
54 mvec{x, n}.setZero();
55 }
56 if (init_z) {
59 else
60 mvec{z_L, n}.setZero();
63 else
64 mvec{z_U, n}.setZero();
65 }
66 if (init_lambda) {
67 if (initial_guess_multipliers.size() > 0)
69 else
70 mvec{lambda, m}.setZero();
71 }
72 return true;
73}
74
78 return true;
79}
80
82 [[maybe_unused]] bool new_x, Number *grad_f) {
83 problem.eval_grad_f(cmvec{x, n}, mvec{grad_f, n});
84 return true;
85}
86
88 Index m, Number *g) {
89 problem.eval_g(cmvec{x, n}, mvec{g, m});
90 return true;
91}
92
94 [[maybe_unused]] bool new_x,
98 throw std::logic_error("Missing required function: eval_jac_g");
99 if (values == nullptr) { // Initialize sparsity
100 std::ranges::copy(cvt_sparsity_jac_g.get_sparsity().row_indices, iRow);
101 std::ranges::copy(cvt_sparsity_jac_g.get_sparsity().col_indices, jCol);
102 } else { // Evaluate values
105 }
106 return true;
107}
108
114 throw std::logic_error("Missing required function: eval_hess_L");
115 if (values == nullptr) { // Initialize sparsity
116 std::ranges::copy(cvt_sparsity_hess_L.get_sparsity().row_indices, iRow);
117 std::ranges::copy(cvt_sparsity_hess_L.get_sparsity().col_indices, jCol);
118 } else { // Evaluate values
121 cvt_sparsity_hess_L.convert_values(work_hess_L,
123 }
124 return true;
125}
126void IpoptAdapter::finalize_solution(Ipopt::SolverReturn status, Index n,
127 const Number *x, const Number *z_L,
128 const Number *z_U, Index m,
129 const Number *g, const Number *lambda,
131 const Ipopt::IpoptData *ip_data,
132 Ipopt::IpoptCalculatedQuantities *ip_cq) {
133 results.status = status;
134 results.solution_x = cmvec{x, n};
138 results.solution_g = cmvec{g, m};
140 results.infeasibility = ip_cq->curr_constraint_violation();
141 results.nlp_error = ip_cq->unscaled_curr_nlp_error();
142 results.iter_count = ip_data->iter_count();
143}
144
145} // namespace alpaqa
IpoptAdapter(const Problem &problem)
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
SparsityConv cvt_sparsity_jac_g
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
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
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
SparsityConv cvt_sparsity_hess_L
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, .
void eval_jac_g(crvec x, rvec J_values) const
[Optional] Function that evaluates the nonzero values of the Jacobian matrix of the constraints,
Sparsity get_jac_g_sparsity() const
[Optional] Function that returns (a view of) the sparsity pattern of the Jacobian of the constraints.
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.
Sparsity get_hess_L_sparsity() const
[Optional] Function that returns (a view of) the sparsity pattern of the Hessian of the Lagrangian.
length_t get_m() const
[Required] Number of constraints.
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,
void eval_hess_L(crvec x, crvec y, real_t scale, rvec H_values) const
[Optional] Function that evaluates the nonzero values of the Hessian of the Lagrangian,
const Box & get_box_C() const
[Optional] Get the rectangular constraint set of the decision variables, .
Symmetry
Describes the symmetry of matrices.
Definition sparsity.hpp:11
typename Conf::mvec mvec
Definition config.hpp:67
typename Conf::cmvec cmvec
Definition config.hpp:68
constexpr const auto inf
Definition config.hpp:85