Crocoddyl
 
Loading...
Searching...
No Matches
residual-base.hpp
1
2// 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_RESIDUAL_BASE_HPP_
10#define CROCODDYL_CORE_RESIDUAL_BASE_HPP_
11
12#include "crocoddyl/core/activation-base.hpp"
13#include "crocoddyl/core/cost-base.hpp"
14#include "crocoddyl/core/data-collector-base.hpp"
15#include "crocoddyl/core/fwd.hpp"
16#include "crocoddyl/core/state-base.hpp"
17
18namespace crocoddyl {
19
21 public:
22 virtual ~ResidualModelBase() = default;
23
25};
26
47template <typename _Scalar>
49 public:
50 EIGEN_MAKE_ALIGNED_OPERATOR_NEW
51 CROCODDYL_BASE_DERIVED_CAST(ResidualModelBase, ResidualModelAbstractTpl)
52
53 typedef _Scalar Scalar;
60 typedef typename MathBase::VectorXs VectorXs;
61 typedef typename MathBase::MatrixXs MatrixXs;
62 typedef typename MathBase::DiagonalMatrixXs DiagonalMatrixXs;
63
77 ResidualModelAbstractTpl(std::shared_ptr<StateAbstract> state,
78 const std::size_t nr, const std::size_t nu,
79 const bool q_dependent = true,
80 const bool v_dependent = true,
81 const bool u_dependent = true);
82
97 ResidualModelAbstractTpl(std::shared_ptr<StateAbstract> state,
98 const std::size_t nr, const bool q_dependent = true,
99 const bool v_dependent = true,
100 const bool u_dependent = true);
101 virtual ~ResidualModelAbstractTpl() = default;
102
110 virtual void calc(const std::shared_ptr<ResidualDataAbstract>& data,
111 const Eigen::Ref<const VectorXs>& x,
112 const Eigen::Ref<const VectorXs>& u);
113
123 virtual void calc(const std::shared_ptr<ResidualDataAbstract>& data,
124 const Eigen::Ref<const VectorXs>& x);
125
136 virtual void calcDiff(const std::shared_ptr<ResidualDataAbstract>& data,
137 const Eigen::Ref<const VectorXs>& x,
138 const Eigen::Ref<const VectorXs>& u);
139
150 virtual void calcDiff(const std::shared_ptr<ResidualDataAbstract>& data,
151 const Eigen::Ref<const VectorXs>& x);
152
164 virtual std::shared_ptr<ResidualDataAbstract> createData(
165 DataCollectorAbstract* const data);
166
179 virtual void calcCostDiff(
180 const std::shared_ptr<CostDataAbstract>& cdata,
181 const std::shared_ptr<ResidualDataAbstract>& rdata,
182 const std::shared_ptr<ActivationDataAbstract>& adata,
183 const bool update_u = true);
184
188 const std::shared_ptr<StateAbstract>& get_state() const;
189
193 std::size_t get_nr() const;
194
198 std::size_t get_nu() const;
199
203 bool get_q_dependent() const;
204
208 bool get_v_dependent() const;
209
213 bool get_u_dependent() const;
214
218 template <class Scalar>
219 friend std::ostream& operator<<(
220 std::ostream& os, const ResidualModelAbstractTpl<Scalar>& model);
221
227 virtual void print(std::ostream& os) const;
228
229 protected:
230 std::shared_ptr<StateAbstract> state_;
231 std::size_t nr_;
232 std::size_t nu_;
233 VectorXs unone_;
241 : state_(nullptr),
242 nr_(0),
243 nu_(0),
244 q_dependent_(false),
245 v_dependent_(false),
246 u_dependent_(false) {};
247};
248
249template <typename _Scalar>
251 EIGEN_MAKE_ALIGNED_OPERATOR_NEW
252
253 typedef _Scalar Scalar;
256 typedef typename MathBase::VectorXs VectorXs;
257 typedef typename MathBase::MatrixXs MatrixXs;
258
259 template <template <typename Scalar> class Model>
260 ResidualDataAbstractTpl(Model<Scalar>* const model,
261 DataCollectorAbstract* const data)
262 : shared(data),
263 r(model->get_nr()),
264 Rx(model->get_nr(), model->get_state()->get_ndx()),
265 Ru(model->get_nr(), model->get_nu()),
266 Arr_Rx(model->get_nr(), model->get_state()->get_ndx()),
267 Arr_Ru(model->get_nr(), model->get_nu()) {
268 r.setZero();
269 Rx.setZero();
270 Ru.setZero();
271 Arr_Rx.setZero();
272 Arr_Ru.setZero();
273 }
274 virtual ~ResidualDataAbstractTpl() = default;
275
277 VectorXs r;
278 MatrixXs Rx;
279 MatrixXs Ru;
280 MatrixXs Arr_Rx;
281 MatrixXs Arr_Ru;
282};
283
284} // namespace crocoddyl
285
286/* --- Details -------------------------------------------------------------- */
287/* --- Details -------------------------------------------------------------- */
288/* --- Details -------------------------------------------------------------- */
289#include "crocoddyl/core/residual-base.hxx"
290
291CROCODDYL_DECLARE_EXTERN_TEMPLATE_CLASS(crocoddyl::ResidualModelAbstractTpl)
292CROCODDYL_DECLARE_EXTERN_TEMPLATE_STRUCT(crocoddyl::ResidualDataAbstractTpl)
293
294#endif // CROCODDYL_CORE_RESIDUAL_BASE_HPP_
Abstract class for residual models.
friend std::ostream & operator<<(std::ostream &os, const ResidualModelAbstractTpl< Scalar > &model)
Print information on the residual model.
bool get_q_dependent() const
Return true if the residual function depends on q.
virtual void calcDiff(const std::shared_ptr< ResidualDataAbstract > &data, const Eigen::Ref< const VectorXs > &x, const Eigen::Ref< const VectorXs > &u)
Compute the Jacobian of the residual vector.
std::shared_ptr< StateAbstract > state_
State description.
ResidualModelAbstractTpl(std::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 print(std::ostream &os) const
Print relevant information of the residual model.
virtual void calcDiff(const std::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 calcCostDiff(const std::shared_ptr< CostDataAbstract > &cdata, const std::shared_ptr< ResidualDataAbstract > &rdata, const std::shared_ptr< ActivationDataAbstract > &adata, const bool update_u=true)
Compute the derivative of the cost function.
virtual void calc(const std::shared_ptr< ResidualDataAbstract > &data, const Eigen::Ref< const VectorXs > &x, const Eigen::Ref< const VectorXs > &u)
Compute the residual vector.
bool get_v_dependent() const
Return true if the residual function depends on v.
virtual std::shared_ptr< ResidualDataAbstract > createData(DataCollectorAbstract *const data)
Create the residual data.
virtual void calc(const std::shared_ptr< ResidualDataAbstract > &data, const Eigen::Ref< const VectorXs > &x)
Compute the residual vector for nodes that depends only on the state.
std::size_t nu_
Control dimension.
ResidualModelAbstractTpl(std::shared_ptr< StateAbstract > state, const std::size_t nr, const bool q_dependent=true, const bool v_dependent=true, const bool u_dependent=true)
VectorXs unone_
No control vector.
const std::shared_ptr< StateAbstract > & get_state() const
Return the state.
std::size_t nr_
Residual vector dimension.
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.
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.