Crocoddyl
diff-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_DIFF_ACTION_BASE_HPP_
11 #define CROCODDYL_CORE_DIFF_ACTION_BASE_HPP_
12 
13 #include <boost/make_shared.hpp>
14 #include <memory>
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 
118 template <typename _Scalar>
120  public:
121  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
122 
123  typedef _Scalar Scalar;
128  typedef typename MathBase::VectorXs VectorXs;
129  typedef typename MathBase::MatrixXs MatrixXs;
130 
142  DifferentialActionModelAbstractTpl(std::shared_ptr<StateAbstract> state,
143  const std::size_t nu,
144  const std::size_t nr = 0,
145  const std::size_t ng = 0,
146  const std::size_t nh = 0,
147  const std::size_t ng_T = 0,
148  const std::size_t nh_T = 0);
150 
158  virtual void calc(const std::shared_ptr<DifferentialActionDataAbstract>& data,
159  const Eigen::Ref<const VectorXs>& x,
160  const Eigen::Ref<const VectorXs>& u) = 0;
161 
173  virtual void calc(const std::shared_ptr<DifferentialActionDataAbstract>& data,
174  const Eigen::Ref<const VectorXs>& x);
175 
188  virtual void calcDiff(
189  const std::shared_ptr<DifferentialActionDataAbstract>& data,
190  const Eigen::Ref<const VectorXs>& x,
191  const Eigen::Ref<const VectorXs>& u) = 0;
192 
204  virtual void calcDiff(
205  const std::shared_ptr<DifferentialActionDataAbstract>& data,
206  const Eigen::Ref<const VectorXs>& x);
207 
213  virtual std::shared_ptr<DifferentialActionDataAbstract> createData();
214 
218  virtual bool checkData(
219  const std::shared_ptr<DifferentialActionDataAbstract>& data);
220 
234  virtual void quasiStatic(
235  const std::shared_ptr<DifferentialActionDataAbstract>& data,
236  Eigen::Ref<VectorXs> u, const Eigen::Ref<const VectorXs>& x,
237  const std::size_t maxiter = 100, const Scalar tol = Scalar(1e-9));
238 
250  VectorXs quasiStatic_x(
251  const std::shared_ptr<DifferentialActionDataAbstract>& data,
252  const VectorXs& x, const std::size_t maxiter = 100,
253  const Scalar tol = Scalar(1e-9));
254 
258  std::size_t get_nu() const;
259 
263  std::size_t get_nr() const;
264 
268  virtual std::size_t get_ng() const;
269 
273  virtual std::size_t get_nh() const;
274 
278  virtual std::size_t get_ng_T() const;
279 
283  virtual std::size_t get_nh_T() const;
284 
288  const std::shared_ptr<StateAbstract>& get_state() const;
289 
293  virtual const VectorXs& get_g_lb() const;
294 
298  virtual const VectorXs& get_g_ub() const;
299 
303  const VectorXs& get_u_lb() const;
304 
308  const VectorXs& get_u_ub() const;
309 
314 
318  void set_g_lb(const VectorXs& g_lb);
319 
323  void set_g_ub(const VectorXs& g_ub);
324 
328  void set_u_lb(const VectorXs& u_lb);
329 
333  void set_u_ub(const VectorXs& u_ub);
334 
338  template <class Scalar>
339  friend std::ostream& operator<<(
340  std::ostream& os,
342 
348  virtual void print(std::ostream& os) const;
349 
350  private:
351  std::size_t ng_internal_;
353  std::size_t nh_internal_;
355 
356  protected:
357  std::size_t nu_;
358  std::size_t nr_;
359  std::size_t ng_;
360  std::size_t nh_;
361  std::size_t ng_T_;
362  std::size_t nh_T_;
363  std::shared_ptr<StateAbstract> state_;
364  VectorXs unone_;
365  VectorXs g_lb_;
366  VectorXs g_ub_;
367  VectorXs u_lb_;
368  VectorXs u_ub_;
371 
377 
378  template <class Scalar>
380  template <class Scalar>
381  friend class ConstraintModelManagerTpl;
382 };
383 
384 template <typename _Scalar>
386  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
387 
388  typedef _Scalar Scalar;
390  typedef typename MathBase::VectorXs VectorXs;
391  typedef typename MathBase::MatrixXs MatrixXs;
392 
393  template <template <typename Scalar> class Model>
394  explicit DifferentialActionDataAbstractTpl(Model<Scalar>* const model)
395  : cost(Scalar(0.)),
396  xout(model->get_state()->get_nv()),
397  Fx(model->get_state()->get_nv(), model->get_state()->get_ndx()),
398  Fu(model->get_state()->get_nv(), model->get_nu()),
399  r(model->get_nr()),
400  Lx(model->get_state()->get_ndx()),
401  Lu(model->get_nu()),
402  Lxx(model->get_state()->get_ndx(), model->get_state()->get_ndx()),
403  Lxu(model->get_state()->get_ndx(), model->get_nu()),
404  Luu(model->get_nu(), model->get_nu()),
405  g(model->get_ng() > model->get_ng_T() ? model->get_ng()
406  : model->get_ng_T()),
407  Gx(model->get_ng() > model->get_ng_T() ? model->get_ng()
408  : model->get_ng_T(),
409  model->get_state()->get_ndx()),
410  Gu(model->get_ng() > model->get_ng_T() ? model->get_ng()
411  : model->get_ng_T(),
412  model->get_nu()),
413  h(model->get_nh() > model->get_nh_T() ? model->get_nh()
414  : model->get_nh_T()),
415  Hx(model->get_nh() > model->get_nh_T() ? model->get_nh()
416  : model->get_nh_T(),
417  model->get_state()->get_ndx()),
418  Hu(model->get_nh() > model->get_nh_T() ? model->get_nh()
419  : model->get_nh_T(),
420  model->get_nu()) {
421  xout.setZero();
422  Fx.setZero();
423  Fu.setZero();
424  r.setZero();
425  Lx.setZero();
426  Lu.setZero();
427  Lxx.setZero();
428  Lxu.setZero();
429  Luu.setZero();
430  g.setZero();
431  Gx.setZero();
432  Gu.setZero();
433  h.setZero();
434  Hx.setZero();
435  Hu.setZero();
436  }
438 
439  Scalar cost;
440  VectorXs xout;
441  MatrixXs Fx;
442  MatrixXs
443  Fu;
444  VectorXs r;
445  VectorXs Lx;
446  VectorXs Lu;
447  MatrixXs Lxx;
448  MatrixXs Lxu;
450  MatrixXs Luu;
451  VectorXs g;
452  MatrixXs Gx;
454  MatrixXs Gu;
456  VectorXs h;
457  MatrixXs Hx;
459  MatrixXs Hu;
461 };
462 
463 } // namespace crocoddyl
464 
465 /* --- Details -------------------------------------------------------------- */
466 /* --- Details -------------------------------------------------------------- */
467 /* --- Details -------------------------------------------------------------- */
468 #include "crocoddyl/core/diff-action-base.hxx"
469 
470 #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:46
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 .