Directory: | ./ |
---|---|
File: | include/crocoddyl/core/numdiff/control.hpp |
Date: | 2025-02-24 23:41:29 |
Exec | Total | Coverage | |
---|---|---|---|
Lines: | 9 | 9 | 100.0% |
Branches: | 7 | 12 | 58.3% |
Line | Branch | Exec | Source |
---|---|---|---|
1 | /////////////////////////////////////////////////////////////////////////////// | ||
2 | // BSD 3-Clause License | ||
3 | // | ||
4 | // Copyright (C) 2021-2023, LAAS-CNRS, New York University, Max Planck | ||
5 | // Gesellschaft, | ||
6 | // University of Edinburgh, University of Trento, | ||
7 | // Heriot-Watt University | ||
8 | // Copyright note valid unless otherwise stated in individual files. | ||
9 | // All rights reserved. | ||
10 | /////////////////////////////////////////////////////////////////////////////// | ||
11 | |||
12 | #ifndef CROCODDYL_CORE_NUMDIFF_CONTROL_HPP_ | ||
13 | #define CROCODDYL_CORE_NUMDIFF_CONTROL_HPP_ | ||
14 | |||
15 | #include <boost/make_shared.hpp> | ||
16 | #include <memory> | ||
17 | #include <vector> | ||
18 | |||
19 | #include "crocoddyl/core/control-base.hpp" | ||
20 | #include "crocoddyl/core/fwd.hpp" | ||
21 | |||
22 | namespace crocoddyl { | ||
23 | |||
24 | template <typename _Scalar> | ||
25 | class ControlParametrizationModelNumDiffTpl | ||
26 | : public ControlParametrizationModelAbstractTpl<_Scalar> { | ||
27 | public: | ||
28 | EIGEN_MAKE_ALIGNED_OPERATOR_NEW | ||
29 | |||
30 | typedef _Scalar Scalar; | ||
31 | typedef MathBaseTpl<Scalar> MathBase; | ||
32 | typedef ControlParametrizationModelAbstractTpl<_Scalar> Base; | ||
33 | typedef ControlParametrizationDataNumDiffTpl<_Scalar> Data; | ||
34 | typedef ControlParametrizationDataAbstractTpl<_Scalar> | ||
35 | ControlParametrizationDataAbstract; | ||
36 | typedef typename MathBase::VectorXs VectorXs; | ||
37 | typedef typename MathBase::MatrixXs MatrixXs; | ||
38 | |||
39 | /** | ||
40 | * @brief Construct a new ControlParametrizationModelNumDiff object | ||
41 | * | ||
42 | * @param model | ||
43 | */ | ||
44 | explicit ControlParametrizationModelNumDiffTpl(std::shared_ptr<Base> model); | ||
45 | virtual ~ControlParametrizationModelNumDiffTpl(); | ||
46 | |||
47 | /** | ||
48 | * @brief Get the value of the control at the specified time | ||
49 | * | ||
50 | * @param[in] data Control-parametrization numdiff data | ||
51 | * @param[in] t Time in [0,1] | ||
52 | * @param[in] u Control parameters | ||
53 | */ | ||
54 | void calc(const std::shared_ptr<ControlParametrizationDataAbstract>& data, | ||
55 | const Scalar t, const Eigen::Ref<const VectorXs>& u) const; | ||
56 | |||
57 | /** | ||
58 | * @brief Get the value of the Jacobian of the control with respect to the | ||
59 | * parameters | ||
60 | * | ||
61 | * @param[in] data Control-parametrization numdiff data | ||
62 | * @param[in] t Time in [0,1] | ||
63 | * @param[in] u Control parameters | ||
64 | */ | ||
65 | void calcDiff(const std::shared_ptr<ControlParametrizationDataAbstract>& data, | ||
66 | const Scalar t, const Eigen::Ref<const VectorXs>& u) const; | ||
67 | |||
68 | /** | ||
69 | * @brief Create the control-parametrization data | ||
70 | * | ||
71 | * @return the control-parametrization data | ||
72 | */ | ||
73 | virtual std::shared_ptr<ControlParametrizationDataAbstract> createData(); | ||
74 | |||
75 | /** | ||
76 | * @brief Get a value of the control parameters such that the control at the | ||
77 | * specified time t is equal to the specified value u | ||
78 | * | ||
79 | * @param[in] data Control-parametrization numdiff data | ||
80 | * @param[in] t Time in [0,1] | ||
81 | * @param[in] w Control values | ||
82 | */ | ||
83 | void params(const std::shared_ptr<ControlParametrizationDataAbstract>& data, | ||
84 | const Scalar t, const Eigen::Ref<const VectorXs>& w) const; | ||
85 | |||
86 | /** | ||
87 | * @brief Convert the bounds on the control to bounds on the control | ||
88 | * parameters | ||
89 | * | ||
90 | * @param[in] w_lb Control lower bound | ||
91 | * @param[in] w_ub Control upper bound | ||
92 | * @param[in] u_lb Control parameter lower bound | ||
93 | * @param[in] u_ub Control parameter upper bound | ||
94 | */ | ||
95 | void convertBounds(const Eigen::Ref<const VectorXs>& w_lb, | ||
96 | const Eigen::Ref<const VectorXs>& w_ub, | ||
97 | Eigen::Ref<VectorXs> u_lb, | ||
98 | Eigen::Ref<VectorXs> u_ub) const; | ||
99 | |||
100 | /** | ||
101 | * @brief Compute the product between a specified matrix and the Jacobian of | ||
102 | * the control (with respect to the parameters) | ||
103 | * | ||
104 | * @param[in] data Control-parametrization numdiff data | ||
105 | * @param[in] A A matrix to multiply times the Jacobian | ||
106 | * @param[out] out Product between the matrix A and the Jacobian of the | ||
107 | * control with respect to the parameters | ||
108 | * @param[in] op Assignment operator which sets, adds, or removes the | ||
109 | * given results | ||
110 | */ | ||
111 | void multiplyByJacobian( | ||
112 | const std::shared_ptr<ControlParametrizationDataAbstract>& data, | ||
113 | const Eigen::Ref<const MatrixXs>& A, Eigen::Ref<MatrixXs> out, | ||
114 | const AssignmentOp = setto) const; | ||
115 | |||
116 | /** | ||
117 | * @brief Compute the product between the transposed Jacobian of the control | ||
118 | * (with respect to the parameters) and a specified matrix | ||
119 | * | ||
120 | * @param[in] data Control-parametrization numdiff data | ||
121 | * @param[in] A A matrix to multiply times the Jacobian | ||
122 | * @param[out] out Product between the transposed Jacobian of the control | ||
123 | * with respect to the parameters and the matrix A | ||
124 | * @param[in] op Assignment operator which sets, adds, or removes the | ||
125 | * given results | ||
126 | */ | ||
127 | void multiplyJacobianTransposeBy( | ||
128 | const std::shared_ptr<ControlParametrizationDataAbstract>& data, | ||
129 | const Eigen::Ref<const MatrixXs>& A, Eigen::Ref<MatrixXs> out, | ||
130 | const AssignmentOp = setto) const; | ||
131 | |||
132 | /** | ||
133 | * @brief Get the model_ object | ||
134 | * | ||
135 | * @return Base& | ||
136 | */ | ||
137 | const std::shared_ptr<Base>& get_model() const; | ||
138 | |||
139 | /** | ||
140 | * @brief Return the disturbance constant used in the numerical | ||
141 | * differentiation routine | ||
142 | */ | ||
143 | const Scalar get_disturbance() const; | ||
144 | |||
145 | /** | ||
146 | * @brief Modify the disturbance constant used in the numerical | ||
147 | * differentiation routine | ||
148 | */ | ||
149 | void set_disturbance(const Scalar disturbance); | ||
150 | |||
151 | private: | ||
152 | std::shared_ptr<Base> | ||
153 | model_; //!< model we need to compute the numerical differentiation | ||
154 | Scalar e_jac_; //!< Constant used for computing disturbances in Jacobian | ||
155 | //!< calculation | ||
156 | |||
157 | protected: | ||
158 | using Base::nu_; | ||
159 | using Base::nw_; | ||
160 | }; | ||
161 | |||
162 | template <typename _Scalar> | ||
163 | struct ControlParametrizationDataNumDiffTpl | ||
164 | : public ControlParametrizationDataAbstractTpl<_Scalar> { | ||
165 | EIGEN_MAKE_ALIGNED_OPERATOR_NEW | ||
166 | |||
167 | typedef _Scalar Scalar; | ||
168 | typedef MathBaseTpl<Scalar> MathBase; | ||
169 | typedef typename MathBase::VectorXs VectorXs; | ||
170 | typedef typename MathBase::MatrixXs MatrixXs; | ||
171 | typedef ControlParametrizationDataAbstractTpl<Scalar> Base; | ||
172 | |||
173 | template <template <typename Scalar> class Model> | ||
174 | 12 | explicit ControlParametrizationDataNumDiffTpl(Model<Scalar>* const model) | |
175 |
1/2✓ Branch 3 taken 12 times.
✗ Branch 4 not taken.
|
12 | : Base(model), du(model->get_nu()) { |
176 |
1/2✓ Branch 1 taken 12 times.
✗ Branch 2 not taken.
|
12 | du.setZero(); |
177 | |||
178 | 12 | const std::size_t nu = model->get_model()->get_nu(); | |
179 |
1/2✓ Branch 3 taken 12 times.
✗ Branch 4 not taken.
|
12 | data_0 = model->get_model()->createData(); |
180 |
2/2✓ Branch 0 taken 270 times.
✓ Branch 1 taken 12 times.
|
282 | for (std::size_t i = 0; i < nu; ++i) { |
181 |
2/4✓ Branch 3 taken 270 times.
✗ Branch 4 not taken.
✓ Branch 6 taken 270 times.
✗ Branch 7 not taken.
|
270 | data_u.push_back(model->get_model()->createData()); |
182 | } | ||
183 | 12 | } | |
184 | |||
185 | 24 | virtual ~ControlParametrizationDataNumDiffTpl() {} | |
186 | |||
187 | VectorXs du; //!< temporary variable used for finite differencing | ||
188 | std::shared_ptr<Base> data_0; //!< The data that contains the final results | ||
189 | std::vector<std::shared_ptr<Base> > | ||
190 | data_u; //!< The temporary data associated with the control variation | ||
191 | }; | ||
192 | |||
193 | } // namespace crocoddyl | ||
194 | |||
195 | /* --- Details -------------------------------------------------------------- */ | ||
196 | /* --- Details -------------------------------------------------------------- */ | ||
197 | /* --- Details -------------------------------------------------------------- */ | ||
198 | #include "crocoddyl/core/numdiff/control.hxx" | ||
199 | |||
200 | #endif // CROCODDYL_CORE_NUMDIFF_CONTROL_HPP_ | ||
201 |