GCC Code Coverage Report


Directory: ./
File: include/crocoddyl/core/controls/poly-two-rk.hpp
Date: 2025-02-24 23:41:29
Exec Total Coverage
Lines: 5 5 100.0%
Branches: 2 4 50.0%

Line Branch Exec Source
1 ///////////////////////////////////////////////////////////////////////////////
2 // BSD 3-Clause License
3 //
4 // Copyright (C) 2021-2025, University of Edinburgh, University of Trento,
5 // Heriot-Watt University
6 // Copyright note valid unless otherwise stated in individual files.
7 // All rights reserved.
8 ///////////////////////////////////////////////////////////////////////////////
9
10 #ifndef CROCODDYL_CORE_CONTROLS_POLY_TWO_RK_HPP_
11 #define CROCODDYL_CORE_CONTROLS_POLY_TWO_RK_HPP_
12
13 #include "crocoddyl/core/control-base.hpp"
14 #include "crocoddyl/core/fwd.hpp"
15 #include "crocoddyl/core/integrator/rk.hpp"
16 #include "crocoddyl/core/utils/exception.hpp"
17
18 namespace crocoddyl {
19
20 /**
21 * @brief A polynomial function of time of degree two, that is a quadratic
22 * function
23 *
24 * The size of the parameters \f$\mathbf{u}\f$ is 3 times the size of the
25 * control input \f$\mathbf{w}\f$. It defines a polynomial of degree two,
26 * customized for the RK4 and RK4 integrators (even though it can be used with
27 * whatever integration scheme). The first third of \f$\mathbf{u}\f$ represents
28 * the value of \f$\mathbf{w}\f$ at time 0. The second third of \f$\mathbf{u}\f$
29 * represents the value of \f$\mathbf{w}\f$ at time 0.5 or 1/3 for RK4 and RK3
30 * parametrization, respectively. The last third of \f$\mathbf{u}\f$ represents
31 * the value of \f$\mathbf{w}\f$ at time 1 or 2/3 for the RK4 and RK3
32 * parametrization, respectively. This parametrization is suitable to be used
33 * with the RK-4 or RK-3 integration schemes, because they require the value of
34 * \f$\mathbf{w}\f$ exactly at 0, 0.5, 1 (for RK4) or 0, 1/3, 2/3 (for RK3).
35 *
36 * The main computations are carried out in `calc`, `multiplyByJacobian` and
37 * `multiplyJacobianTransposeBy`, where the former computes control input
38 * \f$\mathbf{w}\in\mathbb{R}^{nw}\f$ from a set of control parameters
39 * \f$\mathbf{u}\in\mathbb{R}^{nu}\f$ where `nw` and `nu` represent the
40 * dimension of the control inputs and parameters, respectively, and the latter
41 * defines useful operations across the Jacobian of the control-parametrization
42 * model. Finally, `params` allows us to obtain the control parameters from a
43 * the control input, i.e., it is the inverse of `calc`. Note that
44 * `multiplyByJacobian` and `multiplyJacobianTransposeBy` requires to run `calc`
45 * first.
46 *
47 * \sa `ControlParametrizationAbstractTpl`, `calc()`, `calcDiff()`,
48 * `createData()`, `params`, `multiplyByJacobian`, `multiplyJacobianTransposeBy`
49 */
50 template <typename _Scalar>
51 class ControlParametrizationModelPolyTwoRKTpl
52 : public ControlParametrizationModelAbstractTpl<_Scalar> {
53 public:
54 typedef _Scalar Scalar;
55 typedef MathBaseTpl<Scalar> MathBase;
56 typedef ControlParametrizationDataAbstractTpl<Scalar>
57 ControlParametrizationDataAbstract;
58 typedef ControlParametrizationModelAbstractTpl<Scalar> Base;
59 typedef ControlParametrizationDataPolyTwoRKTpl<Scalar> Data;
60 typedef typename MathBase::VectorXs VectorXs;
61 typedef typename MathBase::MatrixXs MatrixXs;
62
63 /**
64 * @brief Initialize the poly-two RK control parametrization
65 *
66 * @param[in] nw Dimension of control vector
67 * @param[in] rktype Type of RK parametrization
68 */
69 explicit ControlParametrizationModelPolyTwoRKTpl(const std::size_t nw,
70 const RKType rktype);
71 virtual ~ControlParametrizationModelPolyTwoRKTpl();
72
73 /**
74 * @brief Get the value of the control at the specified time
75 *
76 * @param[in] data Poly-two-RK data
77 * @param[in] t Time in [0,1]
78 * @param[in] u Control parameters
79 */
80 virtual void calc(
81 const std::shared_ptr<ControlParametrizationDataAbstract>& data,
82 const Scalar t, const Eigen::Ref<const VectorXs>& u) const;
83
84 /**
85 * @brief Get the value of the Jacobian of the control with respect to the
86 * parameters
87 *
88 * It assumes that `calc()` has been run first
89 *
90 * @param[in] data Poly-two-RK data
91 * @param[in] t Time in [0,1]
92 * @param[in] u Control parameters
93 */
94 virtual void calcDiff(
95 const std::shared_ptr<ControlParametrizationDataAbstract>& data,
96 const Scalar t, const Eigen::Ref<const VectorXs>& u) const;
97
98 /**
99 * @brief Create the control-parametrization data
100 *
101 * @return the control-parametrization data
102 */
103 virtual std::shared_ptr<ControlParametrizationDataAbstract> createData();
104
105 /**
106 * @brief Get a value of the control parameters u such that the control at the
107 * specified time t is equal to the specified value w
108 *
109 * @param[in] data Poly-two-RK data
110 * @param[in] t Time in [0,1]
111 * @param[in] w Control values
112 */
113 virtual void params(
114 const std::shared_ptr<ControlParametrizationDataAbstract>& data,
115 const Scalar t, const Eigen::Ref<const VectorXs>& w) const;
116
117 /**
118 * @brief Map the specified bounds from the control space to the parameter
119 * space
120 *
121 * @param[in] w_lb Control lower bound
122 * @param[in] w_ub Control lower bound
123 * @param[out] u_lb Control parameters lower bound
124 * @param[out] u_ub Control parameters upper bound
125 */
126 virtual void convertBounds(const Eigen::Ref<const VectorXs>& w_lb,
127 const Eigen::Ref<const VectorXs>& w_ub,
128 Eigen::Ref<VectorXs> u_lb,
129 Eigen::Ref<VectorXs> u_ub) const;
130
131 /**
132 * @brief Compute the product between a specified matrix and the Jacobian of
133 * the control (with respect to the parameters)
134 *
135 * It assumes that `calc()` has been run first
136 *
137 * @param[in] data Poly-two-RK data
138 * @param[in] A A matrix to multiply times the Jacobian
139 * @param[out] out Product between the matrix A and the Jacobian of the
140 * control with respect to the parameters
141 * @param[in] op Assignment operator which sets, adds, or removes the
142 * given results
143 */
144 virtual void multiplyByJacobian(
145 const std::shared_ptr<ControlParametrizationDataAbstract>& data,
146 const Eigen::Ref<const MatrixXs>& A, Eigen::Ref<MatrixXs> out,
147 const AssignmentOp op = setto) const;
148
149 /**
150 * @brief Compute the product between the transposed Jacobian of the control
151 * (with respect to the parameters) and a specified matrix
152 *
153 * It assumes that `calc()` has been run first
154 *
155 * @param[in] data Poly-two-RK data
156 * @param[in] A A matrix to multiply times the Jacobian
157 * @param[out] out Product between the transposed Jacobian of the control
158 * with respect to the parameters and the matrix A
159 * @param[in] op Assignment operator which sets, adds, or removes the
160 * given results
161 */
162 virtual void multiplyJacobianTransposeBy(
163 const std::shared_ptr<ControlParametrizationDataAbstract>& data,
164 const Eigen::Ref<const MatrixXs>& A, Eigen::Ref<MatrixXs> out,
165 const AssignmentOp op = setto) const;
166
167 protected:
168 using Base::nu_;
169 using Base::nw_;
170
171 private:
172 RKType rktype_;
173 };
174
175 template <typename _Scalar>
176 struct ControlParametrizationDataPolyTwoRKTpl
177 : public ControlParametrizationDataAbstractTpl<_Scalar> {
178 EIGEN_MAKE_ALIGNED_OPERATOR_NEW
179
180 typedef _Scalar Scalar;
181 typedef MathBaseTpl<Scalar> MathBase;
182 typedef ControlParametrizationDataAbstractTpl<Scalar> Base;
183 typedef typename MathBase::Vector3s Vector3s;
184
185 template <template <typename Scalar> class Model>
186 16359 explicit ControlParametrizationDataPolyTwoRKTpl(Model<Scalar>* const model)
187
1/2
✓ Branch 2 taken 16359 times.
✗ Branch 3 not taken.
16359 : Base(model), tmp_t2(0.) {
188
1/2
✓ Branch 1 taken 16359 times.
✗ Branch 2 not taken.
16359 c.setZero();
189 16359 }
190
191 32722 virtual ~ControlParametrizationDataPolyTwoRKTpl() {}
192
193 Vector3s c; //!< Polynomial coefficients of the second-order control model
194 //!< that depends on time
195 Scalar tmp_t2; //!< Temporary variable to store the square of the time
196 };
197
198 } // namespace crocoddyl
199
200 /* --- Details -------------------------------------------------------------- */
201 /* --- Details -------------------------------------------------------------- */
202 /* --- Details -------------------------------------------------------------- */
203 #include "crocoddyl/core/controls/poly-two-rk.hxx"
204
205 #endif // CROCODDYL_CORE_CONTROLS_POLY_TWO_RK_HPP_
206