Line |
Branch |
Exec |
Source |
1 |
|
|
/////////////////////////////////////////////////////////////////////////////// |
2 |
|
|
// BSD 3-Clause License |
3 |
|
|
// |
4 |
|
|
// Copyright (C) 2021-2025, Heriot-Watt University, University of Edinburgh |
5 |
|
|
// Copyright note valid unless otherwise stated in individual files. |
6 |
|
|
// All rights reserved. |
7 |
|
|
/////////////////////////////////////////////////////////////////////////////// |
8 |
|
|
|
9 |
|
|
namespace crocoddyl { |
10 |
|
|
|
11 |
|
|
template <typename Scalar> |
12 |
|
✗ |
ConstraintModelResidualTpl<Scalar>::ConstraintModelResidualTpl( |
13 |
|
|
std::shared_ptr<typename Base::StateAbstract> state, |
14 |
|
|
std::shared_ptr<ResidualModelAbstract> residual, const VectorXs& lower, |
15 |
|
|
const VectorXs& upper, const bool T_act) |
16 |
|
✗ |
: Base(state, residual, residual->get_nr(), 0) { |
17 |
|
✗ |
lb_ = lower; |
18 |
|
✗ |
ub_ = upper; |
19 |
|
✗ |
for (std::size_t i = 0; i < residual_->get_nr(); ++i) { |
20 |
|
✗ |
if (isfinite(lb_(i)) && isfinite(ub_(i))) { |
21 |
|
✗ |
if (lb_(i) - ub_(i) > Scalar(0.)) { |
22 |
|
✗ |
throw_pretty( |
23 |
|
|
"Invalid argument: the upper bound is not equal to / higher than " |
24 |
|
|
"the lower bound.") |
25 |
|
|
} |
26 |
|
|
} |
27 |
|
|
} |
28 |
|
✗ |
if ((lb_.array() == std::numeric_limits<Scalar>::infinity()).any() || |
29 |
|
✗ |
(lb_.array() == std::numeric_limits<Scalar>::max()).any()) { |
30 |
|
✗ |
throw_pretty( |
31 |
|
|
"Invalid argument: the lower bound cannot contain a positive " |
32 |
|
|
"infinity/max value"); |
33 |
|
|
} |
34 |
|
✗ |
if ((ub_.array() == -std::numeric_limits<Scalar>::infinity()).any() || |
35 |
|
✗ |
(ub_.array() == -std::numeric_limits<Scalar>::infinity()).any()) { |
36 |
|
✗ |
throw_pretty( |
37 |
|
|
"Invalid argument: the lower bound cannot contain a negative " |
38 |
|
|
"infinity/max value"); |
39 |
|
|
} |
40 |
|
✗ |
if (!T_act) { |
41 |
|
✗ |
T_constraint_ = false; |
42 |
|
|
} |
43 |
|
✗ |
} |
44 |
|
|
|
45 |
|
|
template <typename Scalar> |
46 |
|
✗ |
ConstraintModelResidualTpl<Scalar>::ConstraintModelResidualTpl( |
47 |
|
|
std::shared_ptr<typename Base::StateAbstract> state, |
48 |
|
|
std::shared_ptr<ResidualModelAbstract> residual, const bool T_act) |
49 |
|
✗ |
: Base(state, residual, 0, residual->get_nr()) { |
50 |
|
✗ |
if (!T_act) { |
51 |
|
✗ |
T_constraint_ = false; |
52 |
|
|
} |
53 |
|
✗ |
} |
54 |
|
|
|
55 |
|
|
template <typename Scalar> |
56 |
|
✗ |
void ConstraintModelResidualTpl<Scalar>::calc( |
57 |
|
|
const std::shared_ptr<ConstraintDataAbstract>& data, |
58 |
|
|
const Eigen::Ref<const VectorXs>& x, const Eigen::Ref<const VectorXs>& u) { |
59 |
|
|
// Compute the constraint residual |
60 |
|
✗ |
residual_->calc(data->residual, x, u); |
61 |
|
|
|
62 |
|
|
// Fill the residual values for its corresponding type of constraint |
63 |
|
✗ |
updateCalc(data); |
64 |
|
✗ |
} |
65 |
|
|
|
66 |
|
|
template <typename Scalar> |
67 |
|
✗ |
void ConstraintModelResidualTpl<Scalar>::calc( |
68 |
|
|
const std::shared_ptr<ConstraintDataAbstract>& data, |
69 |
|
|
const Eigen::Ref<const VectorXs>& x) { |
70 |
|
|
// Compute the constraint residual |
71 |
|
✗ |
residual_->calc(data->residual, x); |
72 |
|
|
|
73 |
|
|
// Fill the residual values for its corresponding type of constraint |
74 |
|
✗ |
updateCalc(data); |
75 |
|
✗ |
} |
76 |
|
|
|
77 |
|
|
template <typename Scalar> |
78 |
|
✗ |
void ConstraintModelResidualTpl<Scalar>::calcDiff( |
79 |
|
|
const std::shared_ptr<ConstraintDataAbstract>& data, |
80 |
|
|
const Eigen::Ref<const VectorXs>& x, const Eigen::Ref<const VectorXs>& u) { |
81 |
|
|
// Compute the derivatives of the residual function |
82 |
|
✗ |
residual_->calcDiff(data->residual, x, u); |
83 |
|
|
|
84 |
|
|
// Fill the residual values for its corresponding type of constraint |
85 |
|
✗ |
updateCalcDiff(data); |
86 |
|
✗ |
} |
87 |
|
|
|
88 |
|
|
template <typename Scalar> |
89 |
|
✗ |
void ConstraintModelResidualTpl<Scalar>::calcDiff( |
90 |
|
|
const std::shared_ptr<ConstraintDataAbstract>& data, |
91 |
|
|
const Eigen::Ref<const VectorXs>& x) { |
92 |
|
|
// Compute the derivatives of the residual function |
93 |
|
✗ |
residual_->calcDiff(data->residual, x); |
94 |
|
|
|
95 |
|
|
// Fill the residual values for its corresponding type of constraint |
96 |
|
✗ |
updateCalcDiff(data); |
97 |
|
✗ |
} |
98 |
|
|
|
99 |
|
|
template <typename Scalar> |
100 |
|
|
std::shared_ptr<ConstraintDataAbstractTpl<Scalar> > |
101 |
|
✗ |
ConstraintModelResidualTpl<Scalar>::createData( |
102 |
|
|
DataCollectorAbstract* const data) { |
103 |
|
✗ |
return std::allocate_shared<Data>(Eigen::aligned_allocator<Data>(), this, |
104 |
|
✗ |
data); |
105 |
|
|
} |
106 |
|
|
|
107 |
|
|
template <typename Scalar> |
108 |
|
✗ |
void ConstraintModelResidualTpl<Scalar>::updateCalc( |
109 |
|
|
const std::shared_ptr<ConstraintDataAbstract>& data) { |
110 |
|
✗ |
switch (type_) { |
111 |
|
✗ |
case ConstraintType::Inequality: |
112 |
|
✗ |
data->g = data->residual->r; |
113 |
|
✗ |
break; |
114 |
|
✗ |
case ConstraintType::Equality: |
115 |
|
✗ |
data->h = data->residual->r; |
116 |
|
✗ |
break; |
117 |
|
✗ |
case ConstraintType::Both: // this condition is not supported and possible |
118 |
|
✗ |
break; |
119 |
|
|
} |
120 |
|
✗ |
} |
121 |
|
|
|
122 |
|
|
template <typename Scalar> |
123 |
|
✗ |
void ConstraintModelResidualTpl<Scalar>::updateCalcDiff( |
124 |
|
|
const std::shared_ptr<ConstraintDataAbstract>& data) { |
125 |
|
✗ |
const bool is_rq = residual_->get_q_dependent(); |
126 |
|
✗ |
const bool is_rv = residual_->get_v_dependent(); |
127 |
|
✗ |
const bool is_ru = residual_->get_u_dependent() || nu_ == 0; |
128 |
|
✗ |
switch (type_) { |
129 |
|
✗ |
case ConstraintType::Inequality: |
130 |
|
✗ |
if (is_rq && is_rv) { |
131 |
|
✗ |
data->Gx = data->residual->Rx; |
132 |
|
✗ |
} else if (is_rq) { |
133 |
|
✗ |
const std::size_t nv = state_->get_nv(); |
134 |
|
✗ |
data->Gx.leftCols(nv) = data->residual->Rx.leftCols(nv); |
135 |
|
✗ |
data->Gx.rightCols(nv).setZero(); |
136 |
|
✗ |
} else if (is_rv) { |
137 |
|
✗ |
const std::size_t nv = state_->get_nv(); |
138 |
|
✗ |
data->Gx.leftCols(nv).setZero(); |
139 |
|
✗ |
data->Gx.rightCols(nv) = data->residual->Rx.rightCols(nv); |
140 |
|
|
} |
141 |
|
✗ |
if (is_ru) { |
142 |
|
✗ |
data->Gu = data->residual->Ru; |
143 |
|
|
} |
144 |
|
✗ |
break; |
145 |
|
✗ |
case ConstraintType::Equality: |
146 |
|
✗ |
if (is_rq && is_rv) { |
147 |
|
✗ |
data->Hx = data->residual->Rx; |
148 |
|
✗ |
} else if (is_rq) { |
149 |
|
✗ |
const std::size_t nv = state_->get_nv(); |
150 |
|
✗ |
data->Hx.leftCols(nv) = data->residual->Rx.leftCols(nv); |
151 |
|
✗ |
data->Hx.rightCols(nv).setZero(); |
152 |
|
✗ |
} else if (is_rv) { |
153 |
|
✗ |
const std::size_t nv = state_->get_nv(); |
154 |
|
✗ |
data->Hx.leftCols(nv).setZero(); |
155 |
|
✗ |
data->Hx.rightCols(nv) = data->residual->Rx.rightCols(nv); |
156 |
|
|
} |
157 |
|
✗ |
if (is_ru) { |
158 |
|
✗ |
data->Hu = data->residual->Ru; |
159 |
|
|
} |
160 |
|
✗ |
break; |
161 |
|
✗ |
case ConstraintType::Both: // this condition is not supported and possible |
162 |
|
✗ |
break; |
163 |
|
|
} |
164 |
|
✗ |
} |
165 |
|
|
|
166 |
|
|
template <typename Scalar> |
167 |
|
|
template <typename NewScalar> |
168 |
|
✗ |
ConstraintModelResidualTpl<NewScalar> ConstraintModelResidualTpl<Scalar>::cast() |
169 |
|
|
const { |
170 |
|
|
typedef ConstraintModelResidualTpl<NewScalar> ReturnType; |
171 |
|
✗ |
if (type_ == ConstraintType::Inequality) { |
172 |
|
✗ |
ReturnType ret(state_->template cast<NewScalar>(), |
173 |
|
✗ |
residual_->template cast<NewScalar>(), |
174 |
|
✗ |
lb_.template cast<NewScalar>(), |
175 |
|
✗ |
ub_.template cast<NewScalar>(), T_constraint_); |
176 |
|
✗ |
return ret; |
177 |
|
✗ |
} else { |
178 |
|
✗ |
ReturnType ret(state_->template cast<NewScalar>(), |
179 |
|
✗ |
residual_->template cast<NewScalar>(), T_constraint_); |
180 |
|
✗ |
return ret; |
181 |
|
✗ |
} |
182 |
|
|
} |
183 |
|
|
|
184 |
|
|
template <typename Scalar> |
185 |
|
✗ |
void ConstraintModelResidualTpl<Scalar>::print(std::ostream& os) const { |
186 |
|
✗ |
os << "ConstraintModelResidual {" << *residual_ << "}"; |
187 |
|
✗ |
} |
188 |
|
|
|
189 |
|
|
} // namespace crocoddyl |
190 |
|
|
|