Crocoddyl
action-base.hpp
1 // BSD 3-Clause License
3 //
4 // Copyright (C) 2019-2023, LAAS-CNRS, University of Edinburgh,
5 // University of Oxford, Heriot-Watt University
6 // Copyright note valid unless otherwise stated in individual files.
7 // All rights reserved.
9 
10 #ifndef CROCODDYL_CORE_ACTION_BASE_HPP_
11 #define CROCODDYL_CORE_ACTION_BASE_HPP_
12 
13 #include <boost/make_shared.hpp>
14 #include <boost/shared_ptr.hpp>
15 #include <stdexcept>
16 
17 #include "crocoddyl/core/fwd.hpp"
18 #include "crocoddyl/core/state-base.hpp"
19 #include "crocoddyl/core/utils/math.hpp"
20 
21 namespace crocoddyl {
22 
94 template <typename _Scalar>
96  public:
97  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
98 
99  typedef _Scalar Scalar;
103  typedef typename MathBase::VectorXs VectorXs;
104 
114  ActionModelAbstractTpl(boost::shared_ptr<StateAbstract> state,
115  const std::size_t nu, const std::size_t nr = 0,
116  const std::size_t ng = 0, const std::size_t nh = 0);
117  virtual ~ActionModelAbstractTpl();
118 
126  virtual void calc(const boost::shared_ptr<ActionDataAbstract>& data,
127  const Eigen::Ref<const VectorXs>& x,
128  const Eigen::Ref<const VectorXs>& u) = 0;
129 
141  virtual void calc(const boost::shared_ptr<ActionDataAbstract>& data,
142  const Eigen::Ref<const VectorXs>& x);
143 
156  virtual void calcDiff(const boost::shared_ptr<ActionDataAbstract>& data,
157  const Eigen::Ref<const VectorXs>& x,
158  const Eigen::Ref<const VectorXs>& u) = 0;
159 
171  virtual void calcDiff(const boost::shared_ptr<ActionDataAbstract>& data,
172  const Eigen::Ref<const VectorXs>& x);
173 
179  virtual boost::shared_ptr<ActionDataAbstract> createData();
180 
184  virtual bool checkData(const boost::shared_ptr<ActionDataAbstract>& data);
185 
199  virtual void quasiStatic(const boost::shared_ptr<ActionDataAbstract>& data,
200  Eigen::Ref<VectorXs> u,
201  const Eigen::Ref<const VectorXs>& x,
202  const std::size_t maxiter = 100,
203  const Scalar tol = Scalar(1e-9));
204 
216  VectorXs quasiStatic_x(const boost::shared_ptr<ActionDataAbstract>& data,
217  const VectorXs& x, const std::size_t maxiter = 100,
218  const Scalar tol = Scalar(1e-9));
219 
223  std::size_t get_nu() const;
224 
228  std::size_t get_nr() const;
229 
233  virtual std::size_t get_ng() const;
234 
238  virtual std::size_t get_nh() const;
239 
243  const boost::shared_ptr<StateAbstract>& get_state() const;
244 
248  virtual const VectorXs& get_g_lb() const;
249 
253  virtual const VectorXs& get_g_ub() const;
254 
258  const VectorXs& get_u_lb() const;
259 
263  const VectorXs& get_u_ub() const;
264 
269 
273  void set_g_lb(const VectorXs& g_lb);
274 
278  void set_g_ub(const VectorXs& g_ub);
279 
283  void set_u_lb(const VectorXs& u_lb);
284 
288  void set_u_ub(const VectorXs& u_ub);
289 
293  template <class Scalar>
294  friend std::ostream& operator<<(std::ostream& os,
295  const ActionModelAbstractTpl<Scalar>& model);
296 
302  virtual void print(std::ostream& os) const;
303 
304  protected:
305  std::size_t nu_;
306  std::size_t nr_;
307  std::size_t ng_;
308  std::size_t nh_;
309  boost::shared_ptr<StateAbstract> state_;
310  VectorXs unone_;
311  VectorXs g_lb_;
312  VectorXs g_ub_;
313  VectorXs u_lb_;
314  VectorXs u_ub_;
317 
323 
324  template <class Scalar>
325  friend class ConstraintModelManagerTpl;
326 };
327 
328 template <typename _Scalar>
330  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
331 
332  typedef _Scalar Scalar;
334  typedef typename MathBase::VectorXs VectorXs;
335  typedef typename MathBase::MatrixXs MatrixXs;
336 
337  template <template <typename Scalar> class Model>
338  explicit ActionDataAbstractTpl(Model<Scalar>* const model)
339  : cost(Scalar(0.)),
340  xnext(model->get_state()->get_nx()),
341  Fx(model->get_state()->get_ndx(), model->get_state()->get_ndx()),
342  Fu(model->get_state()->get_ndx(), model->get_nu()),
343  r(model->get_nr()),
344  Lx(model->get_state()->get_ndx()),
345  Lu(model->get_nu()),
346  Lxx(model->get_state()->get_ndx(), model->get_state()->get_ndx()),
347  Lxu(model->get_state()->get_ndx(), model->get_nu()),
348  Luu(model->get_nu(), model->get_nu()),
349  g(model->get_ng()),
350  Gx(model->get_ng(), model->get_state()->get_ndx()),
351  Gu(model->get_ng(), model->get_nu()),
352  h(model->get_nh()),
353  Hx(model->get_nh(), model->get_state()->get_ndx()),
354  Hu(model->get_nh(), model->get_nu()) {
355  xnext.setZero();
356  Fx.setZero();
357  Fu.setZero();
358  r.setZero();
359  Lx.setZero();
360  Lu.setZero();
361  Lxx.setZero();
362  Lxu.setZero();
363  Luu.setZero();
364  g.setZero();
365  Gx.setZero();
366  Gu.setZero();
367  h.setZero();
368  Hx.setZero();
369  Hu.setZero();
370  }
371  virtual ~ActionDataAbstractTpl() {}
372 
373  Scalar cost;
374  VectorXs xnext;
375  MatrixXs Fx;
376  MatrixXs
377  Fu;
378  VectorXs r;
379  VectorXs Lx;
380  VectorXs Lu;
381  MatrixXs Lxx;
382  MatrixXs Lxu;
384  MatrixXs Luu;
385  VectorXs g;
386  MatrixXs Gx;
388  MatrixXs Gu;
390  VectorXs h;
391  MatrixXs Hx;
393  MatrixXs Hu;
395 };
396 
397 } // namespace crocoddyl
398 
399 /* --- Details -------------------------------------------------------------- */
400 /* --- Details -------------------------------------------------------------- */
401 /* --- Details -------------------------------------------------------------- */
402 #include "crocoddyl/core/action-base.hxx"
403 
404 #endif // CROCODDYL_CORE_ACTION_BASE_HPP_
Abstract class for action model.
Definition: action-base.hpp:95
virtual std::size_t get_ng() const
Return the number of inequality constraints.
void set_g_lb(const VectorXs &g_lb)
Modify the lower bound of the inequality constraints.
const boost::shared_ptr< StateAbstract > & get_state() const
Return the state.
VectorXs u_lb_
Lower control limits.
VectorXs u_ub_
Upper control limits.
virtual std::size_t get_nh() const
Return the number of equality constraints.
virtual void print(std::ostream &os) const
Print relevant information of the action model.
void set_u_ub(const VectorXs &u_ub)
Modify the control upper bounds.
const VectorXs & get_u_ub() const
Return the control upper bound.
VectorXs g_ub_
Lower bound of the inequality constraints.
bool get_has_control_limits() const
Indicates if there are defined control limits.
VectorXs quasiStatic_x(const boost::shared_ptr< ActionDataAbstract > &data, const VectorXs &x, const std::size_t maxiter=100, const Scalar tol=Scalar(1e-9))
virtual const VectorXs & get_g_lb() const
Return the lower bound of the inequality constraints.
void update_has_control_limits()
Update the status of the control limits (i.e. if there are defined limits)
boost::shared_ptr< StateAbstract > state_
Model of the state.
std::size_t nu_
Control dimension.
VectorXs unone_
Neutral state.
ActionModelAbstractTpl(boost::shared_ptr< StateAbstract > state, const std::size_t nu, const std::size_t nr=0, const std::size_t ng=0, const std::size_t nh=0)
Initialize the action model.
std::size_t nr_
Dimension of the cost residual.
std::size_t nh_
Number of equality constraints.
virtual bool checkData(const boost::shared_ptr< ActionDataAbstract > &data)
Checks that a specific data belongs to this model.
virtual void calcDiff(const boost::shared_ptr< ActionDataAbstract > &data, const Eigen::Ref< const VectorXs > &x, const Eigen::Ref< const VectorXs > &u)=0
Compute the derivatives of the dynamics and cost functions.
std::size_t get_nr() const
Return the dimension of the cost-residual vector.
void set_g_ub(const VectorXs &g_ub)
Modify the upper bound of the inequality constraints.
virtual void calc(const boost::shared_ptr< ActionDataAbstract > &data, const Eigen::Ref< const VectorXs > &x, const Eigen::Ref< const VectorXs > &u)=0
Compute the next state and cost value.
void set_u_lb(const VectorXs &u_lb)
Modify the control lower bounds.
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.
friend std::ostream & operator<<(std::ostream &os, const ActionModelAbstractTpl< Scalar > &model)
Print information on the action model.
VectorXs g_lb_
Lower bound of the inequality constraints.
virtual const VectorXs & get_g_ub() const
Return the upper bound of the inequality constraints.
const VectorXs & get_u_lb() const
Return the control lower bound.
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.
std::size_t ng_
Number of inequality constraints.
virtual boost::shared_ptr< ActionDataAbstract > createData()
Create the action data.
std::size_t get_nu() const
Return the dimension of the control input.
virtual void quasiStatic(const boost::shared_ptr< ActionDataAbstract > &data, Eigen::Ref< VectorXs > u, const Eigen::Ref< const VectorXs > &x, const std::size_t maxiter=100, const Scalar tol=Scalar(1e-9))
Computes the quasic static commands.
Manage the individual constraint models.
Abstract class for the state representation.
Definition: state-base.hpp:46
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 .
VectorXs h
Equality constraint values.
MatrixXs Luu
Hessian of the cost w.r.t. the control .
VectorXs g
Inequality constraint values.
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.