aligator 0.19.0
A versatile and efficient C++ library for real-time constrained trajectory optimization.
Loading...
Searching...
No Matches
stage-model.hpp
Go to the documentation of this file.
1
3#pragma once
4
7#include <fmt/format.h>
8
9namespace aligator {
10using xyz::polymorphic;
11
12#define ALIGATOR_CHECK_DERIVED_CLASS(Base, Derived) \
13 static_assert((std::is_base_of_v<Base, Derived>), \
14 "Failed check for derived class.")
15
23template <typename _Scalar> struct StageModelTpl {
24public:
25 using Scalar = _Scalar;
27
29 using PolyManifold = polymorphic<Manifold>;
31 using PolyDynamics = polymorphic<Dynamics>;
32 using PolyFunction = polymorphic<StageFunctionTpl<Scalar>>;
33 using PolyConstraintSet = polymorphic<ConstraintSetTpl<Scalar>>;
35 using PolyCost = polymorphic<Cost>;
37
50
52 template <typename U> U *getCost() {
54 return dynamic_cast<U *>(&*cost_);
55 }
56
58 template <typename U> const U *getCost() const {
60 return dynamic_cast<const U *>(&*cost_);
61 }
62
64 template <typename U> U *getDynamics() {
66 return dynamic_cast<U *>(&*dynamics_);
67 }
68
70 template <typename U> const U *getDynamics() const {
72 return dynamic_cast<const U *>(&*dynamics_);
73 }
74
78 virtual ~StageModelTpl() = default;
79
80 const Manifold &xspace() const { return *xspace_; }
81 const Manifold &uspace() const { return *uspace_; }
82 const Manifold &xspace_next() const { return *xspace_next_; }
83
84 const Cost &cost() const { return *cost_; }
88 virtual bool hasDynModel() const { return true; }
89
90 int nx1() const { return xspace_->nx(); }
91 int ndx1() const { return xspace_->ndx(); }
92 int nu() const { return uspace_->ndx(); }
93 int nx2() const { return xspace_next_->nx(); }
94 int ndx2() const { return xspace_next_->ndx(); }
96 int nc() const { return (int)constraints_.totalDim(); }
97
99 std::size_t numConstraints() const { return constraints_.size(); }
100
102 int numDual() const { return ndx2() + nc(); }
103
105 void addConstraint(const PolyFunction &func,
106 const PolyConstraintSet &cstr_set);
107
108 /* Evaluate costs, constraints, ... */
109
112 virtual void evaluate(const ConstVectorRef &x, const ConstVectorRef &u,
113 Data &data) const;
114
116 virtual void computeFirstOrderDerivatives(const ConstVectorRef &x,
117 const ConstVectorRef &u,
118 Data &data) const;
119
121 virtual void computeSecondOrderDerivatives(const ConstVectorRef &x,
122 const ConstVectorRef &u,
123 Data &data) const;
124
126 virtual shared_ptr<Data> createData() const;
127};
128
129#ifdef ALIGATOR_ENABLE_TEMPLATE_INSTANTIATION
130extern template struct StageModelTpl<context::Scalar>;
131#endif
132
133} // namespace aligator
134
135template <typename Scalar>
136struct fmt::formatter<aligator::StageModelTpl<Scalar>> {
137 constexpr auto parse(format_parse_context &ctx) const
138 -> decltype(ctx.begin()) {
139 return ctx.end();
140 }
141
142 auto format(const aligator::StageModelTpl<Scalar> &stage,
143 format_context &ctx) const -> decltype(ctx.out()) {
144 if (stage.ndx1() == stage.ndx2()) {
145 return fmt::format_to(ctx.out(),
146 "StageModel {{"
147 "\n ndx: {:d},"
148 "\n nu: {:d},"
149 "\n nc: {:d}, [{:d} constraints]"
150 "\n}}",
151 stage.ndx1(), stage.nu(), stage.nc(),
152 stage.numConstraints());
153 } else {
154 return fmt::format_to(ctx.out(),
155 "StageModel {{"
156 "\n ndx1: {:d},"
157 "\n nu: {:d},"
158 "\n nc: {:d}, [{:d} constraints]"
159 "\n ndx2: {:d},"
160 "\n}}",
161 stage.ndx1(), stage.nu(), stage.nc(),
162 stage.numConstraints(), stage.ndx2());
163 }
164 }
165};
166
167namespace aligator {
168template <typename Scalar>
169std::ostream &operator<<(std::ostream &oss, const StageModelTpl<Scalar> &sm) {
170 return oss << fmt::format("{}", sm);
171}
172} // namespace aligator
Defines the constraint object and constraint stack manager for this library.
Base definitions for ternary functions.
Namespace for modelling system dynamics.
Main package namespace.
std::ostream & operator<<(std::ostream &oss, const StageFunctionDataTpl< T > &self)
#define ALIGATOR_CHECK_DERIVED_CLASS(Base, Derived)
Convenience class to manage a stack of constraints.
Stage costs for control problems.
Explicit forward dynamics model .
Base class for manifolds, to use in cost funcs, solvers...
Data struct for stage models StageModelTpl.
A stage in the control problem.
virtual ~StageModelTpl()=default
const U * getCost() const
CostAbstractTpl< Scalar > Cost
virtual void computeFirstOrderDerivatives(const ConstVectorRef &x, const ConstVectorRef &u, Data &data) const
Compute the first-order derivatives of the StageModelTpl.
U * getDynamics()
Get a pointer to an expected concrete type for the dynamics class.
virtual bool hasDynModel() const
virtual void computeSecondOrderDerivatives(const ConstVectorRef &x, const ConstVectorRef &u, Data &data) const
Compute the second-order derivatives of the StageModelTpl.
const Manifold & xspace_next() const
polymorphic< Manifold > PolyManifold
ManifoldAbstractTpl< Scalar > Manifold
ConstraintStackTpl< Scalar > constraints_
virtual void evaluate(const ConstVectorRef &x, const ConstVectorRef &u, Data &data) const
Evaluate all the functions (cost, dynamics, constraints) at this node.
polymorphic< ConstraintSetTpl< Scalar > > PolyConstraintSet
ExplicitDynamicsModelTpl< Scalar > Dynamics
StageModelTpl(const PolyCost &cost, const PolyDynamics &dynamics)
virtual shared_ptr< Data > createData() const
Create a StageData object.
const Manifold & uspace() const
StageDataTpl< Scalar > Data
int numDual() const
Number of dual variables, i.e. Lagrange multipliers.
void addConstraint(const PolyFunction &func, const PolyConstraintSet &cstr_set)
Add a constraint to the stage.
const U * getDynamics() const
U * getCost()
Get a pointer to an expected concrete type for the cost function.
polymorphic< Dynamics > PolyDynamics
std::size_t numConstraints() const
Number of constraint objects.
const Manifold & xspace() const
polymorphic< StageFunctionTpl< Scalar > > PolyFunction
int nc() const
Total number of constraints.