crocoddyl  1.6.0
Contact RObot COntrol by Differential DYnamic programming Library (Crocoddyl)
diff-action-base.hpp
1 // BSD 3-Clause License
3 //
4 // Copyright (C) 2019-2020, LAAS-CNRS, University of Edinburgh
5 // Copyright note valid unless otherwise stated in individual files.
6 // All rights reserved.
8 
9 #ifndef CROCODDYL_CORE_DIFF_ACTION_BASE_HPP_
10 #define CROCODDYL_CORE_DIFF_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/to-string.hpp"
19 #include "crocoddyl/core/utils/math.hpp"
20 
21 namespace crocoddyl {
22 
53 template <typename _Scalar>
55  public:
56  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
57 
58  typedef _Scalar Scalar;
62  typedef typename MathBase::VectorXs VectorXs;
63  typedef typename MathBase::MatrixXs MatrixXs;
64 
72  DifferentialActionModelAbstractTpl(boost::shared_ptr<StateAbstract> state, const std::size_t& nu,
73  const std::size_t& nr = 0);
75 
83  virtual void calc(const boost::shared_ptr<DifferentialActionDataAbstract>& data, const Eigen::Ref<const VectorXs>& x,
84  const Eigen::Ref<const VectorXs>& u) = 0;
85 
97  virtual void calcDiff(const boost::shared_ptr<DifferentialActionDataAbstract>& data,
98  const Eigen::Ref<const VectorXs>& x, const Eigen::Ref<const VectorXs>& u) = 0;
99 
105  virtual boost::shared_ptr<DifferentialActionDataAbstract> createData();
106 
110  virtual bool checkData(const boost::shared_ptr<DifferentialActionDataAbstract>& data);
111 
118  void calc(const boost::shared_ptr<DifferentialActionDataAbstract>& data, const Eigen::Ref<const VectorXs>& x);
119 
126  void calcDiff(const boost::shared_ptr<DifferentialActionDataAbstract>& data, const Eigen::Ref<const VectorXs>& x);
127 
140  virtual void quasiStatic(const boost::shared_ptr<DifferentialActionDataAbstract>& data, Eigen::Ref<VectorXs> u,
141  const Eigen::Ref<const VectorXs>& x, const std::size_t& maxiter = 100,
142  const Scalar& tol = Scalar(1e-9));
143 
155  VectorXs quasiStatic_x(const boost::shared_ptr<DifferentialActionDataAbstract>& data, const VectorXs& x,
156  const std::size_t& maxiter = 100, const Scalar& tol = Scalar(1e-9));
157 
161  const std::size_t& get_nu() const;
162 
166  const std::size_t& get_nr() const;
167 
171  const boost::shared_ptr<StateAbstract>& get_state() const;
172 
176  const VectorXs& get_u_lb() const;
177 
181  const VectorXs& get_u_ub() const;
182 
186  bool const& get_has_control_limits() const;
187 
191  void set_u_lb(const VectorXs& u_lb);
192 
196  void set_u_ub(const VectorXs& u_ub);
197 
198  protected:
199  std::size_t nu_;
200  std::size_t nr_;
201  boost::shared_ptr<StateAbstract> state_;
202  VectorXs unone_;
203  VectorXs u_lb_;
204  VectorXs u_ub_;
206 
207  void update_has_control_limits();
208 };
209 
210 template <typename _Scalar>
212  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
213 
214  typedef _Scalar Scalar;
216  typedef typename MathBase::VectorXs VectorXs;
217  typedef typename MathBase::MatrixXs MatrixXs;
218 
219  template <template <typename Scalar> class Model>
220  explicit DifferentialActionDataAbstractTpl(Model<Scalar>* const model)
221  : cost(0.),
222  xout(model->get_state()->get_nv()),
223  Fx(model->get_state()->get_nv(), model->get_state()->get_ndx()),
224  Fu(model->get_state()->get_nv(), model->get_nu()),
225  r(model->get_nr()),
226  Lx(model->get_state()->get_ndx()),
227  Lu(model->get_nu()),
228  Lxx(model->get_state()->get_ndx(), model->get_state()->get_ndx()),
229  Lxu(model->get_state()->get_ndx(), model->get_nu()),
230  Luu(model->get_nu(), model->get_nu()) {
231  xout.setZero();
232  r.setZero();
233  Fx.setZero();
234  Fu.setZero();
235  Lx.setZero();
236  Lu.setZero();
237  Lxx.setZero();
238  Lxu.setZero();
239  Luu.setZero();
240  }
241  virtual ~DifferentialActionDataAbstractTpl() {}
242 
243  Scalar cost;
244  VectorXs xout;
245  MatrixXs Fx;
246  MatrixXs Fu;
247  VectorXs r;
248  VectorXs Lx;
249  VectorXs Lu;
250  MatrixXs Lxx;
251  MatrixXs Lxu;
252  MatrixXs Luu;
253 };
254 
255 } // namespace crocoddyl
256 
257 /* --- Details -------------------------------------------------------------- */
258 /* --- Details -------------------------------------------------------------- */
259 /* --- Details -------------------------------------------------------------- */
260 #include "crocoddyl/core/diff-action-base.hxx"
261 
262 #endif // CROCODDYL_CORE_DIFF_ACTION_BASE_HPP_
VectorXs Lx
Jacobian of the cost function.
bool const & get_has_control_limits() const
Indicates if there are defined control limits.
MatrixXs Fx
Jacobian of the dynamics.
const boost::shared_ptr< StateAbstract > & get_state() const
Return the state.
const VectorXs & get_u_ub() const
Return the control upper bound.
const std::size_t & get_nr() const
Return the dimension of the cost-residual vector.
MatrixXs Lxx
Hessian of the cost function.
const std::size_t & get_nu() const
Return the dimension of the control input.
Abstract class for differential action model.
MatrixXs Luu
Hessian of the cost function.
virtual void calcDiff(const boost::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 bool checkData(const boost::shared_ptr< DifferentialActionDataAbstract > &data)
Checks that a specific data belongs to this model.
std::size_t nr_
Dimension of the cost residual.
Abstract class for the state representation.
Definition: fwd.hpp:106
void set_u_lb(const VectorXs &u_lb)
Modify the control lower bounds.
virtual void calc(const boost::shared_ptr< DifferentialActionDataAbstract > &data, const Eigen::Ref< const VectorXs > &x, const Eigen::Ref< const VectorXs > &u)=0
Compute the system acceleration and cost value.
bool has_control_limits_
Indicates whether any of the control limits is finite.
virtual boost::shared_ptr< DifferentialActionDataAbstract > createData()
Create the differential action data.
MatrixXs Lxu
Hessian of the cost function.
virtual void quasiStatic(const boost::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.
boost::shared_ptr< StateAbstract > state_
Model of the state.
VectorXs quasiStatic_x(const boost::shared_ptr< DifferentialActionDataAbstract > &data, const VectorXs &x, const std::size_t &maxiter=100, const Scalar &tol=Scalar(1e-9))
DifferentialActionModelAbstractTpl(boost::shared_ptr< StateAbstract > state, const std::size_t &nu, const std::size_t &nr=0)
Initialize the differential action model.
void set_u_ub(const VectorXs &u_ub)
Modify the control upper bounds.
MatrixXs Fu
Jacobian of the dynamics.
VectorXs Lu
Jacobian of the cost function.
const VectorXs & get_u_lb() const
Return the control lower bound.