Crocoddyl
action-base.hpp
1 // BSD 3-Clause License
3 //
4 // Copyright (C) 2019-2024, 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 
116  ActionModelAbstractTpl(boost::shared_ptr<StateAbstract> state,
117  const std::size_t nu, const std::size_t nr = 0,
118  const std::size_t ng = 0, const std::size_t nh = 0,
119  const std::size_t ng_T = 0,
120  const std::size_t nh_T = 0);
121  virtual ~ActionModelAbstractTpl();
122 
130  virtual void calc(const boost::shared_ptr<ActionDataAbstract>& data,
131  const Eigen::Ref<const VectorXs>& x,
132  const Eigen::Ref<const VectorXs>& u) = 0;
133 
145  virtual void calc(const boost::shared_ptr<ActionDataAbstract>& data,
146  const Eigen::Ref<const VectorXs>& x);
147 
160  virtual void calcDiff(const boost::shared_ptr<ActionDataAbstract>& data,
161  const Eigen::Ref<const VectorXs>& x,
162  const Eigen::Ref<const VectorXs>& u) = 0;
163 
175  virtual void calcDiff(const boost::shared_ptr<ActionDataAbstract>& data,
176  const Eigen::Ref<const VectorXs>& x);
177 
183  virtual boost::shared_ptr<ActionDataAbstract> createData();
184 
188  virtual bool checkData(const boost::shared_ptr<ActionDataAbstract>& data);
189 
203  virtual void quasiStatic(const boost::shared_ptr<ActionDataAbstract>& data,
204  Eigen::Ref<VectorXs> u,
205  const Eigen::Ref<const VectorXs>& x,
206  const std::size_t maxiter = 100,
207  const Scalar tol = Scalar(1e-9));
208 
220  VectorXs quasiStatic_x(const boost::shared_ptr<ActionDataAbstract>& data,
221  const VectorXs& x, const std::size_t maxiter = 100,
222  const Scalar tol = Scalar(1e-9));
223 
227  std::size_t get_nu() const;
228 
232  std::size_t get_nr() const;
233 
237  virtual std::size_t get_ng() const;
238 
242  virtual std::size_t get_nh() const;
243 
247  virtual std::size_t get_ng_T() const;
248 
252  virtual std::size_t get_nh_T() const;
253 
257  const boost::shared_ptr<StateAbstract>& get_state() const;
258 
262  virtual const VectorXs& get_g_lb() const;
263 
267  virtual const VectorXs& get_g_ub() const;
268 
272  const VectorXs& get_u_lb() const;
273 
277  const VectorXs& get_u_ub() const;
278 
283 
287  void set_g_lb(const VectorXs& g_lb);
288 
292  void set_g_ub(const VectorXs& g_ub);
293 
297  void set_u_lb(const VectorXs& u_lb);
298 
302  void set_u_ub(const VectorXs& u_ub);
303 
307  template <class Scalar>
308  friend std::ostream& operator<<(std::ostream& os,
309  const ActionModelAbstractTpl<Scalar>& model);
310 
316  virtual void print(std::ostream& os) const;
317 
318  protected:
319  std::size_t nu_;
320  std::size_t nr_;
321  std::size_t ng_;
322  std::size_t nh_;
323  std::size_t ng_T_;
324  std::size_t nh_T_;
325  boost::shared_ptr<StateAbstract> state_;
326  VectorXs unone_;
327  VectorXs g_lb_;
328  VectorXs g_ub_;
329  VectorXs u_lb_;
330  VectorXs u_ub_;
333 
339 
340  template <class Scalar>
341  friend class ConstraintModelManagerTpl;
342 };
343 
344 template <typename _Scalar>
346  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
347 
348  typedef _Scalar Scalar;
350  typedef typename MathBase::VectorXs VectorXs;
351  typedef typename MathBase::MatrixXs MatrixXs;
352 
353  template <template <typename Scalar> class Model>
354  explicit ActionDataAbstractTpl(Model<Scalar>* const model)
355  : cost(Scalar(0.)),
356  xnext(model->get_state()->get_nx()),
357  Fx(model->get_state()->get_ndx(), model->get_state()->get_ndx()),
358  Fu(model->get_state()->get_ndx(), model->get_nu()),
359  r(model->get_nr()),
360  Lx(model->get_state()->get_ndx()),
361  Lu(model->get_nu()),
362  Lxx(model->get_state()->get_ndx(), model->get_state()->get_ndx()),
363  Lxu(model->get_state()->get_ndx(), model->get_nu()),
364  Luu(model->get_nu(), model->get_nu()),
365  g(model->get_ng() > model->get_ng_T() ? model->get_ng()
366  : model->get_ng_T()),
367  Gx(model->get_ng() > model->get_ng_T() ? model->get_ng()
368  : model->get_ng_T(),
369  model->get_state()->get_ndx()),
370  Gu(model->get_ng() > model->get_ng_T() ? model->get_ng()
371  : model->get_ng_T(),
372  model->get_nu()),
373  h(model->get_nh() > model->get_nh_T() ? model->get_nh()
374  : model->get_nh_T()),
375  Hx(model->get_nh() > model->get_nh_T() ? model->get_nh()
376  : model->get_nh_T(),
377  model->get_state()->get_ndx()),
378  Hu(model->get_nh() > model->get_nh_T() ? model->get_nh()
379  : model->get_nh_T(),
380  model->get_nu()) {
381  xnext.setZero();
382  Fx.setZero();
383  Fu.setZero();
384  r.setZero();
385  Lx.setZero();
386  Lu.setZero();
387  Lxx.setZero();
388  Lxu.setZero();
389  Luu.setZero();
390  g.setZero();
391  Gx.setZero();
392  Gu.setZero();
393  h.setZero();
394  Hx.setZero();
395  Hu.setZero();
396  }
397  virtual ~ActionDataAbstractTpl() {}
398 
399  Scalar cost;
400  VectorXs xnext;
401  MatrixXs Fx;
402  MatrixXs
403  Fu;
404  VectorXs r;
405  VectorXs Lx;
406  VectorXs Lu;
407  MatrixXs Lxx;
408  MatrixXs Lxu;
410  MatrixXs Luu;
411  VectorXs g;
412  MatrixXs Gx;
414  MatrixXs Gu;
416  VectorXs h;
417  MatrixXs Hx;
419  MatrixXs Hu;
421 };
422 
423 } // namespace crocoddyl
424 
425 /* --- Details -------------------------------------------------------------- */
426 /* --- Details -------------------------------------------------------------- */
427 /* --- Details -------------------------------------------------------------- */
428 #include "crocoddyl/core/action-base.hxx"
429 
430 #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.
std::size_t nh_T_
Number of equality terminal constraints.
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)
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, const std::size_t ng_T=0, const std::size_t nh_T=0)
Initialize the action model.
virtual std::size_t get_nh_T() const
Return the number of equality terminal constraints.
boost::shared_ptr< StateAbstract > state_
Model of the state.
std::size_t nu_
Control dimension.
VectorXs unone_
Neutral state.
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.
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 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.
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.
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.