aligator  0.15.0
A versatile and efficient C++ library for real-time constrained trajectory optimization.
Loading...
Searching...
No Matches
lagrangian.hpp
Go to the documentation of this file.
1#pragma once
2
9#include "aligator/tracy.hpp"
10
11namespace aligator {
12
14template <typename Scalar> struct LagrangianDerivatives {
18 using BlkView = BlkMatrix<ConstVectorRef, -1, 1>;
19
22 static void compute(const TrajOptProblem &problem, const TrajOptData &pd,
23 const std::vector<VectorXs> &lams,
24 const std::vector<VectorXs> &vs,
25 std::vector<VectorXs> &Lxs, std::vector<VectorXs> &Lus);
26};
27
28template <typename Scalar>
30 const TrajOptData &pd,
31 const std::vector<VectorXs> &lams,
32 const std::vector<VectorXs> &vs,
33 std::vector<VectorXs> &Lxs,
34 std::vector<VectorXs> &Lus) {
35 using ConstraintStack = ConstraintStackTpl<Scalar>;
36 using StageFunctionData = StageFunctionDataTpl<Scalar>;
37 using DynamicsData = ExplicitDynamicsDataTpl<Scalar>;
38 using CostData = CostDataAbstractTpl<Scalar>;
39 using StageModel = StageModelTpl<Scalar>;
40 using StageData = StageDataTpl<Scalar>;
41 const std::size_t nsteps = problem.numSteps();
42 bool has_lbdas = !lams.empty();
43
44 ALIGATOR_TRACY_ZONE_SCOPED_N("LagrangianDerivatives::compute");
46
47 math::setZero(Lxs);
48 math::setZero(Lus);
49
50 // initial condition
51 const StageFunctionData &init_cond = *pd.init_data;
52 if (has_lbdas)
53 Lxs[0].noalias() = init_cond.Jx_.transpose() * lams[0];
54
55 for (std::size_t i = 0; i < nsteps; i++) {
56 const StageModel &sm = *problem.stages_[i];
57 const StageData &sd = *pd.stage_data[i];
58 const ConstraintStack &stack = sm.constraints_;
59 const DynamicsData &dd = *sd.dynamics_data;
60 Lxs[i] += sd.cost_data->Lx_;
61 Lus[i] = sd.cost_data->Lu_;
62 if (has_lbdas) {
63 Lxs[i].noalias() += dd.Jx().transpose() * lams[i + 1];
64 Lus[i].noalias() += dd.Ju().transpose() * lams[i + 1];
65 }
66
67 BlkView v_(vs[i], stack.dims());
68 for (std::size_t j = 0; j < stack.size(); j++) {
69 const StageFunctionData &cd = *sd.constraint_data[j];
70 Lxs[i].noalias() += cd.Jx_.transpose() * v_[j];
71 Lus[i].noalias() += cd.Ju_.transpose() * v_[j];
72 }
73
74 if (has_lbdas) {
75 Lxs[i + 1] = -lams[i + 1];
76 } else {
77 Lxs[i + 1].setZero();
78 }
79 }
80
81 // terminal node
82 {
83 const CostData &cdterm = *pd.term_cost_data;
84 Lxs[nsteps] += cdterm.Lx_; // [1] eqn. 24d
85 const ConstraintStack &stack = problem.term_cstrs_;
86 BlkView vN(vs[nsteps], stack.dims());
87 for (std::size_t j = 0; j < stack.size(); j++) {
88 const StageFunctionData &cd = *pd.term_cstr_data[j];
89 Lxs[nsteps].noalias() += cd.Jx_.transpose() * vN[j];
90 }
91 }
92}
93
94} // namespace aligator
Block matrix class, with a fixed or dynamic-size number of row and column blocks.
#define ALIGATOR_NOMALLOC_SCOPED
Definition math.hpp:44
void setZero(std::vector< T > &mats)
Definition math.hpp:216
Main package namespace.
Convenience class to manage a stack of constraints.
Data struct for CostAbstractTpl.
Specific data struct for explicit dynamics ExplicitDynamicsModelTpl.
Compute the derivatives of the problem Lagrangian.
static void compute(const TrajOptProblem &problem, const TrajOptData &pd, const std::vector< VectorXs > &lams, const std::vector< VectorXs > &vs, std::vector< VectorXs > &Lxs, std::vector< VectorXs > &Lus)
TrajOptDataTpl< Scalar > TrajOptData
BlkMatrix< ConstVectorRef, -1, 1 > BlkView
TrajOptProblemTpl< Scalar > TrajOptProblem
Data struct for stage models StageModelTpl.
Base struct for function data.
A stage in the control problem.
Problem data struct.
std::vector< shared_ptr< StageFunctionData > > term_cstr_data
Terminal constraint data.
shared_ptr< CostData > term_cost_data
Terminal cost data.
std::vector< shared_ptr< StageData > > stage_data
Data structs for each stage of the problem.
shared_ptr< StageFunctionData > init_data
Data for the initial condition.
Trajectory optimization problem.
ConstraintStackTpl< Scalar > term_cstrs_
Terminal constraints.
std::vector< xyz::polymorphic< StageModel > > stages_
Stages of the control problem.
std::size_t numSteps() const