GCC Code Coverage Report


Directory: ./
File: include/crocoddyl/core/integrator/rk4.hpp
Date: 2025-01-16 08:47:40
Exec Total Coverage
Lines: 0 45 0.0%
Branches: 0 146 0.0%

Line Branch Exec Source
1 ///////////////////////////////////////////////////////////////////////////////
2 // BSD 3-Clause License
3 //
4 // Copyright (C) 2019-2022, LAAS-CNRS, IRI: CSIC-UPC, 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_INTEGRATOR_RK4_HPP_
11 #define CROCODDYL_CORE_INTEGRATOR_RK4_HPP_
12
13 #include "crocoddyl/core/fwd.hpp"
14 #include "crocoddyl/core/integ-action-base.hpp"
15 #include "crocoddyl/core/utils/deprecate.hpp"
16
17 namespace crocoddyl {
18
19 /**
20 * @brief Standard RK4 integrator
21 *
22 * It applies a standard RK4 integration scheme to a differential (i.e.,
23 * continuous time) action model.
24 *
25 * This standard RK4 scheme introduces also the possibility to parametrize the
26 * control trajectory inside an integration step, for instance using
27 * polynomials. This requires introducing some notation to clarify the
28 * difference between the control inputs of the differential model and the
29 * control inputs to the integrated model. We have decided to use
30 * \f$\mathbf{w}\f$ to refer to the control inputs of the differential model and
31 * \f$\mathbf{u}\f$ for the control inputs of the integrated action model.
32 *
33 * \sa `IntegratedActionModelAbstractTpl`, `calc()`, `calcDiff()`,
34 * `createData()`
35 */
36 template <typename _Scalar>
37 class IntegratedActionModelRK4Tpl
38 : public IntegratedActionModelAbstractTpl<_Scalar> {
39 public:
40 EIGEN_MAKE_ALIGNED_OPERATOR_NEW
41
42 typedef _Scalar Scalar;
43 typedef MathBaseTpl<Scalar> MathBase;
44 typedef IntegratedActionModelAbstractTpl<Scalar> Base;
45 typedef IntegratedActionDataRK4Tpl<Scalar> Data;
46 typedef ActionDataAbstractTpl<Scalar> ActionDataAbstract;
47 typedef DifferentialActionModelAbstractTpl<Scalar>
48 DifferentialActionModelAbstract;
49 typedef ControlParametrizationModelAbstractTpl<Scalar>
50 ControlParametrizationModelAbstract;
51 typedef typename MathBase::VectorXs VectorXs;
52 typedef typename MathBase::MatrixXs MatrixXs;
53
54 /**
55 * @brief Initialize the RK4 integrator
56 *
57 * @param[in] model Differential action model
58 * @param[in] control Control parametrization
59 * @param[in] time_step Step time (default 1e-3)
60 * @param[in] with_cost_residual Compute cost residual (default true)
61 */
62 DEPRECATED("Use IntegratedActionModelRK",
63 IntegratedActionModelRK4Tpl(
64 boost::shared_ptr<DifferentialActionModelAbstract> model,
65 boost::shared_ptr<ControlParametrizationModelAbstract> control,
66 const Scalar time_step = Scalar(1e-3),
67 const bool with_cost_residual = true);)
68
69 /**
70 * @brief Initialize the RK4 integrator
71 *
72 * This initialization uses `ControlParametrizationPolyZeroTpl` for the
73 * control parametrization.
74 *
75 * @param[in] model Differential action model
76 * @param[in] time_step Step time (default 1e-3)
77 * @param[in] with_cost_residual Compute cost residual (default true)
78 */
79 DEPRECATED("Use IntegratedActionModelRK",
80 IntegratedActionModelRK4Tpl(
81 boost::shared_ptr<DifferentialActionModelAbstract> model,
82 const Scalar time_step = Scalar(1e-3),
83 const bool with_cost_residual = true);)
84 virtual ~IntegratedActionModelRK4Tpl();
85
86 /**
87 * @brief Integrate the differential action model using RK4 scheme
88 *
89 * @param[in] data RK4 integrator data
90 * @param[in] x State point \f$\mathbf{x}\in\mathbb{R}^{ndx}\f$
91 * @param[in] u Control input \f$\mathbf{u}\in\mathbb{R}^{nu}\f$
92 */
93 virtual void calc(const boost::shared_ptr<ActionDataAbstract>& data,
94 const Eigen::Ref<const VectorXs>& x,
95 const Eigen::Ref<const VectorXs>& u);
96
97 /**
98 * @brief Integrate the total cost value for nodes that depends only on the
99 * state using RK4 scheme
100 *
101 * It computes the total cost and defines the next state as the current one.
102 * This function is used in the terminal nodes of an optimal control problem.
103 *
104 * @param[in] data RK4 integrator data
105 * @param[in] x State point \f$\mathbf{x}\in\mathbb{R}^{ndx}\f$
106 */
107 virtual void calc(const boost::shared_ptr<ActionDataAbstract>& data,
108 const Eigen::Ref<const VectorXs>& x);
109
110 /**
111 * @brief Compute the partial derivatives of the RK4 integrator
112 *
113 * @param[in] data RK4 integrator data
114 * @param[in] x State point \f$\mathbf{x}\in\mathbb{R}^{ndx}\f$
115 * @param[in] u Control input \f$\mathbf{u}\in\mathbb{R}^{nu}\f$
116 */
117 virtual void calcDiff(const boost::shared_ptr<ActionDataAbstract>& data,
118 const Eigen::Ref<const VectorXs>& x,
119 const Eigen::Ref<const VectorXs>& u);
120
121 /**
122 * @brief Compute the partial derivatives of the cost
123 *
124 * It updates the derivatives of the cost function with respect to the state
125 * only. This function is used in the terminal nodes of an optimal control
126 * problem.
127 *
128 * @param[in] data RK4 integrator data
129 * @param[in] x State point \f$\mathbf{x}\in\mathbb{R}^{ndx}\f$
130 */
131 virtual void calcDiff(const boost::shared_ptr<ActionDataAbstract>& data,
132 const Eigen::Ref<const VectorXs>& x);
133
134 /**
135 * @brief Create the RK4 integrator data
136 *
137 * @return the RK4 integrator data
138 */
139 virtual boost::shared_ptr<ActionDataAbstract> createData();
140
141 /**
142 * @brief Checks that a specific data belongs to this model
143 */
144 virtual bool checkData(const boost::shared_ptr<ActionDataAbstract>& data);
145
146 /**
147 * @brief Computes the quasic static commands
148 *
149 * The quasic static commands are the ones produced for a the reference
150 * posture as an equilibrium point, i.e. for
151 * \f$\mathbf{f^q_x}\delta\mathbf{q}+\mathbf{f_u}\delta\mathbf{u}=\mathbf{0}\f$
152 *
153 * @param[in] data RK4 integrator data
154 * @param[out] u Quasic static commands
155 * @param[in] x State point (velocity has to be zero)
156 * @param[in] maxiter Maximum allowed number of iterations
157 * @param[in] tol Tolerance
158 */
159 virtual void quasiStatic(const boost::shared_ptr<ActionDataAbstract>& data,
160 Eigen::Ref<VectorXs> u,
161 const Eigen::Ref<const VectorXs>& x,
162 const std::size_t maxiter = 100,
163 const Scalar tol = Scalar(1e-9));
164
165 /**
166 * @brief Print relevant information of the RK4 integrator model
167 *
168 * @param[out] os Output stream object
169 */
170 virtual void print(std::ostream& os) const;
171
172 protected:
173 using Base::control_; //!< Control parametrization
174 using Base::differential_; //!< Differential action model
175 using Base::ng_; //!< Number of inequality constraints
176 using Base::nh_; //!< Number of equality constraints
177 using Base::nu_; //!< Dimension of the control
178 using Base::state_; //!< Model of the state
179 using Base::time_step2_; //!< Square of the time step used for integration
180 using Base::time_step_; //!< Time step used for integration
181 using Base::with_cost_residual_; //!< Flag indicating whether a cost residual
182 //!< is used
183
184 private:
185 std::array<Scalar, 4> rk4_c_;
186 };
187
188 template <typename _Scalar>
189 struct IntegratedActionDataRK4Tpl
190 : public IntegratedActionDataAbstractTpl<_Scalar> {
191 EIGEN_MAKE_ALIGNED_OPERATOR_NEW
192
193 typedef _Scalar Scalar;
194 typedef MathBaseTpl<Scalar> MathBase;
195 typedef IntegratedActionDataAbstractTpl<Scalar> Base;
196 typedef DifferentialActionDataAbstractTpl<Scalar>
197 DifferentialActionDataAbstract;
198 typedef ControlParametrizationDataAbstractTpl<Scalar>
199 ControlParametrizationDataAbstract;
200 typedef typename MathBase::VectorXs VectorXs;
201 typedef typename MathBase::MatrixXs MatrixXs;
202
203 template <template <typename Scalar> class Model>
204 explicit IntegratedActionDataRK4Tpl(Model<Scalar>* const model)
205 : Base(model),
206 integral(4, Scalar(0.)),
207 dx(model->get_state()->get_ndx()),
208 ki(4, VectorXs::Zero(model->get_state()->get_ndx())),
209 y(4, VectorXs::Zero(model->get_state()->get_nx())),
210 ws(4, VectorXs::Zero(model->get_control()->get_nw())),
211 dx_rk4(4, VectorXs::Zero(model->get_state()->get_ndx())),
212 dki_dx(4, MatrixXs::Zero(model->get_state()->get_ndx(),
213 model->get_state()->get_ndx())),
214 dki_du(4,
215 MatrixXs::Zero(model->get_state()->get_ndx(), model->get_nu())),
216 dyi_dx(4, MatrixXs::Zero(model->get_state()->get_ndx(),
217 model->get_state()->get_ndx())),
218 dyi_du(4,
219 MatrixXs::Zero(model->get_state()->get_ndx(), model->get_nu())),
220 dli_dx(4, VectorXs::Zero(model->get_state()->get_ndx())),
221 dli_du(4, VectorXs::Zero(model->get_nu())),
222 ddli_ddx(4, MatrixXs::Zero(model->get_state()->get_ndx(),
223 model->get_state()->get_ndx())),
224 ddli_ddw(4, MatrixXs::Zero(model->get_control()->get_nw(),
225 model->get_control()->get_nw())),
226 ddli_ddu(4, MatrixXs::Zero(model->get_nu(), model->get_nu())),
227 ddli_dxdw(4, MatrixXs::Zero(model->get_state()->get_ndx(),
228 model->get_control()->get_nw())),
229 ddli_dxdu(
230 4, MatrixXs::Zero(model->get_state()->get_ndx(), model->get_nu())),
231 ddli_dwdu(
232 4, MatrixXs::Zero(model->get_control()->get_nw(), model->get_nu())),
233 Luu_partialx(4, MatrixXs::Zero(model->get_nu(), model->get_nu())),
234 Lxu_i(4,
235 MatrixXs::Zero(model->get_state()->get_ndx(), model->get_nu())),
236 Lxx_partialx(4, MatrixXs::Zero(model->get_state()->get_ndx(),
237 model->get_state()->get_ndx())),
238 Lxx_partialu(
239 4, MatrixXs::Zero(model->get_state()->get_ndx(), model->get_nu())) {
240 dx.setZero();
241
242 for (std::size_t i = 0; i < 4; ++i) {
243 differential.push_back(boost::shared_ptr<DifferentialActionDataAbstract>(
244 model->get_differential()->createData()));
245 control.push_back(boost::shared_ptr<ControlParametrizationDataAbstract>(
246 model->get_control()->createData()));
247 }
248
249 const std::size_t nv = model->get_state()->get_nv();
250 dyi_dx[0].diagonal().setOnes();
251 dki_dx[0].topRightCorner(nv, nv).diagonal().setOnes();
252 }
253 virtual ~IntegratedActionDataRK4Tpl() {}
254
255 std::vector<boost::shared_ptr<DifferentialActionDataAbstract> >
256 differential; //!< List of differential model data
257 std::vector<boost::shared_ptr<ControlParametrizationDataAbstract> >
258 control; //!< List of control parametrization data
259 std::vector<Scalar> integral;
260 VectorXs dx; //!< State rate
261 std::vector<VectorXs> ki; //!< List of RK4 terms related to system dynamics
262 std::vector<VectorXs>
263 y; //!< List of states where f is evaluated in the RK4 integration
264 std::vector<VectorXs>
265 ws; //!< Control inputs evaluated in the RK4 integration
266 std::vector<VectorXs> dx_rk4;
267
268 std::vector<MatrixXs>
269 dki_dx; //!< List of partial derivatives of RK4 nodes with respect to the
270 //!< state of the RK4 integration. dki/dx
271 std::vector<MatrixXs>
272 dki_du; //!< List of partial derivatives of RK4 nodes with respect to the
273 //!< control parameters of the RK4 integration. dki/du
274
275 std::vector<MatrixXs>
276 dyi_dx; //!< List of partial derivatives of RK4 dynamics with respect to
277 //!< the state of the RK4 integrator. dyi/dx
278 std::vector<MatrixXs>
279 dyi_du; //!< List of partial derivatives of RK4 dynamics with respect to
280 //!< the control parameters of the RK4 integrator. dyi/du
281
282 std::vector<VectorXs>
283 dli_dx; //!< List of partial derivatives of the cost with respect to the
284 //!< state of the RK4 integration. dli_dx
285 std::vector<VectorXs>
286 dli_du; //!< List of partial derivatives of the cost with respect to the
287 //!< control input of the RK4 integration. dli_du
288
289 std::vector<MatrixXs>
290 ddli_ddx; //!< List of second partial derivatives of the cost with
291 //!< respect to the state of the RK4 integration. ddli_ddx
292 std::vector<MatrixXs>
293 ddli_ddw; //!< List of second partial derivatives of the cost with
294 //!< respect to the control parameters of the RK4 integration.
295 //!< ddli_ddw
296 std::vector<MatrixXs> ddli_ddu; //!< List of second partial derivatives of
297 //!< the cost with respect to the control
298 //!< input of the RK4 integration. ddli_ddu
299 std::vector<MatrixXs>
300 ddli_dxdw; //!< List of second partial derivatives of the cost with
301 //!< respect to the state and control input of the RK4
302 //!< integration. ddli_dxdw
303 std::vector<MatrixXs>
304 ddli_dxdu; //!< List of second partial derivatives of the cost with
305 //!< respect to the state and control parameters of the RK4
306 //!< integration. ddli_dxdu
307 std::vector<MatrixXs>
308 ddli_dwdu; //!< List of second partial derivatives of the cost with
309 //!< respect to the control parameters and inputs control of
310 //!< the RK4 integration. ddli_dwdu
311
312 std::vector<MatrixXs> Luu_partialx;
313 std::vector<MatrixXs> Lxu_i;
314 std::vector<MatrixXs> Lxx_partialx;
315 std::vector<MatrixXs> Lxx_partialu;
316
317 using Base::cost;
318 using Base::Fu;
319 using Base::Fx;
320 using Base::Lu;
321 using Base::Luu;
322 using Base::Lx;
323 using Base::Lxu;
324 using Base::Lxx;
325 using Base::r;
326 using Base::xnext;
327 };
328
329 } // namespace crocoddyl
330
331 /* --- Details -------------------------------------------------------------- */
332 /* --- Details -------------------------------------------------------------- */
333 /* --- Details -------------------------------------------------------------- */
334 #include "crocoddyl/core/integrator/rk4.hxx"
335
336 #endif // CROCODDYL_CORE_INTEGRATOR_RK4_HPP_
337