alpaqa 1.0.0a19
Nonconvex constrained optimization
Loading...
Searching...
No Matches
dl-problem.cpp
Go to the documentation of this file.
5
6#include <algorithm>
7#include <cassert>
8#include <charconv>
9#include <iostream>
10#include <list>
11#include <memory>
12#include <mutex>
13#include <stdexcept>
14
15#if _WIN32
16#include <windows.h>
17#else
18#include <dlfcn.h>
19#endif
20
21namespace alpaqa::dl {
22
23namespace {
24
26 std::string s(16, '0');
27 auto begin = s.data(), end = begin + s.size();
28 auto [ptr, ec] = std::to_chars(begin, end, version, 16);
29 if (ec != std::errc())
30 throw std::logic_error(std::make_error_code(ec).message());
31 std::rotate(begin, ptr, end);
32 return s;
33}
34
35void check_abi_version(uint64_t abi_version) {
36 if (abi_version != ALPAQA_DL_ABI_VERSION) {
37 auto prob_version = format_abi_version(abi_version);
40 "alpaqa::dl::DLProblem::DLProblem: "
41 "Incompatible problem definition (problem ABI version 0x" +
42 prob_version + ", this version of alpaqa supports 0x" +
43 alpaqa_version + ")");
44 }
45}
46
48std::list<std::shared_ptr<void>> leaked_modules;
49void leak_lib(std::shared_ptr<void> handle) {
50 std::lock_guard lck{leaked_modules_mutex};
51 leaked_modules.emplace_back(std::move(handle));
52}
53
54// clang-format off
55template <Config Conf>
58 switch (sp.kind) {
61 using Dense = sparsity::Dense<config_t>;
62 return Dense{
63 .rows = sp.dense.rows,
64 .cols = sp.dense.cols,
65 .symmetry = static_cast<Symmetry>(sp.dense.symmetry),
66 };
68 using SparseCSC = sparsity::SparseCSC<config_t, int>;
69 return SparseCSC{
70 .rows = sp.sparse_csc.rows,
71 .cols = sp.sparse_csc.cols,
72 .symmetry = static_cast<Symmetry>(sp.sparse_csc.symmetry),
73 .inner_idx = typename SparseCSC::index_vector_map_t{sp.sparse_csc.inner_idx, sp.sparse_csc.nnz},
74 .outer_ptr = typename SparseCSC::index_vector_map_t{sp.sparse_csc.outer_ptr, sp.sparse_csc.cols + 1},
75 .order = static_cast<typename SparseCSC::Order>(sp.sparse_csc.order),
76 };
79 return SparseCSCl{
80 .rows = sp.sparse_csc_l.rows,
81 .cols = sp.sparse_csc_l.cols,
82 .symmetry = static_cast<Symmetry>(sp.sparse_csc_l.symmetry),
83 .inner_idx = typename SparseCSCl::index_vector_map_t{sp.sparse_csc_l.inner_idx, sp.sparse_csc_l.nnz},
84 .outer_ptr = typename SparseCSCl::index_vector_map_t{sp.sparse_csc_l.outer_ptr, sp.sparse_csc_l.cols + 1},
85 .order = static_cast<typename SparseCSCl::Order>(sp.sparse_csc_l.order),
86 };
89 return SparseCSCll{
90 .rows = sp.sparse_csc_ll.rows,
91 .cols = sp.sparse_csc_ll.cols,
92 .symmetry = static_cast<Symmetry>(sp.sparse_csc_ll.symmetry),
93 .inner_idx = typename SparseCSCll::index_vector_map_t{sp.sparse_csc_ll.inner_idx, sp.sparse_csc_ll.nnz},
94 .outer_ptr = typename SparseCSCll::index_vector_map_t{sp.sparse_csc_ll.outer_ptr, sp.sparse_csc_ll.cols + 1},
95 .order = static_cast<typename SparseCSCll::Order>(sp.sparse_csc_ll.order),
96 };
98 using SparseCOO = sparsity::SparseCOO<config_t, int>;
99 return SparseCOO{
100 .rows = sp.sparse_coo.rows,
101 .cols = sp.sparse_coo.cols,
102 .symmetry = static_cast<Symmetry>(sp.sparse_coo.symmetry),
103 .row_indices = typename SparseCOO::index_vector_map_t{sp.sparse_coo.row_indices, sp.sparse_coo.nnz},
104 .col_indices = typename SparseCOO::index_vector_map_t{sp.sparse_coo.col_indices, sp.sparse_coo.nnz},
105 .order = static_cast<typename SparseCOO::Order>(sp.sparse_coo.order),
106 .first_index = sp.sparse_coo.first_index,
107 };
110 return SparseCOOl{
111 .rows = sp.sparse_coo_l.rows,
112 .cols = sp.sparse_coo_l.cols,
113 .symmetry = static_cast<Symmetry>(sp.sparse_coo_l.symmetry),
114 .row_indices = typename SparseCOOl::index_vector_map_t{sp.sparse_coo_l.row_indices, sp.sparse_coo_l.nnz},
115 .col_indices = typename SparseCOOl::index_vector_map_t{sp.sparse_coo_l.col_indices, sp.sparse_coo_l.nnz},
116 .order = static_cast<typename SparseCOOl::Order>(sp.sparse_coo_l.order),
117 .first_index = sp.sparse_coo_l.first_index,
118 };
121 return SparseCOOll{
122 .rows = sp.sparse_coo_ll.rows,
123 .cols = sp.sparse_coo_ll.cols,
124 .symmetry = static_cast<Symmetry>(sp.sparse_coo_ll.symmetry),
125 .row_indices = typename SparseCOOll::index_vector_map_t{sp.sparse_coo_ll.row_indices, sp.sparse_coo_ll.nnz},
126 .col_indices = typename SparseCOOll::index_vector_map_t{sp.sparse_coo_ll.col_indices, sp.sparse_coo_ll.nnz},
127 .order = static_cast<typename SparseCOOll::Order>(sp.sparse_coo_ll.order),
128 .first_index = sp.sparse_coo_ll.first_index,
129 };
130 default: throw std::invalid_argument("Invalid sparsity kind");
131 }
132}
133// clang-format on
134
135} // namespace
136
137DLProblem::DLProblem(const std::filesystem::path &so_filename,
138 const std::string &function_name,
140 : BoxConstrProblem{0, 0}, file{so_filename} {
141 if (so_filename.empty())
142 throw std::invalid_argument("Invalid problem filename");
144 try {
145 auto *version_func = reinterpret_cast<alpaqa_dl_abi_version_t (*)()>(
146 util::load_func(handle.get(), function_name + "_version"));
147 check_abi_version(version_func());
148 } catch (const dynamic_load_error &) {
149 std::cerr << "Warning: problem " << so_filename
150 << " does not provide a function to query the ABI version, "
151 "alpaqa_dl_abi_version_t "
152 << function_name << "_version(void)\n";
153 }
154 auto *register_func =
155 reinterpret_cast<problem_register_t (*)(alpaqa_register_arg_t)>(
157 auto r = register_func(user_param);
158 // Avoid leaking if we throw (or if std::shared_ptr constructor throws)
159 std::unique_ptr<void, void (*)(void *)> unique_inst{r.instance, r.cleanup};
160 std::unique_ptr<alpaqa_function_dict_t> unique_extra{r.extra_functions};
161 std::unique_ptr<alpaqa_exception_ptr_t> unique_exception{r.exception};
162 check_abi_version(r.abi_version);
163 // Check exception thrown by plugin
164 if (unique_exception) {
165 // Here we're facing an interesting problem: the exception we throw
166 // might propagate upwards to a point where this instance is destroyed.
167 // This would cause the shared module to be closed using dlclose.
168 // However, the exception is still stored somewhere in the memory of
169 // that module, causing a segmentation fault when accessed.
170 // To get around this issue, we need to ensure that the shared module
171 // is not closed. Here we simply leak it by storing a shared_ptr to it
172 // in a global variable.
173 leak_lib(handle);
174 std::rethrow_exception(unique_exception->exc);
175 }
176 if (!r.functions)
177 throw std::logic_error("alpaqa::dl::DLProblem::DLProblem: plugin did "
178 "not return any functions");
179 // Store data returned by plugin
180 instance = std::shared_ptr<void>{std::move(unique_inst)};
181 functions = r.functions;
182 extra_funcs = std::shared_ptr<function_dict_t>{std::move(unique_extra)};
183
184 this->n = functions->n;
185 this->m = functions->m;
186 this->C = Box{this->n};
187 this->D = Box{this->m};
189 functions->initialize_box_C(instance.get(), this->C.lowerbound.data(),
190 this->C.upperbound.data());
192 functions->initialize_box_D(instance.get(), this->D.lowerbound.data(),
193 this->D.upperbound.data());
195 length_t = 0;
196 functions->initialize_l1_reg(instance.get(), nullptr, &);
197 if ( > 0) {
198 this->l1_reg.resize(nλ);
199 functions->initialize_l1_reg(instance.get(), this->l1_reg.data(),
200 &);
201 }
202 }
203}
204
210
211DLProblem::DLProblem(const std::filesystem::path &so_filename,
212 const std::string &function_name,
213 std::span<std::string_view> user_param)
217
218auto DLProblem::eval_proj_diff_g(crvec z, rvec e) const -> void {
219 if (functions->eval_proj_diff_g)
220 return functions->eval_proj_diff_g(instance.get(), z.data(), e.data());
222}
223
225 if (functions->eval_proj_multipliers)
226 return functions->eval_proj_multipliers(instance.get(), y.data(), M);
228}
229
231 rvec p) const -> real_t {
232 if (functions->eval_prox_grad_step)
233 return functions->eval_prox_grad_step(
234 instance.get(), γ, x.data(), grad_ψ.data(), x̂.data(), p.data());
235 return BoxConstrProblem<config_t>::eval_prox_grad_step(γ, x, grad_ψ, x̂, p);
236}
237
239 rindexvec J) const -> index_t {
240 if (functions->eval_inactive_indices_res_lna)
241 return functions->eval_inactive_indices_res_lna(
242 instance.get(), γ, x.data(), grad_ψ.data(), J.data());
244 grad_ψ, J);
245}
246
247auto DLProblem::get_name() const -> std::string {
248 if (functions->name)
249 return functions->name;
250 return file.filename().string();
251}
252
253// clang-format off
254auto DLProblem::eval_f(crvec x) const -> real_t { return functions->eval_f(instance.get(), x.data()); }
255auto DLProblem::eval_grad_f(crvec x, rvec grad_fx) const -> void { return functions->eval_grad_f(instance.get(), x.data(), grad_fx.data()); }
256auto DLProblem::eval_g(crvec x, rvec gx) const -> void { return functions->eval_g(instance.get(), x.data(), gx.data()); }
257auto 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()); }
258auto 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()); }
259auto 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()); }
261auto 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()); }
262auto 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()); }
264auto 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()); }
265auto 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()); }
267auto 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()); }
268auto DLProblem::eval_f_g(crvec x, rvec g) const -> real_t { return functions->eval_f_g(instance.get(), x.data(), g.data()); }
269auto 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()); }
270auto 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()); }
271auto 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()); }
272auto 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()); }
273auto 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()); }
274
275bool DLProblem::provides_eval_f() const { return functions->eval_f != nullptr; }
276bool DLProblem::provides_eval_grad_f() const { return functions->eval_grad_f != nullptr; }
277bool DLProblem::provides_eval_g() const { return functions->eval_g != nullptr; }
279bool DLProblem::provides_eval_jac_g() const { return functions->eval_jac_g != nullptr; }
281bool DLProblem::provides_eval_grad_gi() const { return functions->eval_grad_gi != nullptr; }
283bool DLProblem::provides_eval_hess_L() const { return functions->eval_hess_L != nullptr; }
286bool DLProblem::provides_eval_hess_ψ() const { return functions->eval_hess_ψ != nullptr; }
289bool DLProblem::provides_eval_f_g() const { return functions->eval_f_g != nullptr; }
291bool DLProblem::provides_eval_grad_L() const { return functions->eval_grad_L != nullptr; }
292bool DLProblem::provides_eval_ψ() const { return functions->eval_ψ != nullptr; }
293bool DLProblem::provides_eval_grad_ψ() const { return functions->eval_grad_ψ != nullptr; }
296bool DLProblem::provides_get_box_D() const { return functions->eval_proj_diff_g == nullptr; }
298// clang-format on
299
300#if ALPAQA_WITH_OCP
301
303 const std::string &function_name,
305 if (so_filename.empty())
306 throw std::invalid_argument("Invalid problem filename");
308 try {
309 auto *version_func = reinterpret_cast<alpaqa_dl_abi_version_t (*)()>(
310 util::load_func(handle.get(), function_name + "_version"));
311 check_abi_version(version_func());
312 } catch (const dynamic_load_error &) {
313 std::cerr << "Warning: problem " << so_filename
314 << " does not provide a function to query the ABI version, "
315 "alpaqa_dl_abi_version_t "
316 << function_name << "_version(void)\n";
317 }
318 auto *register_func =
321 auto r = register_func(user_param);
322 // Avoid leaking if we throw (or if std::shared_ptr constructor throws)
323 std::unique_ptr<void, void (*)(void *)> unique_inst{r.instance, r.cleanup};
324 std::unique_ptr<alpaqa_function_dict_t> unique_extra{r.extra_functions};
325 std::unique_ptr<alpaqa_exception_ptr_t> unique_exception{r.exception};
326 check_abi_version(r.abi_version);
327 // Check exception thrown by plugin
328 if (unique_exception) {
329 // Here we're facing an interesting problem: the exception we throw
330 // might propagate upwards to a point where this instance is destroyed.
331 // This would cause the shared module to be closed using dlclose.
332 // However, the exception is still stored somewhere in the memory of
333 // that module, causing a segmentation fault when accessed.
334 // To get around this issue, we need to ensure that the shared module
335 // is not closed. Here we simply leak it by storing a shared_ptr to it
336 // in a global variable.
337 leak_lib(handle);
338 std::rethrow_exception(unique_exception->exc);
339 }
340 if (!functions)
341 throw std::logic_error("alpaqa::dl::DLControlProblem::DLControlProblem:"
342 " plugin did not return any functions");
343 // Store data returned by plugin
344 instance = std::shared_ptr<void>{std::move(unique_inst)};
345 functions = r.functions;
346 extra_funcs = std::shared_ptr<function_dict_t>{std::move(unique_extra)};
347}
348
349// clang-format off
350auto DLControlProblem::get_U(Box &U) const -> void { return functions->get_U(instance.get(), U.lowerbound.data(), U.upperbound.data()); }
351auto DLControlProblem::get_D(Box &D) const -> void { return functions->get_D(instance.get(), D.lowerbound.data(), D.upperbound.data()); }
352auto DLControlProblem::get_D_N(Box &D) const -> void { return functions->get_D_N(instance.get(), D.lowerbound.data(), D.upperbound.data()); }
353auto DLControlProblem::get_x_init(rvec x_init) const -> void { return functions->get_x_init(instance.get(), x_init.data()); }
354auto 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()); }
355auto 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()); }
356auto 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()); }
357auto 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()); }
358auto DLControlProblem::eval_h_N(crvec x, rvec h) const -> void { return functions->eval_h_N(instance.get(), x.data(), h.data()); }
359auto DLControlProblem::eval_l(index_t timestep, crvec h) const -> real_t { return functions->eval_l(instance.get(), timestep, h.data()); }
360auto DLControlProblem::eval_l_N(crvec h) const -> real_t { return functions->eval_l_N(instance.get(), h.data()); }
361auto 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()); }
362auto 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()); }
363auto 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()); }
364auto 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()); }
365auto 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()); }
366auto 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()); }
367auto 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()); }
368auto 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()); }
371auto DLControlProblem::eval_constr(index_t timestep, crvec x, rvec c) const -> void { return functions->eval_constr(instance.get(), timestep, x.data(), c.data()); }
372auto DLControlProblem::eval_constr_N(crvec x, rvec c) const -> void { return functions->eval_constr_N(instance.get(), x.data(), c.data()); }
373auto 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()); }
374auto 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()); }
375auto 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()); }
376auto 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()); }
377
378bool DLControlProblem::provides_get_D() const { return functions->get_D != nullptr; }
379bool DLControlProblem::provides_get_D_N() const { return functions->get_D_N != nullptr; }
391// clang-format on
392
393#endif
394
395} // namespace alpaqa::dl
Implements common problem functions for minimization problems with box constraints.
Box C
Constraints of the decision variables, .
index_t eval_inactive_indices_res_lna(real_t γ, crvec x, crvec grad_ψ, rindexvec J) const
void eval_proj_diff_g(crvec z, rvec p) const
bool provides_get_box_C() const
Only supported if the ℓ₁-regularization term is zero.
length_t m
Number of constraints, dimension of g(x) and z.
length_t n
Number of decision variables, dimension of x.
void eval_proj_multipliers(rvec y, real_t M) const
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
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.
DLControlProblem(const std::filesystem::path &so_filename, const std::string &function_name="register_alpaqa_control_problem", alpaqa_register_arg_t user_param={})
Load a problem from a shared library.
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
Class that loads a problem using dlopen.
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_proj_multipliers(rvec y, real_t M) 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_proj_diff_g(crvec z, rvec e) 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
bool provides_eval_inactive_indices_res_lna() 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
bool provides_eval_f() const
index_t eval_inactive_indices_res_lna(real_t γ, crvec x, crvec grad_ψ, rindexvec J) 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
DLProblem(const std::filesystem::path &so_filename, const std::string &function_name="register_alpaqa_problem", alpaqa_register_arg_t user_param={})
Load a problem from a shared library.
bool provides_eval_f_grad_f() const
bool provides_eval_grad_f() const
bool provides_eval_grad_gi() const
std::filesystem::path file
Path to the shared module file.
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
bool provides_get_box_D() 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
std::string get_name() const
#define USING_ALPAQA_CONFIG(Conf)
Definition config.hpp:77
#define ALPAQA_DL_ABI_VERSION
Definition dl-problem.h:8
@ alpaqa_register_arg_strings
The alpaqa_register_arg_t::data pointer points to a dynamic C++ std::span of std::string_view.
Definition dl-problem.h:56
@ alpaqa_register_arg_std_any
The alpaqa_register_arg_t::data pointer points to a C++ std::any object.
Definition dl-problem.h:51
uint64_t alpaqa_dl_abi_version_t
Definition dl-problem.h:34
User-provided argument that is passed to the problem registration functions.
Definition dl-problem.h:65
std::string format_abi_version(uint64_t version)
std::list< std::shared_ptr< void > > leaked_modules
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
void * load_func(void *handle, const std::string &name)
Definition dl.cpp:64
std::shared_ptr< void > load_lib(const std::filesystem::path &so_filename)
Definition dl.cpp:51
typename Conf::rmat rmat
Definition config.hpp:96
typename Conf::real_t real_t
Definition config.hpp:86
typename Conf::rindexvec rindexvec
Definition config.hpp:106
typename Conf::index_t index_t
Definition config.hpp:104
typename Conf::length_t length_t
Definition config.hpp:103
constexpr const auto inf
Definition config.hpp:112
typename Conf::rvec rvec
Definition config.hpp:91
typename Conf::crvec crvec
Definition config.hpp:92
typename Conf::crindexvec crindexvec
Definition config.hpp:107
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:604
alpaqa_length_t(* get_S_work_size)(void *instance)
Definition dl-problem.h:582
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:599
void(* get_D)(void *instance, alpaqa_real_t *lb, alpaqa_real_t *ub)
Definition dl-problem.h:476
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:593
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:561
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:610
alpaqa_length_t(* get_R_work_size)(void *instance)
Definition dl-problem.h:580
void(* get_D_N)(void *instance, alpaqa_real_t *lb, alpaqa_real_t *ub)
Definition dl-problem.h:480
void(* eval_constr)(void *instance, alpaqa_index_t timestep, const alpaqa_real_t *x, alpaqa_real_t *c)
Definition dl-problem.h:584
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:540
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:571
void(* eval_constr_N)(void *instance, const alpaqa_real_t *x, alpaqa_real_t *c)
Definition dl-problem.h:589
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:229
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:378
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:333
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:321
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:414
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:263
alpaqa_real_t(* eval_f_g)(void *instance, const alpaqa_real_t *x, alpaqa_real_t *g)
Cost and constraints.
Definition dl-problem.h:355
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:361
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:369
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:349
alpaqa_real_t(* eval_f)(void *instance, const alpaqa_real_t *x)
Cost function.
Definition dl-problem.h:224
alpaqa_sparsity_t(* get_hess_L_sparsity)(void *instance)
Get the sparsity pattern of the Hessian of the Lagrangian.
Definition dl-problem.h:317
alpaqa_sparsity_t(* get_hess_ψ_sparsity)(void *instance)
Get the sparsity pattern of the Hessian of the augmented Lagrangian.
Definition dl-problem.h:344
alpaqa_length_t m
Number of constraints.
Definition dl-problem.h:216
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:388
alpaqa_length_t n
Number of decision variables.
Definition dl-problem.h:213
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:293
const char * name
Name of the problem.
Definition dl-problem.h:219
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:430
alpaqa_index_t(* eval_inactive_indices_res_lna)(void *instance, alpaqa_real_t γ, const alpaqa_real_t *x, const alpaqa_real_t *grad_ψ, alpaqa_index_t *J)
Active indices for proximal operator.
Definition dl-problem.h:274
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:300
void(* eval_g)(void *instance, const alpaqa_real_t *x, alpaqa_real_t *gx)
Constraints function.
Definition dl-problem.h:235
void(* eval_proj_diff_g)(void *instance, const alpaqa_real_t *z, alpaqa_real_t *e)
Difference between point and its projection onto the general constraint set D.
Definition dl-problem.h:250
alpaqa_sparsity_t(* get_jac_g_sparsity)(void *instance)
Get the sparsity pattern of the Jacobian of the constraints function.
Definition dl-problem.h:289
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:420
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:283
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:400
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:309
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:241
Sparsity of matrices.
Definition dl-problem.h:187