Crocoddyl
diff-lqr.hpp
1 // 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.
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 
46 template <typename _Scalar>
48  : public DifferentialActionModelAbstractTpl<_Scalar> {
49  public:
50  typedef _Scalar Scalar;
57  typedef typename MathBase::VectorXs VectorXs;
58  typedef typename MathBase::MatrixXs MatrixXs;
59 
70  DifferentialActionModelLQRTpl(const MatrixXs& Aq, const MatrixXs& Av,
71  const MatrixXs& B, const MatrixXs& Q,
72  const MatrixXs& R, const MatrixXs& N);
73 
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 
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 
126  DifferentialActionModelLQRTpl(const std::size_t nq, const std::size_t nu,
127  const bool drift_free = true);
128 
131 
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 
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 
163  const MatrixXs& get_Aq() const;
164 
166  const MatrixXs& get_Av() const;
167 
169  const MatrixXs& get_B() const;
170 
172  const VectorXs& get_f() const;
173 
175  const MatrixXs& get_Q() const;
176 
178  const MatrixXs& get_R() const;
179 
181  const MatrixXs& get_N() const;
182 
184  const MatrixXs& get_G() const;
185 
187  const MatrixXs& get_H() const;
188 
190  const VectorXs& get_q() const;
191 
193  const VectorXs& get_r() const;
194 
196  const VectorXs& get_g() const;
197 
199  const VectorXs& get_h() const;
200 
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 
275  virtual void print(std::ostream& os) const;
276 
277  protected:
278  using Base::ng_;
279  using Base::nh_;
280  using Base::nu_;
281  using Base::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>
304  : public DifferentialActionDataAbstractTpl<_Scalar> {
305  typedef _Scalar Scalar;
308  typedef typename MathBase::VectorXs VectorXs;
309  typedef typename MathBase::MatrixXs MatrixXs;
310 
311  template <template <typename Scalar> class Model>
312  explicit DifferentialActionDataLQRTpl(Model<Scalar>* const model)
313  : Base(model) {
314  // Setting the linear model and quadratic cost as they are constant
315  const std::size_t nq = model->get_state()->get_nq();
316  const std::size_t nu = model->get_nu();
317  Fx.leftCols(nq) = model->get_Aq();
318  Fx.rightCols(nq) = model->get_Av();
319  Fu = model->get_B();
320  Lxx = model->get_Q();
321  Luu = model->get_R();
322  Lxu = model->get_N();
323  Gx = model->get_G().leftCols(2 * nq);
324  Gu = model->get_G().rightCols(nu);
325  Hx = model->get_H().leftCols(2 * nq);
326  Hu = model->get_H().rightCols(nu);
327  }
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_
Abstract class for differential action model.
std::shared_ptr< StateAbstract > state_
Model of the state.
std::size_t nh_
Number of equality constraints.
std::size_t ng_
Number of inequality constraints.
Linear-quadratic regulator (LQR) differential action model.
Definition: diff-lqr.hpp:48
const MatrixXs & get_R() const
Return the input weight matrix.
const VectorXs & get_q() const
Return the state weight vector.
void set_LQR(const MatrixXs &Aq, const MatrixXs &Av, const MatrixXs &B, const MatrixXs &Q, const MatrixXs &R, const MatrixXs &N, const MatrixXs &G, const MatrixXs &H, const VectorXs &f, const VectorXs &q, const VectorXs &r, const VectorXs &g, const VectorXs &h)
Modify the LQR action model.
static DifferentialActionModelLQRTpl Random(const std::size_t nq, const std::size_t nu, const std::size_t ng=0, const std::size_t nh=0)
Create a random LQR model.
const VectorXs & get_f() const
Return the dynamics drift.
virtual std::shared_ptr< DifferentialActionDataAbstract > createData()
Create the differential action data.
virtual void print(std::ostream &os) const
Print relevant information of the LQR model.
DifferentialActionModelLQRTpl(const MatrixXs &Aq, const MatrixXs &Av, const MatrixXs &B, const MatrixXs &Q, const MatrixXs &R, const MatrixXs &N)
Initialize the LQR action model.
const VectorXs & get_r() const
Return the input weight vector.
DifferentialActionModelLQRTpl(const DifferentialActionModelLQRTpl &copy)
Copy constructor.
virtual void calcDiff(const std::shared_ptr< DifferentialActionDataAbstract > &data, const Eigen::Ref< const VectorXs > &x, const Eigen::Ref< const VectorXs > &u)
Compute the derivatives of the dynamics and cost functions.
virtual void calc(const std::shared_ptr< DifferentialActionDataAbstract > &data, const Eigen::Ref< const VectorXs > &x, const Eigen::Ref< const VectorXs > &u)
Compute the system acceleration and cost value.
const VectorXs & get_h() const
Return the state-input equality constraint bias.
const MatrixXs & get_Av() const
Return the velocity matrix.
virtual bool checkData(const std::shared_ptr< DifferentialActionDataAbstract > &data)
Checks that a specific data belongs to this model.
DifferentialActionModelLQRTpl(const MatrixXs &Aq, const MatrixXs &Av, const MatrixXs &B, const MatrixXs &Q, const MatrixXs &R, const MatrixXs &N, const MatrixXs &G, const MatrixXs &H, const VectorXs &f, const VectorXs &q, const VectorXs &r, const VectorXs &g, const VectorXs &h)
Initialize the LQR action model.
const VectorXs & get_g() const
Return the state-input inequality constraint bias.
DifferentialActionModelLQRTpl(const std::size_t nq, const std::size_t nu, const bool drift_free=true)
Initialize the LQR action model.
const MatrixXs & get_N() const
Return the state-input weight matrix.
virtual void calcDiff(const std::shared_ptr< DifferentialActionDataAbstract > &data, const Eigen::Ref< const VectorXs > &x)
Compute the derivatives of the cost functions with respect to the state only.
const MatrixXs & get_G() const
Return the state-input inequality constraint matrix.
const MatrixXs & get_B() const
Return the input matrix.
const MatrixXs & get_Aq() const
Return the position matrix.
virtual void calc(const std::shared_ptr< DifferentialActionDataAbstract > &data, const Eigen::Ref< const VectorXs > &x)
Compute the total cost value for nodes that depends only on the state.
DifferentialActionModelLQRTpl(const MatrixXs &Aq, const MatrixXs &Av, const MatrixXs &B, const MatrixXs &Q, const MatrixXs &R, const MatrixXs &N, const VectorXs &f, const VectorXs &q, const VectorXs &r)
Initialize the LQR action model.
const MatrixXs & get_Q() const
Return the state weight matrix.
const MatrixXs & get_H() const
Return the state-input equality constraint matrix.
MatrixXs Fx
Jacobian of the dynamics w.r.t. the state .
MatrixXs Fu
Jacobian of the dynamics w.r.t. the control .
MatrixXs Luu
Hessian of the cost w.r.t. the control .
VectorXs Lx
Jacobian of the cost w.r.t. the state .
MatrixXs Lxx
Hessian of the cost w.r.t. the state .
VectorXs Lu
Jacobian of the cost w.r.t. the control .
MatrixXs Fx
Jacobian of the dynamics w.r.t. the state .
MatrixXs Fu
Jacobian of the dynamics w.r.t. the control .
MatrixXs Luu
Hessian of the cost w.r.t. the control .
MatrixXs Lxx
Hessian of the cost w.r.t. the state .