alpaqa 1.0.0a8
Nonconvex constrained optimization
Loading...
Searching...
No Matches
ocproblem.hpp
Go to the documentation of this file.
1#pragma once
2
11#include <array>
12#include <concepts>
13#include <stdexcept>
14#include <type_traits>
15
16#if !ALPAQA_WITH_OCP
17#error "OCP support disabled"
18#endif
19
20#ifndef NDEBUG
21#include <iostream>
22#endif
23
24namespace alpaqa {
25
26template <Config Conf>
27struct OCPDim {
30};
31
32template <Config Conf>
36
37 template <class F>
39 template <class F>
42
43 // clang-format off
51 get_D = nullptr;
56 required_const_function_t<void(index_t timestep, crvec x, crvec u, rvec fxu)>
58 required_const_function_t<void(index_t timestep, crvec x, crvec u, rmat J_fxu)>
60 required_const_function_t<void(index_t timestep, crvec x, crvec u, crvec p, rvec grad_fxu_p)>
62 optional_const_function_t<void(index_t timestep, crvec x, crvec u, rvec h)>
63 eval_h = nullptr;
65 eval_h_N = nullptr;
70 required_const_function_t<void(index_t timestep, crvec xu, crvec h, rvec qr)>
74 required_const_function_t<void(index_t timestep, crvec xu, crvec h, rmat Q)>
78 required_const_function_t<void(index_t timestep, crvec xu, crvec h, crindexvec mask, rmat R, rvec work)>
80 required_const_function_t<void(index_t timestep, crvec xu, crvec h, crindexvec mask, rmat S, rvec work)>
82 optional_const_function_t<void(index_t timestep, crvec xu, crvec h, crindexvec mask_J, crindexvec mask_K, crvec v, rvec out, rvec work)>
84 optional_const_function_t<void(index_t timestep, crvec xu, crvec h, crindexvec mask_K, crvec v, rvec out, rvec work)>
90 optional_const_function_t<void(index_t timestep, crvec x, rvec c)>
91 eval_constr = nullptr;
94 optional_const_function_t<void(index_t timestep, crvec x, crvec p, rvec grad_cx_p)>
96 optional_const_function_t<void(crvec x, crvec p, rvec grad_cx_p)>
98 optional_const_function_t<void(index_t timestep, crvec x, crvec M, rmat out)>
104 // clang-format on
105
107
108 template <class P>
109 ControlProblemVTable(std::in_place_t, P &p) : util::BasicVTable{std::in_place, p} {
113 ALPAQA_TE_OPTIONAL_METHOD(*this, P, get_D, p);
119 ALPAQA_TE_OPTIONAL_METHOD(*this, P, eval_h, p);
140 N = p.get_N();
141 nu = p.get_nu();
142 nx = p.get_nx();
143 nh = p.get_nh();
144 nh_N = p.get_nh_N();
145 nc = p.get_nc();
146 nc_N = p.get_nc_N();
147 if (nc > 0 && get_D == nullptr)
148 throw std::runtime_error("ControlProblem: missing 'get_D'");
149 if (nc > 0 && eval_constr == nullptr)
150 throw std::runtime_error("ControlProblem: missing 'eval_constr'");
151 if (nc > 0 && eval_grad_constr_prod == nullptr)
152 throw std::runtime_error("ControlProblem: missing 'eval_grad_constr_prod'");
153 if (nh > 0 && eval_h == nullptr)
154 throw std::runtime_error("ControlProblem: missing 'eval_h'");
155 if (nh_N > 0 && eval_h_N == nullptr)
156 throw std::runtime_error("ControlProblem: missing 'eval_h_N'");
157 }
159
160 ALPAQA_EXPORT static void default_get_D_N(const void *self, Box &D,
161 const ControlProblemVTable &vtable);
162 ALPAQA_EXPORT static void default_eval_add_Q_N(const void *self, crvec x, crvec h, rmat Q,
163 const ControlProblemVTable &vtable);
164 ALPAQA_EXPORT static void default_eval_add_R_prod_masked(const void *, index_t, crvec, crvec,
166 rvec, const ControlProblemVTable &);
167 ALPAQA_EXPORT static void default_eval_add_S_prod_masked(const void *, index_t, crvec, crvec,
169 const ControlProblemVTable &);
170 [[nodiscard]] ALPAQA_EXPORT static length_t
171 default_get_R_work_size(const void *, const ControlProblemVTable &);
172 [[nodiscard]] ALPAQA_EXPORT static length_t
173 default_get_S_work_size(const void *, const ControlProblemVTable &);
174 ALPAQA_EXPORT static void default_eval_constr_N(const void *self, crvec x, rvec c,
175 const ControlProblemVTable &vtable);
176 ALPAQA_EXPORT static void default_eval_grad_constr_prod_N(const void *self, crvec x, crvec p,
177 rvec grad_cx_p,
178 const ControlProblemVTable &vtable);
179 ALPAQA_EXPORT static void default_eval_add_gn_hess_constr_N(const void *self, crvec x, crvec M,
180 rmat out,
181 const ControlProblemVTable &vtable);
182};
183
188#ifdef ALPAQA_WITH_QUAD_PRECISION
190#endif
191
192/**
193 * Nonlinear optimal control problem with finite horizon @f$ N @f$.
194 * @f[
195 * \newcommand\U{U}
196 * \newcommand\D{D}
197 * \newcommand\nnu{{n_u}}
198 * \newcommand\nnx{{n_x}}
199 * \newcommand\nny{{n_y}}
200 * \newcommand\xinit{x_\text{init}}
201 * \begin{equation}\label{eq:OCP} \tag{OCP}\hspace{-0.8em}
202 * \begin{aligned}
203 * &\minimize_{u,x} && \sum_{k=0}^{N-1} \ell_k\big(h_k(x^k, u^k)\big) + \ell_N\big(h_N(x^N)\big)\hspace{-0.8em} \\
204 * &\subjto && u^k \in \U \\
205 * &&& c_k(x^k) \in \D \\
206 * &&& c_N(x^N) \in \D_N \\
207 * &&& x^0 = \xinit \\
208 * &&& x^{k+1} = f(x^k, u^k) \quad\quad (0 \le k \lt N)
209 * \end{aligned}
210 * \end{equation}
211 * @f]
212 *
213 * The function @f$ f : \R^\nnx \times \R^\nnu \to \R^\nnx @f$ models the
214 * discrete-time, nonlinear dynamics of the system, which starts from an initial
215 * state @f$ \xinit @f$.
216 * The functions @f$ h_k : \R^\nnx \times \R^\nnu \to \R^{n_h} @f$ for
217 * @f$ 0 \le k \lt N @f$ and @f$ h_N : \R^\nnx \to \R^{n_h^N} @f$ can be used to
218 * represent the (possibly time-varying) output mapping of the system,
219 * and the convex functions @f$ \ell_k : \R^{n_h} \to \R @f$ and
220 * @f$ \ell_N : \R^{n_h^N} \to \R @f$ define the stage costs and the terminal
221 * cost respectively. Stage constraints and terminal constraints are represented
222 * by the functions @f$ c_k : \R^{n_x} \to \R^{n_c} @f$ and
223 * @f$ c_N : \R^{n_x} \to \R^{n_c^N} @f$, and the boxes @f$ D @f$ and
224 * @f$ D_N @f$.
225 *
226 * Additional functions for computing Gauss-Newton approximations of the cost
227 * Hessian are included as well:
228 * @f[ \begin{aligned}
229 * q^k &\defeq \tp{\jac_{h_k}^x\!(\barxuk)} \nabla \ell_k(\hhbar^k) \\
230 * r^k &\defeq \tp{\jac_{h_k}^u\!(\barxuk)} \nabla \ell_k(\hhbar^k) \\
231 * \Lambda_k &\defeq \partial^2 \ell_k(\hhbar^k) \\
232 * Q_k &\defeq \tp{\jac_{h_k}^x\!(\barxuk)} \Lambda_k\, \jac_{h_k}^x\!(\barxuk) \\
233 * S_k &\defeq \tp{\jac_{h_k}^u\!(\barxuk)} \Lambda_k\, \jac_{h_k}^x\!(\barxuk) \\
234 * R_k &\defeq \tp{\jac_{h_k}^u\!(\barxuk)} \Lambda_k\, \jac_{h_k}^u\!(\barxuk). \\
235 * \end{aligned} @f]
236 * See @cite pas2022gaussnewton for more details.
237 *
238 * @ingroup grp_Problems
239 */
240template <Config Conf = DefaultConfig, class Allocator = std::allocator<std::byte>>
241class TypeErasedControlProblem : public util::TypeErased<ControlProblemVTable<Conf>, Allocator> {
242 public:
245 using allocator_type = Allocator;
246 using Box = typename VTable::Box;
250
251 protected:
252 using TypeErased::call;
253 using TypeErased::self;
254 using TypeErased::vtable;
255
256 public:
257 template <class T, class... Args>
258 static TypeErasedControlProblem make(Args &&...args) {
259 return TypeErased::template make<TypeErasedControlProblem, T>(std::forward<Args>(args)...);
260 }
261
262 /// @name Problem dimensions
263 /// @{
264
265 /// Horizon length.
266 [[nodiscard]] length_t get_N() const { return vtable.N; }
267 /// Number of inputs.
268 [[nodiscard]] length_t get_nu() const { return vtable.nu; }
269 /// Number of states.
270 [[nodiscard]] length_t get_nx() const { return vtable.nx; }
271 /// Number of outputs.
272 [[nodiscard]] length_t get_nh() const { return vtable.nh; }
273 [[nodiscard]] length_t get_nh_N() const { return vtable.nh_N; }
274 /// Number of constraints.
275 [[nodiscard]] length_t get_nc() const { return vtable.nc; }
276 [[nodiscard]] length_t get_nc_N() const { return vtable.nc_N; }
277 /// All dimensions
278 [[nodiscard]] Dim get_dim() const {
279 return {
280 .N = vtable.N,
281 .nx = vtable.nx,
282 .nu = vtable.nu,
283 .nh = vtable.nh,
284 .nh_N = vtable.nh_N,
285 .nc = vtable.nc,
286 .nc_N = vtable.nc_N,
287 };
288 }
289 /// Total number of variables.
290 [[nodiscard]] length_t get_n() const { return get_N() * get_nu(); }
291 /// Total number of constraints.
292 [[nodiscard]] length_t get_m() const { return get_N() * get_nc() + get_nc_N(); }
293
294 /// @}
295
296 /// @name Projections onto constraint sets
297 /// @{
298
299 /// **[Required]**
300 /// Function that evaluates the difference between the given point @f$ z @f$
301 /// and its projection onto the constraint set @f$ D @f$.
302 /// @param [in] z
303 /// Slack variable, @f$ z \in \R^m @f$
304 /// @param [out] e
305 /// The difference relative to its projection,
306 /// @f$ e = z - \Pi_D(z) \in \R^m @f$
307 /// @note @p z and @p e can refer to the same vector.
308 void eval_proj_diff_g(crvec z, rvec e) const;
309 /// **[Required]**
310 /// Function that projects the Lagrange multipliers for ALM.
311 /// @param [inout] y
312 /// Multipliers, @f$ y \leftarrow \Pi_Y(y) \in \R^m @f$
313 /// @param [in] M
314 /// The radius/size of the set @f$ Y @f$.
315 /// See @ref ALMParams::max_multiplier.
317
318 /// @}
319
320 /// @name Constraint sets
321 /// @{
322
323 /// Input box constraints @f$ U @f$.
324 void get_U(Box &U) const;
325 /// Stage box constraints @f$ D @f$.
326 void get_D(Box &D) const;
327 /// Terminal box constraints @f$ D_N @f$.
328 void get_D_N(Box &D) const;
329
330 /// @}
331
332 /// @name Dynamics and initial state
333 /// @{
334
335 /// Initial state @f$ x_\text{init} @f$.
336 void get_x_init(rvec x_init) const;
337 /// Discrete-time dynamics @f$ x^{k+1} = f_k(x^k, u^k) @f$.
338 void eval_f(index_t timestep, crvec x, crvec u, rvec fxu) const;
339 /// Jacobian of discrete-time dynamics @f$ \jac_f(x^k, u^k) @f$.
340 void eval_jac_f(index_t timestep, crvec x, crvec u, rmat J_fxu) const;
341 /// Gradient-vector product of discrete-time dynamics @f$ \nabla f(x^k, u^k)\,p @f$.
342 void eval_grad_f_prod(index_t timestep, crvec x, crvec u, crvec p, rvec grad_fxu_p) const;
343
344 /// @}
345
346 /// @name Output mapping
347 /// @{
348
349 /// Stage output mapping @f$ h_k(x^k, u^k) @f$.
350 void eval_h(index_t timestep, crvec x, crvec u, rvec h) const;
351 /// Terminal output mapping @f$ h_N(x^N) @f$.
352 void eval_h_N(crvec x, rvec h) const;
353
354 /// @}
355
356 /// @name Stage and terminal cost
357 /// @{
358
359 /// Stage cost @f$ \ell_k(\hbar^k) @f$.
360 [[nodiscard]] real_t eval_l(index_t timestep, crvec h) const;
361 /// Terminal cost @f$ \ell_N(\hbar^N) @f$.
362 [[nodiscard]] real_t eval_l_N(crvec h) const;
363
364 /// @}
365
366 /// @name Gauss-Newton approximations
367 /// @{
368
369 /// Cost gradients w.r.t. states and inputs
370 /// @f$ q^k = \tp{\jac_{h_k}^x\!(\barxuk)} \nabla \ell_k(\hbar^k) @f$ and
371 /// @f$ r^k = \tp{\jac_{h_k}^u\!(\barxuk)} \nabla \ell_k(\hbar^k) @f$.
372 void eval_qr(index_t timestep, crvec xu, crvec h, rvec qr) const;
373 /// Terminal cost gradient w.r.t. states
374 /// @f$ q^N = \tp{\jac_{h_N}(\bar x^N)} \nabla \ell_k(\hbar^N) @f$.
375 void eval_q_N(crvec x, crvec h, rvec q) const;
376 /// Cost Hessian w.r.t. states @f$ Q_k = \tp{\jac_{h_k}^x\!(\barxuk)}
377 /// \partial^2\ell_k(\hbar^k)\, \jac_{h_k}^x\!(\barxuk) @f$,
378 /// added to the given matrix @p Q.
379 /// @f$ Q \leftarrow Q + Q_k @f$.
380 void eval_add_Q(index_t timestep, crvec xu, crvec h, rmat Q) const;
381 /// Terminal cost Hessian w.r.t. states @f$ Q_N = \tp{\jac_{h_N}(\bar x^N)}
382 /// \partial^2\ell_N(\hbar^N)\, \jac_{h_N}(\bar x^N) @f$,
383 /// added to the given matrix @p Q.
384 /// @f$ Q \leftarrow Q + Q_N @f$.
385 void eval_add_Q_N(crvec x, crvec h, rmat Q) const;
386 /// Cost Hessian w.r.t. inputs @f$ R_k = \tp{\jac_{h_k}^u\!(\barxuk)}
387 /// \partial^2\ell_k(\hbar^k)\, \jac_{h_k}^u\!(\barxuk) @f$, keeping only
388 /// rows and columns in the mask @f$ \mathcal J @f$, added to the given
389 /// matrix @p R.
390 /// @f$ R \leftarrow R + R_k[\mathcal J, \mathcal J] @f$.
391 /// The size of @p work should be @ref get_R_work_size().
392 void eval_add_R_masked(index_t timestep, crvec xu, crvec h, crindexvec mask, rmat R,
393 rvec work) const;
394 /// Cost Hessian w.r.t. inputs and states @f$ S_k = \tp{\jac_{h_k}^u\!(\barxuk)}
395 /// \partial^2\ell_k(\hbar^k)\, \jac_{h_k}^x\!(\barxuk) @f$, keeping only
396 /// rows in the mask @f$ \mathcal J @f$, added to the given matrix @p S.
397 /// @f$ S \leftarrow S + S_k[\mathcal J, \cdot] @f$.
398 /// The size of @p work should be @ref get_S_work_size().
399 void eval_add_S_masked(index_t timestep, crvec xu, crvec h, crindexvec mask, rmat S,
400 rvec work) const;
401 /// @f$ out \leftarrow out + R[\mathcal J, \mathcal K]\,v[\mathcal K] @f$.
402 /// Work should contain the contents written to it by a prior call to
403 /// @ref eval_add_R_masked() in the same point.
405 crindexvec mask_K, crvec v, rvec out, rvec work) const;
406 /// @f$ out \leftarrow out + \tp{S[\mathcal K, \cdot]}\, v[\mathcal K] @f$.
407 /// Work should contain the contents written to it by a prior call to
408 /// @ref eval_add_S_masked() in the same point.
409 void eval_add_S_prod_masked(index_t timestep, crvec xu, crvec h, crindexvec mask_K, crvec v,
410 rvec out, rvec work) const;
411 /// Size of the workspace required by @ref eval_add_R_masked() and
412 /// @ref eval_add_R_prod_masked().
413 [[nodiscard]] length_t get_R_work_size() const;
414 /// Size of the workspace required by @ref eval_add_S_masked() and
415 /// @ref eval_add_S_prod_masked().
416 [[nodiscard]] length_t get_S_work_size() const;
417
418 /// @}
419
420 /// @name Constraints
421 /// @{
422
423 /// Stage constraints @f$ c_k(x^k) @f$.
424 void eval_constr(index_t timestep, crvec x, rvec c) const;
425 /// Terminal constraints @f$ c_N(x^N) @f$.
426 void eval_constr_N(crvec x, rvec c) const;
427 /// Gradient-vector product of stage constraints @f$ \nabla c_k(x^k)\, p @f$.
428 void eval_grad_constr_prod(index_t timestep, crvec x, crvec p, rvec grad_cx_p) const;
429 /// Gradient-vector product of terminal constraints @f$ \nabla c_N(x^N)\, p @f$.
430 void eval_grad_constr_prod_N(crvec x, crvec p, rvec grad_cx_p) const;
431 /// Gauss-Newton Hessian of stage constraints @f$ \tp{\jac_{c_k}}(x^k)\,
432 /// \operatorname{diag}(M)\; \jac_{c_k}(x^k) @f$.
433 void eval_add_gn_hess_constr(index_t timestep, crvec x, crvec M, rmat out) const;
434 /// Gauss-Newton Hessian of terminal constraints @f$ \tp{\jac_{c_N}}(x^N)\,
435 /// \operatorname{diag}(M)\; \jac_{c_N}(x^N) @f$.
437
438 /// @}
439
440 /// @name Checks
441 /// @{
442
443 /// Check that the problem formulation is well-defined, the dimensions match,
444 /// etc. Throws an exception if this is not the case.
445 void check() const;
446
447 /// @}
448
449 /// @name Querying specialized implementations
450 /// @{
451
452 // clang-format off
453 [[nodiscard]] bool provides_get_D() const { return vtable.get_D != nullptr; }
454 [[nodiscard]] bool provides_get_D_N() const { return vtable.get_D_N != &vtable.default_get_D_N; }
455 [[nodiscard]] bool provides_eval_h() const { return vtable.eval_h != nullptr; }
456 [[nodiscard]] bool provides_eval_h_N() const { return vtable.eval_h_N != nullptr; }
457 [[nodiscard]] bool provides_eval_add_Q_N() const { return vtable.eval_add_Q_N != &vtable.default_eval_add_Q_N; }
462 [[nodiscard]] bool provides_eval_constr() const { return vtable.eval_constr != nullptr; }
463 [[nodiscard]] bool provides_eval_constr_N() const { return vtable.eval_constr_N != &vtable.default_eval_constr_N; }
464 [[nodiscard]] bool provides_eval_grad_constr_prod() const { return vtable.eval_grad_constr_prod != nullptr; }
466 [[nodiscard]] bool provides_eval_add_gn_hess_constr() const { return vtable.eval_add_gn_hess_constr != nullptr; }
468 // clang-format on
469
470 /// @}
471};
472
473// clang-format off
474#ifdef NDEBUG
475[[gnu::always_inline]] inline void check_finiteness(auto &&, auto &&) {}
476template <Config Conf, class Allocator> [[gnu::always_inline]] inline void TypeErasedControlProblem<Conf, Allocator>::eval_proj_diff_g(crvec z, rvec e) const { return call(vtable.eval_proj_diff_g, z, e); }
477template <Config Conf, class Allocator> [[gnu::always_inline]] inline void TypeErasedControlProblem<Conf, Allocator>::eval_proj_multipliers(rvec y, real_t M) const { return call(vtable.eval_proj_multipliers, y, M); }
478template <Config Conf, class Allocator> [[gnu::always_inline]] inline void TypeErasedControlProblem<Conf, Allocator>::get_U(Box &U) const { return call(vtable.get_U, U); }
479template <Config Conf, class Allocator> [[gnu::always_inline]] inline void TypeErasedControlProblem<Conf, Allocator>::get_D(Box &D) const { return call(vtable.get_D, D); }
480template <Config Conf, class Allocator> [[gnu::always_inline]] inline void TypeErasedControlProblem<Conf, Allocator>::get_D_N(Box &D) const { return call(vtable.get_D_N, D); }
481template <Config Conf, class Allocator> [[gnu::always_inline]] inline void TypeErasedControlProblem<Conf, Allocator>::get_x_init(rvec x_init) const { return call(vtable.get_x_init, x_init); }
482template <Config Conf, class Allocator> [[gnu::always_inline]] inline void TypeErasedControlProblem<Conf, Allocator>::eval_f(index_t timestep, crvec x, crvec u, rvec fxu) const { return call(vtable.eval_f, timestep, x, u, fxu); }
483template <Config Conf, class Allocator> [[gnu::always_inline]] inline void TypeErasedControlProblem<Conf, Allocator>::eval_jac_f(index_t timestep, crvec x, crvec u, rmat J_fxu) const { return call(vtable.eval_jac_f, timestep, x, u, J_fxu); }
484template <Config Conf, class Allocator> [[gnu::always_inline]] inline void TypeErasedControlProblem<Conf, Allocator>::eval_grad_f_prod(index_t timestep, crvec x, crvec u, crvec p, rvec grad_fxu_p) const { return call(vtable.eval_grad_f_prod, timestep, x, u, p, grad_fxu_p); }
485template <Config Conf, class Allocator> [[gnu::always_inline]] inline void TypeErasedControlProblem<Conf, Allocator>::eval_h(index_t timestep, crvec x, crvec u, rvec h) const { return call(vtable.eval_h, timestep, x, u, h); }
486template <Config Conf, class Allocator> [[gnu::always_inline]] inline void TypeErasedControlProblem<Conf, Allocator>::eval_h_N(crvec x, rvec h) const { return call(vtable.eval_h_N, x, h); }
487template <Config Conf, class Allocator> [[gnu::always_inline]] inline auto TypeErasedControlProblem<Conf, Allocator>::eval_l(index_t timestep, crvec h) const -> real_t { return call(vtable.eval_l, timestep, h); }
488template <Config Conf, class Allocator> [[gnu::always_inline]] inline auto TypeErasedControlProblem<Conf, Allocator>::eval_l_N(crvec h) const -> real_t { return call(vtable.eval_l_N, h); }
489template <Config Conf, class Allocator> [[gnu::always_inline]] inline void TypeErasedControlProblem<Conf, Allocator>::eval_qr(index_t timestep, crvec xu, crvec h, rvec qr) const { return call(vtable.eval_qr, timestep, xu, h, qr); }
490template <Config Conf, class Allocator> [[gnu::always_inline]] inline void TypeErasedControlProblem<Conf, Allocator>::eval_q_N(crvec x, crvec h, rvec q) const { return call(vtable.eval_q_N, x, h, q); }
491template <Config Conf, class Allocator> [[gnu::always_inline]] inline void TypeErasedControlProblem<Conf, Allocator>::eval_add_Q(index_t timestep, crvec xu, crvec h, rmat Q) const { return call(vtable.eval_add_Q, timestep, xu, h, Q); }
492template <Config Conf, class Allocator> [[gnu::always_inline]] inline void TypeErasedControlProblem<Conf, Allocator>::eval_add_Q_N(crvec x, crvec h, rmat Q) const { return call(vtable.eval_add_Q_N, x, h, Q); }
493template <Config Conf, class Allocator> [[gnu::always_inline]] inline void TypeErasedControlProblem<Conf, Allocator>::eval_add_R_masked(index_t timestep, crvec xu, crvec h, crindexvec mask, rmat R, rvec work) const { return call(vtable.eval_add_R_masked, timestep, xu, h, mask, R, work); }
494template <Config Conf, class Allocator> [[gnu::always_inline]] inline void TypeErasedControlProblem<Conf, Allocator>::eval_add_S_masked(index_t timestep, crvec xu, crvec h, crindexvec mask, rmat S, rvec work) const { return call(vtable.eval_add_S_masked, timestep, xu, h, mask, S, work); }
495template <Config Conf, class Allocator> [[gnu::always_inline]] inline void TypeErasedControlProblem<Conf, Allocator>::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 { return call(vtable.eval_add_R_prod_masked, timestep, xu, h, mask_J, mask_K, v, out, work); }
496template <Config Conf, class Allocator> [[gnu::always_inline]] inline void TypeErasedControlProblem<Conf, Allocator>::eval_add_S_prod_masked(index_t timestep, crvec xu, crvec h, crindexvec mask_K, crvec v, rvec out, rvec work) const { return call(vtable.eval_add_S_prod_masked, timestep, xu, h, mask_K, v, out, work); }
497template <Config Conf, class Allocator> [[gnu::always_inline]] inline auto TypeErasedControlProblem<Conf, Allocator>::get_R_work_size() const -> length_t { return call(vtable.get_R_work_size); }
498template <Config Conf, class Allocator> [[gnu::always_inline]] inline auto TypeErasedControlProblem<Conf, Allocator>::get_S_work_size() const -> length_t { return call(vtable.get_S_work_size); }
499template <Config Conf, class Allocator> [[gnu::always_inline]] inline void TypeErasedControlProblem<Conf, Allocator>::eval_constr(index_t timestep, crvec x, rvec c) const { return call(vtable.eval_constr, timestep, x, c); }
500template <Config Conf, class Allocator> [[gnu::always_inline]] inline void TypeErasedControlProblem<Conf, Allocator>::eval_constr_N(crvec x, rvec c) const { return call(vtable.eval_constr_N, x, c); }
501template <Config Conf, class Allocator> [[gnu::always_inline]] inline void TypeErasedControlProblem<Conf, Allocator>::eval_grad_constr_prod(index_t timestep, crvec x, crvec p, rvec grad_cx_p) const { return call(vtable.eval_grad_constr_prod, timestep, x, p, grad_cx_p); }
502template <Config Conf, class Allocator> [[gnu::always_inline]] inline void TypeErasedControlProblem<Conf, Allocator>::eval_grad_constr_prod_N(crvec x, crvec p, rvec grad_cx_p) const { return call(vtable.eval_grad_constr_prod_N, x, p, grad_cx_p); }
503template <Config Conf, class Allocator> [[gnu::always_inline]] inline void TypeErasedControlProblem<Conf, Allocator>::eval_add_gn_hess_constr(index_t timestep, crvec x, crvec M, rmat out) const { return call(vtable.eval_add_gn_hess_constr, timestep, x, M, out); }
504template <Config Conf, class Allocator> [[gnu::always_inline]] inline void TypeErasedControlProblem<Conf, Allocator>::eval_add_gn_hess_constr_N(crvec x, crvec M, rmat out) const { return call(vtable.eval_add_gn_hess_constr_N, x, M, out); }
505template <Config Conf, class Allocator> [[gnu::always_inline]] inline void TypeErasedControlProblem<Conf, Allocator>::check() const { return call(vtable.check); }
506#else
507/// If the given vector @p v is not finite, break or throw an exception with the
508/// given message @p msg.
509inline void check_finiteness(const auto &v, std::string_view msg) {
510 using std::begin;
511 using std::end;
512 if (!v.allFinite()) {
513 std::cout << msg << std::endl;
514 throw std::runtime_error(std::string(msg));
515 }
516}
517inline void check_finiteness(const std::floating_point auto &v, std::string_view msg) {
518 if (!std::isfinite(v)) {
519 std::cout << msg << std::endl;
520 throw std::runtime_error(std::string(msg));
521 }
522}
523template <Config Conf, class Allocator> [[gnu::always_inline]] inline void TypeErasedControlProblem<Conf, Allocator>::eval_proj_diff_g(crvec z, rvec e) const { return call(vtable.eval_proj_diff_g, z, e); }
524template <Config Conf, class Allocator> [[gnu::always_inline]] inline void TypeErasedControlProblem<Conf, Allocator>::eval_proj_multipliers(rvec y, real_t M) const { return call(vtable.eval_proj_multipliers, y, M); }
525template <Config Conf, class Allocator> [[gnu::always_inline]] inline void TypeErasedControlProblem<Conf, Allocator>::get_U(Box &U) const { return call(vtable.get_U, U); }
526template <Config Conf, class Allocator> [[gnu::always_inline]] inline void TypeErasedControlProblem<Conf, Allocator>::get_D(Box &D) const { return call(vtable.get_D, D); }
527template <Config Conf, class Allocator> [[gnu::always_inline]] inline void TypeErasedControlProblem<Conf, Allocator>::get_D_N(Box &D_N) const { return call(vtable.get_D_N, D_N); }
528template <Config Conf, class Allocator> [[gnu::always_inline]] inline void TypeErasedControlProblem<Conf, Allocator>::get_x_init(rvec x_init) const { call(vtable.get_x_init, x_init); check_finiteness(x_init, "Infinite output of get_x_init"); }
529template <Config Conf, class Allocator> [[gnu::always_inline]] inline void TypeErasedControlProblem<Conf, Allocator>::eval_f(index_t timestep, crvec x, crvec u, rvec fxu) const { check_finiteness(x, "Infinite input x of f"); check_finiteness(u, "Infinite input u of f"); call(vtable.eval_f, timestep, x, u, fxu); check_finiteness(fxu, "Infinite output of f"); }
530template <Config Conf, class Allocator> [[gnu::always_inline]] inline void TypeErasedControlProblem<Conf, Allocator>::eval_jac_f(index_t timestep, crvec x, crvec u, rmat J_fxu) const { check_finiteness(x, "Infinite input x of jac_f"); check_finiteness(u, "Infinite input u of jac_f"); call(vtable.eval_jac_f, timestep, x, u, J_fxu); check_finiteness(J_fxu.reshaped(), "Infinite output of jac_f"); }
531template <Config Conf, class Allocator> [[gnu::always_inline]] inline void TypeErasedControlProblem<Conf, Allocator>::eval_grad_f_prod(index_t timestep, crvec x, crvec u, crvec p, rvec grad_fxu_p) const { check_finiteness(x, "Infinite input x of grad_f_prod"); check_finiteness(u, "Infinite input u of grad_f_prod"); check_finiteness(p, "Infinite input p of grad_f_prod"); call(vtable.eval_grad_f_prod, timestep, x, u, p, grad_fxu_p); check_finiteness(grad_fxu_p, "Infinite output of jac_f"); }
532template <Config Conf, class Allocator> [[gnu::always_inline]] inline void TypeErasedControlProblem<Conf, Allocator>::eval_h(index_t timestep, crvec x, crvec u, rvec h) const { check_finiteness(x, "Infinite input x of h"); check_finiteness(u, "Infinite input u of h"); call(vtable.eval_h, timestep, x, u, h); check_finiteness(h, "Infinite output of h"); }
533template <Config Conf, class Allocator> [[gnu::always_inline]] inline void TypeErasedControlProblem<Conf, Allocator>::eval_h_N(crvec x, rvec h) const { check_finiteness(x, "Infinite input x of h_N"); call(vtable.eval_h_N, x, h); check_finiteness(h, "Infinite output of h_N"); }
534template <Config Conf, class Allocator> [[gnu::always_inline]] inline auto TypeErasedControlProblem<Conf, Allocator>::eval_l(index_t timestep, crvec h) const -> real_t { check_finiteness(h, "Infinite input h of l"); auto l = call(vtable.eval_l, timestep, h); check_finiteness(l, "Infinite output of l"); return l; }
535template <Config Conf, class Allocator> [[gnu::always_inline]] inline auto TypeErasedControlProblem<Conf, Allocator>::eval_l_N(crvec h) const -> real_t { check_finiteness(h, "Infinite input h of l_N"); auto l = call(vtable.eval_l_N, h); check_finiteness(l, "Infinite output of l_N"); return l; }
536template <Config Conf, class Allocator> [[gnu::always_inline]] inline void TypeErasedControlProblem<Conf, Allocator>::eval_qr(index_t timestep, crvec xu, crvec h, rvec qr) const { return call(vtable.eval_qr, timestep, xu, h, qr); }
537template <Config Conf, class Allocator> [[gnu::always_inline]] inline void TypeErasedControlProblem<Conf, Allocator>::eval_q_N(crvec x, crvec h, rvec q) const { check_finiteness(x, "Infinite input x of q_N"); check_finiteness(h, "Infinite input h of q_N"); call(vtable.eval_q_N, x, h, q); check_finiteness(q, "Infinite output of q_N"); }
538template <Config Conf, class Allocator> [[gnu::always_inline]] inline void TypeErasedControlProblem<Conf, Allocator>::eval_add_Q(index_t timestep, crvec xu, crvec h, rmat Q) const { return call(vtable.eval_add_Q, timestep, xu, h, Q); }
539template <Config Conf, class Allocator> [[gnu::always_inline]] inline void TypeErasedControlProblem<Conf, Allocator>::eval_add_Q_N(crvec x, crvec h, rmat Q) const { return call(vtable.eval_add_Q_N, x, h, Q); }
540template <Config Conf, class Allocator> [[gnu::always_inline]] inline void TypeErasedControlProblem<Conf, Allocator>::eval_add_R_masked(index_t timestep, crvec xu, crvec h, crindexvec mask, rmat R, rvec work) const { return call(vtable.eval_add_R_masked, timestep, xu, h, mask, R, work); }
541template <Config Conf, class Allocator> [[gnu::always_inline]] inline void TypeErasedControlProblem<Conf, Allocator>::eval_add_S_masked(index_t timestep, crvec xu, crvec h, crindexvec mask, rmat S, rvec work) const { return call(vtable.eval_add_S_masked, timestep, xu, h, mask, S, work); }
542template <Config Conf, class Allocator> [[gnu::always_inline]] inline void TypeErasedControlProblem<Conf, Allocator>::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 { return call(vtable.eval_add_R_prod_masked, timestep, xu, h, mask_J, mask_K, v, out, work); }
543template <Config Conf, class Allocator> [[gnu::always_inline]] inline void TypeErasedControlProblem<Conf, Allocator>::eval_add_S_prod_masked(index_t timestep, crvec xu, crvec h, crindexvec mask_K, crvec v, rvec out, rvec work) const { return call(vtable.eval_add_S_prod_masked, timestep, xu, h, mask_K, v, out, work); }
544template <Config Conf, class Allocator> [[gnu::always_inline]] inline auto TypeErasedControlProblem<Conf, Allocator>::get_R_work_size() const -> length_t { return call(vtable.get_R_work_size); }
545template <Config Conf, class Allocator> [[gnu::always_inline]] inline auto TypeErasedControlProblem<Conf, Allocator>::get_S_work_size() const -> length_t { return call(vtable.get_S_work_size); }
546template <Config Conf, class Allocator> [[gnu::always_inline]] inline void TypeErasedControlProblem<Conf, Allocator>::eval_constr(index_t timestep, crvec x, rvec c) const { return call(vtable.eval_constr, timestep, x, c); }
547template <Config Conf, class Allocator> [[gnu::always_inline]] inline void TypeErasedControlProblem<Conf, Allocator>::eval_constr_N(crvec x, rvec c) const { return call(vtable.eval_constr_N, x, c); }
548template <Config Conf, class Allocator> [[gnu::always_inline]] inline void TypeErasedControlProblem<Conf, Allocator>::eval_grad_constr_prod(index_t timestep, crvec x, crvec p, rvec grad_cx_p) const { return call(vtable.eval_grad_constr_prod, timestep, x, p, grad_cx_p); }
549template <Config Conf, class Allocator> [[gnu::always_inline]] inline void TypeErasedControlProblem<Conf, Allocator>::eval_grad_constr_prod_N(crvec x, crvec p, rvec grad_cx_p) const { return call(vtable.eval_grad_constr_prod_N, x, p, grad_cx_p); }
550template <Config Conf, class Allocator> [[gnu::always_inline]] inline void TypeErasedControlProblem<Conf, Allocator>::eval_add_gn_hess_constr(index_t timestep, crvec x, crvec M, rmat out) const { return call(vtable.eval_add_gn_hess_constr, timestep, x, M, out); }
551template <Config Conf, class Allocator> [[gnu::always_inline]] inline void TypeErasedControlProblem<Conf, Allocator>::eval_add_gn_hess_constr_N(crvec x, crvec M, rmat out) const { return call(vtable.eval_add_gn_hess_constr_N, x, M, out); }
552template <Config Conf, class Allocator> [[gnu::always_inline]] inline void TypeErasedControlProblem<Conf, Allocator>::check() const { return call(vtable.check); }
553#endif
554// clang-format on
555
556template <class Problem>
558 USING_ALPAQA_CONFIG_TEMPLATE(std::remove_cvref_t<Problem>::config_t);
560
561 [[nodiscard, gnu::always_inline]] length_t get_N() const { return problem.get_N(); }
562 [[nodiscard, gnu::always_inline]] length_t get_nu() const { return problem.get_nu(); }
563 [[nodiscard, gnu::always_inline]] length_t get_nx() const { return problem.get_nx(); }
564 [[nodiscard, gnu::always_inline]] length_t get_nh() const { return problem.get_nh(); }
565 [[nodiscard, gnu::always_inline]] length_t get_nh_N() const { return problem.get_nh_N(); }
566 [[nodiscard, gnu::always_inline]] length_t get_nc() const { return problem.get_nc(); }
567 [[nodiscard, gnu::always_inline]] length_t get_nc_N() const { return problem.get_nc_N(); }
568
569 // clang-format off
570 [[gnu::always_inline]] void eval_proj_diff_g(crvec z, rvec e) const { return problem.eval_proj_diff_g(z, e); }
571 [[gnu::always_inline]] void eval_proj_multipliers(rvec y, real_t M) const { return problem.eval_proj_multipliers(y, M); }
572 [[gnu::always_inline]] void get_x_init(rvec x_init) const { return problem.get_x_init(x_init); }
573 [[nodiscard, gnu::always_inline]] length_t get_R_work_size() const requires requires { &std::remove_cvref_t<Problem>::get_R_work_size; } { return problem.get_R_work_size(); }
574 [[nodiscard, gnu::always_inline]] length_t get_S_work_size() const requires requires { &std::remove_cvref_t<Problem>::get_S_work_size; } { return problem.get_S_work_size(); }
575 [[gnu::always_inline]] void get_U(Box &U) const requires requires { &std::remove_cvref_t<Problem>::get_U; } { return problem.get_U(U); }
576 [[gnu::always_inline]] void get_D(Box &D) const requires requires { &std::remove_cvref_t<Problem>::get_D; } { return problem.get_D(D); }
577 [[gnu::always_inline]] void get_D_N(Box &D) const requires requires { &std::remove_cvref_t<Problem>::get_D_N; } { return problem.get_D_N(D); }
578 [[gnu::always_inline]] void eval_f(index_t timestep, crvec x, crvec u, rvec fxu) const { ++evaluations->f; return timed(evaluations->time.f, std::bind(&std::remove_cvref_t<Problem>::eval_f, &problem, timestep, x, u, fxu)); }
579 [[gnu::always_inline]] void eval_jac_f(index_t timestep, crvec x, crvec u, rmat J_fxu) const { ++evaluations->jac_f; return timed(evaluations->time.jac_f, std::bind(&std::remove_cvref_t<Problem>::eval_jac_f, &problem, timestep, x, u, J_fxu)); }
580 [[gnu::always_inline]] void eval_grad_f_prod(index_t timestep, crvec x, crvec u, crvec p, rvec grad_fxu_p) const { ++evaluations->grad_f_prod; return timed(evaluations->time.grad_f_prod, std::bind(&std::remove_cvref_t<Problem>::eval_grad_f_prod, &problem, timestep, x, u, p, grad_fxu_p)); }
581 [[gnu::always_inline]] void eval_h(index_t timestep, crvec x, crvec u, rvec h) const { ++evaluations->h; return timed(evaluations->time.h, std::bind(&std::remove_cvref_t<Problem>::eval_h, &problem, timestep, x, u, h)); }
582 [[gnu::always_inline]] void eval_h_N(crvec x, rvec h) const { ++evaluations->h_N; return timed(evaluations->time.h_N, std::bind(&std::remove_cvref_t<Problem>::eval_h_N, &problem, x, h)); }
583 [[nodiscard, gnu::always_inline]] real_t eval_l(index_t timestep, crvec h) const { ++evaluations->l; return timed(evaluations->time.l, std::bind(&std::remove_cvref_t<Problem>::eval_l, &problem, timestep, h)); }
584 [[nodiscard, gnu::always_inline]] real_t eval_l_N(crvec h) const { ++evaluations->l_N; return timed(evaluations->time.l_N, std::bind(&std::remove_cvref_t<Problem>::eval_l_N, &problem, h)); }
585 [[gnu::always_inline]] void eval_qr(index_t timestep, crvec xu, crvec h, rvec qr) const { ++evaluations->qr; return timed(evaluations->time.qr, std::bind(&std::remove_cvref_t<Problem>::eval_qr, &problem, timestep, xu, h, qr)); }
586 [[gnu::always_inline]] void eval_q_N(crvec x, crvec h, rvec q) const requires requires { &std::remove_cvref_t<Problem>::eval_q_N; } { ++evaluations->q_N; return timed(evaluations->time.q_N, std::bind(&std::remove_cvref_t<Problem>::eval_q_N, &problem, x, h, q)); }
587 [[gnu::always_inline]] void eval_add_Q(index_t timestep, crvec xu, crvec h, rmat Q) const { ++evaluations->add_Q; return timed(evaluations->time.add_Q, std::bind(&std::remove_cvref_t<Problem>::eval_add_Q, &problem, timestep, xu, h, Q)); }
588 [[gnu::always_inline]] void eval_add_Q_N(crvec x, crvec h, rmat Q) const requires requires { &std::remove_cvref_t<Problem>::eval_add_Q_N; } { ++evaluations->add_Q_N; return timed(evaluations->time.add_Q_N, std::bind(&std::remove_cvref_t<Problem>::eval_add_Q_N, &problem, x, h, Q)); }
589 [[gnu::always_inline]] void eval_add_R_masked(index_t timestep, crvec xu, crvec h, crindexvec mask, rmat R, rvec work) const { ++evaluations->add_R_masked; return timed(evaluations->time.add_R_masked, std::bind(&std::remove_cvref_t<Problem>::eval_add_R_masked, &problem, timestep, xu, h, mask, R, work)); }
590 [[gnu::always_inline]] void eval_add_S_masked(index_t timestep, crvec xu, crvec h, crindexvec mask, rmat S, rvec work) const { ++evaluations->add_S_masked; return timed(evaluations->time.add_S_masked, std::bind(&std::remove_cvref_t<Problem>::eval_add_S_masked, &problem, timestep, xu, h, mask, S, work)); }
591 [[gnu::always_inline]] 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 requires requires { &std::remove_cvref_t<Problem>::eval_add_R_prod_masked; } { ++evaluations->add_R_prod_masked; return timed(evaluations->time.add_R_prod_masked, std::bind(&std::remove_cvref_t<Problem>::eval_add_R_prod_masked, &problem, timestep, xu, h, mask_J, mask_K, v, out, work)); }
592 [[gnu::always_inline]] void eval_add_S_prod_masked(index_t timestep, crvec xu, crvec h, crindexvec mask_K, crvec v, rvec out, rvec work) const requires requires { &std::remove_cvref_t<Problem>::eval_add_S_prod_masked; } { ++evaluations->add_S_prod_masked; return timed(evaluations->time.add_S_prod_masked, std::bind(&std::remove_cvref_t<Problem>::eval_add_S_prod_masked, &problem, timestep, xu, h, mask_K, v, out, work)); }
593 [[gnu::always_inline]] void eval_constr(index_t timestep, crvec x, rvec c) const requires requires { &std::remove_cvref_t<Problem>::eval_constr; } { ++evaluations->constr; return timed(evaluations->time.constr, std::bind(&std::remove_cvref_t<Problem>::eval_constr, &problem, timestep, x, c)); }
594 [[gnu::always_inline]] void eval_constr_N(crvec x, rvec c) const requires requires { &std::remove_cvref_t<Problem>::eval_constr_N; } { ++evaluations->constr_N; return timed(evaluations->time.constr_N, std::bind(&std::remove_cvref_t<Problem>::eval_constr_N, &problem, x, c)); }
595 [[gnu::always_inline]] void eval_grad_constr_prod(index_t timestep, crvec x, crvec p, rvec grad_cx_p) const requires requires { &std::remove_cvref_t<Problem>::eval_grad_constr_prod; } { ++evaluations->grad_constr_prod; return timed(evaluations->time.grad_constr_prod, std::bind(&std::remove_cvref_t<Problem>::eval_grad_constr_prod, &problem, timestep, x, p, grad_cx_p)); }
596 [[gnu::always_inline]] void eval_grad_constr_prod_N(crvec x, crvec p, rvec grad_cx_p) const requires requires { &std::remove_cvref_t<Problem>::eval_grad_constr_prod_N; } { ++evaluations->grad_constr_prod_N; return timed(evaluations->time.grad_constr_prod_N, std::bind(&std::remove_cvref_t<Problem>::eval_grad_constr_prod_N, &problem, x, p, grad_cx_p)); }
597 [[gnu::always_inline]] void eval_add_gn_hess_constr(index_t timestep, crvec x, crvec M, rmat out) const requires requires { &std::remove_cvref_t<Problem>::eval_add_gn_hess_constr; } { ++evaluations->add_gn_hess_constr; return timed(evaluations->time.add_gn_hess_constr, std::bind(&std::remove_cvref_t<Problem>::eval_add_gn_hess_constr, &problem, timestep, x, M, out)); }
598 [[gnu::always_inline]] void eval_add_gn_hess_constr_N(crvec x, crvec M, rmat out) const requires requires { &std::remove_cvref_t<Problem>::eval_add_gn_hess_constr_N; } { ++evaluations->add_gn_hess_constr_N; return timed(evaluations->time.add_gn_hess_constr_N, std::bind(&std::remove_cvref_t<Problem>::eval_add_gn_hess_constr_N, &problem, x, M, out)); }
599 [[gnu::always_inline]] void check() const { problem.check(); }
600
601 [[nodiscard]] bool provides_get_D() const requires requires (Problem p) { { p.provides_get_D() } -> std::convertible_to<bool>; } { return problem.provides_get_D(); }
602 [[nodiscard]] bool provides_get_D_N() const requires requires (Problem p) { { p.provides_get_D_N() } -> std::convertible_to<bool>; } { return problem.provides_get_D_N(); }
603 [[nodiscard]] bool provides_eval_add_Q_N() const requires requires (Problem p) { { p.provides_eval_add_Q_N() } -> std::convertible_to<bool>; } { return problem.provides_eval_add_Q_N(); }
604 [[nodiscard]] bool provides_eval_add_R_prod_masked() const requires requires (Problem p) { { p.provides_eval_add_R_prod_masked() } -> std::convertible_to<bool>; } { return problem.provides_eval_add_R_prod_masked(); }
605 [[nodiscard]] bool provides_eval_add_S_prod_masked() const requires requires (Problem p) { { p.provides_eval_add_S_prod_masked() } -> std::convertible_to<bool>; } { return problem.provides_eval_add_S_prod_masked(); }
606 [[nodiscard]] bool provides_get_R_work_size() const requires requires (Problem p) { { p.provides_get_R_work_size() } -> std::convertible_to<bool>; } { return problem.provides_get_R_work_size(); }
607 [[nodiscard]] bool provides_get_S_work_size() const requires requires (Problem p) { { p.provides_get_S_work_size() } -> std::convertible_to<bool>; } { return problem.provides_get_S_work_size(); }
608 [[nodiscard]] bool provides_eval_constr() const requires requires (Problem p) { { p.provides_eval_constr() } -> std::convertible_to<bool>; } { return problem.provides_eval_constr(); }
609 [[nodiscard]] bool provides_eval_constr_N() const requires requires (Problem p) { { p.provides_eval_constr_N() } -> std::convertible_to<bool>; } { return problem.provides_eval_constr_N(); }
610 [[nodiscard]] bool provides_eval_grad_constr_prod() const requires requires (Problem p) { { p.provides_eval_grad_constr_prod() } -> std::convertible_to<bool>; } { return problem.provides_eval_grad_constr_prod(); }
611 [[nodiscard]] bool provides_eval_grad_constr_prod_N() const requires requires (Problem p) { { p.provides_eval_grad_constr_prod_N() } -> std::convertible_to<bool>; } { return problem.provides_eval_grad_constr_prod_N(); }
612 [[nodiscard]] bool provides_eval_add_gn_hess_constr() const requires requires (Problem p) { { p.provides_eval_add_gn_hess_constr() } -> std::convertible_to<bool>; } { return problem.provides_eval_add_gn_hess_constr(); }
613 [[nodiscard]] bool provides_eval_add_gn_hess_constr_N() const requires requires (Problem p) { { p.provides_eval_add_gn_hess_constr_N() } -> std::convertible_to<bool>; } { return problem.provides_eval_add_gn_hess_constr_N(); }
614 // clang-format on
615
616 std::shared_ptr<OCPEvalCounter> evaluations = std::make_shared<OCPEvalCounter>();
617 Problem problem;
618
620 requires std::is_default_constructible_v<Problem>
621 = default;
622 template <class P>
624 requires std::is_same_v<std::remove_cvref_t<P>, std::remove_cvref_t<Problem>>
625 : problem{std::forward<P>(problem)} {}
626 template <class... Args>
627 explicit ControlProblemWithCounters(std::in_place_t, Args &&...args)
628 requires(!std::is_lvalue_reference_v<Problem>)
629 : problem{std::forward<Args>(args)...} {}
630
631 /// Reset all evaluation counters and timers to zero. Affects all instances
632 /// that share the same evaluations. If you only want to reset the counters
633 /// of this instance, use @ref decouple_evaluations first.
634 void reset_evaluations() { evaluations.reset(); }
635 /// Give this instance its own evaluation counters and timers, decoupling
636 /// it from any other instances they might have previously been shared with.
637 /// The evaluation counters and timers are preserved (a copy is made).
638 void decouple_evaluations() { evaluations = std::make_shared<OCPEvalCounter>(*evaluations); }
639
640 private:
641 template <class TimeT, class FunT>
642 [[gnu::always_inline]] static decltype(auto) timed(TimeT &time, FunT &&f) {
644 return std::forward<FunT>(f)();
645 }
646};
647
648template <class Problem>
649[[nodiscard]] auto ocproblem_with_counters(Problem &&p) {
650 using Prob = std::remove_cvref_t<Problem>;
651 using ProbWithCnt = ControlProblemWithCounters<Prob>;
652 return ProbWithCnt{std::forward<Problem>(p)};
653}
654
655template <class Problem>
656[[nodiscard]] auto ocproblem_with_counters_ref(Problem &p) {
657 using Prob = std::remove_cvref_t<Problem>;
659 return ProbWithCnt{p};
660}
661
662} // namespace alpaqa
Nonlinear optimal control problem with finite horizon .
Definition: ocproblem.hpp:241
Dim get_dim() const
All dimensions.
Definition: ocproblem.hpp:278
void eval_jac_f(index_t timestep, crvec x, crvec u, rmat J_fxu) const
Jacobian of discrete-time dynamics .
Definition: ocproblem.hpp:530
bool provides_eval_grad_constr_prod() const
Definition: ocproblem.hpp:464
bool provides_eval_add_gn_hess_constr() const
Definition: ocproblem.hpp:466
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
.
Definition: ocproblem.hpp:542
bool provides_eval_add_R_prod_masked() const
Definition: ocproblem.hpp:458
length_t get_nc() const
Number of constraints.
Definition: ocproblem.hpp:275
void eval_add_gn_hess_constr_N(crvec x, crvec M, rmat out) const
Gauss-Newton Hessian of terminal constraints .
Definition: ocproblem.hpp:551
length_t get_S_work_size() const
Size of the workspace required by eval_add_S_masked() and eval_add_S_prod_masked().
Definition: ocproblem.hpp:545
typename VTable::Box Box
Definition: ocproblem.hpp:246
void check() const
Check that the problem formulation is well-defined, the dimensions match, etc.
Definition: ocproblem.hpp:552
void eval_qr(index_t timestep, crvec xu, crvec h, rvec qr) const
Cost gradients w.r.t.
Definition: ocproblem.hpp:536
void eval_add_S_prod_masked(index_t timestep, crvec xu, crvec h, crindexvec mask_K, crvec v, rvec out, rvec work) const
.
Definition: ocproblem.hpp:543
void eval_constr_N(crvec x, rvec c) const
Terminal constraints .
Definition: ocproblem.hpp:547
void eval_grad_constr_prod_N(crvec x, crvec p, rvec grad_cx_p) const
Gradient-vector product of terminal constraints .
Definition: ocproblem.hpp:549
length_t get_nu() const
Number of inputs.
Definition: ocproblem.hpp:268
void eval_add_R_masked(index_t timestep, crvec xu, crvec h, crindexvec mask, rmat R, rvec work) const
Cost Hessian w.r.t.
Definition: ocproblem.hpp:540
void get_U(Box &U) const
Input box constraints .
Definition: ocproblem.hpp:525
real_t eval_l_N(crvec h) const
Terminal cost .
Definition: ocproblem.hpp:535
length_t get_m() const
Total number of constraints.
Definition: ocproblem.hpp:292
real_t eval_l(index_t timestep, crvec h) const
Stage cost .
Definition: ocproblem.hpp:534
void eval_grad_f_prod(index_t timestep, crvec x, crvec u, crvec p, rvec grad_fxu_p) const
Gradient-vector product of discrete-time dynamics .
Definition: ocproblem.hpp:531
void eval_add_gn_hess_constr(index_t timestep, crvec x, crvec M, rmat out) const
Gauss-Newton Hessian of stage constraints .
Definition: ocproblem.hpp:550
void eval_constr(index_t timestep, crvec x, rvec c) const
Stage constraints .
Definition: ocproblem.hpp:546
length_t get_nh() const
Number of outputs.
Definition: ocproblem.hpp:272
void eval_h(index_t timestep, crvec x, crvec u, rvec h) const
Stage output mapping .
Definition: ocproblem.hpp:532
void get_D_N(Box &D) const
Terminal box constraints .
Definition: ocproblem.hpp:527
static TypeErasedControlProblem make(Args &&...args)
Definition: ocproblem.hpp:258
bool provides_eval_grad_constr_prod_N() const
Definition: ocproblem.hpp:465
length_t get_R_work_size() const
Size of the workspace required by eval_add_R_masked() and eval_add_R_prod_masked().
Definition: ocproblem.hpp:544
length_t get_nx() const
Number of states.
Definition: ocproblem.hpp:270
void eval_add_S_masked(index_t timestep, crvec xu, crvec h, crindexvec mask, rmat S, rvec work) const
Cost Hessian w.r.t.
Definition: ocproblem.hpp:541
void get_x_init(rvec x_init) const
Initial state .
Definition: ocproblem.hpp:528
void get_D(Box &D) const
Stage box constraints .
Definition: ocproblem.hpp:526
void eval_proj_multipliers(rvec y, real_t M) const
[Required] Function that projects the Lagrange multipliers for ALM.
Definition: ocproblem.hpp:524
void eval_q_N(crvec x, crvec h, rvec q) const
Terminal cost gradient w.r.t.
Definition: ocproblem.hpp:537
bool provides_eval_add_S_prod_masked() const
Definition: ocproblem.hpp:459
void eval_add_Q_N(crvec x, crvec h, rmat Q) const
Terminal cost Hessian w.r.t.
Definition: ocproblem.hpp:539
void eval_grad_constr_prod(index_t timestep, crvec x, crvec p, rvec grad_cx_p) const
Gradient-vector product of stage constraints .
Definition: ocproblem.hpp:548
void eval_add_Q(index_t timestep, crvec xu, crvec h, rmat Q) const
Cost Hessian w.r.t.
Definition: ocproblem.hpp:538
length_t get_n() const
Total number of variables.
Definition: ocproblem.hpp:290
void eval_proj_diff_g(crvec z, rvec e) const
[Required] Function that evaluates the difference between the given point and its projection onto th...
Definition: ocproblem.hpp:523
void eval_f(index_t timestep, crvec x, crvec u, rvec fxu) const
Discrete-time dynamics .
Definition: ocproblem.hpp:529
void eval_h_N(crvec x, rvec h) const
Terminal output mapping .
Definition: ocproblem.hpp:533
bool provides_eval_add_gn_hess_constr_N() const
Definition: ocproblem.hpp:467
length_t get_N() const
Horizon length.
Definition: ocproblem.hpp:266
Class for polymorphism through type erasure.
decltype(auto) call(Ret(*f)(const void *, FArgs...), Args &&...args) const
Call the vtable function f with the given arguments args, implicitly passing the self pointer and vta...
TypeErased() noexcept(noexcept(allocator_type()))=default
Default constructor.
void * self
Pointer to the stored object.
#define USING_ALPAQA_CONFIG(Conf)
Definition: config.hpp:42
#define USING_ALPAQA_CONFIG_TEMPLATE(Conf)
Definition: config.hpp:46
#define ALPAQA_EXPORT_EXTERN_TEMPLATE(...)
Definition: export.hpp:21
length_t nh
Definition: ocproblem.hpp:29
length_t nx
Definition: ocproblem.hpp:29
auto ocproblem_with_counters_ref(Problem &p)
Definition: ocproblem.hpp:656
typename Conf::rmat rmat
Definition: config.hpp:60
length_t nc
Definition: ocproblem.hpp:29
auto ocproblem_with_counters(Problem &&p)
Definition: ocproblem.hpp:649
length_t N
Definition: ocproblem.hpp:29
length_t nu
Definition: ocproblem.hpp:29
typename Conf::real_t real_t
Definition: config.hpp:51
typename Conf::index_t index_t
Definition: config.hpp:63
void check_finiteness(const auto &v, std::string_view msg)
If the given vector v is not finite, break or throw an exception with the given message msg.
Definition: ocproblem.hpp:509
typename Conf::length_t length_t
Definition: config.hpp:62
typename Conf::rvec rvec
Definition: config.hpp:55
typename Conf::crvec crvec
Definition: config.hpp:56
typename Conf::crindexvec crindexvec
Definition: config.hpp:66
#define ALPAQA_TE_OPTIONAL_METHOD(vtable, type, member, instance)
#define ALPAQA_TE_REQUIRED_METHOD(vtable, type, member)
required_const_function_t< void(index_t timestep, crvec x, crvec u, rmat J_fxu)> eval_jac_f
Definition: ocproblem.hpp:59
optional_const_function_t< length_t()> get_S_work_size
Definition: ocproblem.hpp:89
optional_const_function_t< void(crvec x, crvec h, rmat Q)> eval_add_Q_N
Definition: ocproblem.hpp:77
optional_const_function_t< void(index_t timestep, crvec x, crvec p, rvec grad_cx_p)> eval_grad_constr_prod
Definition: ocproblem.hpp:95
ControlProblemVTable(std::in_place_t, P &p)
Definition: ocproblem.hpp:109
static length_t default_get_R_work_size(const void *, const ControlProblemVTable &)
Definition: ocproblem.tpp:31
static length_t default_get_S_work_size(const void *, const ControlProblemVTable &)
Definition: ocproblem.tpp:36
static void default_eval_add_S_prod_masked(const void *, index_t, crvec, crvec, crindexvec, crvec, rvec, rvec, const ControlProblemVTable &)
Definition: ocproblem.tpp:25
required_const_function_t< void(index_t timestep, crvec xu, crvec h, crindexvec mask, rmat S, rvec work)> eval_add_S_masked
Definition: ocproblem.hpp:81
optional_const_function_t< void(Box &D)> get_D
Definition: ocproblem.hpp:51
optional_const_function_t< void(crvec x, crvec M, rmat out)> eval_add_gn_hess_constr_N
Definition: ocproblem.hpp:101
required_const_function_t< real_t(crvec h)> eval_l_N
Definition: ocproblem.hpp:69
optional_const_function_t< void(crvec x, rvec h)> eval_h_N
Definition: ocproblem.hpp:65
optional_const_function_t< void(crvec x, rvec c)> eval_constr_N
Definition: ocproblem.hpp:93
optional_const_function_t< void(Box &D)> get_D_N
Definition: ocproblem.hpp:53
required_const_function_t< real_t(index_t timestep, crvec h)> eval_l
Definition: ocproblem.hpp:67
required_const_function_t< void()> check
Definition: ocproblem.hpp:103
optional_const_function_t< void(crvec x, crvec p, rvec grad_cx_p)> eval_grad_constr_prod_N
Definition: ocproblem.hpp:97
static void default_eval_constr_N(const void *self, crvec x, rvec c, const ControlProblemVTable &vtable)
Definition: ocproblem.tpp:41
optional_const_function_t< void(index_t timestep, crvec x, crvec u, rvec h)> eval_h
Definition: ocproblem.hpp:63
required_const_function_t< void(crvec x, crvec h, rvec q)> eval_q_N
Definition: ocproblem.hpp:73
required_const_function_t< void(rvec x_init)> get_x_init
Definition: ocproblem.hpp:55
required_const_function_t< void(crvec z, rvec e)> eval_proj_diff_g
Definition: ocproblem.hpp:45
util::BasicVTable::optional_const_function_t< F, ControlProblemVTable > optional_const_function_t
Definition: ocproblem.hpp:41
static void default_eval_add_R_prod_masked(const void *, index_t, crvec, crvec, crindexvec, crindexvec, crvec, rvec, rvec, const ControlProblemVTable &)
Definition: ocproblem.tpp:18
alpaqa::Box< config_t > Box
Definition: ocproblem.hpp:35
static void default_eval_grad_constr_prod_N(const void *self, crvec x, crvec p, rvec grad_cx_p, const ControlProblemVTable &vtable)
Definition: ocproblem.tpp:46
required_const_function_t< void(index_t timestep, crvec x, crvec u, crvec p, rvec grad_fxu_p)> eval_grad_f_prod
Definition: ocproblem.hpp:61
util::BasicVTable::optional_function_t< F, ControlProblemVTable > optional_function_t
Definition: ocproblem.hpp:38
static void default_eval_add_gn_hess_constr_N(const void *self, crvec x, crvec M, rmat out, const ControlProblemVTable &vtable)
Definition: ocproblem.tpp:51
static void default_eval_add_Q_N(const void *self, crvec x, crvec h, rmat Q, const ControlProblemVTable &vtable)
Definition: ocproblem.tpp:13
static void default_get_D_N(const void *self, Box &D, const ControlProblemVTable &vtable)
Definition: ocproblem.tpp:8
optional_const_function_t< void(index_t timestep, crvec xu, crvec h, crindexvec mask_J, crindexvec mask_K, crvec v, rvec out, rvec work)> eval_add_R_prod_masked
Definition: ocproblem.hpp:83
required_const_function_t< void(Box &U)> get_U
Definition: ocproblem.hpp:49
required_const_function_t< void(index_t timestep, crvec xu, crvec h, rvec qr)> eval_qr
Definition: ocproblem.hpp:71
optional_const_function_t< void(index_t timestep, crvec xu, crvec h, crindexvec mask_K, crvec v, rvec out, rvec work)> eval_add_S_prod_masked
Definition: ocproblem.hpp:85
optional_const_function_t< void(index_t timestep, crvec x, crvec M, rmat out)> eval_add_gn_hess_constr
Definition: ocproblem.hpp:99
required_const_function_t< void(index_t timestep, crvec x, crvec u, rvec fxu)> eval_f
Definition: ocproblem.hpp:57
required_const_function_t< void(index_t timestep, crvec xu, crvec h, crindexvec mask, rmat R, rvec work)> eval_add_R_masked
Definition: ocproblem.hpp:79
optional_const_function_t< length_t()> get_R_work_size
Definition: ocproblem.hpp:87
required_const_function_t< void(rvec y, real_t M)> eval_proj_multipliers
Definition: ocproblem.hpp:47
required_const_function_t< void(index_t timestep, crvec xu, crvec h, rmat Q)> eval_add_Q
Definition: ocproblem.hpp:75
optional_const_function_t< void(index_t timestep, crvec x, rvec c)> eval_constr
Definition: ocproblem.hpp:91
void eval_jac_f(index_t timestep, crvec x, crvec u, rmat J_fxu) const
Definition: ocproblem.hpp:579
void eval_add_gn_hess_constr_N(crvec x, crvec M, rmat out) const
Definition: ocproblem.hpp:598
void eval_add_gn_hess_constr(index_t timestep, crvec x, crvec M, rmat out) const
Definition: ocproblem.hpp:597
void decouple_evaluations()
Give this instance its own evaluation counters and timers, decoupling it from any other instances the...
Definition: ocproblem.hpp:638
void eval_qr(index_t timestep, crvec xu, crvec h, rvec qr) const
Definition: ocproblem.hpp:585
void eval_add_R_masked(index_t timestep, crvec xu, crvec h, crindexvec mask, rmat R, rvec work) const
Definition: ocproblem.hpp:589
void eval_grad_f_prod(index_t timestep, crvec x, crvec u, crvec p, rvec grad_fxu_p) const
Definition: ocproblem.hpp:580
bool provides_eval_add_R_prod_masked() const
Definition: ocproblem.hpp:604
void eval_constr(index_t timestep, crvec x, rvec c) const
Definition: ocproblem.hpp:593
real_t eval_l(index_t timestep, crvec h) const
Definition: ocproblem.hpp:583
void eval_grad_constr_prod_N(crvec x, crvec p, rvec grad_cx_p) const
Definition: ocproblem.hpp:596
void eval_h(index_t timestep, crvec x, crvec u, rvec h) const
Definition: ocproblem.hpp:581
void reset_evaluations()
Reset all evaluation counters and timers to zero.
Definition: ocproblem.hpp:634
void eval_add_S_prod_masked(index_t timestep, crvec xu, crvec h, crindexvec mask_K, crvec v, rvec out, rvec work) const
Definition: ocproblem.hpp:592
bool provides_eval_add_gn_hess_constr() const
Definition: ocproblem.hpp:612
void eval_q_N(crvec x, crvec h, rvec q) const
Definition: ocproblem.hpp:586
void eval_add_S_masked(index_t timestep, crvec xu, crvec h, crindexvec mask, rmat S, rvec work) const
Definition: ocproblem.hpp:590
std::shared_ptr< OCPEvalCounter > evaluations
Definition: ocproblem.hpp:616
ControlProblemWithCounters(std::in_place_t, Args &&...args)
Definition: ocproblem.hpp:627
void get_x_init(rvec x_init) const
Definition: ocproblem.hpp:572
typename TypeErasedControlProblem< config_t >::Box Box
Definition: ocproblem.hpp:559
void eval_proj_multipliers(rvec y, real_t M) const
Definition: ocproblem.hpp:571
bool provides_eval_add_S_prod_masked() const
Definition: ocproblem.hpp:605
void eval_add_Q_N(crvec x, crvec h, rmat Q) const
Definition: ocproblem.hpp:588
real_t eval_l_N(crvec h) const
Definition: ocproblem.hpp:584
void eval_add_Q(index_t timestep, crvec xu, crvec h, rmat Q) const
Definition: ocproblem.hpp:587
void eval_constr_N(crvec x, rvec c) const
Definition: ocproblem.hpp:594
void eval_grad_constr_prod(index_t timestep, crvec x, crvec p, rvec grad_cx_p) const
Definition: ocproblem.hpp:595
bool provides_eval_grad_constr_prod_N() const
Definition: ocproblem.hpp:611
void eval_proj_diff_g(crvec z, rvec e) const
Definition: ocproblem.hpp:570
void eval_f(index_t timestep, crvec x, crvec u, rvec fxu) const
Definition: ocproblem.hpp:578
static decltype(auto) timed(TimeT &time, FunT &&f)
Definition: ocproblem.hpp:642
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
Definition: ocproblem.hpp:591
bool provides_eval_add_gn_hess_constr_N() const
Definition: ocproblem.hpp:613
void eval_h_N(crvec x, rvec h) const
Definition: ocproblem.hpp:582
Double-precision double configuration.
Definition: config.hpp:115
Single-precision float configuration.
Definition: config.hpp:111
long double configuration.
Definition: config.hpp:120
Struct that stores the size of a polymorphic object, as well as pointers to functions to copy,...
typename optional_function< F, VTable >::type optional_function_t
An optional function includes a void pointer to self, the arguments of F, and an additional reference...
typename required_const_function< F >::type required_const_function_t
A required function includes a void pointer to self, in addition to the arguments of F.
typename optional_const_function< F, VTable >::type optional_const_function_t
An optional function includes a void pointer to self, the arguments of F, and an additional reference...