GCC Code Coverage Report


Directory: ./
File: include/crocoddyl/core/actions/diff-lqr.hpp
Date: 2025-02-24 23:41:29
Exec Total Coverage
Lines: 15 24 62.5%
Branches: 16 32 50.0%

Line Branch Exec Source
1 ///////////////////////////////////////////////////////////////////////////////
2 // BSD 3-Clause License
3 //
4 // Copyright (C) 2019-2024, LAAS-CNRS, University of Edinburgh,
5 // Heriot-Watt University
6 // Copyright note valid unless otherwise stated in individual files.
7 // All rights reserved.
8 ///////////////////////////////////////////////////////////////////////////////
9
10 #ifndef CROCODDYL_CORE_ACTIONS_DIFF_LQR_HPP_
11 #define CROCODDYL_CORE_ACTIONS_DIFF_LQR_HPP_
12
13 #include <stdexcept>
14
15 #include "crocoddyl/core/diff-action-base.hpp"
16 #include "crocoddyl/core/fwd.hpp"
17 #include "crocoddyl/core/states/euclidean.hpp"
18
19 namespace crocoddyl {
20
21 /**
22 * @brief Linear-quadratic regulator (LQR) differential action model
23 *
24 * A linear-quadratic regulator (LQR) action has a transition model of the form
25 * \f[ \begin{equation}
26 * \mathbf{\dot{v}} = \mathbf{A_q q + A_v v + B u + f}.
27 * \end{equation} \f]
28 * Its cost function is quadratic of the form:
29 * \f[ \begin{equation}
30 * \ell(\mathbf{x},\mathbf{u}) = \begin{bmatrix}1
31 * \\ \mathbf{x} \\ \mathbf{u}\end{bmatrix}^T \begin{bmatrix}0 &
32 * \mathbf{q}^T & \mathbf{r}^T \\ \mathbf{q} & \mathbf{Q}
33 * &
34 * \mathbf{N}^T \\
35 * \mathbf{r} & \mathbf{N} & \mathbf{R}\end{bmatrix}
36 * \begin{bmatrix}1 \\ \mathbf{x} \\
37 * \mathbf{u}\end{bmatrix}
38 * \end{equation} \f]
39 * and the linear equality and inequality constraints has the form:
40 * \f[ \begin{aligned}
41 * \mathbf{g(x,u)} = \mathbf{G}\begin{bmatrix} \mathbf{x} \\ \mathbf{u}
42 * \end{bmatrix} [x,u] + \mathbf{g} \leq \mathbf{0}
43 * &\mathbf{h(x,u)} = \mathbf{H}\begin{bmatrix} \mathbf{x} \\ \mathbf{u}
44 * \end{bmatrix} [x,u] + \mathbf{h} \end{aligned} \f]
45 */
46 template <typename _Scalar>
47 class DifferentialActionModelLQRTpl
48 : public DifferentialActionModelAbstractTpl<_Scalar> {
49 public:
50 typedef _Scalar Scalar;
51 typedef MathBaseTpl<Scalar> MathBase;
52 typedef DifferentialActionModelAbstractTpl<Scalar> Base;
53 typedef DifferentialActionDataLQRTpl<Scalar> Data;
54 typedef StateVectorTpl<Scalar> StateVector;
55 typedef DifferentialActionDataAbstractTpl<Scalar>
56 DifferentialActionDataAbstract;
57 typedef typename MathBase::VectorXs VectorXs;
58 typedef typename MathBase::MatrixXs MatrixXs;
59
60 /**
61 * @brief Initialize the LQR action model
62 *
63 * @param[in] Aq Position matrix
64 * @param[in] Av Velocity matrix
65 * @param[in] B Input matrix
66 * @param[in] Q State weight matrix
67 * @param[in] R Input weight matrix
68 * @param[in] N State-input weight matrix
69 */
70 DifferentialActionModelLQRTpl(const MatrixXs& Aq, const MatrixXs& Av,
71 const MatrixXs& B, const MatrixXs& Q,
72 const MatrixXs& R, const MatrixXs& N);
73
74 /**
75 * @brief Initialize the LQR action model
76 *
77 * @param[in] Aq Position matrix
78 * @param[in] Av Velocity matrix
79 * @param[in] B Input matrix
80 * @param[in] Q State weight matrix
81 * @param[in] R Input weight matrix
82 * @param[in] N State-input weight matrix
83 * @param[in] f Dynamics drift
84 * @param[in] q State weight vector
85 * @param[in] r Input weight vector
86 */
87 DifferentialActionModelLQRTpl(const MatrixXs& Aq, const MatrixXs& Av,
88 const MatrixXs& B, const MatrixXs& Q,
89 const MatrixXs& R, const MatrixXs& N,
90 const VectorXs& f, const VectorXs& q,
91 const VectorXs& r);
92
93 /**
94 * @brief Initialize the LQR action model
95 *
96 * @param[in] Aq Position matrix
97 * @param[in] Av Velocity matrix
98 * @param[in] B Input matrix
99 * @param[in] Q State weight matrix
100 * @param[in] R Input weight matrix
101 * @param[in] N State-input weight matrix
102 * @param[in] G State-input inequality constraint matrix
103 * @param[in] H State-input equality constraint matrix
104 * @param[in] f Dynamics drift
105 * @param[in] q State weight vector
106 * @param[in] r Input weight vector
107 * @param[in] g State-input inequality constraint bias
108 * @param[in] h State-input equality constraint bias
109 */
110 DifferentialActionModelLQRTpl(const MatrixXs& Aq, const MatrixXs& Av,
111 const MatrixXs& B, const MatrixXs& Q,
112 const MatrixXs& R, const MatrixXs& N,
113 const MatrixXs& G, const MatrixXs& H,
114 const VectorXs& f, const VectorXs& q,
115 const VectorXs& r, const VectorXs& g,
116 const VectorXs& h);
117
118 /**
119 * @brief Initialize the LQR action model
120 *
121 * @param[in] nq Dimension of position vector
122 * @param[in] nu Dimension of control vector
123 * @param[in] drif_free Enable / disable the bias term of the linear dynamics
124 * (default true)
125 */
126 DifferentialActionModelLQRTpl(const std::size_t nq, const std::size_t nu,
127 const bool drift_free = true);
128
129 /** @brief Copy constructor */
130 DifferentialActionModelLQRTpl(const DifferentialActionModelLQRTpl& copy);
131
132 virtual ~DifferentialActionModelLQRTpl();
133
134 virtual void calc(const std::shared_ptr<DifferentialActionDataAbstract>& data,
135 const Eigen::Ref<const VectorXs>& x,
136 const Eigen::Ref<const VectorXs>& u);
137 virtual void calc(const std::shared_ptr<DifferentialActionDataAbstract>& data,
138 const Eigen::Ref<const VectorXs>& x);
139 virtual void calcDiff(
140 const std::shared_ptr<DifferentialActionDataAbstract>& data,
141 const Eigen::Ref<const VectorXs>& x, const Eigen::Ref<const VectorXs>& u);
142 virtual void calcDiff(
143 const std::shared_ptr<DifferentialActionDataAbstract>& data,
144 const Eigen::Ref<const VectorXs>& x);
145 virtual std::shared_ptr<DifferentialActionDataAbstract> createData();
146 virtual bool checkData(
147 const std::shared_ptr<DifferentialActionDataAbstract>& data);
148
149 /**
150 * @brief Create a random LQR model
151 *
152 * @param[in] nq Position dimension
153 * @param[in] nu Control dimension
154 * @param[in] ng Inequality constraint dimension (default 0)
155 * @param[in] nh Equality constraint dimension (default 0)
156 */
157 static DifferentialActionModelLQRTpl Random(const std::size_t nq,
158 const std::size_t nu,
159 const std::size_t ng = 0,
160 const std::size_t nh = 0);
161
162 /** @brief Return the position matrix */
163 const MatrixXs& get_Aq() const;
164
165 /** @brief Return the velocity matrix */
166 const MatrixXs& get_Av() const;
167
168 /** @brief Return the input matrix */
169 const MatrixXs& get_B() const;
170
171 /** @brief Return the dynamics drift */
172 const VectorXs& get_f() const;
173
174 /** @brief Return the state weight matrix */
175 const MatrixXs& get_Q() const;
176
177 /** @brief Return the input weight matrix */
178 const MatrixXs& get_R() const;
179
180 /** @brief Return the state-input weight matrix */
181 const MatrixXs& get_N() const;
182
183 /** @brief Return the state-input inequality constraint matrix */
184 const MatrixXs& get_G() const;
185
186 /** @brief Return the state-input equality constraint matrix */
187 const MatrixXs& get_H() const;
188
189 /** @brief Return the state weight vector */
190 const VectorXs& get_q() const;
191
192 /** @brief Return the input weight vector */
193 const VectorXs& get_r() const;
194
195 /** @brief Return the state-input inequality constraint bias */
196 const VectorXs& get_g() const;
197
198 /** @brief Return the state-input equality constraint bias */
199 const VectorXs& get_h() const;
200
201 /**
202 * @brief Modify the LQR action model
203 *
204 * @param[in] Aq Position matrix
205 * @param[in] Av Velocity matrix
206 * @param[in] B Input matrix
207 * @param[in] Q State weight matrix
208 * @param[in] R Input weight matrix
209 * @param[in] N State-input weight matrix
210 * @param[in] G State-input inequality constraint matrix
211 * @param[in] H State-input equality constraint matrix
212 * @param[in] f Dynamics drift
213 * @param[in] q State weight vector
214 * @param[in] r Input weight vector
215 * @param[in] g State-input inequality constraint bias
216 * @param[in] h State-input equality constraint bias
217 */
218 void set_LQR(const MatrixXs& Aq, const MatrixXs& Av, const MatrixXs& B,
219 const MatrixXs& Q, const MatrixXs& R, const MatrixXs& N,
220 const MatrixXs& G, const MatrixXs& H, const VectorXs& f,
221 const VectorXs& q, const VectorXs& r, const VectorXs& g,
222 const VectorXs& h);
223
224 DEPRECATED("Use get_Aq", const MatrixXs& get_Fq() const { return get_Aq(); })
225 DEPRECATED("Use get_Av", const MatrixXs& get_Fv() const { return get_Av(); })
226 DEPRECATED("Use get_B", const MatrixXs& get_Fu() const { return get_B(); })
227 DEPRECATED("Use get_f", const VectorXs& get_f0() const { return get_f(); })
228 DEPRECATED("Use get_q", const VectorXs& get_lx() const { return get_q(); })
229 DEPRECATED("Use get_r", const VectorXs& get_lu() const { return get_r(); })
230 DEPRECATED("Use get_Q", const MatrixXs& get_Lxx() const { return get_Q(); })
231 DEPRECATED("Use get_N", const MatrixXs& get_Lxu() const { return get_N(); })
232 DEPRECATED("Use get_R", const MatrixXs& get_Luu() const { return get_R(); })
233 DEPRECATED(
234 "Use set_LQR", void set_Fq(const MatrixXs& Aq) {
235 set_LQR(Aq, Av_, B_, Q_, R_, N_, G_, H_, f_, q_, r_, g_, h_);
236 })
237 DEPRECATED(
238 "Use set_LQR", void set_Fv(const MatrixXs& Av) {
239 set_LQR(Aq_, Av, B_, Q_, R_, N_, G_, H_, f_, q_, r_, g_, h_);
240 })
241 DEPRECATED(
242 "Use set_LQR", void set_Fu(const MatrixXs& B) {
243 set_LQR(Aq_, Av_, B, Q_, R_, N_, G_, H_, f_, q_, r_, g_, h_);
244 })
245 DEPRECATED(
246 "Use set_LQR", void set_f0(const VectorXs& f) {
247 set_LQR(Aq_, Av_, B_, Q_, R_, N_, G_, H_, f, q_, r_, g_, h_);
248 })
249 DEPRECATED(
250 "Use set_LQR", void set_lx(const VectorXs& q) {
251 set_LQR(Aq_, Av_, B_, Q_, R_, N_, G_, H_, f_, q, r_, g_, h_);
252 })
253 DEPRECATED(
254 "Use set_LQR", void set_lu(const VectorXs& r) {
255 set_LQR(Aq_, Av_, B_, Q_, R_, N_, G_, H_, f_, q_, r, g_, h_);
256 })
257 DEPRECATED(
258 "Use set_LQR", void set_Lxx(const MatrixXs& Q) {
259 set_LQR(Aq_, Av_, B_, Q, R_, N_, G_, H_, f_, q_, r_, g_, h_);
260 })
261 DEPRECATED(
262 "Use set_LQR", void set_Lxu(const MatrixXs& N) {
263 set_LQR(Aq_, Av_, B_, Q_, R_, N, G_, H_, f_, q_, r_, g_, h_);
264 })
265 DEPRECATED(
266 "Use set_LQR", void set_Luu(const MatrixXs& R) {
267 set_LQR(Aq_, Av_, B_, Q_, R, N_, G_, H_, f_, q_, r_, g_, h_);
268 })
269
270 /**
271 * @brief Print relevant information of the LQR model
272 *
273 * @param[out] os Output stream object
274 */
275 virtual void print(std::ostream& os) const;
276
277 protected:
278 using Base::ng_; //!< Equality constraint dimension
279 using Base::nh_; //!< Inequality constraint dimension
280 using Base::nu_; //!< Control dimension
281 using Base::state_; //!< Model of the state
282
283 private:
284 MatrixXs Aq_;
285 MatrixXs Av_;
286 MatrixXs B_;
287 MatrixXs Q_;
288 MatrixXs R_;
289 MatrixXs N_;
290 MatrixXs G_;
291 MatrixXs H_;
292 VectorXs f_;
293 VectorXs q_;
294 VectorXs r_;
295 VectorXs g_;
296 VectorXs h_;
297 MatrixXs L_;
298 bool drift_free_;
299 bool updated_lqr_;
300 };
301
302 template <typename _Scalar>
303 struct DifferentialActionDataLQRTpl
304 : public DifferentialActionDataAbstractTpl<_Scalar> {
305 typedef _Scalar Scalar;
306 typedef MathBaseTpl<Scalar> MathBase;
307 typedef DifferentialActionDataAbstractTpl<Scalar> Base;
308 typedef typename MathBase::VectorXs VectorXs;
309 typedef typename MathBase::MatrixXs MatrixXs;
310
311 template <template <typename Scalar> class Model>
312 20322 explicit DifferentialActionDataLQRTpl(Model<Scalar>* const model)
313 20322 : Base(model) {
314 // Setting the linear model and quadratic cost as they are constant
315 20322 const std::size_t nq = model->get_state()->get_nq();
316 20322 const std::size_t nu = model->get_nu();
317
2/4
✓ Branch 2 taken 20322 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 20322 times.
✗ Branch 6 not taken.
20322 Fx.leftCols(nq) = model->get_Aq();
318
2/4
✓ Branch 2 taken 20322 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 20322 times.
✗ Branch 6 not taken.
20322 Fx.rightCols(nq) = model->get_Av();
319
1/2
✓ Branch 2 taken 20322 times.
✗ Branch 3 not taken.
20322 Fu = model->get_B();
320
1/2
✓ Branch 2 taken 20322 times.
✗ Branch 3 not taken.
20322 Lxx = model->get_Q();
321
1/2
✓ Branch 2 taken 20322 times.
✗ Branch 3 not taken.
20322 Luu = model->get_R();
322
1/2
✓ Branch 2 taken 20322 times.
✗ Branch 3 not taken.
20322 Lxu = model->get_N();
323
2/4
✓ Branch 2 taken 20322 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 20322 times.
✗ Branch 6 not taken.
20322 Gx = model->get_G().leftCols(2 * nq);
324
2/4
✓ Branch 2 taken 20322 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 20322 times.
✗ Branch 6 not taken.
20322 Gu = model->get_G().rightCols(nu);
325
2/4
✓ Branch 2 taken 20322 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 20322 times.
✗ Branch 6 not taken.
20322 Hx = model->get_H().leftCols(2 * nq);
326
2/4
✓ Branch 2 taken 20322 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 20322 times.
✗ Branch 6 not taken.
20322 Hu = model->get_H().rightCols(nu);
327 20322 }
328
329 using Base::cost;
330 using Base::Fu;
331 using Base::Fx;
332 using Base::Gu;
333 using Base::Gx;
334 using Base::Hu;
335 using Base::Hx;
336 using Base::Lu;
337 using Base::Luu;
338 using Base::Lx;
339 using Base::Lxu;
340 using Base::Lxx;
341 using Base::r;
342 using Base::xout;
343 };
344
345 } // namespace crocoddyl
346
347 /* --- Details -------------------------------------------------------------- */
348 /* --- Details -------------------------------------------------------------- */
349 /* --- Details -------------------------------------------------------------- */
350 #include "crocoddyl/core/actions/diff-lqr.hxx"
351 #endif // CROCODDYL_CORE_ACTIONS_DIFF_LQR_HPP_
352