alpaqa 1.0.0a19
Nonconvex constrained optimization
Loading...
Searching...
No Matches
dl-problem.hpp
Go to the documentation of this file.
1#pragma once
2
4#include <alpaqa/dl-loader-export.h>
9#include <alpaqa/util/dl.hpp>
10
11#include <filesystem>
12#include <memory>
13#include <span>
14#include <stdexcept>
15#include <string>
16#include <string_view>
17#include <type_traits>
18
19namespace alpaqa::dl {
20
21using util::dynamic_load_error;
22
23struct DL_LOADER_EXPORT invalid_abi_error : dynamic_load_error {
24 using dynamic_load_error::dynamic_load_error;
25};
26
28 public:
29 /// Unique type for calling an extra function that is a member function.
30 struct instance_t;
31
32 ExtraFuncs() = default;
33 ExtraFuncs(std::shared_ptr<function_dict_t> &&extra_funcs)
34 : extra_functions(std::move(extra_funcs)) {}
35
36 /// An associative array of additional functions exposed by the problem.
37 std::shared_ptr<function_dict_t> extra_functions;
38
39 template <class Signature>
40 requires std::is_function_v<Signature>
41 const std::function<Signature> &extra_func(const std::string &name) const {
42 if (!extra_functions)
43 throw std::out_of_range("DLProblem: no extra functions");
44 auto it = extra_functions->dict.find(name);
45 if (it == extra_functions->dict.end())
46 throw std::out_of_range("DLProblem: no extra function named \"" +
47 name + '"');
48 try {
50 return std::any_cast<const func_t &>(it->second).function;
51 } catch (const std::bad_any_cast &) {
52 throw std::logic_error(
53 "DLProblem: incorrect type for extra function \"" + name +
54 "\" (stored type: " + demangled_typename(it->second.type()) +
55 ')');
56 }
57 }
58
59 template <class Func>
60 struct FuncTag {};
61
62 template <class Ret, class... FArgs, class... Args>
63 decltype(auto)
64 call_extra_func_helper(const void *instance,
65 FuncTag<Ret(const instance_t *, FArgs...)>,
66 const std::string &name, Args &&...args) const {
67 return extra_func<Ret(const void *, FArgs...)>(name)(
68 instance, std::forward<Args>(args)...);
69 }
70
71 template <class Ret, class... FArgs, class... Args>
72 decltype(auto)
73 call_extra_func_helper(void *instance, FuncTag<Ret(instance_t *, FArgs...)>,
74 const std::string &name, Args &&...args) {
75 return extra_func<Ret(void *, FArgs...)>(name)(
76 instance, std::forward<Args>(args)...);
77 }
78
79 template <class Ret, class... FArgs, class... Args>
80 decltype(auto) call_extra_func_helper(const void *, FuncTag<Ret(FArgs...)>,
81 const std::string &name,
82 Args &&...args) const {
83 return extra_func<Ret(FArgs...)>(name)(std::forward<Args>(args)...);
84 }
85};
86
87/// Class that loads a problem using `dlopen`.
88///
89/// The shared library should export a C function with the name @c function_name
90/// that accepts a void pointer with user data, and returns a struct of type
91/// @ref alpaqa_problem_register_t that contains all data to represent the
92/// problem, as well as function pointers for all required operations.
93/// See @ref C++/DLProblem/main.cpp and
94/// @ref problems/sparse-logistic-regression.cpp for examples.
95///
96/// @note Copies are shallow, they all share the same problem instance, take
97/// that into account when using multiple threads.
98///
99/// @ingroup grp_Problems
100/// @see @ref TypeErasedProblem
101/// @see @ref alpaqa_problem_functions_t
102/// @see @ref alpaqa_problem_register_t
103class DL_LOADER_EXPORT DLProblem : public BoxConstrProblem<DefaultConfig> {
104 public:
107
108 /// Load a problem from a shared library.
109 DLProblem(
110 /// Filename of the shared library to load.
111 const std::filesystem::path &so_filename,
112 /// Name of the problem registration function.
113 /// Should have signature
114 /// `alpaqa_problem_register_t(alpaqa_register_arg_t user_param)`.
115 const std::string &function_name = "register_alpaqa_problem",
116 /// Pointer to custom user data to pass to the registration function.
117 alpaqa_register_arg_t user_param = {});
118 /// Load a problem from a shared library.
119 DLProblem(
120 /// Filename of the shared library to load.
121 const std::filesystem::path &so_filename,
122 /// Name of the problem registration function.
123 /// Should have signature
124 /// `alpaqa_problem_register_t(alpaqa_register_arg_t user_param)`.
125 const std::string &function_name,
126 /// Custom user data to pass to the registration function.
127 std::any &user_param);
128 /// Load a problem from a shared library.
129 DLProblem(
130 /// Filename of the shared library to load.
131 const std::filesystem::path &so_filename,
132 /// Name of the problem registration function.
133 /// Should have signature
134 /// `alpaqa_problem_register_t(alpaqa_register_arg_t user_param)`.
135 const std::string &function_name,
136 /// Custom string arguments to pass to the registration function.
137 std::span<std::string_view> user_param);
138
139 private:
140 /// Path to the shared module file.
141 std::filesystem::path file;
142 /// Handle to the shared module defining the problem.
143 std::shared_ptr<void> handle;
144 /// Problem instance created by the registration function, including the
145 /// deleter to destroy it.
146 std::shared_ptr<void> instance;
147 /// Pointer to the struct of function pointers for evaluating the objective,
148 /// constraints, their gradients, etc.
149 problem_functions_t *functions = nullptr;
150 /// Dictionary of extra functions that were registered by the problem.
152
153 public:
154 // clang-format off
155 void eval_proj_diff_g(crvec z, rvec e) const;
156 void eval_proj_multipliers(rvec y, real_t M) const;
157 real_t eval_prox_grad_step(real_t γ, crvec x, crvec grad_ψ, rvec x̂, rvec p) const;
158 index_t eval_inactive_indices_res_lna(real_t γ, crvec x, crvec grad_ψ, rindexvec J) const;
159 real_t eval_f(crvec x) const;
160 void eval_grad_f(crvec x, rvec grad_fx) const;
161 void eval_g(crvec x, rvec gx) const;
162 void eval_grad_g_prod(crvec x, crvec y, rvec grad_gxy) const;
163 void eval_jac_g(crvec x, rvec J_values) const;
164 Sparsity get_jac_g_sparsity() const;
165 void eval_grad_gi(crvec x, index_t i, rvec grad_gi) const;
166 void eval_hess_L_prod(crvec x, crvec y, real_t scale, crvec v, rvec Hv) const;
167 void eval_hess_L(crvec x, crvec y, real_t scale, rvec H_values) const;
168 Sparsity get_hess_L_sparsity() const;
169 void eval_hess_ψ_prod(crvec x, crvec y, crvec Σ, real_t scale, crvec v, rvec Hv) const;
170 void eval_hess_ψ(crvec x, crvec y, crvec Σ, real_t scale, rvec H_values) const;
171 Sparsity get_hess_ψ_sparsity() const;
172 real_t eval_f_grad_f(crvec x, rvec grad_fx) const;
173 real_t eval_f_g(crvec x, rvec g) const;
174 void eval_grad_f_grad_g_prod(crvec x, crvec y, rvec grad_f, rvec grad_gxy) const;
175 void eval_grad_L(crvec x, crvec y, rvec grad_L, rvec work_n) const;
176 real_t eval_ψ(crvec x, crvec y, crvec Σ, rvec ŷ) const;
177 void eval_grad_ψ(crvec x, crvec y, crvec Σ, rvec grad_ψ, rvec work_n, rvec work_m) const;
178 real_t eval_ψ_grad_ψ(crvec x, crvec y, crvec Σ, rvec grad_ψ, rvec work_n, rvec work_m) const;
179 std::string get_name() const;
180
181 [[nodiscard]] bool provides_eval_f() const;
182 [[nodiscard]] bool provides_eval_grad_f() const;
183 [[nodiscard]] bool provides_eval_g() const;
184 [[nodiscard]] bool provides_eval_grad_g_prod() const;
185 [[nodiscard]] bool provides_eval_jac_g() const;
186 [[nodiscard]] bool provides_get_jac_g_sparsity() const;
187 [[nodiscard]] bool provides_eval_grad_gi() const;
188 [[nodiscard]] bool provides_eval_hess_L_prod() const;
189 [[nodiscard]] bool provides_eval_hess_L() const;
190 [[nodiscard]] bool provides_get_hess_L_sparsity() const;
191 [[nodiscard]] bool provides_eval_hess_ψ_prod() const;
192 [[nodiscard]] bool provides_eval_hess_ψ() const;
193 [[nodiscard]] bool provides_get_hess_ψ_sparsity() const;
194 [[nodiscard]] bool provides_eval_f_grad_f() const;
195 [[nodiscard]] bool provides_eval_f_g() const;
196 [[nodiscard]] bool provides_eval_grad_f_grad_g_prod() const;
197 [[nodiscard]] bool provides_eval_grad_L() const;
198 [[nodiscard]] bool provides_eval_ψ() const;
199 [[nodiscard]] bool provides_eval_grad_ψ() const;
200 [[nodiscard]] bool provides_eval_ψ_grad_ψ() const;
201 [[nodiscard]] bool provides_get_box_C() const;
202 [[nodiscard]] bool provides_get_box_D() const;
203 [[nodiscard]] bool provides_eval_inactive_indices_res_lna() const;
204 // clang-format on
205
206 using instance_t = ExtraFuncs::instance_t;
207
208 template <class Signature, class... Args>
209 decltype(auto) call_extra_func(const std::string &name,
210 Args &&...args) const {
211 return call_extra_func_helper(instance.get(),
213 std::forward<Args>(args)...);
214 }
215
216 template <class Signature, class... Args>
217 decltype(auto) call_extra_func(const std::string &name, Args &&...args) {
218 return extra_funcs.call_extra_func_helper(
219 instance.get(), ExtraFuncs::FuncTag<Signature>{}, name,
220 std::forward<Args>(args)...);
221 }
222};
223
224#if ALPAQA_WITH_OCP
225
226/// Class that loads an optimal control problem using `dlopen`.
227///
228/// The shared library should export a C function with the name @c function_name
229/// that accepts a void pointer with user data, and returns a struct of type
230/// @ref alpaqa_control_problem_register_t that contains all data to represent
231/// the problem, as well as function pointers for all required operations.
232///
233/// @note Copies are shallow, they all share the same problem instance, take
234/// that into account when using multiple threads.
235///
236/// @ingroup grp_Problems
237/// @see @ref TypeErasedControlProblem
239 public:
242
243 /// Load a problem from a shared library.
245 /// Filename of the shared library to load.
246 const std::filesystem::path &so_filename,
247 /// Name of the problem registration function.
248 /// Should have signature
249 /// `alpaqa_control_problem_register_t(alpaqa_register_arg_t user_param)`.
250 const std::string &function_name = "register_alpaqa_control_problem",
251 /// Pointer to custom user data to pass to the registration function.
252 alpaqa_register_arg_t user_param = {});
253
254 private:
255 /// Handle to the shared module defining the problem.
256 std::shared_ptr<void> handle;
257 /// Problem instance created by the registration function, including the
258 /// deleter to destroy it.
259 std::shared_ptr<void> instance;
260 /// Pointer to the struct of function pointers for evaluating the objective,
261 /// constraints, their gradients, etc.
262 control_problem_functions_t *functions = nullptr;
263 /// Dictionary of extra functions that were registered by the problem.
265
266 public:
267 length_t get_N() const { return functions->N; }
268 length_t get_nx() const { return functions->nx; }
269 length_t get_nu() const { return functions->nu; }
270 length_t get_nh() const { return functions->nh; }
271 length_t get_nh_N() const { return functions->nh_N; }
272 length_t get_nc() const { return functions->nc; }
273 length_t get_nc_N() const { return functions->nc_N; }
274
275 void check() const {} // TODO
276
277 // clang-format off
278 void get_U(Box &U) const;
279 void get_D(Box &D) const;
280 void get_D_N(Box &D) const;
281 void get_x_init(rvec x_init) const;
282 void eval_f(index_t timestep, crvec x, crvec u, rvec fxu) const;
283 void eval_jac_f(index_t timestep, crvec x, crvec u, rmat J_fxu) const;
284 void eval_grad_f_prod(index_t timestep, crvec x, crvec u, crvec p, rvec grad_fxu_p) const;
285 void eval_h(index_t timestep, crvec x, crvec u, rvec h) const;
286 void eval_h_N(crvec x, rvec h) const;
287 [[nodiscard]] real_t eval_l(index_t timestep, crvec h) const;
288 [[nodiscard]] real_t eval_l_N(crvec h) const;
289 void eval_qr(index_t timestep, crvec xu, crvec h, rvec qr) const;
290 void eval_q_N(crvec x, crvec h, rvec q) const;
291 void eval_add_Q(index_t timestep, crvec xu, crvec h, rmat Q) const;
292 void eval_add_Q_N(crvec x, crvec h, rmat Q) const;
293 void eval_add_R_masked(index_t timestep, crvec xu, crvec h, crindexvec mask, rmat R, rvec work) const;
294 void eval_add_S_masked(index_t timestep, crvec xu, crvec h, crindexvec mask, rmat S, rvec work) const;
295 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;
296 void eval_add_S_prod_masked(index_t timestep, crvec xu, crvec h, crindexvec mask_K, crvec v, rvec out, rvec work) const;
297 [[nodiscard]] length_t get_R_work_size() const;
298 [[nodiscard]] length_t get_S_work_size() const;
299 void eval_constr(index_t timestep, crvec x, rvec c) const;
300 void eval_constr_N(crvec x, rvec c) const;
301 void eval_grad_constr_prod(index_t timestep, crvec x, crvec p, rvec grad_cx_p) const;
302 void eval_grad_constr_prod_N(crvec x, crvec p, rvec grad_cx_p) const;
303 void eval_add_gn_hess_constr(index_t timestep, crvec x, crvec M, rmat out) const;
304 void eval_add_gn_hess_constr_N(crvec x, crvec M, rmat out) const;
305
306 [[nodiscard]] bool provides_get_D() const;
307 [[nodiscard]] bool provides_get_D_N() const;
308 [[nodiscard]] bool provides_eval_add_Q_N() const;
309 [[nodiscard]] bool provides_eval_add_R_prod_masked() const;
310 [[nodiscard]] bool provides_eval_add_S_prod_masked() const;
311 [[nodiscard]] bool provides_get_R_work_size() const;
312 [[nodiscard]] bool provides_get_S_work_size() const;
313 [[nodiscard]] bool provides_eval_constr() const;
314 [[nodiscard]] bool provides_eval_constr_N() const;
315 [[nodiscard]] bool provides_eval_grad_constr_prod() const;
316 [[nodiscard]] bool provides_eval_grad_constr_prod_N() const;
317 [[nodiscard]] bool provides_eval_add_gn_hess_constr() const;
318 [[nodiscard]] bool provides_eval_add_gn_hess_constr_N() const;
319 // clang-format on
320
321 using instance_t = ExtraFuncs::instance_t;
322
323 template <class Signature, class... Args>
324 decltype(auto) call_extra_func(const std::string &name,
325 Args &&...args) const {
326 return extra_funcs.call_extra_func_helper(
327 instance.get(), ExtraFuncs::FuncTag<Signature>{}, name,
328 std::forward<Args>(args)...);
329 }
330
331 template <class Signature, class... Args>
332 decltype(auto) call_extra_func(const std::string &name, Args &&...args) {
333 return extra_funcs.call_extra_func_helper(
334 instance.get(), ExtraFuncs::FuncTag<Signature>{}, name,
335 std::forward<Args>(args)...);
336 }
337};
338
339#endif
340
341} // namespace alpaqa::dl
Implements common problem functions for minimization problems with box constraints.
Class that loads an optimal control problem using dlopen.
ExtraFuncs::instance_t instance_t
std::shared_ptr< void > instance
Problem instance created by the registration function, including the deleter to destroy it.
decltype(auto) call_extra_func(const std::string &name, Args &&...args) const
decltype(auto) call_extra_func(const std::string &name, Args &&...args)
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.
Class that loads a problem using dlopen.
ExtraFuncs::instance_t instance_t
std::shared_ptr< void > instance
Problem instance created by the registration function, including the deleter to destroy it.
decltype(auto) call_extra_func(const std::string &name, Args &&...args) const
std::filesystem::path file
Path to the shared module file.
decltype(auto) call_extra_func(const std::string &name, Args &&...args)
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.
decltype(auto) call_extra_func_helper(const void *instance, FuncTag< Ret(const instance_t *, FArgs...)>, const std::string &name, Args &&...args) const
decltype(auto) call_extra_func_helper(void *instance, FuncTag< Ret(instance_t *, FArgs...)>, const std::string &name, Args &&...args)
const std::function< Signature > & extra_func(const std::string &name) const
std::shared_ptr< function_dict_t > extra_functions
An associative array of additional functions exposed by the problem.
ExtraFuncs(std::shared_ptr< function_dict_t > &&extra_funcs)
decltype(auto) call_extra_func_helper(const void *, FuncTag< Ret(FArgs...)>, const std::string &name, Args &&...args) const
#define USING_ALPAQA_CONFIG(Conf)
Definition config.hpp:77
std::string demangled_typename(const std::type_info &t)
Get the pretty name of the given type as a string.
User-provided argument that is passed to the problem registration functions.
Definition dl-problem.h:65
std::function< Signature > function
Definition dl-problem.h:700
Custom type for which we can export the RTTI to support std::any across shared library boundaries whe...
Definition dl-problem.h:699
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
Result(const MemberGetter &) func_t
Double-precision double configuration.
Definition config.hpp:174
Stores any of the supported sparsity patterns.
Definition sparsity.hpp:106
C API providing function pointers to problem functions.
Definition dl-problem.h:210