Crocoddyl
residual.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_NUMDIFF_RESIDUAL_HPP_
10 #define CROCODDYL_CORE_NUMDIFF_RESIDUAL_HPP_
11 
12 #include <boost/function.hpp>
13 
14 #include "crocoddyl/core/residual-base.hpp"
15 #include "crocoddyl/multibody/fwd.hpp"
16 
17 namespace crocoddyl {
18 
28 template <typename _Scalar>
30  public:
31  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
32 
33  typedef _Scalar Scalar;
39  typedef typename MathBaseTpl<Scalar>::VectorXs VectorXs;
40  typedef typename MathBaseTpl<Scalar>::MatrixXs MatrixXs;
41  typedef boost::function<void(const VectorXs&, const VectorXs&)>
42  ReevaluationFunction;
43 
50  explicit ResidualModelNumDiffTpl(const boost::shared_ptr<Base>& model);
51 
56 
60  virtual void calc(const boost::shared_ptr<ResidualDataAbstract>& data,
61  const Eigen::Ref<const VectorXs>& x,
62  const Eigen::Ref<const VectorXs>& u);
63 
68  virtual void calc(const boost::shared_ptr<ResidualDataAbstract>& data,
69  const Eigen::Ref<const VectorXs>& x);
70 
74  virtual void calcDiff(const boost::shared_ptr<ResidualDataAbstract>& data,
75  const Eigen::Ref<const VectorXs>& x,
76  const Eigen::Ref<const VectorXs>& u);
77 
83  virtual void calcDiff(const boost::shared_ptr<ResidualDataAbstract>& data,
84  const Eigen::Ref<const VectorXs>& x);
85 
89  virtual boost::shared_ptr<ResidualDataAbstract> createData(
90  DataCollectorAbstract* const data);
91 
95  const boost::shared_ptr<Base>& get_model() const;
96 
101  const Scalar get_disturbance() const;
102 
107  void set_disturbance(const Scalar disturbance);
108 
116  void set_reevals(const std::vector<ReevaluationFunction>& reevals);
117 
118  protected:
119  using Base::nu_;
120  using Base::state_;
121  using Base::unone_;
122 
123  private:
135  void assertStableStateFD(const Eigen::Ref<const VectorXs>& /*x*/);
136 
137  boost::shared_ptr<Base> model_;
139  Scalar e_jac_;
141  std::vector<ReevaluationFunction>
142  reevals_;
143 };
144 
145 template <typename _Scalar>
147  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
148 
149  typedef _Scalar Scalar;
154  typedef typename MathBaseTpl<Scalar>::VectorXs VectorXs;
155 
162  template <template <typename Scalar> class Model>
163  explicit ResidualDataNumDiffTpl(Model<Scalar>* const model,
164  DataCollectorAbstract* const shared_data)
165  : Base(model, shared_data),
166  dx(model->get_state()->get_ndx()),
167  xp(model->get_state()->get_nx()),
168  du(model->get_nu()),
169  up(model->get_nu()) {
170  dx.setZero();
171  xp.setZero();
172  du.setZero();
173  up.setZero();
174 
175  const std::size_t& ndx = model->get_model()->get_state()->get_ndx();
176  const std::size_t& nu = model->get_model()->get_nu();
177  data_0 = model->get_model()->createData(shared_data);
178  for (std::size_t i = 0; i < ndx; ++i) {
179  data_x.push_back(model->get_model()->createData(shared_data));
180  }
181  for (std::size_t i = 0; i < nu; ++i) {
182  data_u.push_back(model->get_model()->createData(shared_data));
183  }
184  }
185 
186  virtual ~ResidualDataNumDiffTpl() {}
187 
188  using Base::r;
189  using Base::Ru;
190  using Base::Rx;
191  using Base::shared;
192 
193  Scalar x_norm;
194  Scalar
196  Scalar
198  VectorXs dx;
199  VectorXs xp;
201  VectorXs du;
202  VectorXs up;
204  boost::shared_ptr<Base> data_0;
205  std::vector<boost::shared_ptr<Base> >
207  std::vector<boost::shared_ptr<Base> >
209 };
210 
211 } // namespace crocoddyl
212 
213 /* --- Details -------------------------------------------------------------- */
214 /* --- Details -------------------------------------------------------------- */
215 /* --- Details -------------------------------------------------------------- */
216 #include "crocoddyl/core/numdiff/residual.hxx"
217 
218 #endif // CROCODDYL_CORE_NUMDIFF_RESIDUAL_HPP_
Abstract class for residual models.
boost::shared_ptr< StateAbstract > state_
State description.
std::size_t nu_
Control dimension.
VectorXs unone_
No control vector.
This class computes the numerical differentiation of a residual model.
Definition: residual.hpp:29
virtual void calc(const boost::shared_ptr< ResidualDataAbstract > &data, const Eigen::Ref< const VectorXs > &x)
const Scalar get_disturbance() const
Return the disturbance constant used by the numerical differentiation routine.
void set_reevals(const std::vector< ReevaluationFunction > &reevals)
Register functions that updates the shared data computed for a system rollout The updated data is use...
virtual void calcDiff(const boost::shared_ptr< ResidualDataAbstract > &data, const Eigen::Ref< const VectorXs > &x)
void set_disturbance(const Scalar disturbance)
Modify the disturbance constant used by the numerical differentiation routine.
ResidualModelNumDiffTpl(const boost::shared_ptr< Base > &model)
Initialize the numdiff 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.
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.
const boost::shared_ptr< Base > & get_model() const
Return the original residual model.
virtual boost::shared_ptr< ResidualDataAbstract > createData(DataCollectorAbstract *const data)
Create the residual data.
virtual ~ResidualModelNumDiffTpl()
Initialize the numdiff residual model.
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.
std::vector< boost::shared_ptr< Base > > data_u
The temporary data associated with the control variation.
Definition: residual.hpp:208
Scalar x_norm
Norm of the state vector.
Definition: residual.hpp:193
boost::shared_ptr< Base > data_0
The data at the approximation point.
Definition: residual.hpp:204
Scalar uh_jac
Disturbance value used for computing .
Definition: residual.hpp:197
Scalar xh_jac
Disturbance value used for computing .
Definition: residual.hpp:195
ResidualDataNumDiffTpl(Model< Scalar > *const model, DataCollectorAbstract *const shared_data)
Initialize the numdiff residual data.
Definition: residual.hpp:163
VectorXs du
Control disturbance.
Definition: residual.hpp:201
std::vector< boost::shared_ptr< Base > > data_x
The temporary data associated with the state variation.
Definition: residual.hpp:206
VectorXs dx
State disturbance.
Definition: residual.hpp:198