67template <
typename T1,
typename T2>
69 std::vector<T2> &rhs) {
70 static_assert(std::is_base_of_v<Eigen::EigenBase<T1>, T1>,
71 "T1 should be an Eigen object!");
72 static_assert(std::is_base_of_v<Eigen::EigenBase<T2>, T2>,
73 "T2 should be an Eigen object!");
74 if (lhs.size() != rhs.size())
77 const auto same_dims = [](
auto &x,
auto &y) {
78 return (x.cols() == y.cols()) && (x.rows() == y.rows());
81 for (std::size_t i = 0; i < lhs.size(); i++) {
82 if (!same_dims(lhs[i], rhs[i]))
117 std::function<void(
const Self &, std::vector<VectorXs> &)>;
122 std::vector<xyz::polymorphic<StageModel>>
stages_;
135 const std::vector<xyz::polymorphic<StageModel>> &stages,
136 xyz::polymorphic<CostAbstract> term_cost);
140 const std::vector<xyz::polymorphic<StageModel>> &stages,
141 xyz::polymorphic<CostAbstract> term_cost);
149 xyz::polymorphic<CostAbstract> term_cost);
153 xyz::polymorphic<Manifold> space,
154 xyz::polymorphic<CostAbstract> term_cost);
158 assert(init_cond_is_state_error_ == checkInitCondIsStateError());
159 return init_cond_is_state_error_;
163 void addStage(
const xyz::polymorphic<StageModel> &stage);
169 "Initial condition is not a StateErrorResidual.\n");
178 "Initial condition is not a StateErrorResidual.\n");
185 const xyz::polymorphic<ConstraintSet> &set) {
186 this->term_cstrs_.
pushBack(func, set);
196 const std::vector<VectorXs> &us,
Data &prob_data,
197 std::size_t num_threads = 1)
const;
209 const std::vector<VectorXs> &us,
Data &prob_data,
210 std::size_t num_threads = 1,
211 bool compute_second_order =
true)
const;
216 xyz::polymorphic<StageModel>
229 static_assert(std::is_convertible_v<Callable, InitializationStrategy>);
230 this->xs_init_strategy_ = std::forward<Callable>(func);
238 std::vector<VectorXs> &us)
const {
239 xs_init_strategy_(*
this, xs);
245 std::vector<VectorXs> &vs,
246 std::vector<VectorXs> &lbdas)
const {
250 vs.resize(nsteps + 1);
251 lbdas.resize(nsteps + 1);
253 for (
size_t i = 0; i < nsteps; i++) {
255 lbdas[i + 1].setZero(sm.
ndx2());
256 vs[i].setZero(sm.
nc());
266 std::vector<VectorXs> xs, us, vs, lbdas;
268 return std::make_tuple(std::move(xs), std::move(us), std::move(vs),
277 bool checkInitCondIsStateError()
const;
278 bool init_cond_is_state_error_ =
false;
283template <
typename Scalar>
286 const std::size_t nsteps = problem.
numSteps();
287 xs.resize(nsteps + 1);
291 if (problem.
stages_.size() > 0) {
292 xs[0] = problem.
stages_[0]->xspace().neutral();
295 "The problem should have either a StateErrorResidual as an initial "
296 "condition or at least one stage.");
299 for (std::size_t i = 0; i < nsteps; i++) {
307template <
typename Scalar>
310 const std::size_t nsteps = problem.
numSteps();
312 for (std::size_t i = 0; i < nsteps; i++) {
327template <
typename Scalar>
336 }
else if (us_in.empty()) {
348template <
typename Scalar>
349int problem_last_ndx_helper(
const TrajOptProblemTpl<Scalar> &problem) {
350 return problem.term_cost_->ndx();
354#ifdef ALIGATOR_ENABLE_TEMPLATE_INSTANTIATION
#define ALIGATOR_RUNTIME_ERROR(...)
bool assign_no_resize(const std::vector< T1 > &lhs, std::vector< T2 > &rhs)
Assign a vector of Eigen types into another, ensure there is no resize.
void check_initial_guess_and_assign(const TrajOptProblemTpl< Scalar > &problem, const typename math_types< Scalar >::VectorOfVectors &xs_in, const typename math_types< Scalar >::VectorOfVectors &us_in, typename math_types< Scalar >::VectorOfVectors &xs_out, typename math_types< Scalar >::VectorOfVectors &us_out)
Check the input state-control trajectory is a consistent warm-start for the output.
void us_default_init(const TrajOptProblemTpl< Scalar > &problem, std::vector< typename math_types< Scalar >::VectorXs > &us)
Default-initialize a controls trajectory from the neutral element of each control space.
void xs_default_init(const TrajOptProblemTpl< Scalar > &problem, std::vector< typename math_types< Scalar >::VectorXs > &xs)
Default-initialize a trajectory to the neutral states for each state space at each stage.
Base constraint set type.
Convenience class to manage a stack of constraints.
ALIGATOR_DEPRECATED void pushBack(Cstr &&el)
Stage costs for control problems.
Base class for manifolds, to use in cost funcs, solvers...
VectorXs neutral() const
Get the neutral element from the manifold (if this makes sense).
Class representing ternary functions .
A stage in the control problem.
const Manifold & xspace_next() const
const Manifold & uspace() const
int nc() const
Total number of constraints.
Trajectory optimization problem.
void addStage(const xyz::polymorphic< StageModel > &stage)
Add a stage to the control problem.
void addTerminalConstraint(const xyz::polymorphic< StageFunction > &func, const xyz::polymorphic< ConstraintSet > &set)
Add a terminal constraint for the model.
TrajOptProblemTpl(const ConstVectorRef &x0, const std::vector< xyz::polymorphic< StageModel > > &stages, xyz::polymorphic< CostAbstract > term_cost)
Constructor for an initial value problem.
TrajOptProblemTpl(const ConstVectorRef &x0, const int nu, xyz::polymorphic< Manifold > space, xyz::polymorphic< CostAbstract > term_cost)
Constructor for an initial value problem.
ConstraintSetTpl< Scalar > ConstraintSet
ConstraintStackTpl< Scalar > term_cstrs_
bool initCondIsStateError() const
StageFunctionTpl< Scalar > StageFunction
void setInitState(const ConstVectorRef &x0)
Set initial state constraint.
TrajOptDataTpl< Scalar > Data
ALIGATOR_DYNAMIC_TYPEDEFS(Scalar)
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 initializeSolution(std::vector< VectorXs > &xs, std::vector< VectorXs > &us) const
Execute the initialization strategy to generate an initial candidate solution to the problem.
xyz::polymorphic< UnaryFunction > init_constraint_
TrajOptProblemTpl(xyz::polymorphic< UnaryFunction > init_constraint, xyz::polymorphic< CostAbstract > term_cost)
Constructor with a given constraint function of any given type.
void setInitializationStrategy(Callable &&func)
Set a function to initialize the state trajectory.
UnaryFunctionTpl< Scalar > UnaryFunction
void removeTerminalConstraints()
Remove all terminal constraints.
xyz::polymorphic< CostAbstract > term_cost_
CostAbstractTpl< Scalar > CostAbstract
xyz::polymorphic< StageModel > replaceStageCircular(const xyz::polymorphic< StageModel > &model)
Advance each stage model by one slot after inserting a new stage, pop out the first model.
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.
bool checkIntegrity() const
ConstVectorRef getInitState() const
Get initial state constraint.
auto initializeSolution() const
Execute the initialization strategy to generate an initial candidate solution to the problem.
StageModelTpl< Scalar > StageModel
std::vector< xyz::polymorphic< StageModel > > stages_
StateErrorResidualTpl< Scalar > StateErrorResidual
std::function< void(const Self &, std::vector< VectorXs > &)> InitializationStrategy
TrajOptProblemTpl(xyz::polymorphic< UnaryFunction > init_constraint, const std::vector< xyz::polymorphic< StageModel > > &stages, xyz::polymorphic< CostAbstract > term_cost)
Constructor with a given constraint function of any given type.
std::size_t numSteps() const
ManifoldAbstractTpl< Scalar > Manifold
void initializeSolution(std::vector< VectorXs > &xs, std::vector< VectorXs > &us, std::vector< VectorXs > &vs, std::vector< VectorXs > &lbdas) const
Execute the initialization strategy to generate an initial candidate solution to the problem.
Represents unary functions of the form , with no control (or next-state) arguments.
Typedefs for math (Eigen vectors, matrices) depending on scalar type.