Crocoddyl
action-base.hpp
1 // BSD 3-Clause License
3 //
4 // Copyright (C) 2019-2025, 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 "crocoddyl/core/fwd.hpp"
14 #include "crocoddyl/core/state-base.hpp"
15 
16 namespace crocoddyl {
17 
19  public:
20  virtual ~ActionModelBase() = default;
21 
22  CROCODDYL_BASE_CAST(ActionModelBase, ActionModelAbstractTpl)
23 };
24 
96 template <typename _Scalar>
98  public:
99  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
100 
101  typedef _Scalar Scalar;
102  typedef typename ScalarSelector<Scalar>::type ScalarType;
106  typedef typename MathBase::VectorXs VectorXs;
107 
119  ActionModelAbstractTpl(std::shared_ptr<StateAbstract> state,
120  const std::size_t nu, const std::size_t nr = 0,
121  const std::size_t ng = 0, const std::size_t nh = 0,
122  const std::size_t ng_T = 0,
123  const std::size_t nh_T = 0);
129 
130  virtual ~ActionModelAbstractTpl() = default;
131 
139  virtual void calc(const std::shared_ptr<ActionDataAbstract>& data,
140  const Eigen::Ref<const VectorXs>& x,
141  const Eigen::Ref<const VectorXs>& u) = 0;
142 
154  virtual void calc(const std::shared_ptr<ActionDataAbstract>& data,
155  const Eigen::Ref<const VectorXs>& x);
156 
169  virtual void calcDiff(const std::shared_ptr<ActionDataAbstract>& data,
170  const Eigen::Ref<const VectorXs>& x,
171  const Eigen::Ref<const VectorXs>& u) = 0;
172 
184  virtual void calcDiff(const std::shared_ptr<ActionDataAbstract>& data,
185  const Eigen::Ref<const VectorXs>& x);
186 
192  virtual std::shared_ptr<ActionDataAbstract> createData();
193 
197  virtual bool checkData(const std::shared_ptr<ActionDataAbstract>& data);
198 
212  virtual void quasiStatic(const std::shared_ptr<ActionDataAbstract>& data,
213  Eigen::Ref<VectorXs> u,
214  const Eigen::Ref<const VectorXs>& x,
215  const std::size_t maxiter = 100,
216  const Scalar tol = Scalar(1e-9));
217 
229  VectorXs quasiStatic_x(const std::shared_ptr<ActionDataAbstract>& data,
230  const VectorXs& x, const std::size_t maxiter = 100,
231  const Scalar tol = Scalar(1e-9));
232 
236  std::size_t get_nu() const;
237 
241  std::size_t get_nr() const;
242 
246  virtual std::size_t get_ng() const;
247 
251  virtual std::size_t get_nh() const;
252 
256  virtual std::size_t get_ng_T() const;
257 
261  virtual std::size_t get_nh_T() const;
262 
266  const std::shared_ptr<StateAbstract>& get_state() const;
267 
271  virtual const VectorXs& get_g_lb() const;
272 
276  virtual const VectorXs& get_g_ub() const;
277 
281  const VectorXs& get_u_lb() const;
282 
286  const VectorXs& get_u_ub() const;
287 
292 
296  void set_g_lb(const VectorXs& g_lb);
297 
301  void set_g_ub(const VectorXs& g_ub);
302 
306  void set_u_lb(const VectorXs& u_lb);
307 
311  void set_u_ub(const VectorXs& u_ub);
312 
316  template <class Scalar>
317  friend std::ostream& operator<<(std::ostream& os,
318  const ActionModelAbstractTpl<Scalar>& model);
319 
325  virtual void print(std::ostream& os) const;
326 
327  protected:
328  std::size_t nu_;
329  std::size_t nr_;
330  std::size_t ng_;
331  std::size_t nh_;
332  std::size_t ng_T_;
333  std::size_t nh_T_;
334  std::shared_ptr<StateAbstract> state_;
335  VectorXs unone_;
336  VectorXs g_lb_;
337  VectorXs g_ub_;
338  VectorXs u_lb_;
339  VectorXs u_ub_;
343  : nu_(0), nr_(0), ng_(0), nh_(0), ng_T_(0), nh_T_(0), state_(nullptr) {}
344 
350 
351  template <class Scalar>
352  friend class ConstraintModelManagerTpl;
353 };
354 
355 template <typename _Scalar>
357  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
358 
359  typedef _Scalar Scalar;
361  typedef typename MathBase::VectorXs VectorXs;
362  typedef typename MathBase::MatrixXs MatrixXs;
363 
364  template <template <typename Scalar> class Model>
365  explicit ActionDataAbstractTpl(Model<Scalar>* const model)
366  : cost(Scalar(0.)),
367  xnext(model->get_state()->get_nx()),
368  Fx(model->get_state()->get_ndx(), model->get_state()->get_ndx()),
369  Fu(model->get_state()->get_ndx(), model->get_nu()),
370  r(model->get_nr()),
371  Lx(model->get_state()->get_ndx()),
372  Lu(model->get_nu()),
373  Lxx(model->get_state()->get_ndx(), model->get_state()->get_ndx()),
374  Lxu(model->get_state()->get_ndx(), model->get_nu()),
375  Luu(model->get_nu(), model->get_nu()),
376  g(model->get_ng() > model->get_ng_T() ? model->get_ng()
377  : model->get_ng_T()),
378  Gx(model->get_ng() > model->get_ng_T() ? model->get_ng()
379  : model->get_ng_T(),
380  model->get_state()->get_ndx()),
381  Gu(model->get_ng() > model->get_ng_T() ? model->get_ng()
382  : model->get_ng_T(),
383  model->get_nu()),
384  h(model->get_nh() > model->get_nh_T() ? model->get_nh()
385  : model->get_nh_T()),
386  Hx(model->get_nh() > model->get_nh_T() ? model->get_nh()
387  : model->get_nh_T(),
388  model->get_state()->get_ndx()),
389  Hu(model->get_nh() > model->get_nh_T() ? model->get_nh()
390  : model->get_nh_T(),
391  model->get_nu()) {
392  xnext.setZero();
393  Fx.setZero();
394  Fu.setZero();
395  r.setZero();
396  Lx.setZero();
397  Lu.setZero();
398  Lxx.setZero();
399  Lxu.setZero();
400  Luu.setZero();
401  g.setZero();
402  Gx.setZero();
403  Gu.setZero();
404  h.setZero();
405  Hx.setZero();
406  Hu.setZero();
407  }
408  virtual ~ActionDataAbstractTpl() = default;
409 
410  Scalar cost;
411  VectorXs xnext;
412  MatrixXs Fx;
413  MatrixXs
414  Fu;
415  VectorXs r;
416  VectorXs Lx;
417  VectorXs Lu;
418  MatrixXs Lxx;
419  MatrixXs Lxu;
421  MatrixXs Luu;
422  VectorXs g;
423  MatrixXs Gx;
425  MatrixXs Gu;
427  VectorXs h;
428  MatrixXs Hx;
430  MatrixXs Hu;
432 };
433 
434 } // namespace crocoddyl
435 
436 /* --- Details -------------------------------------------------------------- */
437 /* --- Details -------------------------------------------------------------- */
438 /* --- Details -------------------------------------------------------------- */
439 #include "crocoddyl/core/action-base.hxx"
440 
441 CROCODDYL_DECLARE_EXTERN_TEMPLATE_CLASS(crocoddyl::ActionModelAbstractTpl)
442 CROCODDYL_DECLARE_EXTERN_TEMPLATE_STRUCT(crocoddyl::ActionDataAbstractTpl)
443 
444 #endif // CROCODDYL_CORE_ACTION_BASE_HPP_
Abstract class for action model.
Definition: action-base.hpp:97
virtual void calc(const std::shared_ptr< ActionDataAbstract > &data, const Eigen::Ref< const VectorXs > &x, const Eigen::Ref< const VectorXs > &u)=0
Compute the next state and cost value.
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.
std::shared_ptr< StateAbstract > state_
Model of 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 calcDiff(const std::shared_ptr< ActionDataAbstract > &data, const Eigen::Ref< const VectorXs > &x)
Compute the derivatives of the cost functions with respect to the state only.
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.
std::size_t nh_T_
Number of equality terminal constraints.
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)
ActionModelAbstractTpl(const ActionModelAbstractTpl< Scalar > &other)
Copy constructor.
virtual std::size_t get_nh_T() const
Return the number of equality terminal constraints.
const std::shared_ptr< StateAbstract > & get_state() const
Return the state.
std::size_t nu_
Control dimension.
virtual std::shared_ptr< ActionDataAbstract > createData()
Create the action data.
ActionModelAbstractTpl(std::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, const std::size_t ng_T=0, const std::size_t nh_T=0)
Initialize the action model.
VectorXs unone_
Neutral state.
std::size_t nr_
Dimension of the cost residual.
std::size_t nh_
Number of equality constraints.
virtual void calcDiff(const std::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.
VectorXs quasiStatic_x(const std::shared_ptr< ActionDataAbstract > &data, const VectorXs &x, const std::size_t maxiter=100, const Scalar tol=Scalar(1e-9))
std::size_t get_nr() const
Return the dimension of the cost-residual vector.
virtual void quasiStatic(const std::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.
std::size_t ng_T_
Number of inequality terminal constraints.
void set_g_ub(const VectorXs &g_ub)
Modify the upper bound of the inequality constraints.
virtual std::size_t get_ng_T() const
Return the number of inequality terminal constraints.
void set_u_lb(const VectorXs &u_lb)
Modify the control lower bounds.
friend std::ostream & operator<<(std::ostream &os, const ActionModelAbstractTpl< Scalar > &model)
Print information on the action model.
virtual void calc(const std::shared_ptr< ActionDataAbstract > &data, const Eigen::Ref< const VectorXs > &x)
Compute the total cost value for nodes that depends only on the state.
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.
std::size_t ng_
Number of inequality constraints.
std::size_t get_nu() const
Return the dimension of the control input.
virtual bool checkData(const std::shared_ptr< ActionDataAbstract > &data)
Checks that a specific data belongs to this model.
Manage the individual constraint models.
Abstract class for the state representation.
Definition: state-base.hpp:48
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.