Crocoddyl
cost.hpp
1 // BSD 3-Clause License
3 //
4 // Copyright (C) 2019-2023, LAAS-CNRS, University of Edinburgh, New York
5 // University,
6 // Max Planck Gesellschaft,
7 // Heriot-Watt University
8 // Copyright note valid unless otherwise stated in individual files.
9 // All rights reserved.
11 
12 #ifndef CROCODDYL_CORE_NUMDIFF_COST_HPP_
13 #define CROCODDYL_CORE_NUMDIFF_COST_HPP_
14 
15 #include <boost/function.hpp>
16 
17 #include "crocoddyl/core/cost-base.hpp"
18 #include "crocoddyl/multibody/fwd.hpp"
19 
20 namespace crocoddyl {
21 
33 template <typename _Scalar>
34 class CostModelNumDiffTpl : public CostModelAbstractTpl<_Scalar> {
35  public:
36  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
37 
38  typedef _Scalar Scalar;
44  typedef typename MathBaseTpl<Scalar>::VectorXs VectorXs;
45  typedef typename MathBaseTpl<Scalar>::MatrixXs MatrixXs;
46  typedef boost::function<void(const VectorXs&, const VectorXs&)>
47  ReevaluationFunction;
48 
55  explicit CostModelNumDiffTpl(const boost::shared_ptr<Base>& model);
56 
61 
65  virtual void calc(const boost::shared_ptr<CostDataAbstract>& data,
66  const Eigen::Ref<const VectorXs>& x,
67  const Eigen::Ref<const VectorXs>& u);
68 
73  virtual void calc(const boost::shared_ptr<CostDataAbstract>& data,
74  const Eigen::Ref<const VectorXs>& x);
75 
79  virtual void calcDiff(const boost::shared_ptr<CostDataAbstract>& data,
80  const Eigen::Ref<const VectorXs>& x,
81  const Eigen::Ref<const VectorXs>& u);
82 
87  virtual void calcDiff(const boost::shared_ptr<CostDataAbstract>& data,
88  const Eigen::Ref<const VectorXs>& x);
89 
96  virtual boost::shared_ptr<CostDataAbstract> createData(
97  DataCollectorAbstract* const data);
98 
102  const boost::shared_ptr<Base>& get_model() const;
103 
108  const Scalar get_disturbance() const;
109 
114  void set_disturbance(const Scalar disturbance);
115 
123 
131  void set_reevals(const std::vector<ReevaluationFunction>& reevals);
132 
133  protected:
134  using Base::activation_;
135  using Base::nu_;
136  using Base::state_;
137  using Base::unone_;
138 
139  private:
151  void assertStableStateFD(const Eigen::Ref<const VectorXs>& /*x*/);
152 
153  boost::shared_ptr<Base> model_;
155  Scalar e_jac_;
157  std::vector<ReevaluationFunction>
158  reevals_;
159 };
160 
161 template <typename _Scalar>
162 struct CostDataNumDiffTpl : public CostDataAbstractTpl<_Scalar> {
163  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
164 
165  typedef _Scalar Scalar;
170  typedef typename MathBaseTpl<Scalar>::VectorXs VectorXs;
171 
178  template <template <typename Scalar> class Model>
179  explicit CostDataNumDiffTpl(Model<Scalar>* const model,
180  DataCollectorAbstract* const shared_data)
181  : Base(model, shared_data),
182  dx(model->get_state()->get_ndx()),
183  xp(model->get_state()->get_nx()),
184  du(model->get_nu()),
185  up(model->get_nu()) {
186  dx.setZero();
187  xp.setZero();
188  du.setZero();
189  up.setZero();
190 
191  const std::size_t ndx = model->get_model()->get_state()->get_ndx();
192  const std::size_t nu = model->get_model()->get_nu();
193  data_0 = model->get_model()->createData(shared_data);
194  for (std::size_t i = 0; i < ndx; ++i) {
195  data_x.push_back(model->get_model()->createData(shared_data));
196  }
197  for (std::size_t i = 0; i < nu; ++i) {
198  data_u.push_back(model->get_model()->createData(shared_data));
199  }
200  }
201 
202  virtual ~CostDataNumDiffTpl() {}
203 
204  using Base::activation;
205  using Base::cost;
206  using Base::Lu;
207  using Base::Luu;
208  using Base::Lx;
209  using Base::Lxu;
210  using Base::Lxx;
211  using Base::residual;
212  using Base::shared;
213 
214  Scalar x_norm;
215  Scalar
217  Scalar
219  VectorXs dx;
220  VectorXs xp;
222  VectorXs du;
223  VectorXs up;
225  boost::shared_ptr<Base> data_0;
226  std::vector<boost::shared_ptr<Base> >
228  std::vector<boost::shared_ptr<Base> >
230 };
231 
232 } // namespace crocoddyl
233 
234 /* --- Details -------------------------------------------------------------- */
235 /* --- Details -------------------------------------------------------------- */
236 /* --- Details -------------------------------------------------------------- */
237 #include "crocoddyl/core/numdiff/cost.hxx"
238 
239 #endif // CROCODDYL_CORE_NUMDIFF_COST_HPP_
Abstract class for cost models.
Definition: cost-base.hpp:59
boost::shared_ptr< ActivationModelAbstract > activation_
Activation model.
Definition: cost-base.hpp:271
boost::shared_ptr< StateAbstract > state_
State description.
Definition: cost-base.hpp:270
std::size_t nu_
Control dimension.
Definition: cost-base.hpp:273
VectorXs unone_
No control vector.
Definition: cost-base.hpp:274
This class computes the numerical differentiation of a cost model.
Definition: cost.hpp:34
virtual boost::shared_ptr< CostDataAbstract > createData(DataCollectorAbstract *const data)
Create a numdiff cost data.
virtual void calcDiff(const boost::shared_ptr< CostDataAbstract > &data, const Eigen::Ref< const VectorXs > &x)
const Scalar get_disturbance() const
Return the disturbance constant used by the numerical differentiation routine.
bool get_with_gauss_approx()
Identify if the Gauss approximation is going to be used or not.
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 ~CostModelNumDiffTpl()
Initialize the numdiff cost model.
void set_disturbance(const Scalar disturbance)
Modify the disturbance constant used by the numerical differentiation routine.
virtual void calc(const boost::shared_ptr< CostDataAbstract > &data, const Eigen::Ref< const VectorXs > &x, const Eigen::Ref< const VectorXs > &u)
Compute the cost value and its residual vector.
CostModelNumDiffTpl(const boost::shared_ptr< Base > &model)
Initialize the numdiff cost model.
virtual void calcDiff(const boost::shared_ptr< CostDataAbstract > &data, const Eigen::Ref< const VectorXs > &x, const Eigen::Ref< const VectorXs > &u)
Compute the Jacobian and Hessian of cost and its residual vector.
const boost::shared_ptr< Base > & get_model() const
Return the original cost model.
virtual void calc(const boost::shared_ptr< CostDataAbstract > &data, const Eigen::Ref< const VectorXs > &x)
std::vector< boost::shared_ptr< Base > > data_u
The temporary data associated with the control variation.
Definition: cost.hpp:229
Scalar x_norm
Norm of the state vector.
Definition: cost.hpp:214
boost::shared_ptr< Base > data_0
The data at the approximation point.
Definition: cost.hpp:225
Scalar uh_jac
Disturbance value used for computing .
Definition: cost.hpp:218
Scalar xh_jac
Disturbance value used for computing .
Definition: cost.hpp:216
VectorXs du
Control disturbance.
Definition: cost.hpp:222
std::vector< boost::shared_ptr< Base > > data_x
The temporary data associated with the state variation.
Definition: cost.hpp:227
VectorXs dx
State disturbance.
Definition: cost.hpp:219
CostDataNumDiffTpl(Model< Scalar > *const model, DataCollectorAbstract *const shared_data)
Initialize the numdiff cost data.
Definition: cost.hpp:179