Line |
Branch |
Exec |
Source |
1 |
|
|
/////////////////////////////////////////////////////////////////////////////// |
2 |
|
|
// BSD 3-Clause License |
3 |
|
|
// |
4 |
|
|
// Copyright (C) 2019-2025, LAAS-CNRS, University of Edinburgh, |
5 |
|
|
// Heriot-Watt University |
6 |
|
|
// Copyright note valid unless otherwise stated in individual files. |
7 |
|
|
// All rights reserved. |
8 |
|
|
/////////////////////////////////////////////////////////////////////////////// |
9 |
|
|
|
10 |
|
|
#ifndef CROCODDYL_CORE_NUMDIFF_COST_HPP_ |
11 |
|
|
#define CROCODDYL_CORE_NUMDIFF_COST_HPP_ |
12 |
|
|
|
13 |
|
|
#include <boost/function.hpp> |
14 |
|
|
|
15 |
|
|
#include "crocoddyl/core/cost-base.hpp" |
16 |
|
|
#include "crocoddyl/multibody/fwd.hpp" |
17 |
|
|
|
18 |
|
|
namespace crocoddyl { |
19 |
|
|
|
20 |
|
|
/** |
21 |
|
|
* @brief This class computes the numerical differentiation of a cost model. |
22 |
|
|
* |
23 |
|
|
* It computes the Jacobian and Hessian of the cost model via numerical |
24 |
|
|
* differentiation, i.e., \f$\mathbf{\ell_x}\f$, \f$\mathbf{\ell_u}\f$, |
25 |
|
|
* \f$\mathbf{\ell_{xx}}\f$, \f$\mathbf{\ell_{uu}}\f$, and |
26 |
|
|
* \f$\mathbf{\ell_{xu}}\f$ which denote the Jacobians and Hessians of the cost |
27 |
|
|
* function, respectively. |
28 |
|
|
* |
29 |
|
|
* \sa `CostModelAbstractTpl()`, `calcDiff()` |
30 |
|
|
*/ |
31 |
|
|
template <typename _Scalar> |
32 |
|
|
class CostModelNumDiffTpl : public CostModelAbstractTpl<_Scalar> { |
33 |
|
|
public: |
34 |
|
|
EIGEN_MAKE_ALIGNED_OPERATOR_NEW |
35 |
|
✗ |
CROCODDYL_DERIVED_CAST(CostModelBase, CostModelNumDiffTpl) |
36 |
|
|
|
37 |
|
|
typedef _Scalar Scalar; |
38 |
|
|
typedef CostDataAbstractTpl<Scalar> CostDataAbstract; |
39 |
|
|
typedef CostModelAbstractTpl<Scalar> Base; |
40 |
|
|
typedef CostDataNumDiffTpl<Scalar> Data; |
41 |
|
|
typedef DataCollectorAbstractTpl<Scalar> DataCollectorAbstract; |
42 |
|
|
typedef MathBaseTpl<Scalar> MathBase; |
43 |
|
|
typedef typename MathBaseTpl<Scalar>::VectorXs VectorXs; |
44 |
|
|
typedef typename MathBaseTpl<Scalar>::MatrixXs MatrixXs; |
45 |
|
|
typedef boost::function<void(const VectorXs&, const VectorXs&)> |
46 |
|
|
ReevaluationFunction; |
47 |
|
|
|
48 |
|
|
/** |
49 |
|
|
* @brief Initialize the numdiff cost model |
50 |
|
|
* |
51 |
|
|
* @param model Cost model that we want to apply the numerical |
52 |
|
|
* differentiation |
53 |
|
|
*/ |
54 |
|
|
explicit CostModelNumDiffTpl(const std::shared_ptr<Base>& model); |
55 |
|
|
|
56 |
|
|
/** |
57 |
|
|
* @brief Initialize the numdiff cost model |
58 |
|
|
*/ |
59 |
|
✗ |
virtual ~CostModelNumDiffTpl() = default; |
60 |
|
|
|
61 |
|
|
/** |
62 |
|
|
* @brief @copydoc Base::calc() |
63 |
|
|
*/ |
64 |
|
|
virtual void calc(const std::shared_ptr<CostDataAbstract>& data, |
65 |
|
|
const Eigen::Ref<const VectorXs>& x, |
66 |
|
|
const Eigen::Ref<const VectorXs>& u) override; |
67 |
|
|
|
68 |
|
|
/** |
69 |
|
|
* @brief @copydoc Base::calc(const std::shared_ptr<CostDataAbstract>& data, |
70 |
|
|
* const Eigen::Ref<const VectorXs>& x) |
71 |
|
|
*/ |
72 |
|
|
virtual void calc(const std::shared_ptr<CostDataAbstract>& data, |
73 |
|
|
const Eigen::Ref<const VectorXs>& x) override; |
74 |
|
|
|
75 |
|
|
/** |
76 |
|
|
* @brief @copydoc Base::calcDiff() |
77 |
|
|
*/ |
78 |
|
|
virtual void calcDiff(const std::shared_ptr<CostDataAbstract>& data, |
79 |
|
|
const Eigen::Ref<const VectorXs>& x, |
80 |
|
|
const Eigen::Ref<const VectorXs>& u) override; |
81 |
|
|
|
82 |
|
|
/** |
83 |
|
|
* @brief @copydoc Base::calcDiff(const std::shared_ptr<CostDataAbstract>& |
84 |
|
|
* data, const Eigen::Ref<const VectorXs>& x) |
85 |
|
|
*/ |
86 |
|
|
virtual void calcDiff(const std::shared_ptr<CostDataAbstract>& data, |
87 |
|
|
const Eigen::Ref<const VectorXs>& x) override; |
88 |
|
|
|
89 |
|
|
/** |
90 |
|
|
* @brief Create a numdiff cost data |
91 |
|
|
* |
92 |
|
|
* @param data Data collector used by the original model |
93 |
|
|
* @return the numdiff cost data |
94 |
|
|
*/ |
95 |
|
|
virtual std::shared_ptr<CostDataAbstract> createData( |
96 |
|
|
DataCollectorAbstract* const data) override; |
97 |
|
|
|
98 |
|
|
/** |
99 |
|
|
* @brief Cast the cost numdiff model to a different scalar type. |
100 |
|
|
* |
101 |
|
|
* It is useful for operations requiring different precision or scalar types. |
102 |
|
|
* |
103 |
|
|
* @tparam NewScalar The new scalar type to cast to. |
104 |
|
|
* @return CostModelNumDiffTpl<NewScalar> A cost model with the |
105 |
|
|
* new scalar type. |
106 |
|
|
*/ |
107 |
|
|
template <typename NewScalar> |
108 |
|
|
CostModelNumDiffTpl<NewScalar> cast() const; |
109 |
|
|
|
110 |
|
|
/** |
111 |
|
|
* @brief Return the original cost model |
112 |
|
|
*/ |
113 |
|
|
const std::shared_ptr<Base>& get_model() const; |
114 |
|
|
|
115 |
|
|
/** |
116 |
|
|
* @brief Return the disturbance constant used by the numerical |
117 |
|
|
* differentiation routine |
118 |
|
|
*/ |
119 |
|
|
const Scalar get_disturbance() const; |
120 |
|
|
|
121 |
|
|
/** |
122 |
|
|
* @brief Modify the disturbance constant used by the numerical |
123 |
|
|
* differentiation routine |
124 |
|
|
*/ |
125 |
|
|
void set_disturbance(const Scalar disturbance); |
126 |
|
|
|
127 |
|
|
/** |
128 |
|
|
* @brief Identify if the Gauss approximation is going to be used or not. |
129 |
|
|
* |
130 |
|
|
* @return true |
131 |
|
|
* @return false |
132 |
|
|
*/ |
133 |
|
|
bool get_with_gauss_approx(); |
134 |
|
|
|
135 |
|
|
/** |
136 |
|
|
* @brief Register functions that updates the shared data computed for a |
137 |
|
|
* system rollout The updated data is used to evaluate of the gradient and |
138 |
|
|
* Hessian. |
139 |
|
|
* |
140 |
|
|
* @param reevals are the registered functions. |
141 |
|
|
*/ |
142 |
|
|
void set_reevals(const std::vector<ReevaluationFunction>& reevals); |
143 |
|
|
|
144 |
|
|
protected: |
145 |
|
|
using Base::activation_; |
146 |
|
|
using Base::nu_; |
147 |
|
|
using Base::state_; |
148 |
|
|
using Base::unone_; |
149 |
|
|
|
150 |
|
|
private: |
151 |
|
|
/** |
152 |
|
|
* @brief Make sure that when we finite difference the Cost Model, the user |
153 |
|
|
* does not face unknown behaviour because of the finite differencing of a |
154 |
|
|
* quaternion around pi. This behaviour might occur if state cost in and |
155 |
|
|
* floating systems. |
156 |
|
|
* |
157 |
|
|
* For full discussions see issue |
158 |
|
|
* https://gepgitlab.laas.fr/loco-3d/crocoddyl/issues/139 |
159 |
|
|
* |
160 |
|
|
* @param x is the state at which the check is performed. |
161 |
|
|
*/ |
162 |
|
|
void assertStableStateFD(const Eigen::Ref<const VectorXs>& /*x*/); |
163 |
|
|
|
164 |
|
|
std::shared_ptr<Base> model_; //!< Cost model hat we want to apply the |
165 |
|
|
//!< numerical differentiation |
166 |
|
|
Scalar e_jac_; //!< Constant used for computing disturbances in Jacobian |
167 |
|
|
//!< calculation |
168 |
|
|
std::vector<ReevaluationFunction> |
169 |
|
|
reevals_; //!< Functions that needs execution before calc or calcDiff |
170 |
|
|
}; |
171 |
|
|
|
172 |
|
|
template <typename _Scalar> |
173 |
|
|
struct CostDataNumDiffTpl : public CostDataAbstractTpl<_Scalar> { |
174 |
|
|
EIGEN_MAKE_ALIGNED_OPERATOR_NEW |
175 |
|
|
|
176 |
|
|
typedef _Scalar Scalar; |
177 |
|
|
typedef MathBaseTpl<Scalar> MathBase; |
178 |
|
|
typedef CostDataAbstractTpl<Scalar> Base; |
179 |
|
|
typedef DataCollectorAbstractTpl<Scalar> DataCollectorAbstract; |
180 |
|
|
typedef ActivationDataAbstractTpl<Scalar> ActivationDataAbstract; |
181 |
|
|
typedef typename MathBaseTpl<Scalar>::VectorXs VectorXs; |
182 |
|
|
|
183 |
|
|
/** |
184 |
|
|
* @brief Initialize the numdiff cost data |
185 |
|
|
* |
186 |
|
|
* @tparam Model is the type of the `CostModelAbstractTpl`. |
187 |
|
|
* @param model is the object to compute the numerical differentiation from. |
188 |
|
|
*/ |
189 |
|
|
template <template <typename Scalar> class Model> |
190 |
|
✗ |
explicit CostDataNumDiffTpl(Model<Scalar>* const model, |
191 |
|
|
DataCollectorAbstract* const shared_data) |
192 |
|
|
: Base(model, shared_data), |
193 |
|
✗ |
dx(model->get_state()->get_ndx()), |
194 |
|
✗ |
xp(model->get_state()->get_nx()), |
195 |
|
✗ |
du(model->get_nu()), |
196 |
|
✗ |
up(model->get_nu()) { |
197 |
|
✗ |
dx.setZero(); |
198 |
|
✗ |
xp.setZero(); |
199 |
|
✗ |
du.setZero(); |
200 |
|
✗ |
up.setZero(); |
201 |
|
|
|
202 |
|
✗ |
const std::size_t ndx = model->get_model()->get_state()->get_ndx(); |
203 |
|
✗ |
const std::size_t nu = model->get_model()->get_nu(); |
204 |
|
✗ |
data_0 = model->get_model()->createData(shared_data); |
205 |
|
✗ |
for (std::size_t i = 0; i < ndx; ++i) { |
206 |
|
✗ |
data_x.push_back(model->get_model()->createData(shared_data)); |
207 |
|
|
} |
208 |
|
✗ |
for (std::size_t i = 0; i < nu; ++i) { |
209 |
|
✗ |
data_u.push_back(model->get_model()->createData(shared_data)); |
210 |
|
|
} |
211 |
|
|
} |
212 |
|
|
|
213 |
|
✗ |
virtual ~CostDataNumDiffTpl() = default; |
214 |
|
|
|
215 |
|
|
using Base::activation; |
216 |
|
|
using Base::cost; |
217 |
|
|
using Base::Lu; |
218 |
|
|
using Base::Luu; |
219 |
|
|
using Base::Lx; |
220 |
|
|
using Base::Lxu; |
221 |
|
|
using Base::Lxx; |
222 |
|
|
using Base::residual; |
223 |
|
|
using Base::shared; |
224 |
|
|
|
225 |
|
|
Scalar x_norm; //!< Norm of the state vector |
226 |
|
|
Scalar |
227 |
|
|
xh_jac; //!< Disturbance value used for computing \f$ \ell_\mathbf{x} \f$ |
228 |
|
|
Scalar |
229 |
|
|
uh_jac; //!< Disturbance value used for computing \f$ \ell_\mathbf{u} \f$ |
230 |
|
|
VectorXs dx; //!< State disturbance. |
231 |
|
|
VectorXs xp; //!< The integrated state from the disturbance on one DoF "\f$ |
232 |
|
|
//!< \int x dx_i \f$". |
233 |
|
|
VectorXs du; //!< Control disturbance. |
234 |
|
|
VectorXs up; //!< The integrated control from the disturbance on one DoF "\f$ |
235 |
|
|
//!< \int u du_i = u + du \f$". |
236 |
|
|
std::shared_ptr<Base> data_0; //!< The data at the approximation point. |
237 |
|
|
std::vector<std::shared_ptr<Base> > |
238 |
|
|
data_x; //!< The temporary data associated with the state variation. |
239 |
|
|
std::vector<std::shared_ptr<Base> > |
240 |
|
|
data_u; //!< The temporary data associated with the control variation. |
241 |
|
|
}; |
242 |
|
|
|
243 |
|
|
} // namespace crocoddyl |
244 |
|
|
|
245 |
|
|
/* --- Details -------------------------------------------------------------- */ |
246 |
|
|
/* --- Details -------------------------------------------------------------- */ |
247 |
|
|
/* --- Details -------------------------------------------------------------- */ |
248 |
|
|
#include "crocoddyl/core/numdiff/cost.hxx" |
249 |
|
|
|
250 |
|
|
CROCODDYL_DECLARE_EXTERN_TEMPLATE_CLASS(crocoddyl::CostModelNumDiffTpl) |
251 |
|
|
CROCODDYL_DECLARE_EXTERN_TEMPLATE_STRUCT(crocoddyl::CostDataNumDiffTpl) |
252 |
|
|
|
253 |
|
|
#endif // CROCODDYL_CORE_NUMDIFF_COST_HPP_ |
254 |
|
|
|