GCC Code Coverage Report


Directory: ./
File: include/crocoddyl/core/control-base.hpp
Date: 2025-03-26 19:23:43
Exec Total Coverage
Lines: 12 13 92.3%
Branches: 11 22 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_CONTROL_BASE_HPP_
11 #define CROCODDYL_CORE_CONTROL_BASE_HPP_
12
13 #include <memory>
14
15 #include "crocoddyl/core/fwd.hpp"
16 #include "crocoddyl/core/mathbase.hpp"
17
18 namespace crocoddyl {
19
20 class ControlParametrizationModelBase {
21 public:
22 2296 virtual ~ControlParametrizationModelBase() = default;
23
24
2/4
✗ Branch 1 not taken.
✓ Branch 2 taken 12 times.
✓ Branch 5 taken 12 times.
✗ Branch 6 not taken.
48 CROCODDYL_BASE_CAST(ControlParametrizationModelBase,
25 ControlParametrizationModelAbstractTpl)
26 };
27
28 /**
29 * @brief Abstract class for the control trajectory parametrization
30 *
31 * The control trajectory is a function of the (normalized) time.
32 * Normalized time is between 0 and 1, where 0 represents the beginning of the
33 * time step, and 1 represents its end. The trajectory depends on the control
34 * parameters u, whose size may be larger than the size of the control inputs w.
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 dual of `calc`. Note that
44 * `multiplyByJacobian` and `multiplyJacobianTransposeBy` requires to run `calc`
45 * first.
46 *
47 * \sa `calc()`, `calcDiff()`, `createData()`, `params`, `multiplyByJacobian`,
48 * `multiplyJacobianTransposeBy`
49 */
50 template <typename _Scalar>
51 class ControlParametrizationModelAbstractTpl
52 : public ControlParametrizationModelBase {
53 public:
54 EIGEN_MAKE_ALIGNED_OPERATOR_NEW
55
56 typedef _Scalar Scalar;
57 typedef MathBaseTpl<Scalar> MathBase;
58 typedef ControlParametrizationDataAbstractTpl<Scalar>
59 ControlParametrizationDataAbstract;
60 typedef typename MathBase::VectorXs VectorXs;
61 typedef typename MathBase::MatrixXs MatrixXs;
62
63 /**
64 * @brief Initialize the control dimensions
65 *
66 * @param[in] nw Dimension of control inputs
67 * @param[in] nu Dimension of control parameters
68 */
69 ControlParametrizationModelAbstractTpl(const std::size_t nw,
70 const std::size_t nu);
71 2296 virtual ~ControlParametrizationModelAbstractTpl() = default;
72
73 /**
74 * @brief Get the value of the control at the specified time
75 *
76 * @param[in] data Data structure containing the control vector to write
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 = 0;
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 Control-parametrization 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 = 0;
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 Update the control parameters u for a specified time t given the
107 * control input w
108 *
109 * @param[in] data Control-parametrization data
110 * @param[in] t Time in [0,1]
111 * @param[in] w Control inputs
112 */
113 virtual void params(
114 const std::shared_ptr<ControlParametrizationDataAbstract>& data,
115 const Scalar t, const Eigen::Ref<const VectorXs>& w) const = 0;
116
117 /**
118 * @brief Convert the bounds on the control inputs w to bounds on the control
119 * parameters u
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 = 0;
130
131 /**
132 * @brief Compute the product between the given matrix A and the derivative of
133 * the control input with respect to the control parameters (i.e., A*dw_du).
134 *
135 * It assumes that `calc()` has been run first
136 *
137 * @param[in] data Control-parametrization 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 = 0;
148
149 virtual MatrixXs multiplyByJacobian_J(
150 const std::shared_ptr<ControlParametrizationDataAbstract>& data,
151 const Eigen::Ref<const MatrixXs>& A, const AssignmentOp op = setto) const;
152
153 /**
154 * @brief Compute the product between the transpose of the derivative of the
155 * control input with respect to the control parameters and a given matrix A
156 * (i.e., dw_du^T*A)
157 *
158 * It assumes that `calc()` has been run first
159 *
160 * @param[in] data Control-parametrization data
161 * @param[in] A A matrix to multiply times the Jacobian
162 * @param[out] out Product between the transposed Jacobian of the control
163 * with respect to the parameters and the matrix A
164 * @param[in] op Assignment operator which sets, adds, or removes the
165 * given results
166 */
167 virtual void multiplyJacobianTransposeBy(
168 const std::shared_ptr<ControlParametrizationDataAbstract>& data,
169 const Eigen::Ref<const MatrixXs>& A, Eigen::Ref<MatrixXs> out,
170 const AssignmentOp op = setto) const = 0;
171
172 virtual MatrixXs multiplyJacobianTransposeBy_J(
173 const std::shared_ptr<ControlParametrizationDataAbstract>& data,
174 const Eigen::Ref<const MatrixXs>& A, const AssignmentOp op = setto) const;
175
176 /**
177 * @brief Checks that a specific data belongs to this model
178 */
179 virtual bool checkData(
180 const std::shared_ptr<ControlParametrizationDataAbstract>& data);
181
182 /**
183 * @brief Print information on the control model
184 */
185 template <class Scalar>
186 friend std::ostream& operator<<(
187 std::ostream& os,
188 const ControlParametrizationModelAbstractTpl<Scalar>& model);
189
190 /**
191 * @brief Print relevant information of the control model
192 *
193 * @param[out] os Output stream object
194 */
195 virtual void print(std::ostream& os) const;
196
197 /**
198 * @brief Return the dimension of the control inputs
199 */
200 std::size_t get_nw() const;
201
202 /**
203 * @brief Return the dimension of control parameters
204 */
205 std::size_t get_nu() const;
206
207 protected:
208 std::size_t nw_; //!< Control dimension
209 std::size_t nu_; //!< Control parameters dimension
210 ControlParametrizationModelAbstractTpl() : nw_(0), nu_(0) {};
211 };
212
213 template <typename _Scalar>
214 struct ControlParametrizationDataAbstractTpl {
215 EIGEN_MAKE_ALIGNED_OPERATOR_NEW
216
217 typedef _Scalar Scalar;
218 typedef MathBaseTpl<Scalar> MathBase;
219 typedef typename MathBase::VectorXs VectorXs;
220 typedef typename MathBase::MatrixXs MatrixXs;
221
222 template <template <typename Scalar> class Model>
223 105012 explicit ControlParametrizationDataAbstractTpl(Model<Scalar>* const model)
224
1/2
✓ Branch 1 taken 104996 times.
✗ Branch 2 not taken.
105012 : w(model->get_nw()),
225
2/4
✓ Branch 1 taken 104996 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 104996 times.
✗ Branch 5 not taken.
105012 u(model->get_nu()),
226
3/6
✓ Branch 2 taken 104996 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 104996 times.
✗ Branch 6 not taken.
✓ Branch 8 taken 104996 times.
✗ Branch 9 not taken.
210024 dw_du(model->get_nw(), model->get_nu()) {
227
1/2
✓ Branch 1 taken 104996 times.
✗ Branch 2 not taken.
105012 w.setZero();
228
1/2
✓ Branch 1 taken 104996 times.
✗ Branch 2 not taken.
105012 u.setZero();
229
1/2
✓ Branch 1 taken 104996 times.
✗ Branch 2 not taken.
105012 dw_du.setZero();
230 105012 }
231 209154 virtual ~ControlParametrizationDataAbstractTpl() = default;
232
233 VectorXs w; //!< value of the differential control
234 VectorXs u; //!< value of the control parameters
235 MatrixXs dw_du; //!< Jacobian of the differential control with respect to the
236 //!< parameters
237 };
238
239 } // namespace crocoddyl
240
241 /* --- Details -------------------------------------------------------------- */
242 /* --- Details -------------------------------------------------------------- */
243 /* --- Details -------------------------------------------------------------- */
244 #include "crocoddyl/core/control-base.hxx"
245
246 CROCODDYL_DECLARE_EXTERN_TEMPLATE_CLASS(
247 crocoddyl::ControlParametrizationModelAbstractTpl)
248 CROCODDYL_DECLARE_EXTERN_TEMPLATE_STRUCT(
249 crocoddyl::ControlParametrizationDataAbstractTpl)
250
251 #endif // CROCODDYL_CORE_CONTROL_BASE_HPP_
252