Crocoddyl
diff-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_DIFF_ACTION_BASE_HPP_
11 #define CROCODDYL_CORE_DIFF_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 ~DifferentialActionModelBase() = default;
21 
22  CROCODDYL_BASE_CAST(DifferentialActionModelBase,
24 };
25 
121 template <typename _Scalar>
123  public:
124  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
125 
126  typedef _Scalar Scalar;
127  typedef typename ScalarSelector<Scalar>::type ScalarType;
132  typedef typename MathBase::VectorXs VectorXs;
133  typedef typename MathBase::MatrixXs MatrixXs;
134 
146  DifferentialActionModelAbstractTpl(std::shared_ptr<StateAbstract> state,
147  const std::size_t nu,
148  const std::size_t nr = 0,
149  const std::size_t ng = 0,
150  const std::size_t nh = 0,
151  const std::size_t ng_T = 0,
152  const std::size_t nh_T = 0);
153  virtual ~DifferentialActionModelAbstractTpl() = default;
154 
162  virtual void calc(const std::shared_ptr<DifferentialActionDataAbstract>& data,
163  const Eigen::Ref<const VectorXs>& x,
164  const Eigen::Ref<const VectorXs>& u) = 0;
165 
177  virtual void calc(const std::shared_ptr<DifferentialActionDataAbstract>& data,
178  const Eigen::Ref<const VectorXs>& x);
179 
192  virtual void calcDiff(
193  const std::shared_ptr<DifferentialActionDataAbstract>& data,
194  const Eigen::Ref<const VectorXs>& x,
195  const Eigen::Ref<const VectorXs>& u) = 0;
196 
208  virtual void calcDiff(
209  const std::shared_ptr<DifferentialActionDataAbstract>& data,
210  const Eigen::Ref<const VectorXs>& x);
211 
217  virtual std::shared_ptr<DifferentialActionDataAbstract> createData();
218 
222  virtual bool checkData(
223  const std::shared_ptr<DifferentialActionDataAbstract>& data);
224 
238  virtual void quasiStatic(
239  const std::shared_ptr<DifferentialActionDataAbstract>& data,
240  Eigen::Ref<VectorXs> u, const Eigen::Ref<const VectorXs>& x,
241  const std::size_t maxiter = 100, const Scalar tol = Scalar(1e-9));
242 
254  VectorXs quasiStatic_x(
255  const std::shared_ptr<DifferentialActionDataAbstract>& data,
256  const VectorXs& x, const std::size_t maxiter = 100,
257  const Scalar tol = Scalar(1e-9));
258 
262  std::size_t get_nu() const;
263 
267  std::size_t get_nr() const;
268 
272  virtual std::size_t get_ng() const;
273 
277  virtual std::size_t get_nh() const;
278 
282  virtual std::size_t get_ng_T() const;
283 
287  virtual std::size_t get_nh_T() const;
288 
292  const std::shared_ptr<StateAbstract>& get_state() const;
293 
297  virtual const VectorXs& get_g_lb() const;
298 
302  virtual const VectorXs& get_g_ub() const;
303 
307  const VectorXs& get_u_lb() const;
308 
312  const VectorXs& get_u_ub() const;
313 
318 
322  void set_g_lb(const VectorXs& g_lb);
323 
327  void set_g_ub(const VectorXs& g_ub);
328 
332  void set_u_lb(const VectorXs& u_lb);
333 
337  void set_u_ub(const VectorXs& u_ub);
338 
342  template <class Scalar>
343  friend std::ostream& operator<<(
344  std::ostream& os,
346 
352  virtual void print(std::ostream& os) const;
353 
354  private:
355  std::size_t ng_internal_;
357  std::size_t nh_internal_;
359 
360  protected:
361  std::size_t nu_;
362  std::size_t nr_;
363  std::size_t ng_;
364  std::size_t nh_;
365  std::size_t ng_T_;
366  std::size_t nh_T_;
367  std::shared_ptr<StateAbstract> state_;
368  VectorXs unone_;
369  VectorXs g_lb_;
370  VectorXs g_ub_;
371  VectorXs u_lb_;
372  VectorXs u_ub_;
376  : nu_(0), nr_(0), ng_(0), nh_(0), ng_T_(0), nh_T_(0), state_(nullptr) {}
377 
383 
384  template <class Scalar>
386  template <class Scalar>
387  friend class ConstraintModelManagerTpl;
388 };
389 
390 template <typename _Scalar>
392  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
393 
394  typedef _Scalar Scalar;
396  typedef typename MathBase::VectorXs VectorXs;
397  typedef typename MathBase::MatrixXs MatrixXs;
398 
399  template <template <typename Scalar> class Model>
400  explicit DifferentialActionDataAbstractTpl(Model<Scalar>* const model)
401  : cost(Scalar(0.)),
402  xout(model->get_state()->get_nv()),
403  Fx(model->get_state()->get_nv(), model->get_state()->get_ndx()),
404  Fu(model->get_state()->get_nv(), model->get_nu()),
405  r(model->get_nr()),
406  Lx(model->get_state()->get_ndx()),
407  Lu(model->get_nu()),
408  Lxx(model->get_state()->get_ndx(), model->get_state()->get_ndx()),
409  Lxu(model->get_state()->get_ndx(), model->get_nu()),
410  Luu(model->get_nu(), model->get_nu()),
411  g(model->get_ng() > model->get_ng_T() ? model->get_ng()
412  : model->get_ng_T()),
413  Gx(model->get_ng() > model->get_ng_T() ? model->get_ng()
414  : model->get_ng_T(),
415  model->get_state()->get_ndx()),
416  Gu(model->get_ng() > model->get_ng_T() ? model->get_ng()
417  : model->get_ng_T(),
418  model->get_nu()),
419  h(model->get_nh() > model->get_nh_T() ? model->get_nh()
420  : model->get_nh_T()),
421  Hx(model->get_nh() > model->get_nh_T() ? model->get_nh()
422  : model->get_nh_T(),
423  model->get_state()->get_ndx()),
424  Hu(model->get_nh() > model->get_nh_T() ? model->get_nh()
425  : model->get_nh_T(),
426  model->get_nu()) {
427  xout.setZero();
428  Fx.setZero();
429  Fu.setZero();
430  r.setZero();
431  Lx.setZero();
432  Lu.setZero();
433  Lxx.setZero();
434  Lxu.setZero();
435  Luu.setZero();
436  g.setZero();
437  Gx.setZero();
438  Gu.setZero();
439  h.setZero();
440  Hx.setZero();
441  Hu.setZero();
442  }
443  virtual ~DifferentialActionDataAbstractTpl() = default;
444 
445  Scalar cost;
446  VectorXs xout;
447  MatrixXs Fx;
448  MatrixXs
449  Fu;
450  VectorXs r;
451  VectorXs Lx;
452  VectorXs Lu;
453  MatrixXs Lxx;
454  MatrixXs Lxu;
456  MatrixXs Luu;
457  VectorXs g;
458  MatrixXs Gx;
460  MatrixXs Gu;
462  VectorXs h;
463  MatrixXs Hx;
465  MatrixXs Hu;
467 };
468 
469 } // namespace crocoddyl
470 
471 /* --- Details -------------------------------------------------------------- */
472 /* --- Details -------------------------------------------------------------- */
473 /* --- Details -------------------------------------------------------------- */
474 #include "crocoddyl/core/diff-action-base.hxx"
475 
476 CROCODDYL_DECLARE_EXTERN_TEMPLATE_CLASS(
478 CROCODDYL_DECLARE_EXTERN_TEMPLATE_STRUCT(
480 
481 #endif // CROCODDYL_CORE_DIFF_ACTION_BASE_HPP_
Manage the individual constraint models.
Abstract class for differential action model.
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.
virtual std::shared_ptr< DifferentialActionDataAbstract > createData()
Create the differential action data.
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 differential 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.
virtual void calc(const std::shared_ptr< DifferentialActionDataAbstract > &data, const Eigen::Ref< const VectorXs > &x, const Eigen::Ref< const VectorXs > &u)=0
Compute the system acceleration and cost value.
VectorXs g_ub_
Lower bound of the inequality constraints.
friend std::ostream & operator<<(std::ostream &os, const DifferentialActionModelAbstractTpl< Scalar > &model)
Print information on the differential action model.
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.
VectorXs quasiStatic_x(const std::shared_ptr< DifferentialActionDataAbstract > &data, const VectorXs &x, const std::size_t maxiter=100, const Scalar tol=Scalar(1e-9))
void update_has_control_limits()
Update the status of the control limits (i.e. if there are defined limits)
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.
virtual bool checkData(const std::shared_ptr< DifferentialActionDataAbstract > &data)
Checks that a specific data belongs to this model.
std::size_t nr_
Dimension of the cost residual.
std::size_t nh_
Number of equality constraints.
virtual void quasiStatic(const std::shared_ptr< DifferentialActionDataAbstract > &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 get_nr() const
Return the dimension of the cost-residual vector.
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.
DifferentialActionModelAbstractTpl(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 differential action model.
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.
VectorXs g_lb_
Lower bound of the inequality constraints.
virtual const VectorXs & get_g_ub() const
Return the upper bound of the inequality constraints.
virtual void calcDiff(const std::shared_ptr< DifferentialActionDataAbstract > &data, const Eigen::Ref< const VectorXs > &x, const Eigen::Ref< const VectorXs > &u)=0
Compute the derivatives of the dynamics and cost functions.
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.
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.
Abstract class for an integrated action model.
Abstract class for the state representation.
Definition: state-base.hpp:48
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 .