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