alpaqa 1.0.0a16
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 <list>
10#include <memory>
11#include <mutex>
12#include <stdexcept>
13
14#if _WIN32
15#include <windows.h>
16#else
17#include <dlfcn.h>
18#endif
19
20namespace alpaqa::dl {
21
22namespace {
23
25 std::string s(16, '0');
26 auto begin = s.data(), end = begin + s.size();
27 auto [ptr, ec] = std::to_chars(begin, end, version, 16);
28 if (ec != std::errc())
29 throw std::logic_error(std::make_error_code(ec).message());
30 std::rotate(begin, ptr, end);
31 return s;
32}
33
34void check_abi_version(uint64_t abi_version) {
35 if (abi_version != ALPAQA_DL_ABI_VERSION) {
36 auto prob_version = format_abi_version(abi_version);
38 throw std::runtime_error(
39 "alpaqa::dl::DLProblem::DLProblem: "
40 "Incompatible problem definition (problem ABI version 0x" +
41 prob_version + ", this version of alpaqa supports 0x" +
42 alpaqa_version + ")");
43 }
44}
45
46#if _WIN32
47std::shared_ptr<char> get_last_error_msg() {
48 char *err = nullptr;
53 reinterpret_cast<char *>(&err), 0, NULL) != 0) {
54 return std::shared_ptr<char>{err, [](char *e) { LocalFree(e); }};
55 } else {
56 static char msg[] = "(failed to get error message)";
57 return std::shared_ptr<char>{msg, [](char *) {}};
58 }
59}
60
61std::shared_ptr<void> load_lib(const std::filesystem::path &so_filename) {
62 assert(!so_filename.empty());
63 void *h = LoadLibraryW(so_filename.c_str());
64 if (!h)
65 throw std::runtime_error("Unable to load \"" + so_filename.string() +
66 "\": " + get_last_error_msg().get());
67#if ALPAQA_NO_DLCLOSE
68 return std::shared_ptr<void>{h, +[](void *) {}};
69#else
70 return std::shared_ptr<void>{
71 h, +[](void *h) { FreeLibrary(static_cast<HMODULE>(h)); }};
72#endif
73}
74
75template <class F>
76F *load_func(void *handle, const std::string &name) {
77 assert(handle);
78 auto *h = GetProcAddress(static_cast<HMODULE>(handle), name.c_str());
79 if (!h)
80 throw std::runtime_error("Unable to load function '" + name +
81 "': " + get_last_error_msg().get());
82 // We can only hope that the user got the signature right ...
83 return reinterpret_cast<F *>(h);
84}
85#else
86std::shared_ptr<void> load_lib(const std::filesystem::path &so_filename) {
87 assert(!so_filename.empty());
88 ::dlerror();
89 void *h = ::dlopen(so_filename.c_str(), RTLD_LOCAL | RTLD_NOW);
90 if (auto *err = ::dlerror())
91 throw std::runtime_error(err);
92#if ALPAQA_NO_DLCLOSE
93 return std::shared_ptr<void>{h, +[](void *) {}};
94#else
95 return std::shared_ptr<void>{h, &::dlclose};
96#endif
97}
98
99template <class F>
100F *load_func(void *handle, const std::string &name) {
101 assert(handle);
102 ::dlerror();
103 auto *h = ::dlsym(handle, name.c_str());
104 if (auto *err = ::dlerror())
105 throw std::runtime_error("Unable to load function '" + name +
106 "': " + err);
107 // We can only hope that the user got the signature right ...
108 return reinterpret_cast<F *>(h);
109}
110#endif
111
113std::list<std::shared_ptr<void>> leaked_modules;
114void leak_lib(std::shared_ptr<void> handle) {
115 std::lock_guard lck{leaked_modules_mutex};
116 leaked_modules.emplace_back(std::move(handle));
117}
118
119// clang-format off
120template <Config Conf>
123 switch (sp.kind) {
124 using sparsity::Symmetry;
126 using Dense = sparsity::Dense<config_t>;
127 return Dense{
128 .rows = sp.dense.rows,
129 .cols = sp.dense.cols,
130 .symmetry = static_cast<Symmetry>(sp.dense.symmetry),
131 };
133 using SparseCSC = sparsity::SparseCSC<config_t, int>;
134 return SparseCSC{
135 .rows = sp.sparse_csc.rows,
136 .cols = sp.sparse_csc.cols,
137 .symmetry = static_cast<Symmetry>(sp.sparse_csc.symmetry),
138 .inner_idx = typename SparseCSC::index_vector_map_t{sp.sparse_csc.inner_idx, sp.sparse_csc.nnz},
139 .outer_ptr = typename SparseCSC::index_vector_map_t{sp.sparse_csc.outer_ptr, sp.sparse_csc.cols + 1},
140 .order = static_cast<typename SparseCSC::Order>(sp.sparse_csc.order),
141 };
144 return SparseCSCl{
145 .rows = sp.sparse_csc_l.rows,
146 .cols = sp.sparse_csc_l.cols,
147 .symmetry = static_cast<Symmetry>(sp.sparse_csc_l.symmetry),
148 .inner_idx = typename SparseCSCl::index_vector_map_t{sp.sparse_csc_l.inner_idx, sp.sparse_csc_l.nnz},
149 .outer_ptr = typename SparseCSCl::index_vector_map_t{sp.sparse_csc_l.outer_ptr, sp.sparse_csc_l.cols + 1},
150 .order = static_cast<typename SparseCSCl::Order>(sp.sparse_csc_l.order),
151 };
154 return SparseCSCll{
155 .rows = sp.sparse_csc_ll.rows,
156 .cols = sp.sparse_csc_ll.cols,
157 .symmetry = static_cast<Symmetry>(sp.sparse_csc_ll.symmetry),
158 .inner_idx = typename SparseCSCll::index_vector_map_t{sp.sparse_csc_ll.inner_idx, sp.sparse_csc_ll.nnz},
159 .outer_ptr = typename SparseCSCll::index_vector_map_t{sp.sparse_csc_ll.outer_ptr, sp.sparse_csc_ll.cols + 1},
160 .order = static_cast<typename SparseCSCll::Order>(sp.sparse_csc_ll.order),
161 };
163 using SparseCOO = sparsity::SparseCOO<config_t, int>;
164 return SparseCOO{
165 .rows = sp.sparse_coo.rows,
166 .cols = sp.sparse_coo.cols,
167 .symmetry = static_cast<Symmetry>(sp.sparse_coo.symmetry),
168 .row_indices = typename SparseCOO::index_vector_map_t{sp.sparse_coo.row_indices, sp.sparse_coo.nnz},
169 .col_indices = typename SparseCOO::index_vector_map_t{sp.sparse_coo.col_indices, sp.sparse_coo.nnz},
170 .order = static_cast<typename SparseCOO::Order>(sp.sparse_coo.order),
171 .first_index = sp.sparse_coo.first_index,
172 };
175 return SparseCOOl{
176 .rows = sp.sparse_coo_l.rows,
177 .cols = sp.sparse_coo_l.cols,
178 .symmetry = static_cast<Symmetry>(sp.sparse_coo_l.symmetry),
179 .row_indices = typename SparseCOOl::index_vector_map_t{sp.sparse_coo_l.row_indices, sp.sparse_coo_l.nnz},
180 .col_indices = typename SparseCOOl::index_vector_map_t{sp.sparse_coo_l.col_indices, sp.sparse_coo_l.nnz},
181 .order = static_cast<typename SparseCOOl::Order>(sp.sparse_coo_l.order),
182 .first_index = sp.sparse_coo_l.first_index,
183 };
186 return SparseCOOll{
187 .rows = sp.sparse_coo_ll.rows,
188 .cols = sp.sparse_coo_ll.cols,
189 .symmetry = static_cast<Symmetry>(sp.sparse_coo_ll.symmetry),
190 .row_indices = typename SparseCOOll::index_vector_map_t{sp.sparse_coo_ll.row_indices, sp.sparse_coo_ll.nnz},
191 .col_indices = typename SparseCOOll::index_vector_map_t{sp.sparse_coo_ll.col_indices, sp.sparse_coo_ll.nnz},
192 .order = static_cast<typename SparseCOOll::Order>(sp.sparse_coo_ll.order),
193 .first_index = sp.sparse_coo_ll.first_index,
194 };
195 default: throw std::invalid_argument("Invalid sparsity kind");
196 }
197}
198// clang-format on
199
200} // namespace
201
202DLProblem::DLProblem(const std::filesystem::path &so_filename,
203 const std::string &function_name, void *user_param)
204 : BoxConstrProblem{0, 0} {
205 if (so_filename.empty())
206 throw std::invalid_argument("Invalid problem filename");
207 handle = load_lib(so_filename);
208 auto *register_func =
210 auto r = register_func(user_param);
211 // Avoid leaking if we throw (or if std::shared_ptr constructor throws)
212 std::unique_ptr<void, void (*)(void *)> unique_inst{r.instance, r.cleanup};
213 std::unique_ptr<alpaqa_function_dict_t> unique_extra{r.extra_functions};
214 std::unique_ptr<alpaqa_exception_ptr_t> unique_exception{r.exception};
215 check_abi_version(r.abi_version);
216 // Check exception thrown by plugin
217 if (unique_exception) {
218 // Here we're facing an interesting problem: the exception we throw
219 // might propagate upwards to a point where this instance is destroyed.
220 // This would cause the shared module to be closed using dlclose.
221 // However, the exception is still stored somewhere in the memory of
222 // that module, causing a segmentation fault when accessed.
223 // To get around this issue, we need to ensure that the shared module
224 // is not closed. Here we simply leak it by storing a shared_ptr to it
225 // in a global variable.
226 leak_lib(handle);
227 std::rethrow_exception(unique_exception->exc);
228 }
229 if (!r.functions)
230 throw std::logic_error("alpaqa::dl::DLProblem::DLProblem: plugin did "
231 "not return any functions");
232 // Store data returned by plugin
233 instance = std::shared_ptr<void>{std::move(unique_inst)};
234 functions = r.functions;
235 extra_funcs = std::shared_ptr<function_dict_t>{std::move(unique_extra)};
236
237 this->n = functions->n;
238 this->m = functions->m;
239 this->C = Box{this->n};
240 this->D = Box{this->m};
242 functions->initialize_box_C(instance.get(), this->C.lowerbound.data(),
243 this->C.upperbound.data());
245 functions->initialize_box_D(instance.get(), this->D.lowerbound.data(),
246 this->D.upperbound.data());
248 length_t = 0;
249 functions->initialize_l1_reg(instance.get(), nullptr, &);
250 if ( > 0) {
251 this->l1_reg.resize(nλ);
252 functions->initialize_l1_reg(instance.get(), this->l1_reg.data(),
253 &);
254 }
255 }
256}
257
259 rvec p) const -> real_t {
260 if (functions->eval_prox_grad_step)
261 return functions->eval_prox_grad_step(
262 instance.get(), γ, x.data(), grad_ψ.data(), x̂.data(), p.data());
263 return BoxConstrProblem<config_t>::eval_prox_grad_step(γ, x, grad_ψ, x̂, p);
264}
265
266// clang-format off
267auto DLProblem::eval_f(crvec x) const -> real_t { return functions->eval_f(instance.get(), x.data()); }
268auto DLProblem::eval_grad_f(crvec x, rvec grad_fx) const -> void { return functions->eval_grad_f(instance.get(), x.data(), grad_fx.data()); }
269auto DLProblem::eval_g(crvec x, rvec gx) const -> void { return functions->eval_g(instance.get(), x.data(), gx.data()); }
270auto 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()); }
271auto 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()); }
272auto 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()); }
274auto 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()); }
275auto 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()); }
277auto 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()); }
278auto 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()); }
280auto 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()); }
281auto DLProblem::eval_f_g(crvec x, rvec g) const -> real_t { return functions->eval_f_g(instance.get(), x.data(), g.data()); }
282auto 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()); }
283auto 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()); }
284auto 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()); }
285auto 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()); }
286auto 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()); }
287
288bool DLProblem::provides_eval_f() const { return functions->eval_f != nullptr; }
289bool DLProblem::provides_eval_grad_f() const { return functions->eval_grad_f != nullptr; }
290bool DLProblem::provides_eval_g() const { return functions->eval_g != nullptr; }
292bool DLProblem::provides_eval_jac_g() const { return functions->eval_jac_g != nullptr; }
294bool DLProblem::provides_eval_grad_gi() const { return functions->eval_grad_gi != nullptr; }
296bool DLProblem::provides_eval_hess_L() const { return functions->eval_hess_L != nullptr; }
299bool DLProblem::provides_eval_hess_ψ() const { return functions->eval_hess_ψ != nullptr; }
302bool DLProblem::provides_eval_f_g() const { return functions->eval_f_g != nullptr; }
304bool DLProblem::provides_eval_grad_L() const { return functions->eval_grad_L != nullptr; }
305bool DLProblem::provides_eval_ψ() const { return functions->eval_ψ != nullptr; }
306bool DLProblem::provides_eval_grad_ψ() const { return functions->eval_grad_ψ != nullptr; }
310// clang-format on
311
312#if ALPAQA_WITH_OCP
313
315 const std::string &function_name,
316 void *user_param) {
317 if (so_filename.empty())
318 throw std::invalid_argument("Invalid problem filename");
319 handle = load_lib(so_filename);
321 handle.get(), function_name);
322 auto r = register_func(user_param);
323 // Avoid leaking if we throw (or if std::shared_ptr constructor throws)
324 std::unique_ptr<void, void (*)(void *)> unique_inst{r.instance, r.cleanup};
325 std::unique_ptr<alpaqa_function_dict_t> unique_extra{r.extra_functions};
326 std::unique_ptr<alpaqa_exception_ptr_t> unique_exception{r.exception};
327 check_abi_version(r.abi_version);
328 // Check exception thrown by plugin
329 if (unique_exception) {
330 // Here we're facing an interesting problem: the exception we throw
331 // might propagate upwards to a point where this instance is destroyed.
332 // This would cause the shared module to be closed using dlclose.
333 // However, the exception is still stored somewhere in the memory of
334 // that module, causing a segmentation fault when accessed.
335 // To get around this issue, we need to ensure that the shared module
336 // is not closed. Here we simply leak it by storing a shared_ptr to it
337 // in a global variable.
338 leak_lib(handle);
339 std::rethrow_exception(unique_exception->exc);
340 }
341 if (!functions)
342 throw std::logic_error("alpaqa::dl::DLControlProblem::DLControlProblem:"
343 " plugin did not return any functions");
344 // Store data returned by plugin
345 instance = std::shared_ptr<void>{std::move(unique_inst)};
346 functions = r.functions;
347 extra_funcs = std::shared_ptr<function_dict_t>{std::move(unique_extra)};
348}
349
350// clang-format off
351auto DLControlProblem::get_U(Box &U) const -> void { return functions->get_U(instance.get(), U.lowerbound.data(), U.upperbound.data()); }
352auto DLControlProblem::get_D(Box &D) const -> void { return functions->get_D(instance.get(), D.lowerbound.data(), D.upperbound.data()); }
353auto DLControlProblem::get_D_N(Box &D) const -> void { return functions->get_D_N(instance.get(), D.lowerbound.data(), D.upperbound.data()); }
354auto DLControlProblem::get_x_init(rvec x_init) const -> void { return functions->get_x_init(instance.get(), x_init.data()); }
355auto 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()); }
356auto 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()); }
357auto 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()); }
358auto 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()); }
359auto DLControlProblem::eval_h_N(crvec x, rvec h) const -> void { return functions->eval_h_N(instance.get(), x.data(), h.data()); }
360auto DLControlProblem::eval_l(index_t timestep, crvec h) const -> real_t { return functions->eval_l(instance.get(), timestep, h.data()); }
361auto DLControlProblem::eval_l_N(crvec h) const -> real_t { return functions->eval_l_N(instance.get(), h.data()); }
362auto 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()); }
363auto 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()); }
364auto 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()); }
365auto 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()); }
366auto 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()); }
367auto 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()); }
368auto 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()); }
369auto 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()); }
372auto DLControlProblem::eval_constr(index_t timestep, crvec x, rvec c) const -> void { return functions->eval_constr(instance.get(), timestep, x.data(), c.data()); }
373auto DLControlProblem::eval_constr_N(crvec x, rvec c) const -> void { return functions->eval_constr_N(instance.get(), x.data(), c.data()); }
374auto 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()); }
375auto 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()); }
376auto 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()); }
377auto 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()); }
378
379bool DLControlProblem::provides_get_D() const { return functions->get_D != nullptr; }
380bool DLControlProblem::provides_get_D_N() const { return functions->get_D_N != nullptr; }
392// clang-format on
393
394#endif
395
396} // namespace alpaqa::dl
Implements common problem functions for minimization problems with box constraints.
Box C
Constraints of the decision variables, .
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.
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
DLControlProblem(const std::filesystem::path &so_filename, const std::string &function_name="register_alpaqa_control_problem", void *user_param=nullptr)
Load a problem from a shared library.
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.
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
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
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", void *user_param=nullptr)
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
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:63
#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:82
typename Conf::real_t real_t
Definition config.hpp:72
typename Conf::index_t index_t
Definition config.hpp:90
typename Conf::length_t length_t
Definition config.hpp:89
constexpr const auto inf
Definition config.hpp:98
typename Conf::rvec rvec
Definition config.hpp:77
typename Conf::crvec crvec
Definition config.hpp:78
typename Conf::crindexvec crindexvec
Definition config.hpp:93
std::shared_ptr< void > load_lib(const char *so_filename)
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:526
alpaqa_length_t(* get_S_work_size)(void *instance)
Definition dl-problem.h:504
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:521
void(* get_D)(void *instance, alpaqa_real_t *lb, alpaqa_real_t *ub)
Definition dl-problem.h:398
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:515
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:483
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:532
alpaqa_length_t(* get_R_work_size)(void *instance)
Definition dl-problem.h:502
void(* get_D_N)(void *instance, alpaqa_real_t *lb, alpaqa_real_t *ub)
Definition dl-problem.h:402
void(* eval_constr)(void *instance, alpaqa_index_t timestep, const alpaqa_real_t *x, alpaqa_real_t *c)
Definition dl-problem.h:506
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:462
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:493
void(* eval_constr_N)(void *instance, const alpaqa_real_t *x, alpaqa_real_t *c)
Definition dl-problem.h:511
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:178
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:290
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:247
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:235
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:336
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:326
alpaqa_real_t(* eval_f_g)(void *instance, const alpaqa_real_t *x, alpaqa_real_t *g)
Cost and constraints.
Definition dl-problem.h:268
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:274
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:282
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:262
alpaqa_real_t(* eval_f)(void *instance, const alpaqa_real_t *x)
Cost function.
Definition dl-problem.h:173
alpaqa_sparsity_t(* get_hess_L_sparsity)(void *instance)
Get the sparsity pattern of the Hessian of the Lagrangian.
Definition dl-problem.h:231
alpaqa_sparsity_t(* get_hess_ψ_sparsity)(void *instance)
Get the sparsity pattern of the Hessian of the augmented Lagrangian.
Definition dl-problem.h:258
alpaqa_length_t m
Number of constraints.
Definition dl-problem.h:168
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:300
alpaqa_length_t n
Number of decision variables.
Definition dl-problem.h:165
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:207
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:352
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:214
void(* eval_g)(void *instance, const alpaqa_real_t *x, alpaqa_real_t *gx)
Constraints function.
Definition dl-problem.h:184
alpaqa_sparsity_t(* get_jac_g_sparsity)(void *instance)
Get the sparsity pattern of the Jacobian of the constraints function.
Definition dl-problem.h:203
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:342
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:197
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:312
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:223
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:190
Sparsity of matrices.
Definition dl-problem.h:139