Crocoddyl
residual.hpp
1 // BSD 3-Clause License
3 //
4 // Copyright (C) 2021-2025, University of Edinburgh, Heriot-Watt University
5 // Copyright note valid unless otherwise stated in individual files.
6 // All rights reserved.
8 
9 #ifndef CROCODDYL_CORE_COSTS_RESIDUAL_COST_HPP_
10 #define CROCODDYL_CORE_COSTS_RESIDUAL_COST_HPP_
11 
12 #include "crocoddyl/core/cost-base.hpp"
13 #include "crocoddyl/core/fwd.hpp"
14 #include "crocoddyl/core/residual-base.hpp"
15 
16 namespace crocoddyl {
17 
38 template <typename _Scalar>
39 class CostModelResidualTpl : public CostModelAbstractTpl<_Scalar> {
40  public:
41  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
42  CROCODDYL_DERIVED_CAST(CostModelBase, CostModelResidualTpl)
43 
44  typedef _Scalar Scalar;
52  typedef typename MathBase::VectorXs VectorXs;
53  typedef typename MathBase::MatrixXs MatrixXs;
54 
62  CostModelResidualTpl(std::shared_ptr<typename Base::StateAbstract> state,
63  std::shared_ptr<ActivationModelAbstract> activation,
64  std::shared_ptr<ResidualModelAbstract> residual);
74  CostModelResidualTpl(std::shared_ptr<typename Base::StateAbstract> state,
75  std::shared_ptr<ResidualModelAbstract> residual);
76  virtual ~CostModelResidualTpl() = default;
77 
85  virtual void calc(const std::shared_ptr<CostDataAbstract>& data,
86  const Eigen::Ref<const VectorXs>& x,
87  const Eigen::Ref<const VectorXs>& u) override;
88 
98  virtual void calc(const std::shared_ptr<CostDataAbstract>& data,
99  const Eigen::Ref<const VectorXs>& x) override;
100 
108  virtual void calcDiff(const std::shared_ptr<CostDataAbstract>& data,
109  const Eigen::Ref<const VectorXs>& x,
110  const Eigen::Ref<const VectorXs>& u) override;
111 
123  virtual void calcDiff(const std::shared_ptr<CostDataAbstract>& data,
124  const Eigen::Ref<const VectorXs>& x) override;
125 
129  virtual std::shared_ptr<CostDataAbstract> createData(
130  DataCollectorAbstract* const data) override;
131 
141  template <typename NewScalar>
143 
149  virtual void print(std::ostream& os) const override;
150 
151  protected:
152  using Base::activation_;
153  using Base::nu_;
154  using Base::residual_;
155  using Base::state_;
156  using Base::unone_;
157 };
158 
159 template <typename _Scalar>
160 struct CostDataResidualTpl : public CostDataAbstractTpl<_Scalar> {
161  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
162 
163  typedef _Scalar Scalar;
167 
168  template <template <typename Scalar> class Model>
169  CostDataResidualTpl(Model<Scalar>* const model,
170  DataCollectorAbstract* const data)
171  : Base(model, data) {}
172  virtual ~CostDataResidualTpl() = default;
173 
174  using Base::activation;
175  using Base::cost;
176  using Base::Lu;
177  using Base::Luu;
178  using Base::Lx;
179  using Base::Lxu;
180  using Base::Lxx;
181  using Base::residual;
182  using Base::shared;
183 };
184 
185 } // namespace crocoddyl
186 
187 /* --- Details -------------------------------------------------------------- */
188 /* --- Details -------------------------------------------------------------- */
189 /* --- Details -------------------------------------------------------------- */
190 #include "crocoddyl/core/costs/residual.hxx"
191 
192 CROCODDYL_DECLARE_EXTERN_TEMPLATE_CLASS(crocoddyl::CostModelResidualTpl)
193 CROCODDYL_DECLARE_EXTERN_TEMPLATE_STRUCT(crocoddyl::CostDataResidualTpl)
194 
195 #endif // CROCODDYL_CORE_COSTS_RESIDUAL_COST_HPP_
Abstract class for cost models.
Definition: cost-base.hpp:64
std::shared_ptr< ActivationModelAbstract > activation_
Activation model.
Definition: cost-base.hpp:276
std::shared_ptr< StateAbstract > state_
State description.
Definition: cost-base.hpp:275
std::size_t nu_
Control dimension.
Definition: cost-base.hpp:278
VectorXs unone_
No control vector.
Definition: cost-base.hpp:279
std::shared_ptr< ResidualModelAbstract > residual_
Residual model.
Definition: cost-base.hpp:277
Residual-based cost.
Definition: residual.hpp:39
CostModelResidualTpl(std::shared_ptr< typename Base::StateAbstract > state, std::shared_ptr< ResidualModelAbstract > residual)
Initialize the residual cost model.
virtual void calc(const std::shared_ptr< CostDataAbstract > &data, const Eigen::Ref< const VectorXs > &x) override
Compute the residual cost based on state only.
CostModelResidualTpl(std::shared_ptr< typename Base::StateAbstract > state, std::shared_ptr< ActivationModelAbstract > activation, std::shared_ptr< ResidualModelAbstract > residual)
Initialize the residual cost model.
virtual void calcDiff(const std::shared_ptr< CostDataAbstract > &data, const Eigen::Ref< const VectorXs > &x) override
Compute the derivatives of the residual cost with respect to the state only.
virtual void calcDiff(const std::shared_ptr< CostDataAbstract > &data, const Eigen::Ref< const VectorXs > &x, const Eigen::Ref< const VectorXs > &u) override
Compute the derivatives of the residual cost.
CostModelResidualTpl< NewScalar > cast() const
Cast the residual cost model to a different scalar type.
virtual std::shared_ptr< CostDataAbstract > createData(DataCollectorAbstract *const data) override
Create the residual cost data.
virtual void calc(const std::shared_ptr< CostDataAbstract > &data, const Eigen::Ref< const VectorXs > &x, const Eigen::Ref< const VectorXs > &u) override
Compute the residual cost.
virtual void print(std::ostream &os) const override
Print relevant information of the cost-residual model.
Abstract class for residual models.