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