aligator  0.10.0
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#include <fmt/format.h>
6
7#include <optional>
8
9namespace aligator {
10namespace gar {
11
28template <typename Scalar> struct LQRKnotTpl {
30
32 MatrixXs Q, S, R;
33 VectorXs q, r;
34 MatrixXs A, B, E;
35 VectorXs f;
36 MatrixXs C, D;
37 VectorXs d;
38
39 MatrixXs Gth;
40 MatrixXs Gx;
41 MatrixXs Gu;
42 MatrixXs Gv;
43 VectorXs gamma;
44
46
48
49 // reallocates entire buffer for contigousness
51 bool isApprox(const LQRKnotTpl &other,
52 Scalar prec = std::numeric_limits<Scalar>::epsilon()) const;
53
54 friend bool operator==(const LQRKnotTpl &lhs, const LQRKnotTpl &rhs) {
55 return lhs.isApprox(rhs);
56 }
57};
58
59template <typename Scalar> struct LQRProblemTpl {
62 using KnotVector = std::vector<KnotType>;
64 MatrixXs G0;
65 VectorXs g0;
66
67 inline int horizon() const noexcept { return int(stages.size()) - 1; }
68 inline uint nc0() const noexcept { return (uint)g0.rows(); }
69
70 LQRProblemTpl() : stages(), G0(), g0() {}
71
72 LQRProblemTpl(KnotVector &&knots, long nc0) : stages(knots), G0(), g0(nc0) {
73 initialize();
74 }
75
76 LQRProblemTpl(const KnotVector &knots, long nc0)
77 : stages(knots), G0(), g0(nc0) {
78 initialize();
79 }
80
82 if (!isInitialized())
83 return;
84 for (uint i = 0; i <= (uint)horizon(); i++) {
85 stages[i].addParameterization(nth);
86 }
87 }
88
89 inline bool isParameterized() const {
90 return isInitialized() && (stages[0].nth > 0);
91 }
92
93 inline bool isInitialized() const { return !stages.empty(); }
94
95 inline uint ntheta() const { return stages[0].nth; }
96
98 Scalar evaluate(const VectorOfVectors &xs, const VectorOfVectors &us,
99 const std::optional<ConstVectorRef> &theta_) const;
100
101protected:
102 void initialize() {
103 assert(isInitialized());
104 auto nx0 = stages[0].nx;
105 G0.resize(nc0(), nx0);
106 }
107};
108
109template <typename Scalar>
110std::ostream &operator<<(std::ostream &oss, const LQRKnotTpl<Scalar> &self) {
111 oss << "LQRKnot {";
112 oss << fmt::format("\n nx: {:d}", self.nx) //
113 << fmt::format("\n nu: {:d}", self.nu) //
114 << fmt::format("\n nc: {:d}", self.nc);
115 if (self.nth > 0) {
116 oss << fmt::format("\n nth: {:d}", self.nth);
117 }
118#ifndef NDEBUG
119 oss << eigenPrintWithPreamble(self.Q, "\n Q: ") //
120 << eigenPrintWithPreamble(self.S, "\n S: ") //
121 << eigenPrintWithPreamble(self.R, "\n R: ") //
122 << eigenPrintWithPreamble(self.q, "\n q: ") //
123 << eigenPrintWithPreamble(self.r, "\n r: ");
124
125 oss << eigenPrintWithPreamble(self.A, "\n A: ") //
126 << eigenPrintWithPreamble(self.B, "\n B: ") //
127 << eigenPrintWithPreamble(self.E, "\n E: ") //
128 << eigenPrintWithPreamble(self.f, "\n f: ");
129
130 oss << eigenPrintWithPreamble(self.C, "\n C: ") //
131 << eigenPrintWithPreamble(self.D, "\n D: ") //
132 << eigenPrintWithPreamble(self.d, "\n d: ");
133 if (self.nth > 0) {
134 oss << eigenPrintWithPreamble(self.Gth, "\n Gth: ") //
135 << eigenPrintWithPreamble(self.Gx, "\n Gx: ") //
136 << eigenPrintWithPreamble(self.Gu, "\n Gu: ") //
137 << eigenPrintWithPreamble(self.gamma, "\n gamma: ");
138 }
139#endif
140 oss << "\n}";
141 return oss;
142}
143
144} // namespace gar
145} // namespace aligator
146
147#ifdef ALIGATOR_ENABLE_TEMPLATE_INSTANTIATION
148#include "lqr-problem.txx"
149#endif
Math utilities.
unsigned int uint
Definition work.hpp:7
std::ostream & operator<<(std::ostream &oss, const LQRKnotTpl< Scalar > &self)
Main package namespace.
auto eigenPrintWithPreamble(const Eigen::EigenBase< D > &mat, const std::string &text)
Definition math.hpp:26
Struct describing a stage of a constrained LQ problem.
LQRKnotTpl(uint nx, uint nu, uint nc)
friend bool operator==(const LQRKnotTpl &lhs, const LQRKnotTpl &rhs)
bool isApprox(const LQRKnotTpl &other, Scalar prec=std::numeric_limits< Scalar >::epsilon()) const
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
uint nc0() const noexcept
void addParameterization(uint nth)
Scalar evaluate(const VectorOfVectors &xs, const VectorOfVectors &us, const std::optional< ConstVectorRef > &theta_) const
Evaluate the quadratic objective.
int horizon() const noexcept