aligator  0.9.0
A primal-dual augmented Lagrangian-type solver for nonlinear trajectory optimization.
Loading...
Searching...
No Matches
lagrangian.hpp
Go to the documentation of this file.
1#pragma once
2
8#include "aligator/tracy.hpp"
9
10namespace aligator {
11
13template <typename Scalar> struct LagrangianDerivatives {
17 using BlkView = BlkMatrix<ConstVectorRef, -1, 1>;
18
19 static void compute(const TrajOptProblem &problem, const TrajOptData &pd,
20 const std::vector<VectorXs> &lams,
21 const std::vector<VectorXs> &vs,
22 std::vector<VectorXs> &Lxs, std::vector<VectorXs> &Lus);
23};
24
25template <typename Scalar>
27 const TrajOptData &pd,
28 const std::vector<VectorXs> &lams,
29 const std::vector<VectorXs> &vs,
30 std::vector<VectorXs> &Lxs,
31 std::vector<VectorXs> &Lus) {
32 using ConstraintStack = ConstraintStackTpl<Scalar>;
33 using StageFunctionData = StageFunctionDataTpl<Scalar>;
34 using DynamicsData = DynamicsDataTpl<Scalar>;
35 using CostData = CostDataAbstractTpl<Scalar>;
36 using StageModel = StageModelTpl<Scalar>;
37 using StageData = StageDataTpl<Scalar>;
38 const std::size_t nsteps = problem.numSteps();
39
40 ALIGATOR_TRACY_ZONE_SCOPED_N("LagrangianDerivatives::compute");
42
43 math::setZero(Lxs);
44 math::setZero(Lus);
45
46 // initial condition
47 const StageFunctionData &init_cond = *pd.init_data;
48 Lxs[0].noalias() = init_cond.Jx_.transpose() * lams[0];
49
50 for (std::size_t i = 0; i < nsteps; i++) {
51 const StageModel &sm = *problem.stages_[i];
52 const StageData &sd = *pd.stage_data[i];
53 const ConstraintStack &stack = sm.constraints_;
54 const DynamicsData &dd = *sd.dynamics_data;
55 Lxs[i].noalias() +=
56 sd.cost_data->Lx_ + dd.Jx_.transpose() * lams[i + 1]; // [1] eqn. 24c
57 Lus[i].noalias() =
58 sd.cost_data->Lu_ + dd.Ju_.transpose() * lams[i + 1]; // [1] eqn. 24b
59
60 BlkView v_(vs[i], stack.dims());
61 for (std::size_t j = 0; j < stack.size(); j++) {
62 const StageFunctionData &cd = *sd.constraint_data[j];
63 Lxs[i].noalias() += cd.Jx_.transpose() * v_[j]; // [1] eqn. 24c
64 Lus[i].noalias() += cd.Ju_.transpose() * v_[j]; // [1] eqn. 24b
65 }
66
67 Lxs[i + 1].noalias() = dd.Jy_.transpose() * lams[i + 1]; // [1] eqn. 24b
68 }
69
70 // terminal node
71 {
72 const CostData &cdterm = *pd.term_cost_data;
73 Lxs[nsteps] += cdterm.Lx_;
74 const ConstraintStack &stack = problem.term_cstrs_;
75 BlkView vN(vs[nsteps], stack.dims());
76 for (std::size_t j = 0; j < stack.size(); j++) {
77 const StageFunctionData &cd = *pd.term_cstr_data[j];
78 Lxs[nsteps].noalias() += cd.Jx_.transpose() * vN[j];
79 }
80 }
81}
82
83} // namespace aligator
Block matrix class, with a fixed-size number of row and column blocks.
#define ALIGATOR_NOMALLOC_SCOPED
void setZero(std::vector< T > &mats)
Definition math.hpp:60
Main package namespace.
Convenience class to manage a stack of constraints.
Definition fwd.hpp:104
Data struct for CostAbstractTpl.
Definition fwd.hpp:68
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)
Data struct for stage models StageModelTpl.
Definition fwd.hpp:96
Base struct for function data.
Definition fwd.hpp:62
A stage in the control problem.
Definition fwd.hpp:93
Problem data struct.
Definition fwd.hpp:110
shared_ptr< CostData > term_cost_data
Terminal cost data.
std::vector< shared_ptr< StageFunctionData > > term_cstr_data
Terminal constraint data.
shared_ptr< StageFunctionData > init_data
Data for the initial condition.
std::vector< shared_ptr< StageData > > stage_data
Data structs for each stage of the problem.
Trajectory optimization problem.
Definition fwd.hpp:107
ConstraintStackTpl< Scalar > term_cstrs_
Terminal constraints.
std::size_t numSteps() const
std::vector< xyz::polymorphic< StageModel > > stages_
Stages of the control problem.