alpaqa 1.0.0a14
Nonconvex constrained optimization
Loading...
Searching...
No Matches
dl-problem.hpp
Go to the documentation of this file.
1#pragma once
2
8
9#include <memory>
10#include <stdexcept>
11#include <string>
12#include <string_view>
13#include <type_traits>
14
15namespace alpaqa::dl {
16
18 public:
19 /// Unique type for calling an extra function that is a member function.
20 struct instance_t;
21
22 ExtraFuncs() = default;
23 ExtraFuncs(std::shared_ptr<function_dict_t> &&extra_funcs)
24 : extra_functions(std::move(extra_funcs)) {}
25
26 /// An associative array of additional functions exposed by the problem.
27 std::shared_ptr<function_dict_t> extra_functions;
28
29 template <class Signature>
30 requires std::is_function_v<Signature>
31 const std::function<Signature> &extra_func(const std::string &name) const {
32 if (!extra_functions)
33 throw std::out_of_range("DLProblem: no extra functions");
34 auto it = extra_functions->dict.find(name);
35 if (it == extra_functions->dict.end())
36 throw std::out_of_range("DLProblem: no extra function named \"" +
37 name + '"');
38 try {
39 return std::any_cast<const std::function<Signature> &>(it->second);
40 } catch (const std::bad_any_cast &e) {
41 throw std::logic_error(
42 "DLProblem: incorrect type for extra function \"" + name +
43 "\" (stored type: " + demangled_typename(it->second.type()) +
44 ')');
45 }
46 }
47
48 template <class Func>
49 struct FuncTag {};
50
51 template <class Ret, class... FArgs, class... Args>
52 decltype(auto)
53 call_extra_func_helper(const void *instance,
54 FuncTag<Ret(const instance_t *, FArgs...)>,
55 const std::string &name, Args &&...args) const {
56 return extra_func<Ret(const void *, FArgs...)>(name)(
57 instance, std::forward<Args>(args)...);
58 }
59
60 template <class Ret, class... FArgs, class... Args>
61 decltype(auto)
62 call_extra_func_helper(void *instance, FuncTag<Ret(instance_t *, FArgs...)>,
63 const std::string &name, Args &&...args) {
64 return extra_func<Ret(void *, FArgs...)>(name)(
65 instance, std::forward<Args>(args)...);
66 }
67
68 template <class Ret, class... FArgs, class... Args>
69 decltype(auto) call_extra_func_helper(const void *, FuncTag<Ret(FArgs...)>,
70 const std::string &name,
71 Args &&...args) const {
72 return extra_func<Ret(FArgs...)>(name)(std::forward<Args>(args)...);
73 }
74};
75
76/// Class that loads a problem using `dlopen`.
77///
78/// The shared library should export a C function with the name @c function_name
79/// that accepts a void pointer with user data, and returns a struct of type
80/// @ref alpaqa_problem_register_t that contains all data to represent the
81/// problem, as well as function pointers for all required operations.
82/// See @ref C++/DLProblem/main.cpp and
83/// @ref problems/sparse-logistic-regression.cpp for examples.
84///
85/// @note Copies are shallow, they all share the same problem instance, take
86/// that into account when using multiple threads.
87///
88/// @ingroup grp_Problems
89/// @see @ref TypeErasedProblem
90/// @see @ref alpaqa_problem_functions_t
91/// @see @ref alpaqa_problem_register_t
92class DLProblem : public BoxConstrProblem<DefaultConfig> {
93 public:
96
97 /// Load a problem from a shared library.
99 /// Filename of the shared library to load.
100 const std::string &so_filename,
101 /// Name of the problem registration function.
102 /// Should have signature `alpaqa_problem_register_t(void *)`.
103 const std::string &function_name = "register_alpaqa_problem",
104 /// Pointer to custom user data to pass to the registration function.
105 void *user_param = nullptr);
106
107 private:
108 /// Handle to the shared module defining the problem.
109 std::shared_ptr<void> handle;
110 /// Problem instance created by the registration function, including the
111 /// deleter to destroy it.
112 std::shared_ptr<void> instance;
113 /// Pointer to the struct of function pointers for evaluating the objective,
114 /// constraints, their gradients, etc.
116 /// Dictionary of extra functions that were registered by the problem.
118
119 public:
120 // clang-format off
121 real_t eval_prox_grad_step(real_t γ, crvec x, crvec grad_ψ, rvec x̂, rvec p) const;
122 real_t eval_f(crvec x) const;
123 void eval_grad_f(crvec x, rvec grad_fx) const;
124 void eval_g(crvec x, rvec gx) const;
125 void eval_grad_g_prod(crvec x, crvec y, rvec grad_gxy) const;
126 void eval_jac_g(crvec x, rvec J_values) const;
128 void eval_grad_gi(crvec x, index_t i, rvec grad_gi) const;
129 void eval_hess_L_prod(crvec x, crvec y, real_t scale, crvec v, rvec Hv) const;
130 void eval_hess_L(crvec x, crvec y, real_t scale, rvec H_values) const;
132 void eval_hess_ψ_prod(crvec x, crvec y, crvec Σ, real_t scale, crvec v, rvec Hv) const;
133 void eval_hess_ψ(crvec x, crvec y, crvec Σ, real_t scale, rvec H_values) const;
136 real_t eval_f_g(crvec x, rvec g) const;
137 void eval_grad_f_grad_g_prod(crvec x, crvec y, rvec grad_f, rvec grad_gxy) const;
138 void eval_grad_L(crvec x, crvec y, rvec grad_L, rvec work_n) const;
139 real_t eval_ψ(crvec x, crvec y, crvec Σ, rvec ŷ) const;
140 void eval_grad_ψ(crvec x, crvec y, crvec Σ, rvec grad_ψ, rvec work_n, rvec work_m) const;
141 real_t eval_ψ_grad_ψ(crvec x, crvec y, crvec Σ, rvec grad_ψ, rvec work_n, rvec work_m) const;
142
143 [[nodiscard]] bool provides_eval_f() const;
144 [[nodiscard]] bool provides_eval_grad_f() const;
145 [[nodiscard]] bool provides_eval_g() const;
146 [[nodiscard]] bool provides_eval_grad_g_prod() const;
147 [[nodiscard]] bool provides_eval_jac_g() const;
148 [[nodiscard]] bool provides_get_jac_g_sparsity() const;
149 [[nodiscard]] bool provides_eval_grad_gi() const;
150 [[nodiscard]] bool provides_eval_hess_L_prod() const;
151 [[nodiscard]] bool provides_eval_hess_L() const;
153 [[nodiscard]] bool provides_eval_hess_ψ_prod() const;
154 [[nodiscard]] bool provides_eval_hess_ψ() const;
156 [[nodiscard]] bool provides_eval_f_grad_f() const;
157 [[nodiscard]] bool provides_eval_f_g() const;
159 [[nodiscard]] bool provides_eval_grad_L() const;
160 [[nodiscard]] bool provides_eval_ψ() const;
161 [[nodiscard]] bool provides_eval_grad_ψ() const;
162 [[nodiscard]] bool provides_eval_ψ_grad_ψ() const;
163 [[nodiscard]] bool provides_get_box_C() const;
164 // clang-format on
165
166 using instance_t = ExtraFuncs::instance_t;
167
168 template <class Signature, class... Args>
169 decltype(auto) call_extra_func(const std::string &name,
170 Args &&...args) const {
171 return call_extra_func_helper(instance.get(),
173 std::forward<Args>(args)...);
174 }
175
176 template <class Signature, class... Args>
177 decltype(auto) call_extra_func(const std::string &name, Args &&...args) {
180 std::forward<Args>(args)...);
181 }
182};
183
184#if ALPAQA_WITH_OCP
185
186/// Class that loads an optimal control problem using `dlopen`.
187///
188/// The shared library should export a C function with the name @c function_name
189/// that accepts a void pointer with user data, and returns a struct of type
190/// @ref alpaqa_control_problem_register_t that contains all data to represent
191/// the problem, as well as function pointers for all required operations.
192///
193/// @note Copies are shallow, they all share the same problem instance, take
194/// that into account when using multiple threads.
195///
196/// @ingroup grp_Problems
197/// @see @ref TypeErasedControlProblem
199 public:
202
203 /// Load a problem from a shared library.
205 /// Filename of the shared library to load.
206 const std::string &so_filename,
207 /// Name of the problem registration function.
208 /// Should have signature `alpaqa_control_problem_register_t(void *)`.
209 const std::string &function_name = "register_alpaqa_control_problem",
210 /// Pointer to custom user data to pass to the registration function.
211 void *user_param = nullptr);
212
213 private:
214 /// Handle to the shared module defining the problem.
215 std::shared_ptr<void> handle;
216 /// Problem instance created by the registration function, including the
217 /// deleter to destroy it.
218 std::shared_ptr<void> instance;
219 /// Pointer to the struct of function pointers for evaluating the objective,
220 /// constraints, their gradients, etc.
222 /// Dictionary of extra functions that were registered by the problem.
224
225 public:
226 length_t get_N() const { return functions->N; }
227 length_t get_nx() const { return functions->nx; }
228 length_t get_nu() const { return functions->nu; }
229 length_t get_nh() const { return functions->nh; }
230 length_t get_nh_N() const { return functions->nh_N; }
231 length_t get_nc() const { return functions->nc; }
232 length_t get_nc_N() const { return functions->nc_N; }
233
234 void check() const {} // TODO
235
236 // clang-format off
237 void get_U(Box &U) const;
238 void get_D(Box &D) const;
239 void get_D_N(Box &D) const;
240 void get_x_init(rvec x_init) const;
241 void eval_f(index_t timestep, crvec x, crvec u, rvec fxu) const;
242 void eval_jac_f(index_t timestep, crvec x, crvec u, rmat J_fxu) const;
244 void eval_h(index_t timestep, crvec x, crvec u, rvec h) const;
245 void eval_h_N(crvec x, rvec h) const;
247 [[nodiscard]] real_t eval_l_N(crvec h) const;
248 void eval_qr(index_t timestep, crvec xu, crvec h, rvec qr) const;
249 void eval_q_N(crvec x, crvec h, rvec q) const;
250 void eval_add_Q(index_t timestep, crvec xu, crvec h, rmat Q) const;
251 void eval_add_Q_N(crvec x, crvec h, rmat Q) const;
252 void eval_add_R_masked(index_t timestep, crvec xu, crvec h, crindexvec mask, rmat R, rvec work) const;
253 void eval_add_S_masked(index_t timestep, crvec xu, crvec h, crindexvec mask, rmat S, rvec work) const;
258 void eval_constr(index_t timestep, crvec x, rvec c) const;
259 void eval_constr_N(crvec x, rvec c) const;
264
265 [[nodiscard]] bool provides_get_D() const;
266 [[nodiscard]] bool provides_get_D_N() const;
267 [[nodiscard]] bool provides_eval_add_Q_N() const;
270 [[nodiscard]] bool provides_get_R_work_size() const;
271 [[nodiscard]] bool provides_get_S_work_size() const;
272 [[nodiscard]] bool provides_eval_constr() const;
273 [[nodiscard]] bool provides_eval_constr_N() const;
278 // clang-format on
279
280 using instance_t = ExtraFuncs::instance_t;
281
282 template <class Signature, class... Args>
283 decltype(auto) call_extra_func(const std::string &name,
284 Args &&...args) const {
287 std::forward<Args>(args)...);
288 }
289
290 template <class Signature, class... Args>
291 decltype(auto) call_extra_func(const std::string &name, Args &&...args) {
294 std::forward<Args>(args)...);
295 }
296};
297
298#endif
299
300} // namespace alpaqa::dl
Implements common problem functions for minimization problems with box constraints.
Class that loads an optimal control problem using dlopen.
void eval_add_S_masked(index_t timestep, crvec xu, crvec h, crindexvec mask, rmat S, rvec work) const
ExtraFuncs::instance_t instance_t
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,...
decltype(auto) call_extra_func(const std::string &name, Args &&...args) const
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
decltype(auto) call_extra_func(const std::string &name, Args &&...args)
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
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
ExtraFuncs::instance_t instance_t
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
decltype(auto) call_extra_func(const std::string &name, Args &&...args) 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
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
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.
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
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:56
std::string demangled_typename(const std::type_info &t)
Get the pretty name of the given type as a string.
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
Double-precision double configuration.
Definition config.hpp:135
Stores any of the supported sparsity patterns.
Definition sparsity.hpp:106
C API providing function pointers to problem functions.
Definition dl-problem.h:157