Crocoddyl
cost-base.hpp
1 // BSD 3-Clause License
3 //
4 // Copyright (C) 2019-2025, LAAS-CNRS, University of Edinburgh,
5 // Heriot-Watt University
6 // Copyright note valid unless otherwise stated in individual files.
7 // All rights reserved.
9 
10 #ifndef CROCODDYL_CORE_COST_BASE_HPP_
11 #define CROCODDYL_CORE_COST_BASE_HPP_
12 
13 #include "crocoddyl/core/activation-base.hpp"
14 #include "crocoddyl/core/activations/quadratic.hpp"
15 #include "crocoddyl/core/data-collector-base.hpp"
16 #include "crocoddyl/core/fwd.hpp"
17 #include "crocoddyl/core/residual-base.hpp"
18 #include "crocoddyl/core/state-base.hpp"
19 #include "crocoddyl/core/utils/deprecate.hpp"
20 
21 namespace crocoddyl {
22 
24  public:
25  virtual ~CostModelBase() = default;
26 
27  CROCODDYL_BASE_CAST(CostModelBase, CostModelAbstractTpl)
28 };
29 
63 template <typename _Scalar>
65  public:
66  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
67 
68  typedef _Scalar Scalar;
76  typedef typename MathBase::VectorXs VectorXs;
77  typedef typename MathBase::MatrixXs MatrixXs;
78 
86  CostModelAbstractTpl(std::shared_ptr<StateAbstract> state,
87  std::shared_ptr<ActivationModelAbstract> activation,
88  std::shared_ptr<ResidualModelAbstract> residual);
89 
97  CostModelAbstractTpl(std::shared_ptr<StateAbstract> state,
98  std::shared_ptr<ActivationModelAbstract> activation,
99  const std::size_t nu);
100 
109  CostModelAbstractTpl(std::shared_ptr<StateAbstract> state,
110  std::shared_ptr<ActivationModelAbstract> activation);
111 
121  CostModelAbstractTpl(std::shared_ptr<StateAbstract> state,
122  std::shared_ptr<ResidualModelAbstract> residual);
123 
134  CostModelAbstractTpl(std::shared_ptr<StateAbstract> state,
135  const std::size_t nr, const std::size_t nu);
136 
148  CostModelAbstractTpl(std::shared_ptr<StateAbstract> state,
149  const std::size_t nr);
150  virtual ~CostModelAbstractTpl() = default;
151 
159  virtual void calc(const std::shared_ptr<CostDataAbstract>& data,
160  const Eigen::Ref<const VectorXs>& x,
161  const Eigen::Ref<const VectorXs>& u) = 0;
162 
173  virtual void calc(const std::shared_ptr<CostDataAbstract>& data,
174  const Eigen::Ref<const VectorXs>& x);
175 
186  virtual void calcDiff(const std::shared_ptr<CostDataAbstract>& data,
187  const Eigen::Ref<const VectorXs>& x,
188  const Eigen::Ref<const VectorXs>& u) = 0;
189 
201  virtual void calcDiff(const std::shared_ptr<CostDataAbstract>& data,
202  const Eigen::Ref<const VectorXs>& x);
203 
215  virtual std::shared_ptr<CostDataAbstract> createData(
216  DataCollectorAbstract* const data);
217 
221  const std::shared_ptr<StateAbstract>& get_state() const;
222 
226  const std::shared_ptr<ActivationModelAbstract>& get_activation() const;
227 
231  const std::shared_ptr<ResidualModelAbstract>& get_residual() const;
232 
236  std::size_t get_nu() const;
237 
241  template <class Scalar>
242  friend std::ostream& operator<<(std::ostream& os,
243  const CostModelAbstractTpl<Scalar>& model);
244 
248  template <class ReferenceType>
249  void set_reference(ReferenceType ref);
250 
254  template <class ReferenceType>
255  ReferenceType get_reference();
256 
262  virtual void print(std::ostream& os) const;
263 
264  protected:
268  virtual void set_referenceImpl(const std::type_info&, const void*);
269 
273  virtual void get_referenceImpl(const std::type_info&, void*);
274 
275  std::shared_ptr<StateAbstract> state_;
276  std::shared_ptr<ActivationModelAbstract> activation_;
277  std::shared_ptr<ResidualModelAbstract> residual_;
278  std::size_t nu_;
279  VectorXs unone_;
281  : state_(nullptr), activation_(nullptr), residual_(nullptr), nu_(0) {}
282 };
283 
284 template <typename _Scalar>
286  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
287 
288  typedef _Scalar Scalar;
293  typedef typename MathBase::VectorXs VectorXs;
294  typedef typename MathBase::MatrixXs MatrixXs;
295 
296  template <template <typename Scalar> class Model>
297  CostDataAbstractTpl(Model<Scalar>* const model,
298  DataCollectorAbstract* const data)
299  : shared(data),
300  activation(model->get_activation()->createData()),
301  residual(model->get_residual()->createData(data)),
302  cost(Scalar(0.)),
303  Lx(model->get_state()->get_ndx()),
304  Lu(model->get_nu()),
305  Lxx(model->get_state()->get_ndx(), model->get_state()->get_ndx()),
306  Lxu(model->get_state()->get_ndx(), model->get_nu()),
307  Luu(model->get_nu(), model->get_nu()) {
308  Lx.setZero();
309  Lu.setZero();
310  Lxx.setZero();
311  Lxu.setZero();
312  Luu.setZero();
313  }
314  virtual ~CostDataAbstractTpl() = default;
315 
316  DEPRECATED(
317  "Use residual.r", const VectorXs& get_r() const { return residual->r; };)
318  DEPRECATED(
319  "Use residual.Rx",
320  const MatrixXs& get_Rx() const { return residual->Rx; };)
321  DEPRECATED(
322  "Use residual.Ru",
323  const MatrixXs& get_Ru() const { return residual->Ru; };)
324  DEPRECATED(
325  "Use residual.r", void set_r(const VectorXs& r) { residual->r = r; };)
326  DEPRECATED(
327  "Use residual.Rx",
328  void set_Rx(const MatrixXs& Rx) { residual->Rx = Rx; };)
329  DEPRECATED(
330  "Use residual.Ru",
331  void set_Ru(const MatrixXs& Ru) { residual->Ru = Ru; };)
332 
333  DataCollectorAbstract* shared;
334  std::shared_ptr<ActivationDataAbstract> activation;
335  std::shared_ptr<ResidualDataAbstract> residual;
336  Scalar cost;
337  VectorXs Lx;
338  VectorXs Lu;
339  MatrixXs Lxx;
340  MatrixXs Lxu;
341  MatrixXs Luu;
342 };
343 
344 } // namespace crocoddyl
345 
346 /* --- Details -------------------------------------------------------------- */
347 /* --- Details -------------------------------------------------------------- */
348 /* --- Details -------------------------------------------------------------- */
349 #include "crocoddyl/core/cost-base.hxx"
350 
351 CROCODDYL_DECLARE_EXTERN_TEMPLATE_CLASS(crocoddyl::CostModelAbstractTpl)
352 CROCODDYL_DECLARE_EXTERN_TEMPLATE_STRUCT(crocoddyl::CostDataAbstractTpl)
353 
354 #endif // CROCODDYL_CORE_COST_BASE_HPP_
Abstract class for cost models.
Definition: cost-base.hpp:64
std::shared_ptr< ActivationModelAbstract > activation_
Activation model.
Definition: cost-base.hpp:276
virtual std::shared_ptr< CostDataAbstract > createData(DataCollectorAbstract *const data)
Create the cost data.
virtual void calcDiff(const std::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.
const std::shared_ptr< ActivationModelAbstract > & get_activation() const
Return the activation model.
virtual void calc(const std::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.
std::shared_ptr< StateAbstract > state_
State description.
Definition: cost-base.hpp:275
virtual void set_referenceImpl(const std::type_info &, const void *)
Modify the cost reference.
void set_reference(ReferenceType ref)
Modify the cost reference.
CostModelAbstractTpl(std::shared_ptr< StateAbstract > state, const std::size_t nr)
virtual void calc(const std::shared_ptr< CostDataAbstract > &data, const Eigen::Ref< const VectorXs > &x)
Compute the total cost value for nodes that depends only on the state.
CostModelAbstractTpl(std::shared_ptr< StateAbstract > state, std::shared_ptr< ActivationModelAbstract > activation, std::shared_ptr< ResidualModelAbstract > residual)
Initialize the cost model.
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.
CostModelAbstractTpl(std::shared_ptr< StateAbstract > state, std::shared_ptr< ResidualModelAbstract > residual)
CostModelAbstractTpl(std::shared_ptr< StateAbstract > state, const std::size_t nr, const std::size_t nu)
CostModelAbstractTpl(std::shared_ptr< StateAbstract > state, std::shared_ptr< ActivationModelAbstract > activation, const std::size_t nu)
Initialize the cost model.
virtual void get_referenceImpl(const std::type_info &, void *)
Return the cost reference.
const std::shared_ptr< StateAbstract > & get_state() const
Return the state.
std::size_t nu_
Control dimension.
Definition: cost-base.hpp:278
VectorXs unone_
No control vector.
Definition: cost-base.hpp:279
const std::shared_ptr< ResidualModelAbstract > & get_residual() const
Return the residual model.
ReferenceType get_reference()
Return the cost reference.
CostModelAbstractTpl(std::shared_ptr< StateAbstract > state, std::shared_ptr< ActivationModelAbstract > activation)
std::shared_ptr< ResidualModelAbstract > residual_
Residual model.
Definition: cost-base.hpp:277
virtual void calcDiff(const std::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.
std::size_t get_nu() const
Return the dimension of the control input.
Abstract class for residual models.
Abstract class for the state representation.
Definition: state-base.hpp:48