aligator  0.6.1
A primal-dual augmented Lagrangian-type solver for nonlinear trajectory optimization.
Loading...
Searching...
No Matches
riccati-impl.hpp
Go to the documentation of this file.
1
3#pragma once
4
5#include "fwd.hpp"
6#include "blk-matrix.hpp"
7
8#include <proxsuite-nlp/linalg/bunchkaufman.hpp>
9#include <Eigen/LU>
10#include <Eigen/Cholesky>
11
13
14#include <optional>
15
16namespace aligator {
17namespace gar {
18
20template <class T, class A>
21inline boost::span<T> make_span_from_indices(std::vector<T, A> &vec, size_t i0,
22 size_t i1) {
23 return boost::make_span(vec.data() + i0, i1 - i0);
24}
25
27template <class T, class A>
28inline boost::span<const T> make_span_from_indices(const std::vector<T, A> &vec,
29 size_t i0, size_t i1) {
30 return boost::make_span(vec.data() + i0, i1 - i0);
31}
32
34template <typename _Scalar> struct StageFactor {
35 using Scalar = _Scalar;
37 using RowMatrixXs = Eigen::Matrix<Scalar, -1, -1, Eigen::RowMajor>;
38
39 struct value_t {
40 MatrixXs Pmat; //< Riccati matrix
41 VectorXs pvec; //< Riccati bias
42 MatrixXs Vxx; //< "cost-to-go" matrix
43 VectorXs vx; //< "cost-to-go" gradient
44 MatrixXs Vxt;
45 MatrixXs Vtt;
46 VectorXs vt;
47
48 value_t(uint nx, uint nth)
49 : Pmat(nx, nx), pvec(nx), Vxx(nx, nx), vx(nx), Vxt(nx, nth),
50 Vtt(nth, nth), vt(nth) {
51 Vxt.setZero();
52 Vtt.setZero();
53 vt.setZero();
54 }
55 };
56
57 StageFactor(uint nx, uint nu, uint nc, uint nx2, uint nth)
58 : Qhat(nx, nx), Rhat(nu, nu), Shat(nx, nu), qhat(nx), rhat(nu),
59 AtV(nx, nx2), BtV(nu, nx2), Gxhat(nx, nth), Guhat(nu, nth),
60 ff({nu, nc, nx2, nx2}, {1}), fb({nu, nc, nx2, nx2}, {nx}),
61 fth({nu, nc, nx2, nx2}, {nth}), kktMat({nu, nc}, {nu, nc}),
62 kktChol(nu + nc), Efact(nx), yff_pre(nx2), A_pre(nx, nx),
63 Yth_pre(nx2, nth), Ptilde(nx, nx), Einv(nx2, nx2), EinvP(nx2, nx2),
64 schurMat(nx2, nx2), schurChol(nx2), vm(nx, nth) {
65 Qhat.setZero();
66 Rhat.setZero();
67 Shat.setZero();
68 qhat.setZero();
69 rhat.setZero();
70
71 AtV.setZero();
72 BtV.setZero();
73
74 Gxhat.setZero();
75 Guhat.setZero();
76
77 ff.setZero();
78 fb.setZero();
79 fth.setZero();
81
82 yff_pre.setZero();
83 A_pre.setZero();
84 Yth_pre.setZero();
85 Ptilde.setZero();
86 Einv.setZero();
87 EinvP.setZero();
88 schurMat.setZero();
89 }
90
91 MatrixXs Qhat;
92 MatrixXs Rhat;
93 MatrixXs Shat;
94 VectorXs qhat;
95 VectorXs rhat;
98
99 // Parametric
100 MatrixXs Gxhat;
101 MatrixXs Guhat;
102
103 BlkMatrix<VectorXs, 4, 1> ff; //< feedforward gains
104 BlkMatrix<RowMatrixXs, 4, 1> fb; //< feedback gains
105 BlkMatrix<RowMatrixXs, 4, 1> fth; //< parameter feedback gains
106 BlkMatrix<MatrixXs, 2, 2> kktMat; //< reduced KKT matrix buffer
107 Eigen::BunchKaufman<MatrixXs> kktChol; //< reduced KKT LDLT solver
108 Eigen::PartialPivLU<MatrixXs> Efact; //< LU decomp. of E matrix
109 VectorXs yff_pre;
110 MatrixXs A_pre;
111 MatrixXs Yth_pre;
112 MatrixXs Ptilde; //< product Et.inv P * E.inv
113 MatrixXs Einv; //< product P * E.inv
114 MatrixXs EinvP; //< product P * E.inv
115 MatrixXs schurMat; //< Dual-space Schur matrix
116 Eigen::LLT<MatrixXs> schurChol; //< Cholesky decomposition of Schur matrix
117 value_t vm; //< cost-to-go parameters
118};
119
122template <typename Scalar> struct ProximalRiccatiKernel {
124 using RowMatrixXs = Eigen::Matrix<Scalar, -1, -1, Eigen::RowMajor>;
125 using RowMatrixRef = Eigen::Ref<RowMatrixXs>;
126 using ConstRowMatrixRef = Eigen::Ref<const RowMatrixXs>;
127 using KnotType = LQRKnotTpl<Scalar>;
128 using StageFactorType = StageFactor<Scalar>;
130
131 struct kkt0_t {
132 BlkMatrix<MatrixXs, 2, 2> mat;
133 BlkMatrix<VectorXs, 2, 1> ff;
134 BlkMatrix<RowMatrixXs, 2, 1> fth;
135 Eigen::BunchKaufman<MatrixXs> chol{mat.rows()};
136 kkt0_t(uint nx, uint nc, uint nth)
137 : mat({nx, nc}, {nx, nc}), ff(mat.rowDims()),
138 fth(mat.rowDims(), {nth}) {}
139 };
140
141 inline static void terminalSolve(const KnotType &model, const Scalar mueq,
142 StageFactorType &d);
143
144 inline static bool backwardImpl(boost::span<const KnotType> stages,
145 const Scalar mudyn, const Scalar mueq,
147
149 inline static void
150 computeInitial(VectorRef x0, VectorRef lbd0, const kkt0_t &kkt0,
151 const std::optional<ConstVectorRef> &theta_);
152
153 inline static void stageKernelSolve(const KnotType &model, StageFactorType &d,
154 value_t &vn, const Scalar mudyn,
155 const Scalar mueq);
156
158 inline static bool
163 const std::optional<ConstVectorRef> &theta_ = std::nullopt);
164};
165
166} // namespace gar
167} // namespace aligator
168
169#include "./riccati-impl.hxx"
170
171#ifdef ALIGATOR_ENABLE_TEMPLATE_INSTANTIATION
172#include "./riccati-impl.txx"
173#endif
const row_dim_t & rowDims() const
boost::span< T > make_span_from_indices(std::vector< T, A > &vec, size_t i0, size_t i1)
Create a boost::span object from a vector and two indices.
Main package namespace.
unsigned int uint
Definition logger.hpp:11
constexpr span< I > make_span(I *f, std::size_t c) noexcept
Definition make_span.hpp:17
Eigen::BunchKaufman< MatrixXs > chol
Kernel for use in Riccati-like algorithms for the proximal LQ subproblem.
static bool backwardImpl(boost::span< const KnotType > stages, const Scalar mudyn, const Scalar mueq, boost::span< StageFactorType > datas)
Eigen::Ref< const RowMatrixXs > ConstRowMatrixRef
static void terminalSolve(const KnotType &model, const Scalar mueq, StageFactorType &d)
static bool forwardImpl(boost::span< const KnotType > stages, boost::span< const StageFactorType > datas, boost::span< VectorXs > xs, boost::span< VectorXs > us, boost::span< VectorXs > vs, boost::span< VectorXs > lbdas, const std::optional< ConstVectorRef > &theta_=std::nullopt)
Forward sweep.
typename StageFactor< Scalar >::value_t value_t
static void stageKernelSolve(const KnotType &model, StageFactorType &d, value_t &vn, const Scalar mudyn, const Scalar mueq)
static void computeInitial(VectorRef x0, VectorRef lbd0, const kkt0_t &kkt0, const std::optional< ConstVectorRef > &theta_)
Solve initial stage.
Eigen::Matrix< Scalar, -1, -1, Eigen::RowMajor > RowMatrixXs
Eigen::Ref< RowMatrixXs > RowMatrixRef
Per-node struct for all computations in the factorization.
BlkMatrix< RowMatrixXs, 4, 1 > fth
Eigen::Matrix< Scalar, -1, -1, Eigen::RowMajor > RowMatrixXs
Eigen::PartialPivLU< MatrixXs > Efact
BlkMatrix< VectorXs, 4, 1 > ff
Eigen::LLT< MatrixXs > schurChol
BlkMatrix< RowMatrixXs, 4, 1 > fb
Eigen::BunchKaufman< MatrixXs > kktChol
BlkMatrix< MatrixXs, 2, 2 > kktMat
StageFactor(uint nx, uint nu, uint nc, uint nx2, uint nth)