crocoddyl  1.7.0
Contact RObot COntrol by Differential DYnamic programming Library (Crocoddyl)
action-base.hpp
1 // BSD 3-Clause License
3 //
4 // Copyright (C) 2019-2021, LAAS-CNRS, University of Edinburgh, University of Oxford
5 // Copyright note valid unless otherwise stated in individual files.
6 // All rights reserved.
8 
9 #ifndef CROCODDYL_CORE_ACTION_BASE_HPP_
10 #define CROCODDYL_CORE_ACTION_BASE_HPP_
11 
12 #include <stdexcept>
13 #include <boost/shared_ptr.hpp>
14 #include <boost/make_shared.hpp>
15 
16 #include "crocoddyl/core/fwd.hpp"
17 #include "crocoddyl/core/state-base.hpp"
18 #include "crocoddyl/core/utils/math.hpp"
19 #include "crocoddyl/core/utils/to-string.hpp"
20 
21 namespace crocoddyl {
22 
59 template <typename _Scalar>
61  public:
62  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
63 
64  typedef _Scalar Scalar;
68  typedef typename MathBase::VectorXs VectorXs;
69 
77  ActionModelAbstractTpl(boost::shared_ptr<StateAbstract> state, const std::size_t nu, const std::size_t nr = 0);
78  virtual ~ActionModelAbstractTpl();
79 
87  virtual void calc(const boost::shared_ptr<ActionDataAbstract>& data, const Eigen::Ref<const VectorXs>& x,
88  const Eigen::Ref<const VectorXs>& u) = 0;
89 
101  virtual void calcDiff(const boost::shared_ptr<ActionDataAbstract>& data, const Eigen::Ref<const VectorXs>& x,
102  const Eigen::Ref<const VectorXs>& u) = 0;
103 
109  virtual boost::shared_ptr<ActionDataAbstract> createData();
110 
114  virtual bool checkData(const boost::shared_ptr<ActionDataAbstract>& data);
115 
122  void calc(const boost::shared_ptr<ActionDataAbstract>& data, const Eigen::Ref<const VectorXs>& x);
123 
130  void calcDiff(const boost::shared_ptr<ActionDataAbstract>& data, const Eigen::Ref<const VectorXs>& x);
131 
144  virtual void quasiStatic(const boost::shared_ptr<ActionDataAbstract>& data, Eigen::Ref<VectorXs> u,
145  const Eigen::Ref<const VectorXs>& x, const std::size_t maxiter = 100,
146  const Scalar tol = Scalar(1e-9));
147 
159  VectorXs quasiStatic_x(const boost::shared_ptr<ActionDataAbstract>& data, const VectorXs& x,
160  const std::size_t maxiter = 100, const Scalar tol = Scalar(1e-9));
161 
165  std::size_t get_nu() const;
166 
170  std::size_t get_nr() const;
171 
175  const boost::shared_ptr<StateAbstract>& get_state() const;
176 
180  const VectorXs& get_u_lb() const;
181 
185  const VectorXs& get_u_ub() const;
186 
190  bool get_has_control_limits() const;
191 
195  void set_u_lb(const VectorXs& u_lb);
196 
200  void set_u_ub(const VectorXs& u_ub);
201 
205  template <class Scalar>
206  friend std::ostream& operator<<(std::ostream& os, const ActionModelAbstractTpl<Scalar>& action_model);
207 
208  protected:
209  std::size_t nu_;
210  std::size_t nr_;
211  boost::shared_ptr<StateAbstract> state_;
212  VectorXs unone_;
213  VectorXs u_lb_;
214  VectorXs u_ub_;
216 
221 };
222 
223 template <typename _Scalar>
225  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
226 
227  typedef _Scalar Scalar;
229  typedef typename MathBase::VectorXs VectorXs;
230  typedef typename MathBase::MatrixXs MatrixXs;
231 
232  template <template <typename Scalar> class Model>
233  explicit ActionDataAbstractTpl(Model<Scalar>* const model)
234  : cost(Scalar(0.)),
235  xnext(model->get_state()->get_nx()),
236  Fx(model->get_state()->get_ndx(), model->get_state()->get_ndx()),
237  Fu(model->get_state()->get_ndx(), model->get_nu()),
238  r(model->get_nr()),
239  Lx(model->get_state()->get_ndx()),
240  Lu(model->get_nu()),
241  Lxx(model->get_state()->get_ndx(), model->get_state()->get_ndx()),
242  Lxu(model->get_state()->get_ndx(), model->get_nu()),
243  Luu(model->get_nu(), model->get_nu()) {
244  xnext.setZero();
245  Fx.setZero();
246  Fu.setZero();
247  r.setZero();
248  Lx.setZero();
249  Lu.setZero();
250  Lxx.setZero();
251  Lxu.setZero();
252  Luu.setZero();
253  }
254  virtual ~ActionDataAbstractTpl() {}
255 
256  Scalar cost;
257  VectorXs xnext;
258  MatrixXs Fx;
259  MatrixXs Fu;
260  VectorXs r;
261  VectorXs Lx;
262  VectorXs Lu;
263  MatrixXs Lxx;
264  MatrixXs Lxu;
265  MatrixXs Luu;
266 };
267 
268 } // namespace crocoddyl
269 
270 /* --- Details -------------------------------------------------------------- */
271 /* --- Details -------------------------------------------------------------- */
272 /* --- Details -------------------------------------------------------------- */
273 #include "crocoddyl/core/action-base.hxx"
274 
275 #endif // CROCODDYL_CORE_ACTION_BASE_HPP_
void set_u_lb(const VectorXs &u_lb)
Modify the control lower bounds.
Abstract class for action model.
Definition: action-base.hpp:60
bool has_control_limits_
Indicates whether any of the control limits is finite.
const boost::shared_ptr< StateAbstract > & get_state() const
Return the state.
MatrixXs Lxx
Hessian of the cost function.
virtual boost::shared_ptr< ActionDataAbstract > createData()
Create the action data.
std::size_t nu_
Control dimension.
void update_has_control_limits()
Update the status of the control limits (i.e. if there are defined limits)
VectorXs xnext
evolution state
std::size_t get_nu() const
Return the dimension of the control input.
Abstract class for the state representation.
Definition: fwd.hpp:101
bool get_has_control_limits() const
Indicates if there are defined control limits.
void set_u_ub(const VectorXs &u_ub)
Modify the control upper bounds.
const VectorXs & get_u_lb() const
Return the control lower bound.
VectorXs quasiStatic_x(const boost::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.
VectorXs r
Cost residual.
VectorXs Lx
Jacobian of the cost function.
VectorXs unone_
Neutral state.
MatrixXs Fx
Jacobian of the dynamics.
VectorXs Lu
Jacobian of the cost function.
virtual bool checkData(const boost::shared_ptr< ActionDataAbstract > &data)
Checks that a specific data belongs to this model.
ActionModelAbstractTpl(boost::shared_ptr< StateAbstract > state, const std::size_t nu, const std::size_t nr=0)
Initialize the action model.
VectorXs u_ub_
Upper control limits.
MatrixXs Luu
Hessian of the cost function.
VectorXs u_lb_
Lower control limits.
boost::shared_ptr< StateAbstract > state_
Model of the state.
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.
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 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.
MatrixXs Lxu
Hessian of the cost function.
std::size_t nr_
Dimension of the cost residual.
MatrixXs Fu
Jacobian of the dynamics.
const VectorXs & get_u_ub() const
Return the control upper bound.