Directory: | ./ |
---|---|
File: | include/crocoddyl/core/constraint-base.hxx |
Date: | 2025-03-26 19:23:43 |
Exec | Total | Coverage | |
---|---|---|---|
Lines: | 50 | 105 | 47.6% |
Branches: | 30 | 200 | 15.0% |
Line | Branch | Exec | Source |
---|---|---|---|
1 | /////////////////////////////////////////////////////////////////////////////// | ||
2 | // BSD 3-Clause License | ||
3 | // | ||
4 | // Copyright (C) 2020-2025, University of Edinburgh, Heriot-Watt University | ||
5 | // Copyright note valid unless otherwise stated in individual files. | ||
6 | // All rights reserved. | ||
7 | /////////////////////////////////////////////////////////////////////////////// | ||
8 | |||
9 | #include <boost/core/demangle.hpp> | ||
10 | #include <limits> | ||
11 | |||
12 | namespace crocoddyl { | ||
13 | |||
14 | template <typename Scalar> | ||
15 | 2661 | ConstraintModelAbstractTpl<Scalar>::ConstraintModelAbstractTpl( | |
16 | std::shared_ptr<StateAbstract> state, | ||
17 | std::shared_ptr<ResidualModelAbstract> residual, const std::size_t ng, | ||
18 | const std::size_t nh) | ||
19 | 2661 | : ng_internal_(ng), | |
20 | 2661 | nh_internal_(nh), | |
21 | 2661 | state_(state), | |
22 | 2661 | residual_(residual), | |
23 |
3/4✓ Branch 0 taken 236 times.
✓ Branch 1 taken 2425 times.
✓ Branch 2 taken 236 times.
✗ Branch 3 not taken.
|
2661 | type_((ng > 0 && nh > 0) ? ConstraintType::Both |
24 | 2661 | : (ng > 0 ? ConstraintType::Inequality | |
25 | : ConstraintType::Equality)), | ||
26 |
2/4✓ Branch 2 taken 2661 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 2661 times.
✗ Branch 6 not taken.
|
2661 | lb_(VectorXs::Constant(ng, -std::numeric_limits<Scalar>::infinity())), |
27 |
2/4✓ Branch 2 taken 2661 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 2661 times.
✗ Branch 6 not taken.
|
2661 | ub_(VectorXs::Constant(ng, std::numeric_limits<Scalar>::infinity())), |
28 |
1/2✓ Branch 2 taken 2661 times.
✗ Branch 3 not taken.
|
2661 | nu_(residual->get_nu()), |
29 | 2661 | ng_(ng), | |
30 | 2661 | nh_(nh), | |
31 |
2/4✓ Branch 2 taken 2661 times.
✗ Branch 3 not taken.
✓ Branch 6 taken 757 times.
✗ Branch 7 not taken.
|
3418 | T_constraint_(residual->get_q_dependent() || residual->get_v_dependent() |
32 |
3/4✓ Branch 0 taken 757 times.
✓ Branch 1 taken 1904 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 757 times.
|
3418 | ? true |
33 | : false), | ||
34 |
3/6✓ Branch 4 taken 2661 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 2661 times.
✗ Branch 8 not taken.
✓ Branch 10 taken 2661 times.
✗ Branch 11 not taken.
|
5322 | unone_(VectorXs::Zero(residual->get_nu())) { |
35 |
2/4✓ Branch 2 taken 2661 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 2661 times.
|
2661 | if (nh_ > residual_->get_nr()) { |
36 | ✗ | throw_pretty("Invalid argument: " | |
37 | << "the number of equality constraints (nh) is wrong as it is " | ||
38 | "bigger than the residual dimension.") | ||
39 | } | ||
40 |
1/2✓ Branch 2 taken 2661 times.
✗ Branch 3 not taken.
|
2661 | std::size_t max_ng = 2 * (residual_->get_nr() - nh_); |
41 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2661 times.
|
2661 | if (ng_ > max_ng) { |
42 | ✗ | throw_pretty("Invalid argument: " | |
43 | << "the number of inequality constraints (ng) is wrong as it " | ||
44 | "should be in the range [0, " + | ||
45 | std::to_string(max_ng) + "]"); | ||
46 | } | ||
47 | 2661 | } | |
48 | |||
49 | template <typename Scalar> | ||
50 | 140 | ConstraintModelAbstractTpl<Scalar>::ConstraintModelAbstractTpl( | |
51 | std::shared_ptr<StateAbstract> state, const std::size_t nu, | ||
52 | const std::size_t ng, const std::size_t nh, const bool T_const) | ||
53 | 140 | : ng_internal_(ng), | |
54 | 140 | nh_internal_(nh), | |
55 | 140 | state_(state), | |
56 | 140 | residual_(std::make_shared<ResidualModelAbstractTpl<Scalar>>( | |
57 |
1/2✓ Branch 1 taken 140 times.
✗ Branch 2 not taken.
|
140 | state, ng + nh, nu)), |
58 |
3/4✓ Branch 0 taken 70 times.
✓ Branch 1 taken 70 times.
✓ Branch 2 taken 70 times.
✗ Branch 3 not taken.
|
140 | type_((ng > 0 && nh > 0) ? ConstraintType::Both |
59 | 140 | : (ng > 0 ? ConstraintType::Inequality | |
60 | : ConstraintType::Equality)), | ||
61 |
2/4✓ Branch 2 taken 140 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 140 times.
✗ Branch 6 not taken.
|
140 | lb_(VectorXs::Constant(ng, -std::numeric_limits<Scalar>::infinity())), |
62 |
2/4✓ Branch 2 taken 140 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 140 times.
✗ Branch 6 not taken.
|
140 | ub_(VectorXs::Constant(ng, std::numeric_limits<Scalar>::infinity())), |
63 | 140 | nu_(nu), | |
64 | 140 | ng_(ng), | |
65 | 140 | nh_(nh), | |
66 | 140 | T_constraint_(T_const), | |
67 |
2/4✓ Branch 3 taken 140 times.
✗ Branch 4 not taken.
✓ Branch 6 taken 140 times.
✗ Branch 7 not taken.
|
280 | unone_(VectorXs::Zero(nu)) {} |
68 | |||
69 | template <typename Scalar> | ||
70 | ✗ | ConstraintModelAbstractTpl<Scalar>::ConstraintModelAbstractTpl( | |
71 | std::shared_ptr<StateAbstract> state, const std::size_t ng, | ||
72 | const std::size_t nh, const bool T_const) | ||
73 | ✗ | : ng_internal_(ng), | |
74 | ✗ | nh_internal_(nh), | |
75 | ✗ | state_(state), | |
76 | ✗ | residual_( | |
77 | ✗ | std::make_shared<ResidualModelAbstractTpl<Scalar>>(state, ng + nh)), | |
78 | ✗ | type_((ng > 0 && nh > 0) ? ConstraintType::Both | |
79 | ✗ | : (ng > 0 ? ConstraintType::Inequality | |
80 | : ConstraintType::Equality)), | ||
81 | ✗ | lb_(VectorXs::Constant(ng, -std::numeric_limits<Scalar>::infinity())), | |
82 | ✗ | ub_(VectorXs::Constant(ng, std::numeric_limits<Scalar>::infinity())), | |
83 | ✗ | nu_(state->get_nv()), | |
84 | ✗ | ng_(ng), | |
85 | ✗ | nh_(nh), | |
86 | ✗ | T_constraint_(T_const), | |
87 | ✗ | unone_(VectorXs::Zero(state->get_nv())) {} | |
88 | |||
89 | template <typename Scalar> | ||
90 | ✗ | void ConstraintModelAbstractTpl<Scalar>::calc( | |
91 | const std::shared_ptr<ConstraintDataAbstract>& data, | ||
92 | const Eigen::Ref<const VectorXs>& x) { | ||
93 | ✗ | calc(data, x, unone_); | |
94 | } | ||
95 | |||
96 | template <typename Scalar> | ||
97 | ✗ | void ConstraintModelAbstractTpl<Scalar>::calcDiff( | |
98 | const std::shared_ptr<ConstraintDataAbstract>& data, | ||
99 | const Eigen::Ref<const VectorXs>& x) { | ||
100 | ✗ | calcDiff(data, x, unone_); | |
101 | } | ||
102 | |||
103 | template <typename Scalar> | ||
104 | std::shared_ptr<ConstraintDataAbstractTpl<Scalar>> | ||
105 | ✗ | ConstraintModelAbstractTpl<Scalar>::createData( | |
106 | DataCollectorAbstract* const data) { | ||
107 | return std::allocate_shared<ConstraintDataAbstract>( | ||
108 | ✗ | Eigen::aligned_allocator<ConstraintDataAbstract>(), this, data); | |
109 | } | ||
110 | |||
111 | template <typename Scalar> | ||
112 | ✗ | void ConstraintModelAbstractTpl<Scalar>::update_bounds(const VectorXs& lower, | |
113 | const VectorXs& upper) { | ||
114 | ✗ | if (static_cast<std::size_t>(upper.size()) != ng_internal_ || | |
115 | ✗ | static_cast<std::size_t>(lower.size()) != ng_internal_) { | |
116 | ✗ | throw_pretty( | |
117 | "Invalid argument: the dimension of the lower/upper bound is not the " | ||
118 | "same to ng.") | ||
119 | } | ||
120 | ✗ | if (((upper - lower).array() <= Scalar(0.)).any()) { | |
121 | ✗ | throw_pretty( | |
122 | "Invalid argument: the upper bound is not higher than the lower bound.") | ||
123 | } | ||
124 | ✗ | if ((lb_.array() == std::numeric_limits<Scalar>::infinity()).any() || | |
125 | ✗ | (lb_.array() == std::numeric_limits<Scalar>::max()).any()) { | |
126 | ✗ | throw_pretty( | |
127 | "Invalid argument: the lower bound cannot contain a positive " | ||
128 | "infinity/max value"); | ||
129 | } | ||
130 | ✗ | if ((ub_.array() == -std::numeric_limits<Scalar>::infinity()).any() || | |
131 | ✗ | (ub_.array() == -std::numeric_limits<Scalar>::infinity()).any()) { | |
132 | ✗ | throw_pretty( | |
133 | "Invalid argument: the lower bound cannot contain a negative " | ||
134 | "infinity/min value"); | ||
135 | } | ||
136 | ✗ | ng_ = ng_internal_; | |
137 | ✗ | nh_ = nh_internal_; | |
138 | ✗ | lb_ = lower; | |
139 | ✗ | ub_ = upper; | |
140 | ✗ | if (nh_ == 0) { | |
141 | ✗ | type_ = ConstraintType::Inequality; | |
142 | } else { | ||
143 | ✗ | type_ = ConstraintType::Both; | |
144 | } | ||
145 | } | ||
146 | |||
147 | template <typename Scalar> | ||
148 | ✗ | void ConstraintModelAbstractTpl<Scalar>::remove_bounds() { | |
149 | ✗ | ng_ = 0; | |
150 | ✗ | nh_ = nh_internal_ + ng_internal_; | |
151 | ✗ | lb_.setConstant(-std::numeric_limits<Scalar>::infinity()); | |
152 | ✗ | ub_.setConstant(std::numeric_limits<Scalar>::infinity()); | |
153 | ✗ | type_ = ConstraintType::Equality; | |
154 | } | ||
155 | |||
156 | template <typename Scalar> | ||
157 | ✗ | void ConstraintModelAbstractTpl<Scalar>::print(std::ostream& os) const { | |
158 | ✗ | os << boost::core::demangle(typeid(*this).name()); | |
159 | } | ||
160 | |||
161 | template <typename Scalar> | ||
162 | const std::shared_ptr<StateAbstractTpl<Scalar>>& | ||
163 | 467218 | ConstraintModelAbstractTpl<Scalar>::get_state() const { | |
164 | 467218 | return state_; | |
165 | } | ||
166 | |||
167 | template <typename Scalar> | ||
168 | const std::shared_ptr<ResidualModelAbstractTpl<Scalar>>& | ||
169 | 229808 | ConstraintModelAbstractTpl<Scalar>::get_residual() const { | |
170 | 229808 | return residual_; | |
171 | } | ||
172 | |||
173 | template <typename Scalar> | ||
174 | ✗ | ConstraintType ConstraintModelAbstractTpl<Scalar>::get_type() const { | |
175 | ✗ | return type_; | |
176 | } | ||
177 | |||
178 | template <typename Scalar> | ||
179 | const typename MathBaseTpl<Scalar>::VectorXs& | ||
180 | 120535 | ConstraintModelAbstractTpl<Scalar>::get_lb() const { | |
181 | 120535 | return lb_; | |
182 | } | ||
183 | |||
184 | template <typename Scalar> | ||
185 | const typename MathBaseTpl<Scalar>::VectorXs& | ||
186 | 120535 | ConstraintModelAbstractTpl<Scalar>::get_ub() const { | |
187 | 120535 | return ub_; | |
188 | } | ||
189 | |||
190 | template <typename Scalar> | ||
191 | 464687 | std::size_t ConstraintModelAbstractTpl<Scalar>::get_nu() const { | |
192 | 464687 | return nu_; | |
193 | } | ||
194 | |||
195 | template <typename Scalar> | ||
196 | 1061875 | std::size_t ConstraintModelAbstractTpl<Scalar>::get_ng() const { | |
197 | 1061875 | return ng_; | |
198 | } | ||
199 | |||
200 | template <typename Scalar> | ||
201 | 1054568 | std::size_t ConstraintModelAbstractTpl<Scalar>::get_nh() const { | |
202 | 1054568 | return nh_; | |
203 | } | ||
204 | |||
205 | template <typename Scalar> | ||
206 | 15922 | bool ConstraintModelAbstractTpl<Scalar>::get_T_constraint() const { | |
207 | 15922 | return T_constraint_; | |
208 | } | ||
209 | |||
210 | template <class Scalar> | ||
211 | ✗ | std::ostream& operator<<(std::ostream& os, | |
212 | const ConstraintModelAbstractTpl<Scalar>& model) { | ||
213 | ✗ | model.print(os); | |
214 | ✗ | return os; | |
215 | } | ||
216 | |||
217 | } // namespace crocoddyl | ||
218 |