aligator  0.6.1
A primal-dual augmented Lagrangian-type solver for nonlinear trajectory optimization.
Loading...
Searching...
No Matches
traj-opt-problem.hpp
Go to the documentation of this file.
1
3#pragma once
4
7
8namespace aligator {
9
23template <typename _Scalar> struct TrajOptProblemTpl {
24 using Scalar = _Scalar;
25 using StageModel = StageModelTpl<Scalar>;
26 using UnaryFunction = UnaryFunctionTpl<Scalar>;
27 using Data = TrajOptDataTpl<Scalar>;
28 using Manifold = ManifoldAbstractTpl<Scalar>;
29 using CostAbstract = CostAbstractTpl<Scalar>;
30 using ConstraintType = StageConstraintTpl<Scalar>;
31 using StateErrorResidual = StateErrorResidualTpl<Scalar>;
32
88
90 shared_ptr<UnaryFunction> init_condition_;
92 std::vector<shared_ptr<StageModel>> stages_;
94 shared_ptr<CostAbstract> term_cost_;
96 ConstraintStackTpl<Scalar> term_cstrs_;
98 VectorXs unone_;
99
101
103 TrajOptProblemTpl(shared_ptr<UnaryFunction> init_constraint,
104 const std::vector<shared_ptr<StageModel>> &stages,
105 shared_ptr<CostAbstract> term_cost);
106
109 TrajOptProblemTpl(const ConstVectorRef &x0,
110 const std::vector<shared_ptr<StageModel>> &stages,
111 shared_ptr<CostAbstract> term_cost);
112
114
116 TrajOptProblemTpl(shared_ptr<UnaryFunction> init_constraint,
117 shared_ptr<CostAbstract> term_cost);
118
121 TrajOptProblemTpl(const ConstVectorRef &x0, const int nu,
122 shared_ptr<Manifold> space,
123 shared_ptr<CostAbstract> term_cost);
124
125 bool initCondIsStateError() const { return init_state_error_ != nullptr; }
126
128 void addStage(const shared_ptr<StageModel> &stage);
129
131 ConstVectorRef getInitState() const {
132 if (!initCondIsStateError()) {
134 "Initial condition is not a StateErrorResidual.\n");
135 }
136 return init_state_error_->target_;
137 }
138
140 void setInitState(const ConstVectorRef &x0) {
141 if (!initCondIsStateError()) {
143 "Initial condition is not a StateErrorResidual.\n");
144 }
145 init_state_error_->target_ = x0;
146 }
147
152
153 std::size_t numSteps() const;
154
156 Scalar evaluate(const std::vector<VectorXs> &xs,
157 const std::vector<VectorXs> &us, Data &prob_data,
158 std::size_t num_threads = 1) const;
159
169 void computeDerivatives(const std::vector<VectorXs> &xs,
170 const std::vector<VectorXs> &us, Data &prob_data,
171 std::size_t num_threads = 1,
172 bool compute_second_order = true) const;
173
176 void replaceStageCircular(const shared_ptr<StageModel> &model);
177
181 Scalar computeTrajectoryCost(const Data &problem_data) const;
182
183 inline void checkIntegrity() const {
184 checkStages();
185
186 if (term_cost_ == nullptr) {
187 ALIGATOR_RUNTIME_ERROR("Problem has no terminal cost.");
188 }
189 }
190
191protected:
195 void checkStages() const;
196
197private:
198 static auto createStateError(const ConstVectorRef &x0,
199 const shared_ptr<Manifold> &space,
200 const int nu) {
201 return std::make_shared<StateErrorResidual>(space, nu, x0);
202 }
203};
204
205namespace internal {
206template <typename Scalar>
207auto problem_last_state_space_helper(const TrajOptProblemTpl<Scalar> &problem) {
208 return problem.term_cost_->space;
209}
210
212template <typename Scalar>
213int problem_last_ndx_helper(const TrajOptProblemTpl<Scalar> &problem) {
214 return problem_last_state_space_helper(problem)->ndx();
215}
216} // namespace internal
217
218} // namespace aligator
219
220#include "aligator/core/traj-opt-problem.hxx"
221
222#ifdef ALIGATOR_ENABLE_TEMPLATE_INSTANTIATION
223#include "aligator/core/traj-opt-problem.txx"
224#endif
#define ALIGATOR_RUNTIME_ERROR(msg)
Definition exceptions.hpp:6
TrajOptProblemTpl(shared_ptr< UnaryFunction > init_constraint, const std::vector< shared_ptr< StageModel > > &stages, shared_ptr< CostAbstract > term_cost)
TrajOptProblemTpl(const ConstVectorRef &x0, const std::vector< shared_ptr< StageModel > > &stages, shared_ptr< CostAbstract > term_cost)
Constructor for an initial value problem.
TrajOptProblemTpl(shared_ptr< UnaryFunction > init_constraint, shared_ptr< CostAbstract > term_cost)
TrajOptProblemTpl(const ConstVectorRef &x0, const int nu, shared_ptr< Manifold > space, shared_ptr< CostAbstract > term_cost)
Constructor for an initial value problem.
Main package namespace.
Trajectory optimization problem.
ConstraintStackTpl< Scalar > term_cstrs_
Terminal constraints.
void setInitState(const ConstVectorRef &x0)
Set initial state constraint.
StageConstraintTpl< Scalar > ConstraintType
StateErrorResidual * init_state_error_
Pointer to underlying state error residual.
void computeDerivatives(const std::vector< VectorXs > &xs, const std::vector< VectorXs > &us, Data &prob_data, std::size_t num_threads=1, bool compute_second_order=true) const
Rollout the problem derivatives, stage per stage.
void addTerminalConstraint(const ConstraintType &cstr)
Add a terminal constraint for the model.
void removeTerminalConstraints()
Remove all terminal constraints.
Scalar computeTrajectoryCost(const Data &problem_data) const
Helper for computing the trajectory cost (from pre-computed problem data).
shared_ptr< UnaryFunction > init_condition_
Initial condition.
Scalar evaluate(const std::vector< VectorXs > &xs, const std::vector< VectorXs > &us, Data &prob_data, std::size_t num_threads=1) const
Rollout the problem costs, constraints, dynamics, stage per stage.
void addStage(const shared_ptr< StageModel > &stage)
Add a stage to the control problem.
VectorXs unone_
Dummy, "neutral" control value.
void replaceStageCircular(const shared_ptr< StageModel > &model)
Pop out the first StageModel and replace by the supplied one; updates the supplied problem data (Traj...
ConstVectorRef getInitState() const
Get initial state constraint.
void checkStages() const
Check if all stages are non-null.
StateErrorResidualTpl< Scalar > StateErrorResidual
shared_ptr< CostAbstract > term_cost_
Terminal cost.
std::vector< shared_ptr< StageModel > > stages_
Stages of the control problem.
std::size_t numSteps() const
ManifoldAbstractTpl< Scalar > Manifold