Crocoddyl
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_LQR_HPP_
11 #define CROCODDYL_CORE_ACTIONS_LQR_HPP_
12 
13 #include <stdexcept>
14 
15 #include "crocoddyl/core/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>
47 class ActionModelLQRTpl : public ActionModelAbstractTpl<_Scalar> {
48  public:
49  typedef _Scalar Scalar;
55  typedef typename MathBase::VectorXs VectorXs;
56  typedef typename MathBase::MatrixXs MatrixXs;
57 
67  ActionModelLQRTpl(const MatrixXs& A, const MatrixXs& B, const MatrixXs& Q,
68  const MatrixXs& R, const MatrixXs& N);
69 
82  ActionModelLQRTpl(const MatrixXs& A, const MatrixXs& B, const MatrixXs& Q,
83  const MatrixXs& R, const MatrixXs& N, const VectorXs& f,
84  const VectorXs& q, const VectorXs& r);
85 
102  ActionModelLQRTpl(const MatrixXs& A, const MatrixXs& B, const MatrixXs& Q,
103  const MatrixXs& R, const MatrixXs& N, const MatrixXs& G,
104  const MatrixXs& H, const VectorXs& f, const VectorXs& q,
105  const VectorXs& r, const VectorXs& g, const VectorXs& h);
106 
115  ActionModelLQRTpl(const std::size_t nx, const std::size_t nu,
116  const bool drift_free = true);
117 
120 
121  virtual ~ActionModelLQRTpl();
122 
123  virtual void calc(const boost::shared_ptr<ActionDataAbstract>& data,
124  const Eigen::Ref<const VectorXs>& x,
125  const Eigen::Ref<const VectorXs>& u);
126  virtual void calc(const boost::shared_ptr<ActionDataAbstract>& data,
127  const Eigen::Ref<const VectorXs>& x);
128  virtual void calcDiff(const boost::shared_ptr<ActionDataAbstract>& data,
129  const Eigen::Ref<const VectorXs>& x,
130  const Eigen::Ref<const VectorXs>& u);
131  virtual void calcDiff(const boost::shared_ptr<ActionDataAbstract>& data,
132  const Eigen::Ref<const VectorXs>& x);
133  virtual boost::shared_ptr<ActionDataAbstract> createData();
134  virtual bool checkData(const boost::shared_ptr<ActionDataAbstract>& data);
135 
144  static ActionModelLQRTpl Random(const std::size_t nx, const std::size_t nu,
145  const std::size_t ng = 0,
146  const std::size_t nh = 0);
147 
149  const MatrixXs& get_A() const;
150 
152  const MatrixXs& get_B() const;
153 
155  const VectorXs& get_f() const;
156 
158  const MatrixXs& get_Q() const;
159 
161  const MatrixXs& get_R() const;
162 
164  const MatrixXs& get_N() const;
165 
167  const MatrixXs& get_G() const;
168 
170  const MatrixXs& get_H() const;
171 
173  const VectorXs& get_q() const;
174 
176  const VectorXs& get_r() const;
177 
179  const VectorXs& get_g() const;
180 
182  const VectorXs& get_h() const;
183 
200  void set_LQR(const MatrixXs& A, const MatrixXs& B, const MatrixXs& Q,
201  const MatrixXs& R, const MatrixXs& N, const MatrixXs& G,
202  const MatrixXs& H, const VectorXs& f, const VectorXs& q,
203  const VectorXs& r, const VectorXs& g, const VectorXs& h);
204 
205  DEPRECATED("Use get_A", const MatrixXs& get_Fx() const { return get_A(); })
206  DEPRECATED("Use get_B", const MatrixXs& get_Fu() const { return get_B(); })
207  DEPRECATED("Use get_f", const VectorXs& get_f0() const { return get_f(); })
208  DEPRECATED("Use get_q", const VectorXs& get_lx() const { return get_q(); })
209  DEPRECATED("Use get_r", const VectorXs& get_lu() const { return get_r(); })
210  DEPRECATED("Use get_Q", const MatrixXs& get_Lxx() const { return get_Q(); })
211  DEPRECATED("Use get_R", const MatrixXs& get_Lxu() const { return get_R(); })
212  DEPRECATED("Use get_N", const MatrixXs& get_Luu() const { return get_N(); })
213  DEPRECATED(
214  "Use set_LQR", void set_Fx(const MatrixXs& A) {
215  set_LQR(A, B_, Q_, R_, N_, G_, H_, f_, q_, r_, g_, h_);
216  })
217  DEPRECATED(
218  "Use set_LQR", void set_Fu(const MatrixXs& B) {
219  set_LQR(A_, B, Q_, R_, N_, G_, H_, f_, q_, r_, g_, h_);
220  })
221  DEPRECATED(
222  "Use set_LQR", void set_f0(const VectorXs& f) {
223  set_LQR(A_, B_, Q_, R_, N_, G_, H_, f, q_, r_, g_, h_);
224  })
225  DEPRECATED(
226  "Use set_LQR", void set_lx(const VectorXs& q) {
227  set_LQR(A_, B_, Q_, R_, N_, G_, H_, f_, q, r_, g_, h_);
228  })
229  DEPRECATED(
230  "Use set_LQR", void set_lu(const VectorXs& r) {
231  set_LQR(A_, B_, Q_, R_, N_, G_, H_, f_, q_, r, g_, h_);
232  })
233  DEPRECATED(
234  "Use set_LQR", void set_Lxx(const MatrixXs& Q) {
235  set_LQR(A_, B_, Q, R_, N_, G_, H_, f_, q_, r_, g_, h_);
236  })
237  DEPRECATED(
238  "Use set_LQR", void set_Luu(const MatrixXs& R) {
239  set_LQR(A_, B_, Q_, R, N_, G_, H_, f_, q_, r_, g_, h_);
240  })
241  DEPRECATED(
242  "Use set_LQR", void set_Lxu(const MatrixXs& N) {
243  set_LQR(A_, B_, Q_, R_, N, G_, H_, f_, q_, r_, g_, h_);
244  })
245 
251  virtual void print(std::ostream& os) const;
252 
253  protected:
254  using Base::ng_;
255  using Base::nh_;
256  using Base::nu_;
257  using Base::state_;
258 
259  private:
260  MatrixXs A_;
261  MatrixXs B_;
262  MatrixXs Q_;
263  MatrixXs R_;
264  MatrixXs N_;
265  MatrixXs G_;
266  MatrixXs H_;
267  VectorXs f_;
268  VectorXs q_;
269  VectorXs r_;
270  VectorXs g_;
271  VectorXs h_;
272  MatrixXs L_;
273  bool drift_free_;
274  bool updated_lqr_;
275 };
276 
277 template <typename _Scalar>
278 struct ActionDataLQRTpl : public ActionDataAbstractTpl<_Scalar> {
279  typedef _Scalar Scalar;
282  typedef typename MathBase::VectorXs VectorXs;
283 
284  template <template <typename Scalar> class Model>
285  explicit ActionDataLQRTpl(Model<Scalar>* const model)
286  : Base(model),
287  R_u_tmp(VectorXs::Zero(static_cast<Eigen::Index>(model->get_nu()))),
288  Q_x_tmp(VectorXs::Zero(
289  static_cast<Eigen::Index>(model->get_state()->get_ndx()))) {
290  // Setting the linear model and quadratic cost as they are constant
291  const std::size_t nq = model->get_state()->get_nq();
292  const std::size_t nu = model->get_nu();
293  Fx = model->get_A();
294  Fu = model->get_B();
295  Lxx = model->get_Q();
296  Luu = model->get_R();
297  Lxu = model->get_N();
298  Gx = model->get_G().leftCols(2 * nq);
299  Gu = model->get_G().rightCols(nu);
300  Hx = model->get_H().leftCols(2 * nq);
301  Hu = model->get_H().rightCols(nu);
302  }
303 
304  using Base::cost;
305  using Base::Fu;
306  using Base::Fx;
307  using Base::Gu;
308  using Base::Gx;
309  using Base::Hu;
310  using Base::Hx;
311  using Base::Lu;
312  using Base::Luu;
313  using Base::Lx;
314  using Base::Lxu;
315  using Base::Lxx;
316  using Base::r;
317  using Base::xnext;
318 
319  VectorXs R_u_tmp; // Temporary variable for storing Hessian-vector product
320  // (size: nu)
321  VectorXs Q_x_tmp; // Temporary variable for storing Hessian-vector product
322  // (size: nx)
323 };
324 
325 } // namespace crocoddyl
326 
327 /* --- Details -------------------------------------------------------------- */
328 /* --- Details -------------------------------------------------------------- */
329 /* --- Details -------------------------------------------------------------- */
330 #include "crocoddyl/core/actions/lqr.hxx"
331 
332 #endif // CROCODDYL_CORE_ACTIONS_LQR_HPP_
Abstract class for action model.
Definition: action-base.hpp:95
boost::shared_ptr< StateAbstract > state_
Model of the state.
std::size_t nu_
Control dimension.
std::size_t nh_
Number of equality constraints.
std::size_t ng_
Number of inequality constraints.
Linear-quadratic regulator (LQR) action model.
Definition: lqr.hpp:47
const MatrixXs & get_R() const
Return the input weight matrix.
const VectorXs & get_q() const
Return the state weight vector.
const MatrixXs & get_A() const
Return the state matrix.
const VectorXs & get_f() const
Return the dynamics drift.
void set_LQR(const MatrixXs &A, 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.
ActionModelLQRTpl(const ActionModelLQRTpl &copy)
Copy constructor.
virtual void print(std::ostream &os) const
Print relevant information of the LQR model.
const VectorXs & get_r() const
Return the input weight vector.
ActionModelLQRTpl(const MatrixXs &A, 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.
ActionModelLQRTpl(const MatrixXs &A, const MatrixXs &B, const MatrixXs &Q, const MatrixXs &R, const MatrixXs &N)
Initialize the LQR action model.
ActionModelLQRTpl(const MatrixXs &A, 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.
virtual void calc(const boost::shared_ptr< ActionDataAbstract > &data, const Eigen::Ref< const VectorXs > &x, const Eigen::Ref< const VectorXs > &u)
Compute the next state and cost value.
static ActionModelLQRTpl Random(const std::size_t nx, 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_h() const
Return the state-input equality constraint bias.
virtual void calcDiff(const boost::shared_ptr< ActionDataAbstract > &data, const Eigen::Ref< const VectorXs > &x, const Eigen::Ref< const VectorXs > &u)
Compute the derivatives of the dynamics and cost functions.
const VectorXs & get_g() const
Return the state-input inequality constraint bias.
virtual bool checkData(const boost::shared_ptr< ActionDataAbstract > &data)
Checks that a specific data belongs to this model.
const MatrixXs & get_N() const
Return the state-input weight matrix.
ActionModelLQRTpl(const std::size_t nx, const std::size_t nu, const bool drift_free=true)
Initialize the LQR action model.
const MatrixXs & get_G() const
Return the state-input inequality constraint matrix.
const MatrixXs & get_B() const
Return the input matrix.
virtual void calc(const boost::shared_ptr< ActionDataAbstract > &data, const Eigen::Ref< const VectorXs > &x)
Compute the total cost value for nodes that depends only on the state.
virtual void calcDiff(const boost::shared_ptr< ActionDataAbstract > &data, const Eigen::Ref< const VectorXs > &x)
Compute the derivatives of the cost functions with respect to the state only.
virtual boost::shared_ptr< ActionDataAbstract > createData()
Create the action data.
const MatrixXs & get_Q() const
Return the state weight matrix.
const MatrixXs & get_H() const
Return the state-input equality constraint matrix.
VectorXs xnext
evolution state
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 .
VectorXs r
Cost residual.
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 .