17#error "OCP support disabled"
144 if (
nc > 0 &&
get_D ==
nullptr)
145 throw std::runtime_error(
"ControlProblem: missing 'get_D'");
147 throw std::runtime_error(
"ControlProblem: missing 'eval_constr'");
149 throw std::runtime_error(
"ControlProblem: missing 'eval_grad_constr_prod'");
151 throw std::runtime_error(
"ControlProblem: missing 'eval_h'");
153 throw std::runtime_error(
"ControlProblem: missing 'eval_h_N'");
245 using TypeErased::TypeErased;
248 using TypeErased::call;
249 using TypeErased::self;
250 using TypeErased::vtable;
253 template <
class T,
class...
Args>
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); }
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); }
508 if (!
v.allFinite()) {
509 std::cout <<
msg << std::endl;
510 throw std::runtime_error(std::string(
msg));
514 if (!std::isfinite(
v)) {
515 std::cout <<
msg << std::endl;
516 throw std::runtime_error(std::string(
msg));
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"); }
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"); }
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); }
552template <
class Problem>
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); }
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); }); }
612 std::shared_ptr<OCPEvalCounter>
evaluations = std::make_shared<OCPEvalCounter>();
622 template <
class...
Args>
624 requires(!std::is_lvalue_reference_v<Problem>)
637 template <
class TimeT,
class FunT>
640 return std::forward<FunT>(f)();
644template <
class Problem>
646 using Prob = std::remove_cvref_t<Problem>;
651template <
class Problem>
653 using Prob = std::remove_cvref_t<Problem>;
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 .
bool provides_get_D() const
bool provides_get_S_work_size() const
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.
bool provides_eval_add_Q_N() const
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 .
length_t get_nc_N() const
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 .
bool provides_eval_constr() const
bool provides_get_D_N() const
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.
length_t get_nh_N() const
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.
bool provides_eval_h() const
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.
bool provides_eval_constr_N() const
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 .
bool provides_eval_h_N() const
void eval_h_N(crvec x, rvec h) const
Terminal output mapping .
bool provides_get_R_work_size() const
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)
#define ALPAQA_IF_QUADF(...)
#define ALPAQA_IF_LONGD(...)
#define ALPAQA_IF_FLOAT(...)
#define USING_ALPAQA_CONFIG_TEMPLATE(Conf)
#define ALPAQA_EXPORT_EXTERN_TEMPLATE(...)
auto ocproblem_with_counters_ref(Problem &p)
auto ocproblem_with_counters(Problem &&p)
typename Conf::real_t real_t
typename Conf::index_t index_t
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
typename Conf::crvec crvec
typename Conf::crindexvec crindexvec
#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
required_function_t< void(index_t timestep, crvec x, crvec u, rvec fxu) const > eval_f
required_function_t< void(rvec x_init) const > get_x_init
required_function_t< void(rvec y, real_t M) const > eval_proj_multipliers
optional_function_t< void(index_t timestep, crvec x, crvec M, rmat out) const > eval_add_gn_hess_constr
optional_function_t< void(index_t timestep, crvec x, rvec c) const > eval_constr
ControlProblemVTable()=default
required_function_t< void(index_t timestep, crvec xu, crvec h, rvec qr) const > eval_qr
static length_t default_get_R_work_size(const void *, const ControlProblemVTable &)
static length_t default_get_S_work_size(const void *, const ControlProblemVTable &)
optional_function_t< void(crvec x, rvec c) const > eval_constr_N
static void default_eval_add_S_prod_masked(const void *, index_t, crvec, crvec, crindexvec, crvec, rvec, rvec, const ControlProblemVTable &)
required_function_t< void(crvec z, rvec e) const > eval_proj_diff_g
optional_function_t< void(crvec x, crvec M, rmat out) const > eval_add_gn_hess_constr_N
optional_function_t< void(crvec x, rvec h) const > eval_h_N
optional_function_t< void(index_t timestep, crvec x, crvec p, rvec grad_cx_p) const > eval_grad_constr_prod
optional_function_t< length_t() const > get_R_work_size
optional_function_t< void(crvec x, crvec p, rvec grad_cx_p) const > eval_grad_constr_prod_N
required_function_t< void() const > check
static void default_eval_constr_N(const void *self, crvec x, rvec c, const ControlProblemVTable &vtable)
optional_function_t< void(index_t timestep, crvec x, crvec u, rvec h) const > eval_h
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
optional_function_t< void(Box &D) const > get_D_N
required_function_t< void(Box &U) const > get_U
static void default_eval_add_R_prod_masked(const void *, index_t, crvec, crvec, crindexvec, crindexvec, crvec, rvec, rvec, const ControlProblemVTable &)
static void default_eval_grad_constr_prod_N(const void *self, crvec x, crvec p, rvec grad_cx_p, const ControlProblemVTable &vtable)
required_function_t< void(crvec x, crvec h, rvec q) const > eval_q_N
required_function_t< void(index_t timestep, crvec xu, crvec h, crindexvec mask, rmat S, rvec work) const > eval_add_S_masked
util::BasicVTable::optional_function_t< F, ControlProblemVTable > optional_function_t
required_function_t< real_t(index_t timestep, crvec h) const > eval_l
optional_function_t< void(Box &D) const > get_D
static void default_eval_add_gn_hess_constr_N(const void *self, crvec x, crvec M, rmat out, const ControlProblemVTable &vtable)
static void default_eval_add_Q_N(const void *self, crvec x, crvec h, rmat Q, const ControlProblemVTable &vtable)
static void default_get_D_N(const void *self, Box &D, const ControlProblemVTable &vtable)
required_function_t< real_t(crvec h) const > eval_l_N
required_function_t< void(index_t timestep, crvec x, crvec u, crvec p, rvec grad_fxu_p) const > eval_grad_f_prod
required_function_t< void(index_t timestep, crvec xu, crvec h, crindexvec mask, rmat R, rvec work) const > eval_add_R_masked
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
optional_function_t< void(crvec x, crvec h, rmat Q) const > eval_add_Q_N
optional_function_t< length_t() const > get_S_work_size
required_function_t< void(index_t timestep, crvec xu, crvec h, rmat Q) const > eval_add_Q
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
length_t get_R_work_size() 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...
ControlProblemWithCounters()=default
void eval_qr(index_t timestep, crvec xu, crvec h, rvec qr) const
bool provides_get_D_N() const
void eval_add_R_masked(index_t timestep, crvec xu, crvec h, crindexvec mask, rmat R, rvec work) const
bool provides_get_S_work_size() const
length_t get_nc_N() const
bool provides_eval_constr() const
void eval_grad_f_prod(index_t timestep, crvec x, crvec u, crvec p, rvec grad_fxu_p) const
void get_D_N(Box &D) const
bool provides_eval_add_R_prod_masked() const
void eval_constr(index_t timestep, crvec x, rvec c) const
bool provides_eval_grad_constr_prod() const
real_t eval_l(index_t timestep, crvec h) const
bool provides_get_D() const
void eval_grad_constr_prod_N(crvec x, crvec p, rvec grad_cx_p) const
length_t get_nh_N() const
bool provides_eval_constr_N() 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
bool provides_eval_add_gn_hess_constr() const
bool provides_get_R_work_size() 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
bool provides_eval_add_S_prod_masked() const
void eval_add_Q_N(crvec x, crvec h, rmat Q) const
real_t eval_l_N(crvec h) const
length_t get_S_work_size() const
bool provides_eval_add_Q_N() 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
bool provides_eval_grad_constr_prod_N() 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
Double-precision double configuration.
Single-precision float configuration.
long double configuration.
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.