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 |
|
|
namespace crocoddyl { |
11 |
|
|
|
12 |
|
|
template <typename Scalar> |
13 |
|
✗ |
ControlParametrizationModelPolyTwoRKTpl< |
14 |
|
|
Scalar>::ControlParametrizationModelPolyTwoRKTpl(const std::size_t nw, |
15 |
|
|
const RKType rktype) |
16 |
|
✗ |
: Base(nw, 3 * nw), rktype_(rktype) { |
17 |
|
✗ |
if (rktype_ == RKType::two) { |
18 |
|
✗ |
std::cerr << "Invalid argument: RK2 parametrization is not supported" |
19 |
|
✗ |
<< std::endl; |
20 |
|
|
} |
21 |
|
|
} |
22 |
|
|
|
23 |
|
|
template <typename Scalar> |
24 |
|
✗ |
void ControlParametrizationModelPolyTwoRKTpl<Scalar>::calc( |
25 |
|
|
const std::shared_ptr<ControlParametrizationDataAbstract>& data, |
26 |
|
|
const Scalar t, const Eigen::Ref<const VectorXs>& u) const { |
27 |
|
✗ |
if (static_cast<std::size_t>(u.size()) != nu_) { |
28 |
|
✗ |
throw_pretty( |
29 |
|
|
"Invalid argument: " << "u has wrong dimension (it should be " + |
30 |
|
|
std::to_string(nu_) + ")"); |
31 |
|
|
} |
32 |
|
✗ |
Data* d = static_cast<Data*>(data.get()); |
33 |
|
✗ |
const Eigen::VectorBlock<const Eigen::Ref<const VectorXs> >& p0 = u.head(nw_); |
34 |
|
✗ |
const Eigen::VectorBlock<const Eigen::Ref<const VectorXs> >& p1 = |
35 |
|
✗ |
u.segment(nw_, nw_); |
36 |
|
✗ |
const Eigen::VectorBlock<const Eigen::Ref<const VectorXs> >& p2 = u.tail(nw_); |
37 |
|
✗ |
d->tmp_t2 = t * t; |
38 |
|
✗ |
switch (rktype_) { |
39 |
|
✗ |
case two: |
40 |
|
✗ |
std::cerr << "Invalid argument: RK2 parametrization is not supported" |
41 |
|
✗ |
<< std::endl; |
42 |
|
✗ |
break; |
43 |
|
✗ |
case three: |
44 |
|
✗ |
d->c[2] = Scalar(4.5) * d->tmp_t2 - Scalar(1.5) * t; |
45 |
|
✗ |
d->c[1] = -Scalar(9.) * d->tmp_t2 + Scalar(6.) * t; |
46 |
|
✗ |
d->c[0] = Scalar(4.5) * (d->tmp_t2 - t) + Scalar(1.); |
47 |
|
✗ |
break; |
48 |
|
✗ |
case four: |
49 |
|
✗ |
d->c[2] = Scalar(2.) * d->tmp_t2 - t; |
50 |
|
✗ |
d->c[1] = -Scalar(2.) * d->c[2] + Scalar(2.) * t; |
51 |
|
✗ |
d->c[0] = d->c[2] - Scalar(2.) * t + Scalar(1.); |
52 |
|
✗ |
break; |
53 |
|
|
} |
54 |
|
✗ |
d->w = d->c[2] * p2 + d->c[1] * p1 + d->c[0] * p0; |
55 |
|
|
} |
56 |
|
|
|
57 |
|
|
template <typename Scalar> |
58 |
|
✗ |
void ControlParametrizationModelPolyTwoRKTpl<Scalar>::calcDiff( |
59 |
|
|
const std::shared_ptr<ControlParametrizationDataAbstract>& data, |
60 |
|
|
const Scalar, const Eigen::Ref<const VectorXs>&) const { |
61 |
|
✗ |
Data* d = static_cast<Data*>(data.get()); |
62 |
|
✗ |
d->dw_du.leftCols(nw_).diagonal().array() = d->c[0]; |
63 |
|
✗ |
d->dw_du.middleCols(nw_, nw_).diagonal().array() = d->c[1]; |
64 |
|
✗ |
d->dw_du.rightCols(nw_).diagonal().array() = d->c[2]; |
65 |
|
|
} |
66 |
|
|
|
67 |
|
|
template <typename Scalar> |
68 |
|
|
std::shared_ptr<ControlParametrizationDataAbstractTpl<Scalar> > |
69 |
|
✗ |
ControlParametrizationModelPolyTwoRKTpl<Scalar>::createData() { |
70 |
|
✗ |
return std::allocate_shared<Data>(Eigen::aligned_allocator<Data>(), this); |
71 |
|
|
} |
72 |
|
|
|
73 |
|
|
template <typename Scalar> |
74 |
|
✗ |
void ControlParametrizationModelPolyTwoRKTpl<Scalar>::params( |
75 |
|
|
const std::shared_ptr<ControlParametrizationDataAbstract>& data, |
76 |
|
|
const Scalar, const Eigen::Ref<const VectorXs>& w) const { |
77 |
|
✗ |
if (static_cast<std::size_t>(w.size()) != nw_) { |
78 |
|
✗ |
throw_pretty( |
79 |
|
|
"Invalid argument: " << "w has wrong dimension (it should be " + |
80 |
|
|
std::to_string(nw_) + ")"); |
81 |
|
|
} |
82 |
|
✗ |
data->u.head(nw_) = w; |
83 |
|
✗ |
data->u.segment(nw_, nw_) = w; |
84 |
|
✗ |
data->u.tail(nw_) = w; |
85 |
|
|
} |
86 |
|
|
|
87 |
|
|
template <typename Scalar> |
88 |
|
✗ |
void ControlParametrizationModelPolyTwoRKTpl<Scalar>::convertBounds( |
89 |
|
|
const Eigen::Ref<const VectorXs>& w_lb, |
90 |
|
|
const Eigen::Ref<const VectorXs>& w_ub, Eigen::Ref<VectorXs> u_lb, |
91 |
|
|
Eigen::Ref<VectorXs> u_ub) const { |
92 |
|
✗ |
if (static_cast<std::size_t>(u_lb.size()) != nu_) { |
93 |
|
✗ |
throw_pretty( |
94 |
|
|
"Invalid argument: " << "u_lb has wrong dimension (it should be " + |
95 |
|
|
std::to_string(nu_) + ")"); |
96 |
|
|
} |
97 |
|
✗ |
if (static_cast<std::size_t>(u_ub.size()) != nu_) { |
98 |
|
✗ |
throw_pretty( |
99 |
|
|
"Invalid argument: " << "u_ub has wrong dimension (it should be " + |
100 |
|
|
std::to_string(nu_) + ")"); |
101 |
|
|
} |
102 |
|
✗ |
if (static_cast<std::size_t>(w_lb.size()) != nw_) { |
103 |
|
✗ |
throw_pretty( |
104 |
|
|
"Invalid argument: " << "w_lb has wrong dimension (it should be " + |
105 |
|
|
std::to_string(nw_) + ")"); |
106 |
|
|
} |
107 |
|
✗ |
if (static_cast<std::size_t>(w_ub.size()) != nw_) { |
108 |
|
✗ |
throw_pretty( |
109 |
|
|
"Invalid argument: " << "w_ub has wrong dimension (it should be " + |
110 |
|
|
std::to_string(nw_) + ")"); |
111 |
|
|
} |
112 |
|
✗ |
u_lb.head(nw_) = w_lb; |
113 |
|
✗ |
u_lb.segment(nw_, nw_) = w_lb; |
114 |
|
✗ |
u_lb.tail(nw_) = w_lb; |
115 |
|
✗ |
u_ub.head(nw_) = w_ub; |
116 |
|
✗ |
u_ub.segment(nw_, nw_) = w_ub; |
117 |
|
✗ |
u_ub.tail(nw_) = w_ub; |
118 |
|
|
} |
119 |
|
|
|
120 |
|
|
template <typename Scalar> |
121 |
|
✗ |
void ControlParametrizationModelPolyTwoRKTpl<Scalar>::multiplyByJacobian( |
122 |
|
|
const std::shared_ptr<ControlParametrizationDataAbstract>& data, |
123 |
|
|
const Eigen::Ref<const MatrixXs>& A, Eigen::Ref<MatrixXs> out, |
124 |
|
|
const AssignmentOp op) const { |
125 |
|
✗ |
assert_pretty(is_a_AssignmentOp(op), |
126 |
|
|
("op must be one of the AssignmentOp {settop, addto, rmfrom}")); |
127 |
|
✗ |
if (A.rows() != out.rows() || static_cast<std::size_t>(A.cols()) != nw_ || |
128 |
|
✗ |
static_cast<std::size_t>(out.cols()) != nu_) { |
129 |
|
✗ |
throw_pretty("Invalid argument: " << "A and out have wrong dimensions (" + |
130 |
|
|
std::to_string(A.rows()) + "," + |
131 |
|
|
std::to_string(A.cols()) + |
132 |
|
|
" and " + |
133 |
|
|
std::to_string(out.rows()) + "," + |
134 |
|
|
std::to_string(out.cols()) + +")"); |
135 |
|
|
} |
136 |
|
✗ |
Data* d = static_cast<Data*>(data.get()); |
137 |
|
✗ |
switch (op) { |
138 |
|
✗ |
case setto: |
139 |
|
✗ |
out.leftCols(nw_) = d->c[0] * A; |
140 |
|
✗ |
out.middleCols(nw_, nw_) = d->c[1] * A; |
141 |
|
✗ |
out.rightCols(nw_) = d->c[2] * A; |
142 |
|
✗ |
break; |
143 |
|
✗ |
case addto: |
144 |
|
✗ |
out.leftCols(nw_) += d->c[0] * A; |
145 |
|
✗ |
out.middleCols(nw_, nw_) += d->c[1] * A; |
146 |
|
✗ |
out.rightCols(nw_) += d->c[2] * A; |
147 |
|
✗ |
break; |
148 |
|
✗ |
case rmfrom: |
149 |
|
✗ |
out.leftCols(nw_) -= d->c[0] * A; |
150 |
|
✗ |
out.middleCols(nw_, nw_) -= d->c[1] * A; |
151 |
|
✗ |
out.rightCols(nw_) -= d->c[2] * A; |
152 |
|
✗ |
break; |
153 |
|
✗ |
default: |
154 |
|
✗ |
throw_pretty("Invalid argument: allowed operators: setto, addto, rmfrom"); |
155 |
|
|
break; |
156 |
|
|
} |
157 |
|
|
} |
158 |
|
|
|
159 |
|
|
template <typename Scalar> |
160 |
|
✗ |
void ControlParametrizationModelPolyTwoRKTpl<Scalar>:: |
161 |
|
|
multiplyJacobianTransposeBy( |
162 |
|
|
const std::shared_ptr<ControlParametrizationDataAbstract>& data, |
163 |
|
|
const Eigen::Ref<const MatrixXs>& A, Eigen::Ref<MatrixXs> out, |
164 |
|
|
const AssignmentOp op) const { |
165 |
|
✗ |
assert_pretty(is_a_AssignmentOp(op), |
166 |
|
|
("op must be one of the AssignmentOp {settop, addto, rmfrom}")); |
167 |
|
✗ |
if (A.cols() != out.cols() || static_cast<std::size_t>(A.rows()) != nw_ || |
168 |
|
✗ |
static_cast<std::size_t>(out.rows()) != nu_) { |
169 |
|
✗ |
throw_pretty("Invalid argument: " << "A and out have wrong dimensions (" + |
170 |
|
|
std::to_string(A.rows()) + "," + |
171 |
|
|
std::to_string(A.cols()) + |
172 |
|
|
" and " + |
173 |
|
|
std::to_string(out.rows()) + "," + |
174 |
|
|
std::to_string(out.cols()) + ")"); |
175 |
|
|
} |
176 |
|
✗ |
Data* d = static_cast<Data*>(data.get()); |
177 |
|
✗ |
switch (op) { |
178 |
|
✗ |
case setto: |
179 |
|
✗ |
out.topRows(nw_) = d->c[0] * A; |
180 |
|
✗ |
out.middleRows(nw_, nw_) = d->c[1] * A; |
181 |
|
✗ |
out.bottomRows(nw_) = d->c[2] * A; |
182 |
|
✗ |
break; |
183 |
|
✗ |
case addto: |
184 |
|
✗ |
out.topRows(nw_) += d->c[0] * A; |
185 |
|
✗ |
out.middleRows(nw_, nw_) += d->c[1] * A; |
186 |
|
✗ |
out.bottomRows(nw_) += d->c[2] * A; |
187 |
|
✗ |
break; |
188 |
|
✗ |
case rmfrom: |
189 |
|
✗ |
out.topRows(nw_) -= d->c[0] * A; |
190 |
|
✗ |
out.middleRows(nw_, nw_) -= d->c[1] * A; |
191 |
|
✗ |
out.bottomRows(nw_) -= d->c[2] * A; |
192 |
|
✗ |
break; |
193 |
|
✗ |
default: |
194 |
|
✗ |
throw_pretty("Invalid argument: allowed operators: setto, addto, rmfrom"); |
195 |
|
|
break; |
196 |
|
|
} |
197 |
|
|
} |
198 |
|
|
|
199 |
|
|
template <typename Scalar> |
200 |
|
|
template <typename NewScalar> |
201 |
|
|
ControlParametrizationModelPolyTwoRKTpl<NewScalar> |
202 |
|
✗ |
ControlParametrizationModelPolyTwoRKTpl<Scalar>::cast() const { |
203 |
|
|
typedef ControlParametrizationModelPolyTwoRKTpl<NewScalar> ReturnType; |
204 |
|
✗ |
ReturnType ret(nw_, rktype_); |
205 |
|
✗ |
return ret; |
206 |
|
|
} |
207 |
|
|
|
208 |
|
|
template <typename Scalar> |
209 |
|
✗ |
void ControlParametrizationModelPolyTwoRKTpl<Scalar>::print( |
210 |
|
|
std::ostream& os) const { |
211 |
|
✗ |
os << "ControlParametrizationModelPolyTwoRK {nw=" << nw_; |
212 |
|
✗ |
switch (rktype_) { |
213 |
|
✗ |
case two: |
214 |
|
✗ |
os << ", rktype=two}"; |
215 |
|
✗ |
break; |
216 |
|
✗ |
case three: |
217 |
|
✗ |
os << ", rktype=three}"; |
218 |
|
✗ |
break; |
219 |
|
✗ |
case four: |
220 |
|
✗ |
os << ", rktype=four}"; |
221 |
|
✗ |
break; |
222 |
|
|
} |
223 |
|
|
} |
224 |
|
|
|
225 |
|
|
} // namespace crocoddyl |
226 |
|
|
|