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 |
|
✗ |
ControlParametrizationModelPolyOneTpl< |
14 |
|
|
Scalar>::ControlParametrizationModelPolyOneTpl(const std::size_t nw) |
15 |
|
✗ |
: Base(nw, 2 * nw) {} |
16 |
|
|
|
17 |
|
|
template <typename Scalar> |
18 |
|
✗ |
void ControlParametrizationModelPolyOneTpl<Scalar>::calc( |
19 |
|
|
const std::shared_ptr<ControlParametrizationDataAbstract>& data, |
20 |
|
|
const Scalar t, const Eigen::Ref<const VectorXs>& u) const { |
21 |
|
✗ |
if (static_cast<std::size_t>(u.size()) != nu_) { |
22 |
|
✗ |
throw_pretty( |
23 |
|
|
"Invalid argument: " << "u has wrong dimension (it should be " + |
24 |
|
|
std::to_string(nu_) + ")"); |
25 |
|
|
} |
26 |
|
✗ |
Data* d = static_cast<Data*>(data.get()); |
27 |
|
✗ |
d->c[1] = Scalar(2.) * t; |
28 |
|
✗ |
d->c[0] = Scalar(1.) - d->c[1]; |
29 |
|
✗ |
data->w = d->c[0] * u.head(nw_) + d->c[1] * u.tail(nw_); |
30 |
|
|
} |
31 |
|
|
|
32 |
|
|
template <typename Scalar> |
33 |
|
✗ |
void ControlParametrizationModelPolyOneTpl<Scalar>::calcDiff( |
34 |
|
|
const std::shared_ptr<ControlParametrizationDataAbstract>& data, |
35 |
|
|
const Scalar, const Eigen::Ref<const VectorXs>&) const { |
36 |
|
✗ |
Data* d = static_cast<Data*>(data.get()); |
37 |
|
✗ |
data->dw_du.leftCols(nw_).diagonal().array() = d->c[0]; |
38 |
|
✗ |
data->dw_du.rightCols(nw_).diagonal().array() = d->c[1]; |
39 |
|
|
} |
40 |
|
|
|
41 |
|
|
template <typename Scalar> |
42 |
|
|
std::shared_ptr<ControlParametrizationDataAbstractTpl<Scalar> > |
43 |
|
✗ |
ControlParametrizationModelPolyOneTpl<Scalar>::createData() { |
44 |
|
✗ |
return std::allocate_shared<Data>(Eigen::aligned_allocator<Data>(), this); |
45 |
|
|
} |
46 |
|
|
|
47 |
|
|
template <typename Scalar> |
48 |
|
✗ |
void ControlParametrizationModelPolyOneTpl<Scalar>::params( |
49 |
|
|
const std::shared_ptr<ControlParametrizationDataAbstract>& data, |
50 |
|
|
const Scalar, const Eigen::Ref<const VectorXs>& w) const { |
51 |
|
✗ |
if (static_cast<std::size_t>(w.size()) != nw_) { |
52 |
|
✗ |
throw_pretty( |
53 |
|
|
"Invalid argument: " << "w has wrong dimension (it should be " + |
54 |
|
|
std::to_string(nw_) + ")"); |
55 |
|
|
} |
56 |
|
✗ |
data->u.head(nw_) = w; |
57 |
|
✗ |
data->u.tail(nw_) = w; |
58 |
|
|
} |
59 |
|
|
|
60 |
|
|
template <typename Scalar> |
61 |
|
✗ |
void ControlParametrizationModelPolyOneTpl<Scalar>::convertBounds( |
62 |
|
|
const Eigen::Ref<const VectorXs>& w_lb, |
63 |
|
|
const Eigen::Ref<const VectorXs>& w_ub, Eigen::Ref<VectorXs> u_lb, |
64 |
|
|
Eigen::Ref<VectorXs> u_ub) const { |
65 |
|
✗ |
if (static_cast<std::size_t>(u_lb.size()) != nu_) { |
66 |
|
✗ |
throw_pretty( |
67 |
|
|
"Invalid argument: " << "u_lb has wrong dimension (it should be " + |
68 |
|
|
std::to_string(nu_) + ")"); |
69 |
|
|
} |
70 |
|
✗ |
if (static_cast<std::size_t>(u_ub.size()) != nu_) { |
71 |
|
✗ |
throw_pretty( |
72 |
|
|
"Invalid argument: " << "u_ub has wrong dimension (it should be " + |
73 |
|
|
std::to_string(nu_) + ")"); |
74 |
|
|
} |
75 |
|
✗ |
if (static_cast<std::size_t>(w_lb.size()) != nw_) { |
76 |
|
✗ |
throw_pretty( |
77 |
|
|
"Invalid argument: " << "w_lb has wrong dimension (it should be " + |
78 |
|
|
std::to_string(nw_) + ")"); |
79 |
|
|
} |
80 |
|
✗ |
if (static_cast<std::size_t>(w_ub.size()) != nw_) { |
81 |
|
✗ |
throw_pretty( |
82 |
|
|
"Invalid argument: " << "w_ub has wrong dimension (it should be " + |
83 |
|
|
std::to_string(nw_) + ")"); |
84 |
|
|
} |
85 |
|
✗ |
u_lb.head(nw_) = w_lb; |
86 |
|
✗ |
u_lb.tail(nw_) = w_lb; |
87 |
|
✗ |
u_ub.head(nw_) = w_ub; |
88 |
|
✗ |
u_ub.tail(nw_) = w_ub; |
89 |
|
|
} |
90 |
|
|
|
91 |
|
|
template <typename Scalar> |
92 |
|
✗ |
void ControlParametrizationModelPolyOneTpl<Scalar>::multiplyByJacobian( |
93 |
|
|
const std::shared_ptr<ControlParametrizationDataAbstract>& data, |
94 |
|
|
const Eigen::Ref<const MatrixXs>& A, Eigen::Ref<MatrixXs> out, |
95 |
|
|
const AssignmentOp op) const { |
96 |
|
✗ |
assert_pretty(is_a_AssignmentOp(op), |
97 |
|
|
("op must be one of the AssignmentOp {settop, addto, rmfrom}")); |
98 |
|
✗ |
if (A.rows() != out.rows() || static_cast<std::size_t>(A.cols()) != nw_ || |
99 |
|
✗ |
static_cast<std::size_t>(out.cols()) != nu_) { |
100 |
|
✗ |
throw_pretty("Invalid argument: " << "A and out have wrong dimensions (" + |
101 |
|
|
std::to_string(A.rows()) + "," + |
102 |
|
|
std::to_string(A.cols()) + |
103 |
|
|
" and " + |
104 |
|
|
std::to_string(out.rows()) + "," + |
105 |
|
|
std::to_string(out.cols()) + +")"); |
106 |
|
|
} |
107 |
|
✗ |
Data* d = static_cast<Data*>(data.get()); |
108 |
|
✗ |
switch (op) { |
109 |
|
✗ |
case setto: |
110 |
|
✗ |
out.leftCols(nw_) = d->c[0] * A; |
111 |
|
✗ |
out.rightCols(nw_) = d->c[1] * A; |
112 |
|
✗ |
break; |
113 |
|
✗ |
case addto: |
114 |
|
✗ |
out.leftCols(nw_) += d->c[0] * A; |
115 |
|
✗ |
out.rightCols(nw_) += d->c[1] * A; |
116 |
|
✗ |
break; |
117 |
|
✗ |
case rmfrom: |
118 |
|
✗ |
out.leftCols(nw_) -= d->c[0] * A; |
119 |
|
✗ |
out.rightCols(nw_) -= d->c[1] * A; |
120 |
|
✗ |
break; |
121 |
|
✗ |
default: |
122 |
|
✗ |
throw_pretty("Invalid argument: allowed operators: setto, addto, rmfrom"); |
123 |
|
|
break; |
124 |
|
|
} |
125 |
|
|
} |
126 |
|
|
|
127 |
|
|
template <typename Scalar> |
128 |
|
✗ |
void ControlParametrizationModelPolyOneTpl<Scalar>::multiplyJacobianTransposeBy( |
129 |
|
|
const std::shared_ptr<ControlParametrizationDataAbstract>& data, |
130 |
|
|
const Eigen::Ref<const MatrixXs>& A, Eigen::Ref<MatrixXs> out, |
131 |
|
|
const AssignmentOp op) const { |
132 |
|
✗ |
assert_pretty(is_a_AssignmentOp(op), |
133 |
|
|
("op must be one of the AssignmentOp {settop, addto, rmfrom}")); |
134 |
|
✗ |
if (A.cols() != out.cols() || static_cast<std::size_t>(A.rows()) != nw_ || |
135 |
|
✗ |
static_cast<std::size_t>(out.rows()) != nu_) { |
136 |
|
✗ |
throw_pretty("Invalid argument: " << "A and out have wrong dimensions (" + |
137 |
|
|
std::to_string(A.rows()) + "," + |
138 |
|
|
std::to_string(A.cols()) + |
139 |
|
|
" and " + |
140 |
|
|
std::to_string(out.rows()) + "," + |
141 |
|
|
std::to_string(out.cols()) + ")"); |
142 |
|
|
} |
143 |
|
✗ |
Data* d = static_cast<Data*>(data.get()); |
144 |
|
✗ |
switch (op) { |
145 |
|
✗ |
case setto: |
146 |
|
✗ |
out.topRows(nw_) = d->c[0] * A; |
147 |
|
✗ |
out.bottomRows(nw_) = d->c[1] * A; |
148 |
|
✗ |
break; |
149 |
|
✗ |
case addto: |
150 |
|
✗ |
out.topRows(nw_) += d->c[0] * A; |
151 |
|
✗ |
out.bottomRows(nw_) += d->c[1] * A; |
152 |
|
✗ |
break; |
153 |
|
✗ |
case rmfrom: |
154 |
|
✗ |
out.topRows(nw_) -= d->c[0] * A; |
155 |
|
✗ |
out.bottomRows(nw_) -= d->c[1] * A; |
156 |
|
✗ |
break; |
157 |
|
✗ |
default: |
158 |
|
✗ |
throw_pretty("Invalid argument: allowed operators: setto, addto, rmfrom"); |
159 |
|
|
break; |
160 |
|
|
} |
161 |
|
|
} |
162 |
|
|
|
163 |
|
|
template <typename Scalar> |
164 |
|
|
template <typename NewScalar> |
165 |
|
|
ControlParametrizationModelPolyOneTpl<NewScalar> |
166 |
|
✗ |
ControlParametrizationModelPolyOneTpl<Scalar>::cast() const { |
167 |
|
|
typedef ControlParametrizationModelPolyOneTpl<NewScalar> ReturnType; |
168 |
|
✗ |
ReturnType ret(nw_); |
169 |
|
✗ |
return ret; |
170 |
|
|
} |
171 |
|
|
|
172 |
|
|
template <typename Scalar> |
173 |
|
✗ |
void ControlParametrizationModelPolyOneTpl<Scalar>::print( |
174 |
|
|
std::ostream& os) const { |
175 |
|
✗ |
os << "ControlParametrizationModelPolyZero {nw=" << nw_ << "}"; |
176 |
|
|
} |
177 |
|
|
|
178 |
|
|
} // namespace crocoddyl |
179 |
|
|
|