Nonconvex constrained optimization
Loading...
Searching...
No Matches
ipopt-adapter.cpp
Go to the documentation of this file.
2
4
5#include <IpIpoptCalculatedQuantities.hpp>
6#include <IpIpoptData.hpp>
7#include <stdexcept>
8
9namespace alpaqa {
10
12
14 Index &nnz_h_lag, IndexStyleEnum &index_style) {
15 n = static_cast<Index>(problem.get_num_variables());
16 m = static_cast<Index>(problem.get_num_constraints());
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;
21 if (jac_g_index != hess_L_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;
29 using enum 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_variable_bounds();
38 mvec{x_l, n} = C.lower;
39 mvec{x_u, n} = C.upper;
40 const auto &D = problem.get_general_bounds();
41 mvec{g_l, m} = D.lower;
42 mvec{g_u, m} = D.upper;
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) {
58 mvec{z_L, n} = (initial_guess_bounds_multipliers.array() < 0)
60 else
61 mvec{z_L, n}.setZero();
63 mvec{z_U, n} = (initial_guess_bounds_multipliers.array() > 0)
65 else
66 mvec{z_U, n}.setZero();
67 }
68 if (init_lambda) {
69 if (initial_guess_multipliers.size() > 0)
71 else
72 mvec{lambda, m}.setZero();
73 }
74 return true;
75}
76
77bool IpoptAdapter::eval_f(Index n, const Number *x, [[maybe_unused]] bool new_x,
78 Number &obj_value) {
79 obj_value = problem.eval_objective(cmvec{x, n});
80 return true;
81}
82
84 [[maybe_unused]] bool new_x, Number *grad_f) {
85 problem.eval_objective_gradient(cmvec{x, n}, mvec{grad_f, n});
86 return true;
87}
88
89bool IpoptAdapter::eval_g(Index n, const Number *x, [[maybe_unused]] bool new_x,
90 Index m, Number *g) {
91 problem.eval_constraints(cmvec{x, n}, mvec{g, m});
92 return true;
93}
94
96 [[maybe_unused]] bool new_x,
97 [[maybe_unused]] Index m, Index nele_jac,
98 Index *iRow, Index *jCol, Number *values) {
99 if (!problem.provides_eval_constraints_jacobian())
100 throw std::logic_error(
101 "Missing required function: eval_constraints_jacobian");
102 if (values == nullptr) { // Initialize sparsity
103 std::ranges::copy(cvt_sparsity_jac_g.get_sparsity().row_indices, iRow);
104 std::ranges::copy(cvt_sparsity_jac_g.get_sparsity().col_indices, jCol);
105 } else { // Evaluate values
106 cvt_sparsity_jac_g.convert_values_into(
107 std::span{values, static_cast<size_t>(nele_jac)},
108 [&](std::span<real_t> v) {
109 problem.eval_constraints_jacobian(cmvec{x, n}, as_vec(v));
110 });
111 // TODO: reuse workspace
112 }
113 return true;
114}
115
116bool IpoptAdapter::eval_h(Index n, const Number *x, [[maybe_unused]] bool new_x,
117 Number obj_factor, Index m, const Number *lambda,
118 [[maybe_unused]] bool new_lambda, Index nele_hess,
119 Index *iRow, Index *jCol, Number *values) {
120 if (!problem.provides_eval_lagrangian_hessian())
121 throw std::logic_error(
122 "Missing required function: eval_lagrangian_hessian");
123 if (values == nullptr) { // Initialize sparsity
124 std::ranges::copy(cvt_sparsity_hess_L.get_sparsity().row_indices, iRow);
125 std::ranges::copy(cvt_sparsity_hess_L.get_sparsity().col_indices, jCol);
126 } else { // Evaluate values
127 cvt_sparsity_hess_L.convert_values_into(
128 std::span{values, static_cast<size_t>(nele_hess)},
129 [&](std::span<real_t> v) {
130 problem.eval_lagrangian_hessian(cmvec{x, n}, cmvec{lambda, m},
131 obj_factor, as_vec(v));
132 });
133 // TODO: reuse workspace
134 }
135 return true;
136}
137void IpoptAdapter::finalize_solution(Ipopt::SolverReturn status, Index n,
138 const Number *x, const Number *z_L,
139 const Number *z_U, Index m,
140 const Number *g, const Number *lambda,
141 Number obj_value,
142 const Ipopt::IpoptData *ip_data,
143 Ipopt::IpoptCalculatedQuantities *ip_cq) {
144 results.status = status;
145 results.solution_x = cmvec{x, n};
146 results.solution_z_L = cmvec{z_L, n};
147 results.solution_z_U = cmvec{z_U, n};
148 results.solution_y = cmvec{lambda, m};
149 results.solution_g = cmvec{g, m};
150 results.solution_f = obj_value;
151 results.infeasibility = ip_cq->curr_constraint_violation();
152 results.nlp_error = ip_cq->unscaled_curr_nlp_error();
153 results.iter_count = ip_data->iter_count();
154}
155
156} // 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
TypeErasedProblem< config_t > Problem
SparsityConv cvt_sparsity_hess_L
typename Conf::mvec mvec
Definition config.hpp:89
auto as_vec(std::span< T, E > s)
Convert a std::span to an Eigen::Vector view.
Definition span.hpp:51
typename Conf::cmvec cmvec
Definition config.hpp:90