Crocoddyl
residual-base.hpp
1 // BSD 3-Clause License
3 //
4 // Copyright (C) 2021-2023, 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_RESIDUAL_BASE_HPP_
10 #define CROCODDYL_CORE_RESIDUAL_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/cost-base.hpp"
17 #include "crocoddyl/core/data-collector-base.hpp"
18 #include "crocoddyl/core/fwd.hpp"
19 #include "crocoddyl/core/state-base.hpp"
20 
21 namespace crocoddyl {
22 
43 template <typename _Scalar>
45  public:
46  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
47 
48  typedef _Scalar Scalar;
55  typedef typename MathBase::VectorXs VectorXs;
56  typedef typename MathBase::MatrixXs MatrixXs;
57  typedef typename MathBase::DiagonalMatrixXs DiagonalMatrixXs;
58 
72  ResidualModelAbstractTpl(boost::shared_ptr<StateAbstract> state,
73  const std::size_t nr, const std::size_t nu,
74  const bool q_dependent = true,
75  const bool v_dependent = true,
76  const bool u_dependent = true);
77 
92  ResidualModelAbstractTpl(boost::shared_ptr<StateAbstract> state,
93  const std::size_t nr, const bool q_dependent = true,
94  const bool v_dependent = true,
95  const bool u_dependent = true);
96  virtual ~ResidualModelAbstractTpl();
97 
105  virtual void calc(const boost::shared_ptr<ResidualDataAbstract>& data,
106  const Eigen::Ref<const VectorXs>& x,
107  const Eigen::Ref<const VectorXs>& u);
108 
118  virtual void calc(const boost::shared_ptr<ResidualDataAbstract>& data,
119  const Eigen::Ref<const VectorXs>& x);
120 
131  virtual void calcDiff(const boost::shared_ptr<ResidualDataAbstract>& data,
132  const Eigen::Ref<const VectorXs>& x,
133  const Eigen::Ref<const VectorXs>& u);
134 
145  virtual void calcDiff(const boost::shared_ptr<ResidualDataAbstract>& data,
146  const Eigen::Ref<const VectorXs>& x);
147 
159  virtual boost::shared_ptr<ResidualDataAbstract> createData(
160  DataCollectorAbstract* const data);
161 
174  virtual void calcCostDiff(
175  const boost::shared_ptr<CostDataAbstract>& cdata,
176  const boost::shared_ptr<ResidualDataAbstract>& rdata,
177  const boost::shared_ptr<ActivationDataAbstract>& adata,
178  const bool update_u = true);
179 
183  const boost::shared_ptr<StateAbstract>& get_state() const;
184 
188  std::size_t get_nr() const;
189 
193  std::size_t get_nu() const;
194 
198  bool get_q_dependent() const;
199 
203  bool get_v_dependent() const;
204 
208  bool get_u_dependent() const;
209 
213  template <class Scalar>
214  friend std::ostream& operator<<(
215  std::ostream& os, const ResidualModelAbstractTpl<Scalar>& model);
216 
222  virtual void print(std::ostream& os) const;
223 
224  protected:
225  boost::shared_ptr<StateAbstract> state_;
226  std::size_t nr_;
227  std::size_t nu_;
228  VectorXs unone_;
235 };
236 
237 template <typename _Scalar>
239  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
240 
241  typedef _Scalar Scalar;
244  typedef typename MathBase::VectorXs VectorXs;
245  typedef typename MathBase::MatrixXs MatrixXs;
246 
247  template <template <typename Scalar> class Model>
248  ResidualDataAbstractTpl(Model<Scalar>* const model,
249  DataCollectorAbstract* const data)
250  : shared(data),
251  r(model->get_nr()),
252  Rx(model->get_nr(), model->get_state()->get_ndx()),
253  Ru(model->get_nr(), model->get_nu()),
254  Arr_Rx(model->get_nr(), model->get_state()->get_ndx()),
255  Arr_Ru(model->get_nr(), model->get_nu()) {
256  r.setZero();
257  Rx.setZero();
258  Ru.setZero();
259  Arr_Rx.setZero();
260  Arr_Ru.setZero();
261  }
262  virtual ~ResidualDataAbstractTpl() {}
263 
265  VectorXs r;
266  MatrixXs Rx;
267  MatrixXs Ru;
268  MatrixXs Arr_Rx;
269  MatrixXs Arr_Ru;
270 };
271 
272 } // namespace crocoddyl
273 
274 /* --- Details -------------------------------------------------------------- */
275 /* --- Details -------------------------------------------------------------- */
276 /* --- Details -------------------------------------------------------------- */
277 #include "crocoddyl/core/residual-base.hxx"
278 
279 #endif // CROCODDYL_CORE_RESIDUAL_BASE_HPP_
Abstract class for residual models.
ResidualModelAbstractTpl(boost::shared_ptr< StateAbstract > state, const std::size_t nr, const bool q_dependent=true, const bool v_dependent=true, const bool u_dependent=true)
Initialize the residual model.
bool get_q_dependent() const
Return true if the residual function depends on q.
const boost::shared_ptr< StateAbstract > & get_state() const
Return the state.
virtual void calc(const boost::shared_ptr< ResidualDataAbstract > &data, const Eigen::Ref< const VectorXs > &x)
Compute the residual vector for nodes that depends only on the state.
virtual void calcDiff(const boost::shared_ptr< ResidualDataAbstract > &data, const Eigen::Ref< const VectorXs > &x)
Compute the Jacobian of the residual functions with respect to the state only.
virtual void print(std::ostream &os) const
Print relevant information of the residual model.
ResidualModelAbstractTpl(boost::shared_ptr< StateAbstract > state, const std::size_t nr, const std::size_t nu, const bool q_dependent=true, const bool v_dependent=true, const bool u_dependent=true)
Initialize the residual model.
virtual void calc(const boost::shared_ptr< ResidualDataAbstract > &data, const Eigen::Ref< const VectorXs > &x, const Eigen::Ref< const VectorXs > &u)
Compute the residual vector.
friend std::ostream & operator<<(std::ostream &os, const ResidualModelAbstractTpl< Scalar > &model)
Print information on the residual model.
bool get_v_dependent() const
Return true if the residual function depends on v.
virtual void calcCostDiff(const boost::shared_ptr< CostDataAbstract > &cdata, const boost::shared_ptr< ResidualDataAbstract > &rdata, const boost::shared_ptr< ActivationDataAbstract > &adata, const bool update_u=true)
Compute the derivative of the cost function.
boost::shared_ptr< StateAbstract > state_
State description.
std::size_t nu_
Control dimension.
virtual void calcDiff(const boost::shared_ptr< ResidualDataAbstract > &data, const Eigen::Ref< const VectorXs > &x, const Eigen::Ref< const VectorXs > &u)
Compute the Jacobian of the residual vector.
VectorXs unone_
No control vector.
std::size_t nr_
Residual vector dimension.
virtual boost::shared_ptr< ResidualDataAbstract > createData(DataCollectorAbstract *const data)
Create the residual data.
bool get_u_dependent() const
Return true if the residual function depends on u.
std::size_t get_nr() const
Return the dimension of the residual vector.
std::size_t get_nu() const
Return the dimension of the control input.
Abstract class for the state representation.
Definition: state-base.hpp:46
MatrixXs Ru
Jacobian of the residual vector with respect the control.
MatrixXs Rx
Jacobian of the residual vector with respect the state.
DataCollectorAbstract * shared
Shared data allocated by the action model.