Crocoddyl
cost-base.hpp
1 // BSD 3-Clause License
3 //
4 // Copyright (C) 2019-2021, LAAS-CNRS, University of Edinburgh
5 // Copyright note valid unless otherwise stated in individual files.
6 // All rights reserved.
8 
9 #ifndef CROCODDYL_CORE_COST_BASE_HPP_
10 #define CROCODDYL_CORE_COST_BASE_HPP_
11 
12 #include <boost/make_shared.hpp>
13 #include <boost/shared_ptr.hpp>
14 
15 #include "crocoddyl/core/activation-base.hpp"
16 #include "crocoddyl/core/activations/quadratic.hpp"
17 #include "crocoddyl/core/data-collector-base.hpp"
18 #include "crocoddyl/core/fwd.hpp"
19 #include "crocoddyl/core/residual-base.hpp"
20 #include "crocoddyl/core/state-base.hpp"
21 #include "crocoddyl/core/utils/deprecate.hpp"
22 
23 namespace crocoddyl {
24 
58 template <typename _Scalar>
60  public:
61  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
62 
63  typedef _Scalar Scalar;
71  typedef typename MathBase::VectorXs VectorXs;
72  typedef typename MathBase::MatrixXs MatrixXs;
73 
81  CostModelAbstractTpl(boost::shared_ptr<StateAbstract> state,
82  boost::shared_ptr<ActivationModelAbstract> activation,
83  boost::shared_ptr<ResidualModelAbstract> residual);
84 
92  CostModelAbstractTpl(boost::shared_ptr<StateAbstract> state,
93  boost::shared_ptr<ActivationModelAbstract> activation,
94  const std::size_t nu);
95 
104  CostModelAbstractTpl(boost::shared_ptr<StateAbstract> state,
105  boost::shared_ptr<ActivationModelAbstract> activation);
106 
116  CostModelAbstractTpl(boost::shared_ptr<StateAbstract> state,
117  boost::shared_ptr<ResidualModelAbstract> residual);
118 
129  CostModelAbstractTpl(boost::shared_ptr<StateAbstract> state,
130  const std::size_t nr, const std::size_t nu);
131 
143  CostModelAbstractTpl(boost::shared_ptr<StateAbstract> state,
144  const std::size_t nr);
145  virtual ~CostModelAbstractTpl();
146 
154  virtual void calc(const boost::shared_ptr<CostDataAbstract>& data,
155  const Eigen::Ref<const VectorXs>& x,
156  const Eigen::Ref<const VectorXs>& u) = 0;
157 
168  virtual void calc(const boost::shared_ptr<CostDataAbstract>& data,
169  const Eigen::Ref<const VectorXs>& x);
170 
181  virtual void calcDiff(const boost::shared_ptr<CostDataAbstract>& data,
182  const Eigen::Ref<const VectorXs>& x,
183  const Eigen::Ref<const VectorXs>& u) = 0;
184 
196  virtual void calcDiff(const boost::shared_ptr<CostDataAbstract>& data,
197  const Eigen::Ref<const VectorXs>& x);
198 
210  virtual boost::shared_ptr<CostDataAbstract> createData(
211  DataCollectorAbstract* const data);
212 
216  const boost::shared_ptr<StateAbstract>& get_state() const;
217 
221  const boost::shared_ptr<ActivationModelAbstract>& get_activation() const;
222 
226  const boost::shared_ptr<ResidualModelAbstract>& get_residual() const;
227 
231  std::size_t get_nu() const;
232 
236  template <class Scalar>
237  friend std::ostream& operator<<(std::ostream& os,
238  const CostModelAbstractTpl<Scalar>& model);
239 
243  template <class ReferenceType>
244  void set_reference(ReferenceType ref);
245 
249  template <class ReferenceType>
250  ReferenceType get_reference();
251 
257  virtual void print(std::ostream& os) const;
258 
259  protected:
263  virtual void set_referenceImpl(const std::type_info&, const void*);
264 
268  virtual void get_referenceImpl(const std::type_info&, void*);
269 
270  boost::shared_ptr<StateAbstract> state_;
271  boost::shared_ptr<ActivationModelAbstract> activation_;
272  boost::shared_ptr<ResidualModelAbstract> residual_;
273  std::size_t nu_;
274  VectorXs unone_;
275 };
276 
277 template <typename _Scalar>
279  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
280 
281  typedef _Scalar Scalar;
286  typedef typename MathBase::VectorXs VectorXs;
287  typedef typename MathBase::MatrixXs MatrixXs;
288 
289  template <template <typename Scalar> class Model>
290  CostDataAbstractTpl(Model<Scalar>* const model,
291  DataCollectorAbstract* const data)
292  : shared(data),
293  activation(model->get_activation()->createData()),
294  residual(model->get_residual()->createData(data)),
295  cost(Scalar(0.)),
296  Lx(model->get_state()->get_ndx()),
297  Lu(model->get_nu()),
298  Lxx(model->get_state()->get_ndx(), model->get_state()->get_ndx()),
299  Lxu(model->get_state()->get_ndx(), model->get_nu()),
300  Luu(model->get_nu(), model->get_nu()) {
301  Lx.setZero();
302  Lu.setZero();
303  Lxx.setZero();
304  Lxu.setZero();
305  Luu.setZero();
306  }
307  virtual ~CostDataAbstractTpl() {}
308 
309  DEPRECATED(
310  "Use residual.r", const VectorXs& get_r() const { return residual->r; };)
311  DEPRECATED(
312  "Use residual.Rx",
313  const MatrixXs& get_Rx() const { return residual->Rx; };)
314  DEPRECATED(
315  "Use residual.Ru",
316  const MatrixXs& get_Ru() const { return residual->Ru; };)
317  DEPRECATED(
318  "Use residual.r", void set_r(const VectorXs& r) { residual->r = r; };)
319  DEPRECATED(
320  "Use residual.Rx",
321  void set_Rx(const MatrixXs& Rx) { residual->Rx = Rx; };)
322  DEPRECATED(
323  "Use residual.Ru",
324  void set_Ru(const MatrixXs& Ru) { residual->Ru = Ru; };)
325 
326  DataCollectorAbstract* shared;
327  boost::shared_ptr<ActivationDataAbstract> activation;
328  boost::shared_ptr<ResidualDataAbstract> residual;
329  Scalar cost;
330  VectorXs Lx;
331  VectorXs Lu;
332  MatrixXs Lxx;
333  MatrixXs Lxu;
334  MatrixXs Luu;
335 };
336 
337 } // namespace crocoddyl
338 
339 /* --- Details -------------------------------------------------------------- */
340 /* --- Details -------------------------------------------------------------- */
341 /* --- Details -------------------------------------------------------------- */
342 #include "crocoddyl/core/cost-base.hxx"
343 
344 #endif // CROCODDYL_CORE_COST_BASE_HPP_
Abstract class for cost models.
Definition: cost-base.hpp:59
CostModelAbstractTpl(boost::shared_ptr< StateAbstract > state, const std::size_t nr, const std::size_t nu)
Initialize the cost model.
virtual boost::shared_ptr< CostDataAbstract > createData(DataCollectorAbstract *const data)
Create the cost data.
CostModelAbstractTpl(boost::shared_ptr< StateAbstract > state, boost::shared_ptr< ActivationModelAbstract > activation, boost::shared_ptr< ResidualModelAbstract > residual)
Initialize the cost model.
const boost::shared_ptr< StateAbstract > & get_state() const
Return the state.
CostModelAbstractTpl(boost::shared_ptr< StateAbstract > state, boost::shared_ptr< ActivationModelAbstract > activation)
Initialize the cost model.
virtual void set_referenceImpl(const std::type_info &, const void *)
Modify the cost reference.
void set_reference(ReferenceType ref)
Modify the cost reference.
virtual void calcDiff(const boost::shared_ptr< CostDataAbstract > &data, const Eigen::Ref< const VectorXs > &x)
Compute the Jacobian and Hessian of the cost functions with respect to the state only.
virtual void print(std::ostream &os) const
Print relevant information of the cost model.
friend std::ostream & operator<<(std::ostream &os, const CostModelAbstractTpl< Scalar > &model)
Print information on the cost model.
const boost::shared_ptr< ActivationModelAbstract > & get_activation() const
Return the activation model.
boost::shared_ptr< ActivationModelAbstract > activation_
Activation model.
Definition: cost-base.hpp:271
virtual void calc(const boost::shared_ptr< CostDataAbstract > &data, const Eigen::Ref< const VectorXs > &x, const Eigen::Ref< const VectorXs > &u)=0
Compute the cost value and its residual vector.
virtual void get_referenceImpl(const std::type_info &, void *)
Return the cost reference.
boost::shared_ptr< StateAbstract > state_
State description.
Definition: cost-base.hpp:270
std::size_t nu_
Control dimension.
Definition: cost-base.hpp:273
VectorXs unone_
No control vector.
Definition: cost-base.hpp:274
CostModelAbstractTpl(boost::shared_ptr< StateAbstract > state, boost::shared_ptr< ActivationModelAbstract > activation, const std::size_t nu)
Initialize the cost model.
CostModelAbstractTpl(boost::shared_ptr< StateAbstract > state, const std::size_t nr)
Initialize the cost model.
virtual void calcDiff(const boost::shared_ptr< CostDataAbstract > &data, const Eigen::Ref< const VectorXs > &x, const Eigen::Ref< const VectorXs > &u)=0
Compute the Jacobian and Hessian of cost and its residual vector.
ReferenceType get_reference()
Return the cost reference.
boost::shared_ptr< ResidualModelAbstract > residual_
Residual model.
Definition: cost-base.hpp:272
virtual void calc(const boost::shared_ptr< CostDataAbstract > &data, const Eigen::Ref< const VectorXs > &x)
Compute the total cost value for nodes that depends only on the state.
std::size_t get_nu() const
Return the dimension of the control input.
CostModelAbstractTpl(boost::shared_ptr< StateAbstract > state, boost::shared_ptr< ResidualModelAbstract > residual)
Initialize the cost model.
const boost::shared_ptr< ResidualModelAbstract > & get_residual() const
Return the residual model.
Abstract class for residual models.
Abstract class for the state representation.
Definition: state-base.hpp:46