aligator  0.9.0
A primal-dual augmented Lagrangian-type solver for nonlinear trajectory optimization.
Loading...
Searching...
No Matches
stage-model.hpp
Go to the documentation of this file.
1
3#pragma once
4
8
9#include <fmt/ostream.h>
10
11namespace aligator {
12
13#define ALIGATOR_CHECK_DERIVED_CLASS(Base, Derived) \
14 static_assert((std::is_base_of_v<Base, Derived>), \
15 "Failed check for derived class.")
16
24template <typename _Scalar> struct StageModelTpl {
25public:
26 using Scalar = _Scalar;
28
29 using Manifold = ManifoldAbstractTpl<Scalar>;
30 using PolyManifold = xyz::polymorphic<Manifold>;
32 using PolyDynamics = xyz::polymorphic<Dynamics>;
33 using PolyFunction = xyz::polymorphic<StageFunctionTpl<Scalar>>;
34 using PolyConstraintSet = xyz::polymorphic<ConstraintSetTpl<Scalar>>;
36 using PolyCost = xyz::polymorphic<Cost>;
37#pragma GCC diagnostic push
38#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
40#pragma GCC diagnostic pop
42
55
57 template <typename U> U *getCost() {
59 return dynamic_cast<U *>(&*cost_);
60 }
61
63 template <typename U> const U *getCost() const {
65 return dynamic_cast<const U *>(&*cost_);
66 }
67
69 template <typename U> U *getDynamics() {
71 return dynamic_cast<U *>(&*dynamics_);
72 }
73
75 template <typename U> const U *getDynamics() const {
77 return dynamic_cast<const U *>(&*dynamics_);
78 }
79
82 StageModelTpl(const PolyCost &cost, const PolyDynamics &dynamics);
83 virtual ~StageModelTpl() = default;
84
85 const Manifold &xspace() const { return *xspace_; }
86 const Manifold &uspace() const { return *uspace_; }
87 const Manifold &xspace_next() const { return *xspace_next_; }
88
89 const Cost &cost() const { return *cost_; }
93 virtual bool hasDynModel() const { return true; }
94
95 int nx1() const { return xspace_->nx(); }
96 int ndx1() const { return xspace_->ndx(); }
97 int nu() const { return uspace_->ndx(); }
98 int nx2() const { return xspace_next_->nx(); }
99 int ndx2() const { return xspace_next_->ndx(); }
101 int nc() const { return (int)constraints_.totalDim(); }
102
104 std::size_t numConstraints() const { return constraints_.size(); }
105
107 int numPrimal() const { return nu() + ndx2(); }
109 int numDual() const { return ndx2() + nc(); }
110
112 template <typename Cstr, typename = std::enable_if_t<std::is_same_v<
113 std::decay_t<Cstr>, StageConstraint>>>
114 ALIGATOR_DEPRECATED void addConstraint(Cstr &&cstr);
115
118 void addConstraint(const PolyFunction &func,
119 const PolyConstraintSet &cstr_set);
120
121 /* Evaluate costs, constraints, ... */
122
125 virtual void evaluate(const ConstVectorRef &x, const ConstVectorRef &u,
126 const ConstVectorRef &y, Data &data) const;
127
129 virtual void computeFirstOrderDerivatives(const ConstVectorRef &x,
130 const ConstVectorRef &u,
131 const ConstVectorRef &y,
132 Data &data) const;
133
135 virtual void computeSecondOrderDerivatives(const ConstVectorRef &x,
136 const ConstVectorRef &u,
137 Data &data) const;
138
140 virtual shared_ptr<Data> createData() const;
141
142 friend std::ostream &operator<<(std::ostream &oss,
143 const StageModelTpl &stage) {
144 oss << "StageModel { ";
145 if (stage.ndx1() == stage.ndx2()) {
146 oss << "ndx: " << stage.ndx1() << ", "
147 << "nu: " << stage.nu();
148 } else {
149 oss << "ndx1:" << stage.ndx1() << ", "
150 << "nu: " << stage.nu() << ", "
151 << "ndx2:" << stage.ndx2();
152 }
153
154 if (stage.numConstraints() > 0) {
155 oss << ", ";
156 oss << "nc: " << stage.numConstraints();
157 }
158
159 oss << " }";
160 return oss;
161 }
162};
163
164template <typename Scalar>
165template <typename Cstr, typename>
167 const int c_nu = cstr.func->nu;
168 if (c_nu != this->nu()) {
169 ALIGATOR_RUNTIME_ERROR(fmt::format(
170 "Function has the wrong dimension for u: got {:d}, expected {:d}", c_nu,
171 this->nu()));
172 }
173 constraints_.pushBack(std::forward<Cstr>(cstr));
174}
175
176} // namespace aligator
177
178template <typename Scalar>
179struct fmt::formatter<aligator::StageModelTpl<Scalar>>
180 : fmt::ostream_formatter {};
181
182#ifdef ALIGATOR_ENABLE_TEMPLATE_INSTANTIATION
183#include "aligator/core/stage-model.txx"
184#endif
Defines the constraint object and constraint stack manager for this library.
#define ALIGATOR_RUNTIME_ERROR(...)
Definition exceptions.hpp:7
Base definitions for ternary functions.
Main package namespace.
#define ALIGATOR_CHECK_DERIVED_CLASS(Base, Derived)
Convenience class to manage a stack of constraints.
Definition fwd.hpp:104
Stage costs for control problems.
Definition fwd.hpp:65
Dynamics model: describes system dynamics through an implicit relation .
Definition fwd.hpp:71
Simple struct holding together a function and set, to describe a constraint.
Definition fwd.hpp:77
Data struct for stage models StageModelTpl.
Definition fwd.hpp:96
A stage in the control problem.
Definition fwd.hpp:93
PolyDynamics dynamics_
Dynamics model.
virtual void evaluate(const ConstVectorRef &x, const ConstVectorRef &u, const ConstVectorRef &y, Data &data) const
Evaluate all the functions (cost, dynamics, constraints) at this node.
ConstraintStackTpl< Scalar > constraints_
Constraint manager.
friend std::ostream & operator<<(std::ostream &oss, const StageModelTpl &stage)
U * getCost()
Get a pointer to an expected concrete type for the cost function.
ManifoldAbstractTpl< Scalar > Manifold
const U * getDynamics() const
const Manifold & xspace_next() const
virtual bool hasDynModel() const
xyz::polymorphic< Manifold > PolyManifold
PolyCost cost_
Stage cost function.
const Cost & cost() const
void addConstraint(const PolyFunction &func, const PolyConstraintSet &cstr_set)
Add a constraint to the stage.
int numDual() const
Number of dual variables, i.e. Lagrange multipliers.
const U * getCost() const
virtual void computeSecondOrderDerivatives(const ConstVectorRef &x, const ConstVectorRef &u, Data &data) const
Compute the second-order derivatives of the StageModelTpl.
const Manifold & uspace() const
int nc() const
Total number of constraints.
ALIGATOR_DEPRECATED void addConstraint(Cstr &&cstr)
Add a constraint to the stage.
xyz::polymorphic< ConstraintSetTpl< Scalar > > PolyConstraintSet
virtual shared_ptr< Data > createData() const
Create a StageData object.
virtual ~StageModelTpl()=default
StageModelTpl(const PolyCost &cost, const PolyDynamics &dynamics)
virtual void computeFirstOrderDerivatives(const ConstVectorRef &x, const ConstVectorRef &u, const ConstVectorRef &y, Data &data) const
Compute the first-order derivatives of the StageModelTpl.
const Manifold & xspace() const
xyz::polymorphic< StageFunctionTpl< Scalar > > PolyFunction
std::size_t numConstraints() const
Number of constraint objects.
PolyManifold xspace_next_
State space for the next state .
U * getDynamics()
Get a pointer to an expected concrete type for the dynamics class.
int numPrimal() const
Number of primal optimization variables.
xyz::polymorphic< Dynamics > PolyDynamics
PolyManifold uspace_
Control vector space – by default, a simple Euclidean space.
PolyManifold xspace_
State space for the current state .
xyz::polymorphic< Cost > PolyCost