aligator  0.14.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
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
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
143template <typename Scalar>
144template <typename Cstr, typename>
146 const int c_nu = cstr.func->nu;
147 if (c_nu != this->nu()) {
148 ALIGATOR_RUNTIME_ERROR(fmt::format(
149 "Function has the wrong dimension for u: got {:d}, expected {:d}", c_nu,
150 this->nu()));
151 }
152 constraints_.pushBack(std::forward<Cstr>(cstr));
153}
154
155template <typename Scalar>
156std::ostream &operator<<(std::ostream &oss,
157 const StageModelTpl<Scalar> &stage) {
158 return oss << fmt::format("{}", stage);
159}
160
161} // namespace aligator
162
163template <typename Scalar>
164struct fmt::formatter<aligator::StageModelTpl<Scalar>> {
165 constexpr auto parse(format_parse_context &ctx) const
166 -> decltype(ctx.begin()) {
167 return ctx.end();
168 }
169
170 auto format(const aligator::StageModelTpl<Scalar> &stage,
171 format_context &ctx) const -> decltype(ctx.out()) {
172 if (stage.ndx1() == stage.ndx2()) {
173 return fmt::format_to(ctx.out(),
174 "StageModel {{"
175 "\n ndx: {:d},"
176 "\n nu: {:d},"
177 "\n nc: {:d}, [{:d} constraints]"
178 "\n}}",
179 stage.ndx1(), stage.nu(), stage.nc(),
180 stage.numConstraints());
181 } else {
182 return fmt::format_to(ctx.out(),
183 "StageModel {{"
184 "\n ndx1: {:d},"
185 "\n nu: {:d},"
186 "\n nc: {:d}, [{:d} constraints]"
187 "\n ndx2: {:d},"
188 "\n}}",
189 stage.ndx1(), stage.nu(), stage.nc(),
190 stage.numConstraints(), stage.ndx2());
191 }
192 }
193};
194
195#ifdef ALIGATOR_ENABLE_TEMPLATE_INSTANTIATION
196#include "aligator/core/stage-model.txx"
197#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.
Namespace for modelling system dynamics.
Main package namespace.
std::ostream & operator<<(std::ostream &oss, const ExplicitDynamicsDataTpl< S > &self)
#define ALIGATOR_CHECK_DERIVED_CLASS(Base, Derived)
Convenience class to manage a stack of constraints.
Stage costs for control problems.
Dynamics model: describes system dynamics through an implicit relation .
Definition dynamics.hpp:14
Base class for manifolds, to use in cost funcs, solvers...
Simple struct holding together a function and set, to describe a constraint.
Data struct for stage models StageModelTpl.
A stage in the control problem.
virtual ~StageModelTpl()=default
virtual void computeFirstOrderDerivatives(const ConstVectorRef &x, const ConstVectorRef &u, const ConstVectorRef &y, Data &data) const
Compute the first-order derivatives of the StageModelTpl.
const U * getCost() const
CostAbstractTpl< Scalar > Cost
StageConstraintTpl< Scalar > StageConstraint
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
ALIGATOR_DEPRECATED void addConstraint(Cstr &&cstr)
Add a constraint to the stage.
ManifoldAbstractTpl< Scalar > Manifold
xyz::polymorphic< Cost > PolyCost
xyz::polymorphic< Manifold > PolyManifold
ConstraintStackTpl< Scalar > constraints_
DynamicsModelTpl< Scalar > Dynamics
StageModelTpl(const PolyCost &cost, const PolyDynamics &dynamics)
virtual shared_ptr< Data > createData() const
Create a StageData object.
xyz::polymorphic< ConstraintSetTpl< Scalar > > PolyConstraintSet
const Manifold & uspace() const
xyz::polymorphic< Dynamics > PolyDynamics
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.
std::size_t numConstraints() const
Number of constraint objects.
int numPrimal() const
Number of primal optimization variables.
xyz::polymorphic< StageFunctionTpl< Scalar > > PolyFunction
const Manifold & xspace() const
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.
int nc() const
Total number of constraints.