aligator  0.6.1
A primal-dual augmented Lagrangian-type solver for nonlinear trajectory optimization.
Loading...
Searching...
No Matches
lqr-problem.hpp
Go to the documentation of this file.
1
2#pragma once
3
4#include "aligator/math.hpp"
5
6#include <optional>
7
8namespace aligator {
9namespace gar {
10
27template <typename Scalar> struct LQRKnotTpl {
29
31 MatrixXs Q, S, R;
32 VectorXs q, r;
33 MatrixXs A, B, E;
34 VectorXs f;
35 MatrixXs C, D;
36 VectorXs d;
37
38 MatrixXs Gth;
39 MatrixXs Gx;
40 MatrixXs Gu;
41 MatrixXs Gv;
42 VectorXs gamma;
43
44 LQRKnotTpl() = default;
45
47 : nx(nx), nu(nu), nc(nc), nx2(nx2), nth(nth), //
48 Q(nx, nx), S(nx, nu), R(nu, nu), q(nx), r(nu), //
49 A(nx2, nx), B(nx2, nu), E(nx2, nx), f(nx2), //
50 C(nc, nx), D(nc, nu), d(nc), Gth(nth, nth), Gx(nx, nth), Gu(nu, nth),
51 Gv(nc, nth), gamma(nth) {
52 Q.setZero();
53 S.setZero();
54 R.setZero();
55 q.setZero();
56 r.setZero();
57
58 A.setZero();
59 B.setZero();
60 E.setZero();
61 f.setZero();
62
63 C.setZero();
64 D.setZero();
65 d.setZero();
66
67 Gth.setZero();
68 Gx.setZero();
69 Gu.setZero();
70 Gv.setZero();
71 gamma.setZero();
72 }
73
75
76 // reallocates entire buffer for contigousness
78 this->nth = nth;
79 Gth.setZero(nth, nth);
80 Gx.setZero(nx, nth);
81 Gu.setZero(nu, nth);
82 Gv.setZero(nc, nth);
83 gamma.setZero(nth);
84 }
85};
86
87template <typename Scalar> struct LQRProblemTpl {
89 using KnotType = LQRKnotTpl<Scalar>;
90 using KnotVector = std::vector<KnotType>;
92 MatrixXs G0;
93 VectorXs g0;
94
95 inline int horizon() const noexcept { return int(stages.size()) - 1; }
96 inline uint nc0() const noexcept { return (uint)g0.rows(); }
97
98 LQRProblemTpl() : stages(), G0(), g0() {}
99
100 LQRProblemTpl(KnotVector &&knots, long nc0) : stages(knots), G0(), g0(nc0) {
101 initialize();
102 }
103
104 LQRProblemTpl(const KnotVector &knots, long nc0)
105 : stages(knots), G0(), g0(nc0) {
106 initialize();
107 }
108
110 if (!isInitialized())
111 return;
112 for (uint i = 0; i <= (uint)horizon(); i++) {
113 stages[i].addParameterization(nth);
114 }
115 }
116
117 inline bool isParameterized() const {
118 return isInitialized() && (stages[0].nth > 0);
119 }
120
121 inline bool isInitialized() const { return !stages.empty(); }
122
123 inline uint ntheta() const { return stages[0].nth; }
124
126 Scalar evaluate(const VectorOfVectors &xs, const VectorOfVectors &us,
127 const std::optional<ConstVectorRef> &theta_) const;
128
129protected:
130 void initialize() {
131 assert(isInitialized());
132 auto nx0 = stages[0].nx;
133 G0.resize(nc0(), nx0);
134 }
135};
136
137template <typename Scalar>
138std::ostream &operator<<(std::ostream &oss, const LQRKnotTpl<Scalar> &self) {
139 oss << "LQRKnot {";
140 oss << fmt::format("\n nx: {:d}", self.nx) //
141 << fmt::format("\n nu: {:d}", self.nu) //
142 << fmt::format("\n nc: {:d}", self.nc);
143 if (self.nth > 0) {
144 oss << fmt::format("\n nth: {:d}", self.nth);
145 }
146#ifndef NDEBUG
147 oss << eigenPrintWithPreamble(self.Q, "\n Q: ") //
148 << eigenPrintWithPreamble(self.S, "\n S: ") //
149 << eigenPrintWithPreamble(self.R, "\n R: ") //
150 << eigenPrintWithPreamble(self.q, "\n q: ") //
151 << eigenPrintWithPreamble(self.r, "\n r: ");
152
153 oss << eigenPrintWithPreamble(self.A, "\n A: ") //
154 << eigenPrintWithPreamble(self.B, "\n B: ") //
155 << eigenPrintWithPreamble(self.E, "\n E: ") //
156 << eigenPrintWithPreamble(self.f, "\n f: ");
157
158 oss << eigenPrintWithPreamble(self.C, "\n C: ") //
159 << eigenPrintWithPreamble(self.D, "\n D: ") //
160 << eigenPrintWithPreamble(self.d, "\n d: ");
161 if (self.nth > 0) {
162 oss << eigenPrintWithPreamble(self.Gth, "\n Gth: ") //
163 << eigenPrintWithPreamble(self.Gx, "\n Gx: ") //
164 << eigenPrintWithPreamble(self.Gu, "\n Gu: ") //
165 << eigenPrintWithPreamble(self.gamma, "\n gamma: ");
166 }
167#endif
168 oss << "\n}";
169 return oss;
170}
171
172} // namespace gar
173} // namespace aligator
174
175#include "lqr-problem.hxx"
176
177#ifdef ALIGATOR_ENABLE_TEMPLATE_INSTANTIATION
178#include "lqr-problem.txx"
179#endif
Math utilities.
std::ostream & operator<<(std::ostream &oss, const LQRKnotTpl< Scalar > &self)
Main package namespace.
unsigned int uint
Definition logger.hpp:11
auto eigenPrintWithPreamble(const Eigen::EigenBase< D > &mat, const std::string &text)
Definition math.hpp:36
Struct describing a stage of a constrained LQ problem.
LQRKnotTpl(uint nx, uint nu, uint nc)
LQRKnotTpl(uint nx, uint nu, uint nc, uint nx2, uint nth=0)
void addParameterization(uint nth)
LQRProblemTpl(KnotVector &&knots, long nc0)
LQRProblemTpl(const KnotVector &knots, long nc0)
std::vector< KnotType > KnotVector
LQRKnotTpl< Scalar > KnotType
uint nc0() const noexcept
Scalar evaluate(const VectorOfVectors &xs, const VectorOfVectors &us, const std::optional< ConstVectorRef > &theta_) const
Evaluate the quadratic objective.
int horizon() const noexcept