alpaqa cmake-targets
Nonconvex constrained optimization
Loading...
Searching...
No Matches
dl-problem.cpp
Go to the documentation of this file.
5
6#include <dlfcn.h>
7#include <algorithm>
8#include <cassert>
9#include <charconv>
10#include <list>
11#include <memory>
12#include <mutex>
13#include <stdexcept>
14
15namespace alpaqa::dl {
16
17namespace {
18
20 std::string s(16, '0');
21 auto begin = s.data(), end = begin + s.size();
22 auto [ptr, ec] = std::to_chars(begin, end, version, 16);
23 if (ec != std::errc())
24 throw std::logic_error(std::make_error_code(ec).message());
25 std::rotate(begin, ptr, end);
26 return s;
27}
28
29void check_abi_version(uint64_t abi_version) {
30 if (abi_version != ALPAQA_DL_ABI_VERSION) {
31 auto prob_version = format_abi_version(abi_version);
33 throw std::runtime_error(
34 "alpaqa::dl::DLProblem::DLProblem: "
35 "Incompatible problem definition (problem ABI version 0x" +
36 prob_version + ", this version of alpaqa supports 0x" +
37 alpaqa_version + ")");
38 }
39}
40
41std::shared_ptr<void> load_lib(const std::string &so_filename) {
42 assert(!so_filename.empty());
43 ::dlerror();
44 void *h = ::dlopen(so_filename.c_str(), RTLD_LOCAL | RTLD_NOW);
45 if (auto *err = ::dlerror())
46 throw std::runtime_error(err);
47#if ALPAQA_NO_DLCLOSE
48 return std::shared_ptr<void>{h, +[](void *) {}};
49#else
50 return std::shared_ptr<void>{h, &::dlclose};
51#endif
52}
53
54template <class F>
55F *load_func(void *handle, const std::string &name) {
56 assert(handle);
57 ::dlerror();
58 auto *h = ::dlsym(handle, name.c_str());
59 if (auto *err = ::dlerror())
60 throw std::runtime_error("Unable to load function '" + name +
61 "': " + err);
62 // We can only hope that the user got the signature right ...
63 return reinterpret_cast<F *>(h);
64}
65
67std::list<std::shared_ptr<void>> leaked_modules;
68void leak_lib(std::shared_ptr<void> handle) {
69 std::lock_guard lck{leaked_modules_mutex};
70 leaked_modules.emplace_back(std::move(handle));
71}
72
73// clang-format off
74template <Config Conf>
77 switch (sp.kind) {
80 using Dense = sparsity::Dense<config_t>;
81 return Dense{
82 .rows = sp.dense.rows,
83 .cols = sp.dense.cols,
84 .symmetry = static_cast<Symmetry>(sp.dense.symmetry),
85 };
87 using SparseCSC = sparsity::SparseCSC<config_t, int>;
88 return SparseCSC{
89 .rows = sp.sparse_csc.rows,
90 .cols = sp.sparse_csc.cols,
91 .symmetry = static_cast<Symmetry>(sp.sparse_csc.symmetry),
92 .inner_idx = typename SparseCSC::index_vector_map_t{sp.sparse_csc.inner_idx, sp.sparse_csc.nnz},
93 .outer_ptr = typename SparseCSC::index_vector_map_t{sp.sparse_csc.outer_ptr, sp.sparse_csc.cols + 1},
94 .order = static_cast<SparseCSC::Order>(sp.sparse_csc.order),
95 };
98 return SparseCSCl{
99 .rows = sp.sparse_csc_l.rows,
100 .cols = sp.sparse_csc_l.cols,
101 .symmetry = static_cast<Symmetry>(sp.sparse_csc_l.symmetry),
102 .inner_idx = typename SparseCSCl::index_vector_map_t{sp.sparse_csc_l.inner_idx, sp.sparse_csc_l.nnz},
103 .outer_ptr = typename SparseCSCl::index_vector_map_t{sp.sparse_csc_l.outer_ptr, sp.sparse_csc_l.cols + 1},
104 .order = static_cast<SparseCSCl::Order>(sp.sparse_csc_l.order),
105 };
108 return SparseCSCll{
109 .rows = sp.sparse_csc_ll.rows,
110 .cols = sp.sparse_csc_ll.cols,
111 .symmetry = static_cast<Symmetry>(sp.sparse_csc_ll.symmetry),
112 .inner_idx = typename SparseCSCll::index_vector_map_t{sp.sparse_csc_ll.inner_idx, sp.sparse_csc_ll.nnz},
113 .outer_ptr = typename SparseCSCll::index_vector_map_t{sp.sparse_csc_ll.outer_ptr, sp.sparse_csc_ll.cols + 1},
114 .order = static_cast<SparseCSCll::Order>(sp.sparse_csc_ll.order),
115 };
117 using SparseCOO = sparsity::SparseCOO<config_t, int>;
118 return SparseCOO{
119 .rows = sp.sparse_coo.rows,
120 .cols = sp.sparse_coo.cols,
121 .symmetry = static_cast<Symmetry>(sp.sparse_coo.symmetry),
122 .row_indices = typename SparseCOO::index_vector_map_t{sp.sparse_coo.row_indices, sp.sparse_coo.nnz},
123 .col_indices = typename SparseCOO::index_vector_map_t{sp.sparse_coo.col_indices, sp.sparse_coo.nnz},
124 .order = static_cast<SparseCOO::Order>(sp.sparse_coo.order),
125 .first_index = sp.sparse_coo.first_index,
126 };
129 return SparseCOOl{
130 .rows = sp.sparse_coo_l.rows,
131 .cols = sp.sparse_coo_l.cols,
132 .symmetry = static_cast<Symmetry>(sp.sparse_coo_l.symmetry),
133 .row_indices = typename SparseCOOl::index_vector_map_t{sp.sparse_coo_l.row_indices, sp.sparse_coo_l.nnz},
134 .col_indices = typename SparseCOOl::index_vector_map_t{sp.sparse_coo_l.col_indices, sp.sparse_coo_l.nnz},
135 .order = static_cast<SparseCOOl::Order>(sp.sparse_coo_l.order),
136 .first_index = sp.sparse_coo_l.first_index,
137 };
140 return SparseCOOll{
141 .rows = sp.sparse_coo_ll.rows,
142 .cols = sp.sparse_coo_ll.cols,
143 .symmetry = static_cast<Symmetry>(sp.sparse_coo_ll.symmetry),
144 .row_indices = typename SparseCOOll::index_vector_map_t{sp.sparse_coo_ll.row_indices, sp.sparse_coo_ll.nnz},
145 .col_indices = typename SparseCOOll::index_vector_map_t{sp.sparse_coo_ll.col_indices, sp.sparse_coo_ll.nnz},
146 .order = static_cast<SparseCOOll::Order>(sp.sparse_coo_ll.order),
147 .first_index = sp.sparse_coo_ll.first_index,
148 };
149 default: throw std::invalid_argument("Invalid sparsity kind");
150 }
151}
152// clang-format on
153
154} // namespace
155
157 const std::string &function_name, void *user_param)
158 : BoxConstrProblem{0, 0} {
159 handle = load_lib(so_filename);
160 auto *register_func =
162 auto r = register_func(user_param);
163 // Avoid leaking if we throw (or if std::shared_ptr constructor throws)
164 std::unique_ptr<void, void (*)(void *)> unique_inst{r.instance, r.cleanup};
165 std::unique_ptr<alpaqa_function_dict_t> unique_extra{r.extra_functions};
166 std::unique_ptr<alpaqa_exception_ptr_t> unique_exception{r.exception};
167 check_abi_version(r.abi_version);
168 // Check exception thrown by plugin
169 if (unique_exception) {
170 // Here we're facing an interesting problem: the exception we throw
171 // might propagate upwards to a point where this instance is destroyed.
172 // This would cause the shared module to be closed using dlclose.
173 // However, the exception is still stored somewhere in the memory of
174 // that module, causing a segmentation fault when accessed.
175 // To get around this issue, we need to ensure that the shared module
176 // is not closed. Here we simply leak it by storing a shared_ptr to it
177 // in a global variable.
178 leak_lib(handle);
179 std::rethrow_exception(unique_exception->exc);
180 }
181 if (!r.functions)
182 throw std::logic_error("alpaqa::dl::DLProblem::DLProblem: plugin did "
183 "not return any functions");
184 // Store data returned by plugin
185 instance = std::shared_ptr<void>{std::move(unique_inst)};
186 functions = r.functions;
187 extra_funcs = std::shared_ptr<function_dict_t>{std::move(unique_extra)};
188
189 this->n = functions->n;
190 this->m = functions->m;
191 this->C = Box{this->n};
192 this->D = Box{this->m};
194 functions->initialize_box_C(instance.get(), this->C.lowerbound.data(),
195 this->C.upperbound.data());
197 functions->initialize_box_D(instance.get(), this->D.lowerbound.data(),
198 this->D.upperbound.data());
200 length_t = 0;
201 functions->initialize_l1_reg(instance.get(), nullptr, &);
202 if ( > 0) {
203 this->l1_reg.resize(nλ);
204 functions->initialize_l1_reg(instance.get(), this->l1_reg.data(),
205 &);
206 }
207 }
208}
209
211 rvec p) const -> real_t {
212 if (functions->eval_prox_grad_step)
213 return functions->eval_prox_grad_step(
214 instance.get(), γ, x.data(), grad_ψ.data(), x̂.data(), p.data());
215 return BoxConstrProblem<config_t>::eval_prox_grad_step(γ, x, grad_ψ, x̂, p);
216}
217
218// clang-format off
219auto DLProblem::eval_f(crvec x) const -> real_t { return functions->eval_f(instance.get(), x.data()); }
220auto DLProblem::eval_grad_f(crvec x, rvec grad_fx) const -> void { return functions->eval_grad_f(instance.get(), x.data(), grad_fx.data()); }
221auto DLProblem::eval_g(crvec x, rvec gx) const -> void { return functions->eval_g(instance.get(), x.data(), gx.data()); }
222auto DLProblem::eval_grad_g_prod(crvec x, crvec y, rvec grad_gxy) const -> void { return functions->eval_grad_g_prod(instance.get(), x.data(), y.data(), grad_gxy.data()); }
223auto DLProblem::eval_grad_gi(crvec x, index_t i, rvec grad_gi) const -> void { return functions->eval_grad_gi(instance.get(), x.data(), i, grad_gi.data()); }
224auto DLProblem::eval_jac_g(crvec x, rvec J_values) const -> void { return functions->eval_jac_g(instance.get(), x.data(), J_values.size() == 0 ? nullptr : J_values.data()); }
226auto DLProblem::eval_hess_L_prod(crvec x, crvec y, real_t scale, crvec v, rvec Hv) const -> void { return functions->eval_hess_L_prod(instance.get(), x.data(), y.data(), scale, v.data(), Hv.data()); }
227auto DLProblem::eval_hess_L(crvec x, crvec y, real_t scale, rvec H_values) const -> void { return functions->eval_hess_L(instance.get(), x.data(), y.data(), scale, H_values.size() == 0 ? nullptr : H_values.data()); }
229auto DLProblem::eval_hess_ψ_prod(crvec x, crvec y, crvec Σ, real_t scale, crvec v, rvec Hv) const -> void { return functions->eval_hess_ψ_prod(instance.get(), x.data(), y.data(), Σ.data(), scale, D.lowerbound.data(), D.upperbound.data(), v.data(), Hv.data()); }
230auto DLProblem::eval_hess_ψ(crvec x, crvec y, crvec Σ, real_t scale, rvec H_values) const -> void { return functions->eval_hess_ψ(instance.get(), x.data(), y.data(), Σ.data(), scale, D.lowerbound.data(), D.upperbound.data(), H_values.size() == 0 ? nullptr : H_values.data()); }
232auto DLProblem::eval_f_grad_f(crvec x, rvec grad_fx) const -> real_t { return functions->eval_f_grad_f(instance.get(), x.data(), grad_fx.data()); }
233auto DLProblem::eval_f_g(crvec x, rvec g) const -> real_t { return functions->eval_f_g(instance.get(), x.data(), g.data()); }
234auto DLProblem::eval_grad_f_grad_g_prod(crvec x, crvec y, rvec grad_f, rvec grad_gxy) const -> void { return functions->eval_grad_f_grad_g_prod(instance.get(), x.data(), y.data(), grad_f.data(), grad_gxy.data()); }
235auto DLProblem::eval_grad_L(crvec x, crvec y, rvec grad_L, rvec work_n) const -> void { return functions->eval_grad_L(instance.get(), x.data(), y.data(), grad_L.data(), work_n.data()); }
236auto DLProblem::eval_ψ(crvec x, crvec y, crvec Σ, rvec ŷ) const -> real_t { return functions->eval_ψ(instance.get(), x.data(), y.data(), Σ.data(), D.lowerbound.data(), D.upperbound.data(), ŷ.data()); }
237auto DLProblem::eval_grad_ψ(crvec x, crvec y, crvec Σ, rvec grad_ψ, rvec work_n, rvec work_m) const -> void { return functions->eval_grad_ψ(instance.get(), x.data(), y.data(), Σ.data(), D.lowerbound.data(), D.upperbound.data(), grad_ψ.data(), work_n.data(), work_m.data()); }
238auto DLProblem::eval_ψ_grad_ψ(crvec x, crvec y, crvec Σ, rvec grad_ψ, rvec work_n, rvec work_m) const -> real_t { return functions->eval_ψ_grad_ψ(instance.get(), x.data(), y.data(), Σ.data(), D.lowerbound.data(), D.upperbound.data(), grad_ψ.data(), work_n.data(), work_m.data()); }
239
240bool DLProblem::provides_eval_f() const { return functions->eval_f != nullptr; }
241bool DLProblem::provides_eval_grad_f() const { return functions->eval_grad_f != nullptr; }
242bool DLProblem::provides_eval_g() const { return functions->eval_g != nullptr; }
244bool DLProblem::provides_eval_jac_g() const { return functions->eval_jac_g != nullptr; }
246bool DLProblem::provides_eval_grad_gi() const { return functions->eval_grad_gi != nullptr; }
248bool DLProblem::provides_eval_hess_L() const { return functions->eval_hess_L != nullptr; }
251bool DLProblem::provides_eval_hess_ψ() const { return functions->eval_hess_ψ != nullptr; }
254bool DLProblem::provides_eval_f_g() const { return functions->eval_f_g != nullptr; }
256bool DLProblem::provides_eval_grad_L() const { return functions->eval_grad_L != nullptr; }
257bool DLProblem::provides_eval_ψ() const { return functions->eval_ψ != nullptr; }
258bool DLProblem::provides_eval_grad_ψ() const { return functions->eval_grad_ψ != nullptr; }
261// clang-format on
262
263#if ALPAQA_WITH_OCP
264
266 const std::string &function_name,
267 void *user_param) {
268 handle = load_lib(so_filename);
270 handle.get(), function_name);
271 auto r = register_func(user_param);
272 // Avoid leaking if we throw (or if std::shared_ptr constructor throws)
273 std::unique_ptr<void, void (*)(void *)> unique_inst{r.instance, r.cleanup};
274 std::unique_ptr<alpaqa_function_dict_t> unique_extra{r.extra_functions};
275 std::unique_ptr<alpaqa_exception_ptr_t> unique_exception{r.exception};
276 check_abi_version(r.abi_version);
277 // Check exception thrown by plugin
278 if (unique_exception) {
279 // Here we're facing an interesting problem: the exception we throw
280 // might propagate upwards to a point where this instance is destroyed.
281 // This would cause the shared module to be closed using dlclose.
282 // However, the exception is still stored somewhere in the memory of
283 // that module, causing a segmentation fault when accessed.
284 // To get around this issue, we need to ensure that the shared module
285 // is not closed. Here we simply leak it by storing a shared_ptr to it
286 // in a global variable.
287 leak_lib(handle);
288 std::rethrow_exception(unique_exception->exc);
289 }
290 if (!functions)
291 throw std::logic_error("alpaqa::dl::DLControlProblem::DLControlProblem:"
292 " plugin did not return any functions");
293 // Store data returned by plugin
294 instance = std::shared_ptr<void>{std::move(unique_inst)};
295 functions = r.functions;
296 extra_funcs = std::shared_ptr<function_dict_t>{std::move(unique_extra)};
297}
298
299// clang-format off
300auto DLControlProblem::get_U(Box &U) const -> void { return functions->get_U(instance.get(), U.lowerbound.data(), U.upperbound.data()); }
301auto DLControlProblem::get_D(Box &D) const -> void { return functions->get_D(instance.get(), D.lowerbound.data(), D.upperbound.data()); }
302auto DLControlProblem::get_D_N(Box &D) const -> void { return functions->get_D_N(instance.get(), D.lowerbound.data(), D.upperbound.data()); }
303auto DLControlProblem::get_x_init(rvec x_init) const -> void { return functions->get_x_init(instance.get(), x_init.data()); }
304auto DLControlProblem::eval_f(index_t timestep, crvec x, crvec u, rvec fxu) const -> void { return functions->eval_f(instance.get(), timestep, x.data(), u.data(), fxu.data()); }
305auto DLControlProblem::eval_jac_f(index_t timestep, crvec x, crvec u, rmat J_fxu) const -> void { return functions->eval_jac_f(instance.get(), timestep, x.data(), u.data(), J_fxu.data()); }
306auto DLControlProblem::eval_grad_f_prod(index_t timestep, crvec x, crvec u, crvec p, rvec grad_fxu_p) const -> void { return functions->eval_grad_f_prod(instance.get(), timestep, x.data(), u.data(), p.data(), grad_fxu_p.data()); }
307auto DLControlProblem::eval_h(index_t timestep, crvec x, crvec u, rvec h) const -> void { return functions->eval_h(instance.get(), timestep, x.data(), u.data(), h.data()); }
308auto DLControlProblem::eval_h_N(crvec x, rvec h) const -> void { return functions->eval_h_N(instance.get(), x.data(), h.data()); }
309auto DLControlProblem::eval_l(index_t timestep, crvec h) const -> real_t { return functions->eval_l(instance.get(), timestep, h.data()); }
310auto DLControlProblem::eval_l_N(crvec h) const -> real_t { return functions->eval_l_N(instance.get(), h.data()); }
311auto DLControlProblem::eval_qr(index_t timestep, crvec xu, crvec h, rvec qr) const -> void { return functions->eval_qr(instance.get(), timestep, xu.data(), h.data(), qr.data()); }
312auto DLControlProblem::eval_q_N(crvec x, crvec h, rvec q) const -> void { return functions->eval_q_N(instance.get(), x.data(), h.data(), q.data()); }
313auto DLControlProblem::eval_add_Q(index_t timestep, crvec xu, crvec h, rmat Q) const -> void { return functions->eval_add_Q(instance.get(), timestep, xu.data(), h.data(), Q.data()); }
314auto DLControlProblem::eval_add_Q_N(crvec x, crvec h, rmat Q) const -> void { return functions->eval_add_Q_N(instance.get(), x.data(), h.data(), Q.data()); }
315auto DLControlProblem::eval_add_R_masked(index_t timestep, crvec xu, crvec h, crindexvec mask, rmat R, rvec work) const -> void { return functions->eval_add_R_masked(instance.get(), timestep, xu.data(), h.data(), mask.data(), R.data(), work.data()); }
316auto DLControlProblem::eval_add_S_masked(index_t timestep, crvec xu, crvec h, crindexvec mask, rmat S, rvec work) const -> void { return functions->eval_add_S_masked(instance.get(), timestep, xu.data(), h.data(), mask.data(), S.data(), work.data()); }
317auto DLControlProblem::eval_add_R_prod_masked(index_t timestep, crvec xu, crvec h, crindexvec mask_J, crindexvec mask_K, crvec v, rvec out, rvec work) const -> void { return functions->eval_add_R_prod_masked(instance.get(), timestep, xu.data(), h.data(), mask_J.data(), mask_K.data(), v.data(), out.data(), work.data()); }
318auto DLControlProblem::eval_add_S_prod_masked(index_t timestep, crvec xu, crvec h, crindexvec mask_K, crvec v, rvec out, rvec work) const -> void { return functions->eval_add_S_prod_masked(instance.get(), timestep, xu.data(), h.data(), mask_K.data(), v.data(), out.data(), work.data()); }
321auto DLControlProblem::eval_constr(index_t timestep, crvec x, rvec c) const -> void { return functions->eval_constr(instance.get(), timestep, x.data(), c.data()); }
322auto DLControlProblem::eval_constr_N(crvec x, rvec c) const -> void { return functions->eval_constr_N(instance.get(), x.data(), c.data()); }
323auto DLControlProblem::eval_grad_constr_prod(index_t timestep, crvec x, crvec p, rvec grad_cx_p) const -> void { return functions->eval_grad_constr_prod(instance.get(), timestep, x.data(), p.data(), grad_cx_p.data()); }
324auto DLControlProblem::eval_grad_constr_prod_N(crvec x, crvec p, rvec grad_cx_p) const -> void { return functions->eval_grad_constr_prod_N(instance.get(), x.data(), p.data(), grad_cx_p.data()); }
325auto DLControlProblem::eval_add_gn_hess_constr(index_t timestep, crvec x, crvec M, rmat out) const -> void { return functions->eval_add_gn_hess_constr(instance.get(), timestep, x.data(), M.data(), out.data()); }
326auto DLControlProblem::eval_add_gn_hess_constr_N(crvec x, crvec M, rmat out) const -> void { return functions->eval_add_gn_hess_constr_N(instance.get(), x.data(), M.data(), out.data()); }
327
328bool DLControlProblem::provides_get_D() const { return functions->get_D != nullptr; }
329bool DLControlProblem::provides_get_D_N() const { return functions->get_D_N != nullptr; }
341// clang-format on
342
343#endif
344
345} // namespace alpaqa::dl
Implements common problem functions for minimization problems with box constraints.
Box C
Constraints of the decision variables, .
length_t m
Number of constraints, dimension of g(x) and z.
length_t n
Number of decision variables, dimension of x.
real_t eval_prox_grad_step(real_t γ, crvec x, crvec grad_ψ, rvec x̂, rvec p) const
vec l1_reg
(1-norm) regularization parameter.
void eval_add_S_masked(index_t timestep, crvec xu, crvec h, crindexvec mask, rmat S, rvec work) const
bool provides_eval_grad_constr_prod() const
void eval_add_Q_N(crvec x, crvec h, rmat Q) const
bool provides_eval_add_gn_hess_constr() const
DLControlProblem(const std::string &so_filename, const std::string &function_name="register_alpaqa_control_problem", void *user_param=nullptr)
Load a problem from a shared library.
void eval_q_N(crvec x, crvec h, rvec q) const
void eval_add_gn_hess_constr(index_t timestep, crvec x, crvec M, rmat out) const
void eval_grad_constr_prod(index_t timestep, crvec x, crvec p, rvec grad_cx_p) const
void eval_add_R_prod_masked(index_t timestep, crvec xu, crvec h, crindexvec mask_J, crindexvec mask_K, crvec v, rvec out, rvec work) const
bool provides_eval_add_R_prod_masked() const
length_t get_S_work_size() const
void eval_constr_N(crvec x, rvec c) const
void get_D_N(Box &D) const
bool provides_get_S_work_size() const
void eval_grad_f_prod(index_t timestep, crvec x, crvec u, crvec p, rvec grad_fxu_p) const
std::shared_ptr< void > instance
Problem instance created by the registration function, including the deleter to destroy it.
void eval_jac_f(index_t timestep, crvec x, crvec u, rmat J_fxu) const
real_t eval_l_N(crvec h) const
void eval_h_N(crvec x, rvec h) const
real_t eval_l(index_t timestep, crvec h) const
void eval_grad_constr_prod_N(crvec x, crvec p, rvec grad_cx_p) const
control_problem_functions_t * functions
Pointer to the struct of function pointers for evaluating the objective, constraints,...
void eval_add_gn_hess_constr_N(crvec x, crvec M, rmat out) const
void eval_f(index_t timestep, crvec x, crvec u, rvec fxu) const
bool provides_eval_grad_constr_prod_N() const
length_t get_R_work_size() const
void eval_add_S_prod_masked(index_t timestep, crvec xu, crvec h, crindexvec mask_K, crvec v, rvec out, rvec work) const
bool provides_eval_add_S_prod_masked() const
void get_x_init(rvec x_init) const
void eval_h(index_t timestep, crvec x, crvec u, rvec h) const
void eval_add_R_masked(index_t timestep, crvec xu, crvec h, crindexvec mask, rmat R, rvec work) const
std::shared_ptr< void > handle
Handle to the shared module defining the problem.
void eval_qr(index_t timestep, crvec xu, crvec h, rvec qr) const
ExtraFuncs extra_funcs
Dictionary of extra functions that were registered by the problem.
void eval_add_Q(index_t timestep, crvec xu, crvec h, rmat Q) const
bool provides_get_R_work_size() const
bool provides_eval_add_gn_hess_constr_N() const
void eval_constr(index_t timestep, crvec x, rvec c) const
bool provides_eval_hess_L() const
real_t eval_prox_grad_step(real_t γ, crvec x, crvec grad_ψ, rvec x̂, rvec p) const
real_t eval_ψ_grad_ψ(crvec x, crvec y, crvec Σ, rvec grad_ψ, rvec work_n, rvec work_m) const
void eval_grad_gi(crvec x, index_t i, rvec grad_gi) const
bool provides_get_hess_L_sparsity() const
void eval_hess_L_prod(crvec x, crvec y, real_t scale, crvec v, rvec Hv) const
bool provides_eval_hess_ψ_prod() const
void eval_hess_ψ_prod(crvec x, crvec y, crvec Σ, real_t scale, crvec v, rvec Hv) const
bool provides_eval_ψ_grad_ψ() const
bool provides_get_box_C() const
Sparsity get_jac_g_sparsity() const
real_t eval_f_g(crvec x, rvec g) const
void eval_grad_f(crvec x, rvec grad_fx) const
Sparsity get_hess_ψ_sparsity() const
bool provides_eval_jac_g() const
Sparsity get_hess_L_sparsity() const
void eval_grad_f_grad_g_prod(crvec x, crvec y, rvec grad_f, rvec grad_gxy) const
real_t eval_ψ(crvec x, crvec y, crvec Σ, rvec ŷ) const
std::shared_ptr< void > instance
Problem instance created by the registration function, including the deleter to destroy it.
bool provides_eval_g() const
void eval_grad_ψ(crvec x, crvec y, crvec Σ, rvec grad_ψ, rvec work_n, rvec work_m) const
void eval_hess_ψ(crvec x, crvec y, crvec Σ, real_t scale, rvec H_values) const
bool provides_eval_grad_f_grad_g_prod() const
DLProblem(const std::string &so_filename, const std::string &function_name="register_alpaqa_problem", void *user_param=nullptr)
Load a problem from a shared library.
bool provides_eval_f() const
bool provides_get_hess_ψ_sparsity() const
bool provides_eval_hess_L_prod() const
bool provides_eval_grad_g_prod() const
bool provides_get_jac_g_sparsity() const
real_t eval_f_grad_f(crvec x, rvec grad_fx) const
bool provides_eval_f_grad_f() const
bool provides_eval_grad_f() const
bool provides_eval_grad_gi() const
bool provides_eval_f_g() const
problem_functions_t * functions
Pointer to the struct of function pointers for evaluating the objective, constraints,...
void eval_jac_g(crvec x, rvec J_values) const
real_t eval_f(crvec x) const
void eval_hess_L(crvec x, crvec y, real_t scale, rvec H_values) const
bool provides_eval_grad_L() const
void eval_grad_g_prod(crvec x, crvec y, rvec grad_gxy) const
bool provides_eval_grad_ψ() const
std::shared_ptr< void > handle
Handle to the shared module defining the problem.
ExtraFuncs extra_funcs
Dictionary of extra functions that were registered by the problem.
bool provides_eval_hess_ψ() const
void eval_grad_L(crvec x, crvec y, rvec grad_L, rvec work_n) const
void eval_g(crvec x, rvec gx) const
bool provides_eval_ψ() const
#define USING_ALPAQA_CONFIG(Conf)
Definition config.hpp:56
#define ALPAQA_DL_ABI_VERSION
Definition dl-problem.h:9
std::string format_abi_version(uint64_t version)
std::list< std::shared_ptr< void > > leaked_modules
F * load_func(void *handle, const std::string &name)
Sparsity< Conf > convert_sparsity(alpaqa_sparsity_t sp)
void leak_lib(std::shared_ptr< void > handle)
Symmetry
Describes the symmetry of matrices.
Definition sparsity.hpp:12
Dense matrix structure.
Definition sparsity.hpp:21
typename Conf::rmat rmat
Definition config.hpp:74
typename Conf::real_t real_t
Definition config.hpp:65
typename Conf::index_t index_t
Definition config.hpp:77
typename Conf::length_t length_t
Definition config.hpp:76
constexpr const auto inf
Definition config.hpp:85
typename Conf::rvec rvec
Definition config.hpp:69
typename Conf::crvec crvec
Definition config.hpp:70
typename Conf::crindexvec crindexvec
Definition config.hpp:80
Sparse coordinate list structure (COO).
Definition sparsity.hpp:56
Sparse compressed-column structure (CCS or CSC).
Definition sparsity.hpp:29
Stores any of the supported sparsity patterns.
Definition sparsity.hpp:106
void(* eval_add_gn_hess_constr)(void *instance, alpaqa_index_t timestep, const alpaqa_real_t *x, const alpaqa_real_t *M, alpaqa_real_t *out)
Definition dl-problem.h:519
alpaqa_length_t(* get_S_work_size)(void *instance)
Definition dl-problem.h:497
void(* eval_grad_constr_prod_N)(void *instance, const alpaqa_real_t *x, const alpaqa_real_t *p, alpaqa_real_t *grad_cx_p)
Definition dl-problem.h:514
void(* get_D)(void *instance, alpaqa_real_t *lb, alpaqa_real_t *ub)
Definition dl-problem.h:391
void(* eval_grad_constr_prod)(void *instance, alpaqa_index_t timestep, const alpaqa_real_t *x, const alpaqa_real_t *p, alpaqa_real_t *grad_cx_p)
Definition dl-problem.h:508
void(* eval_add_R_prod_masked)(void *instance, alpaqa_index_t timestep, const alpaqa_real_t *xu, const alpaqa_real_t *h, const alpaqa_index_t *mask_J, const alpaqa_index_t *mask_K, const alpaqa_real_t *v, alpaqa_real_t *out, alpaqa_real_t *work)
Definition dl-problem.h:476
void(* eval_add_gn_hess_constr_N)(void *instance, const alpaqa_real_t *x, const alpaqa_real_t *M, alpaqa_real_t *out)
Definition dl-problem.h:525
alpaqa_length_t(* get_R_work_size)(void *instance)
Definition dl-problem.h:495
void(* get_D_N)(void *instance, alpaqa_real_t *lb, alpaqa_real_t *ub)
Definition dl-problem.h:395
void(* eval_constr)(void *instance, alpaqa_index_t timestep, const alpaqa_real_t *x, alpaqa_real_t *c)
Definition dl-problem.h:499
void(* eval_add_Q_N)(void *instance, const alpaqa_real_t *x, const alpaqa_real_t *h, alpaqa_real_t *Q)
Definition dl-problem.h:455
void(* eval_add_S_prod_masked)(void *instance, alpaqa_index_t timestep, const alpaqa_real_t *xu, const alpaqa_real_t *h, const alpaqa_index_t *mask_K, const alpaqa_real_t *v, alpaqa_real_t *out, alpaqa_real_t *work)
Definition dl-problem.h:486
void(* eval_constr_N)(void *instance, const alpaqa_real_t *x, alpaqa_real_t *c)
Definition dl-problem.h:504
void(* eval_grad_f)(void *instance, const alpaqa_real_t *x, alpaqa_real_t *grad_fx)
Gradient of the cost function.
Definition dl-problem.h:173
alpaqa_real_t(* eval_ψ)(void *instance, const alpaqa_real_t *x, const alpaqa_real_t *y, const alpaqa_real_t *Σ, const alpaqa_real_t *zl, const alpaqa_real_t *zu, alpaqa_real_t *ŷ)
Augmented Lagrangian.
Definition dl-problem.h:285
void(* eval_hess_ψ)(void *instance, const alpaqa_real_t *x, const alpaqa_real_t *y, const alpaqa_real_t *Σ, alpaqa_real_t scale, const alpaqa_real_t *zl, const alpaqa_real_t *zu, alpaqa_real_t *H_values)
Hessian of the augmented Lagrangian.
Definition dl-problem.h:242
void(* eval_hess_ψ_prod)(void *instance, const alpaqa_real_t *x, const alpaqa_real_t *y, const alpaqa_real_t *Σ, alpaqa_real_t scale, const alpaqa_real_t *zl, const alpaqa_real_t *zu, const alpaqa_real_t *v, alpaqa_real_t *Hv)
Hessian-vector product of the augmented Lagrangian.
Definition dl-problem.h:230
void(* initialize_box_C)(void *instance, alpaqa_real_t *lb, alpaqa_real_t *ub)
Provide the initial values for the bounds of alpaqa::BoxConstrProblem::C, i.e.
Definition dl-problem.h:331
alpaqa_real_t(* eval_prox_grad_step)(void *instance, alpaqa_real_t γ, const alpaqa_real_t *x, const alpaqa_real_t *grad_ψ, alpaqa_real_t *x̂, alpaqa_real_t *p)
Proximal gradient step.
Definition dl-problem.h:321
alpaqa_real_t(* eval_f_g)(void *instance, const alpaqa_real_t *x, alpaqa_real_t *g)
Cost and constraints.
Definition dl-problem.h:263
void(* eval_grad_f_grad_g_prod)(void *instance, const alpaqa_real_t *x, const alpaqa_real_t *y, alpaqa_real_t *grad_f, alpaqa_real_t *grad_gxy)
Gradient of the cost and gradient-vector product of the constraints.
Definition dl-problem.h:269
void(* eval_grad_L)(void *instance, const alpaqa_real_t *x, const alpaqa_real_t *y, alpaqa_real_t *grad_L, alpaqa_real_t *work_n)
Gradient of the Lagrangian.
Definition dl-problem.h:277
alpaqa_real_t(* eval_f_grad_f)(void *instance, const alpaqa_real_t *x, alpaqa_real_t *grad_fx)
Cost and its gradient.
Definition dl-problem.h:257
alpaqa_real_t(* eval_f)(void *instance, const alpaqa_real_t *x)
Cost function.
Definition dl-problem.h:168
alpaqa_sparsity_t(* get_hess_L_sparsity)(void *instance)
Get the sparsity pattern of the Hessian of the Lagrangian.
Definition dl-problem.h:226
alpaqa_sparsity_t(* get_hess_ψ_sparsity)(void *instance)
Get the sparsity pattern of the Hessian of the augmented Lagrangian.
Definition dl-problem.h:253
alpaqa_length_t m
Number of constraints.
Definition dl-problem.h:163
void(* eval_grad_ψ)(void *instance, const alpaqa_real_t *x, const alpaqa_real_t *y, const alpaqa_real_t *Σ, const alpaqa_real_t *zl, const alpaqa_real_t *zu, alpaqa_real_t *grad_ψ, alpaqa_real_t *work_n, alpaqa_real_t *work_m)
Gradient of the augmented Lagrangian.
Definition dl-problem.h:295
alpaqa_length_t n
Number of decision variables.
Definition dl-problem.h:160
void(* eval_grad_gi)(void *instance, const alpaqa_real_t *x, alpaqa_index_t i, alpaqa_real_t *grad_gi)
Gradient of specific constraint function.
Definition dl-problem.h:202
void(* initialize_l1_reg)(void *instance, alpaqa_real_t *lambda, alpaqa_length_t *size)
Provide the initial values for alpaqa::BoxConstrProblem::l1_reg, the ℓ₁-regularization factor.
Definition dl-problem.h:347
void(* eval_hess_L_prod)(void *instance, const alpaqa_real_t *x, const alpaqa_real_t *y, alpaqa_real_t scale, const alpaqa_real_t *v, alpaqa_real_t *Hv)
Hessian-vector product of the Lagrangian.
Definition dl-problem.h:209
void(* eval_g)(void *instance, const alpaqa_real_t *x, alpaqa_real_t *gx)
Constraints function.
Definition dl-problem.h:179
alpaqa_sparsity_t(* get_jac_g_sparsity)(void *instance)
Get the sparsity pattern of the Jacobian of the constraints function.
Definition dl-problem.h:198
void(* initialize_box_D)(void *instance, alpaqa_real_t *lb, alpaqa_real_t *ub)
Provide the initial values for the bounds of alpaqa::BoxConstrProblem::D, i.e.
Definition dl-problem.h:337
void(* eval_jac_g)(void *instance, const alpaqa_real_t *x, alpaqa_real_t *J_values)
Jacobian of the constraints function.
Definition dl-problem.h:192
alpaqa_real_t(* eval_ψ_grad_ψ)(void *instance, const alpaqa_real_t *x, const alpaqa_real_t *y, const alpaqa_real_t *Σ, const alpaqa_real_t *zl, const alpaqa_real_t *zu, alpaqa_real_t *grad_ψ, alpaqa_real_t *work_n, alpaqa_real_t *work_m)
Augmented Lagrangian and its gradient.
Definition dl-problem.h:307
void(* eval_hess_L)(void *instance, const alpaqa_real_t *x, const alpaqa_real_t *y, alpaqa_real_t scale, alpaqa_real_t *H_values)
Hessian of the Lagrangian.
Definition dl-problem.h:218
void(* eval_grad_g_prod)(void *instance, const alpaqa_real_t *x, const alpaqa_real_t *y, alpaqa_real_t *grad_gxy)
Gradient-vector product of the constraints function.
Definition dl-problem.h:185
Sparsity of matrices.
Definition dl-problem.h:134