aligator  0.16.0
A versatile and efficient C++ library for real-time constrained trajectory optimization.
Loading...
Searching...
No Matches
riccati-kernel.hpp
Go to the documentation of this file.
1
3#pragma once
4
7#include "lqr-problem.hpp"
8
9#include <boost/core/make_span.hpp>
10#include <optional>
11
12namespace aligator {
13namespace gar {
14
16template <class T, class A>
17inline boost::span<T> make_span_from_indices(std::vector<T, A> &vec, size_t i0,
18 size_t i1) {
19 return boost::make_span(vec.data() + i0, i1 - i0);
20}
21
23template <class T, class A>
24inline boost::span<const T> make_span_from_indices(const std::vector<T, A> &vec,
25 size_t i0, size_t i1) {
26 return boost::make_span(vec.data() + i0, i1 - i0);
27}
28
30template <typename _Scalar> struct StageFactor {
31 using Scalar = _Scalar;
34
35 struct CostToGo {
37 ArenaMatrix<MatrixXs> Vxx; //< "cost-to-go" matrix
38 ArenaMatrix<VectorXs> vx; //< "cost-to-go" gradient
39 ArenaMatrix<MatrixXs> Vxt; //< cross-Hessian
40 ArenaMatrix<MatrixXs> Vtt; //< parametric Hessian
41 ArenaMatrix<VectorXs> vt; //< parametric vector
42
43 CostToGo(uint nx, uint nth, const allocator_type &alloc = {})
44 : Vxx(nx, nx, alloc)
45 , vx(nx, alloc)
46 , Vxt(nx, nth, alloc)
47 , Vtt(nth, nth, alloc)
48 , vt(nth, alloc) {
49 Vxt.setZero();
50 Vtt.setZero();
51 vt.setZero();
52 }
53
54 allocator_type get_allocator() const { return Vxx.get_allocator(); }
55
56 CostToGo(const CostToGo &other, const allocator_type &alloc = {})
57 : Vxx(other.Vxx, alloc)
58 , vx(other.vx, alloc)
59 , Vxt(other.Vxt, alloc)
60 , Vtt(other.Vtt, alloc)
61 , vt(other.vt, alloc) {}
62 CostToGo(CostToGo &&other) noexcept = default;
63 CostToGo(CostToGo &&other, const allocator_type &alloc)
64 : Vxx(std::move(other.Vxx), alloc)
65 , vx(std::move(other.vx), alloc)
66 , Vxt(std::move(other.Vxt), alloc)
67 , Vtt(std::move(other.Vtt), alloc)
68 , vt(std::move(other.vt), alloc) {}
69 CostToGo &operator=(const CostToGo &) = default;
70 CostToGo &operator=(CostToGo &&) = default;
71 };
72
74 const allocator_type &alloc = {});
75
76 allocator_type get_allocator() const { return Qhat.get_allocator(); }
77
78 StageFactor(const StageFactor &other, const allocator_type &alloc = {});
79 StageFactor(StageFactor &&) noexcept = default;
80 StageFactor(StageFactor &&other, const allocator_type &alloc);
81
82 StageFactor &operator=(const StageFactor &) = default;
83 StageFactor &operator=(StageFactor &&) = default;
84 ~StageFactor() = default;
85
87 ArenaMatrix<MatrixXs> Qhat;
88 ArenaMatrix<MatrixXs> Rhat;
89 ArenaMatrix<MatrixXs> Shat;
90 ArenaMatrix<VectorXs> qhat;
91 ArenaMatrix<VectorXs> rhat;
92 ArenaMatrix<RowMatrixXs> AtV;
93 ArenaMatrix<RowMatrixXs> BtV;
94 ArenaMatrix<MatrixXs> Gxhat;
95 ArenaMatrix<MatrixXs> Guhat;
96 BlkMatrix<VectorXs, 3, 1> ff; //< feedforward gains
97 BlkMatrix<RowMatrixXs, 3, 1> fb; //< feedback gains
98 BlkMatrix<RowMatrixXs, 3, 1> fth; //< parameter feedback gains
99 BlkMatrix<MatrixXs, 2, 2> kktMat; //< reduced KKT matrix buffer
100 BunchKaufman<MatrixXs> kktChol; //< reduced KKT LDLT solver
101 CostToGo vm; //< cost-to-go parameters
102};
103
106template <typename Scalar> struct ProximalRiccatiKernel {
110 using CostToGo = typename StageFactorType::CostToGo;
111
112 struct kkt0_t {
117 kkt0_t(uint nx, uint nc, uint nth)
118 : mat({nx, nc}, {nx, nc})
119 , ff(mat.rowDims())
120 , fth(mat.rowDims(), {nth}) {}
121 };
122
123 static void terminalSolve(const KnotType &model, const Scalar mueq,
124 StageFactorType &d);
125
126 static bool backwardImpl(boost::span<const KnotType> stages,
127 const Scalar mueq,
128 boost::span<StageFactorType> datas);
129
131 static void computeInitial(VectorRef x0, VectorRef lbd0, const kkt0_t &kkt0,
132 const std::optional<ConstVectorRef> &theta_);
133
134 static void stageKernelSolve(const KnotType &model, StageFactorType &d,
135 CostToGo &vn, const Scalar mueq);
136
138 static bool
139 forwardImpl(boost::span<const KnotType> stages,
140 boost::span<const StageFactorType> datas,
141 boost::span<VectorXs> xs, boost::span<VectorXs> us,
142 boost::span<VectorXs> vs, boost::span<VectorXs> lbdas,
143 const std::optional<ConstVectorRef> &theta_ = std::nullopt);
144};
145
146#ifdef ALIGATOR_ENABLE_TEMPLATE_INSTANTIATION
147extern template struct StageFactor<context::Scalar>;
148extern template struct ProximalRiccatiKernel<context::Scalar>;
149#endif
150
151} // namespace gar
152} // namespace aligator
ArenaMatrix & setZero(Index newSize)
Block matrix class, with a fixed or dynamic-size number of row and column blocks.
const RowDimsType & rowDims() const
A convenience subclass of std::pmr::polymorphic_allocator for bytes.
Definition allocator.hpp:16
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
Struct describing a stage of a constrained LQ problem.
Kernel for use in Riccati-like algorithms for the proximal LQ subproblem.
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.
static bool backwardImpl(boost::span< const KnotType > stages, const Scalar mueq, boost::span< StageFactorType > datas)
static void stageKernelSolve(const KnotType &model, StageFactorType &d, CostToGo &vn, const Scalar mueq)
static void computeInitial(VectorRef x0, VectorRef lbd0, const kkt0_t &kkt0, const std::optional< ConstVectorRef > &theta_)
Solve initial stage.
typename StageFactorType::CostToGo CostToGo
CostToGo & operator=(CostToGo &&)=default
CostToGo(CostToGo &&other) noexcept=default
CostToGo(CostToGo &&other, const allocator_type &alloc)
::aligator::polymorphic_allocator allocator_type
CostToGo & operator=(const CostToGo &)=default
CostToGo(const CostToGo &other, const allocator_type &alloc={})
CostToGo(uint nx, uint nth, const allocator_type &alloc={})
Per-node struct for all computations in the factorization.
StageFactor(const StageFactor &other, const allocator_type &alloc={})
BlkMatrix< RowMatrixXs, 3, 1 > fb
BlkMatrix< RowMatrixXs, 3, 1 > fth
BlkMatrix< VectorXs, 3, 1 > ff
allocator_type get_allocator() const
ALIGATOR_DYNAMIC_TYPEDEFS_WITH_ROW_TYPES(Scalar)
StageFactor(StageFactor &&) noexcept=default
BlkMatrix< MatrixXs, 2, 2 > kktMat
::aligator::polymorphic_allocator allocator_type
StageFactor(uint nx, uint nu, uint nc, uint nx2, uint nth, const allocator_type &alloc={})