Sleipnir C++ API
Loading...
Searching...
No Matches
bounds.hpp
1// Copyright (c) Sleipnir contributors
2
3#pragma once
4
5#include <algorithm>
6#include <cmath>
7#include <limits>
8#include <span>
9#include <utility>
10
11#include <Eigen/Core>
12#include <Eigen/SparseCore>
13#include <gch/small_vector.hpp>
14
15#include "sleipnir/autodiff/expression_type.hpp"
16#include "sleipnir/autodiff/variable.hpp"
17#include "sleipnir/util/assert.hpp"
18
19// See docs/algorithms.md#Works_cited for citation definitions
20
21namespace slp {
22
26template <typename Scalar>
27struct Bounds {
29 Eigen::ArrayX<bool> bound_constraint_mask;
30
32 gch::small_vector<std::pair<Scalar, Scalar>> bounds;
33
36 gch::small_vector<std::pair<Eigen::Index, Eigen::Index>>
38};
39
53template <typename Scalar>
56 const Eigen::SparseMatrix<Scalar>& A_i) {
57 // NB: Casting to long is unspecified if the size of decision_variable.size()
58 // is greater than the max long value, but then we wouldn't be able to fill
59 // A_i correctly anyway.
60 slp_assert(static_cast<Eigen::Index>(decision_variables.size()) ==
61 A_i.cols());
62 slp_assert(static_cast<Eigen::Index>(inequality_constraints.size()) ==
63 A_i.rows());
64
65 // For each row of A_i, count its structural nonzeros and record the column
66 // and value of its most recently visited nonzero. If the count is 1, that's
67 // the row's only nonzero; otherwise, the recorded column and value are
68 // meaningless, but those rows aren't bound constraints, so they're never
69 // read.
70 gch::small_vector<int> row_num_nonzeros(A_i.rows(), 0);
71 gch::small_vector<int> row_nonzero_col(A_i.rows());
72 gch::small_vector<Scalar> row_nonzero_value(A_i.rows());
73 for (int col = 0; col < A_i.outerSize(); ++col) {
74 for (typename Eigen::SparseMatrix<Scalar>::InnerIterator it{A_i, col}; it;
75 ++it) {
76 ++row_num_nonzeros[it.row()];
77 row_nonzero_col[it.row()] = it.col();
78 row_nonzero_value[it.row()] = it.value();
79 }
80 }
81
82 // Maps each decision variable's index to the indices of its upper and lower
83 // bounds if they exist, or NO_BOUND if they do not; used only for bookkeeping
84 // to compute conflicting bounds
85 constexpr Eigen::Index NO_BOUND = -1;
86 gch::small_vector<std::pair<Eigen::Index, Eigen::Index>>
87 decision_var_indices_to_constraint_indices{decision_variables.size(),
88 {NO_BOUND, NO_BOUND}};
89 // Lists pairs of indices of bound constraints in the inequality constraint
90 // list that conflict with each other
91 gch::small_vector<std::pair<Eigen::Index, Eigen::Index>>
92 conflicting_bound_indices;
93
94 // Maps each decision variable's index to its upper and lower bounds
95 gch::small_vector<std::pair<Scalar, Scalar>> decision_var_indices_to_bounds{
96 decision_variables.size(),
97 {-std::numeric_limits<Scalar>::infinity(),
98 std::numeric_limits<Scalar>::infinity()}};
99
100 // Vector corresponding to the inequality constraints where the i-th element
101 // is 1 if the i-th inequality constraint is a bound and 0 otherwise.
102 Eigen::ArrayX<bool> bound_constraint_mask{inequality_constraints.size()};
103 bound_constraint_mask.fill(false);
104
105 for (typename decltype(inequality_constraints)::size_type constraint_index =
106 0;
107 constraint_index < inequality_constraints.size(); ++constraint_index) {
108 // A constraint is a bound iff it is linear and its gradient has a
109 // single nonzero value.
110 if (inequality_constraints[constraint_index].type() !=
111 ExpressionType::LINEAR) {
112 continue;
113 }
114 if (row_num_nonzeros[constraint_index] != 1) {
115 // Constraint is not a bound because either:
116 //
117 // 1. It's in more than one variable
118 // 2. It's in no decision variables (e.g., it only depends on variables
119 // that aren't decision variables of this problem)
120 continue;
121 }
122
123 // Claim: The bound given by a bound constraint is the constraint evaluated
124 // at zero divided by the nonzero element of the constraint's gradient.
125 //
126 // Proof: If c(x) is a bound constraint, then by definition c is a linear
127 // function in one variable, hence there exist a, b ∈ ℝ s.t. c(x) = axᵢ + b
128 // and a ≠ 0. The gradient of c is then aeᵢ (where eᵢ denotes the i-th basis
129 // element), and c(0) = b. If c(x) ≥ 0, then since either a < 0 or a > 0, we
130 // have either x ≤ -b/a or x ≥ -b/a, respectively. ∎
131 //
132 // The nonzero value of the j-th constraint's gradient.
133 const auto constraint_coefficient = row_nonzero_value[constraint_index];
134
135 // The Jacobian's sparsity pattern is structural, so the single stored value
136 // can be zero if the constraint's terms cancel (e.g., x − x + 1 ≥ 0). The
137 // constraint is then constant and not a bound.
138 if (constraint_coefficient == Scalar(0)) {
139 continue;
140 }
141
142 const auto decision_variable_index = row_nonzero_col[constraint_index];
143 const auto decision_variable_value =
144 decision_variables[decision_variable_index].value();
145 Scalar constraint_constant;
146 // We need to evaluate this constraint *exactly* at zero.
147 if (decision_variable_value != Scalar(0)) {
148 decision_variables[decision_variable_index].set_value(Scalar(0));
149 constraint_constant = inequality_constraints[constraint_index].value();
150 decision_variables[decision_variable_index].set_value(
151 decision_variable_value);
152 } else {
153 constraint_constant = inequality_constraints[constraint_index].value();
154 }
155
156 using std::isfinite;
157
158 // The constraint's constant or coefficient can be nonfinite if the user
159 // wrote a constraint like x ≥ −∞ or ∞x ≥ 1. Note that this constraint will
160 // cause the solver to report a nonfinite initial guess later.
161 if (!isfinite(constraint_constant) || !isfinite(constraint_coefficient)) {
162 continue;
163 }
164
165 // Update bounds; we assume constraints of the form c(x) ≥ 0.
166 auto& [lower_bound, upper_bound] =
167 decision_var_indices_to_bounds[decision_variable_index];
168 auto& [lower_index, upper_index] =
169 decision_var_indices_to_constraint_indices[decision_variable_index];
170 const auto detected_bound = -constraint_constant / constraint_coefficient;
171 if (constraint_coefficient < Scalar(0) && detected_bound < upper_bound) {
172 upper_bound = detected_bound;
173 upper_index = constraint_index;
174 } else if (constraint_coefficient > Scalar(0) &&
175 detected_bound > lower_bound) {
176 lower_bound = detected_bound;
177 lower_index = constraint_index;
178 }
179
180 // Update conflicting bounds
181 if (lower_bound > upper_bound) {
182 conflicting_bound_indices.emplace_back(lower_index, upper_index);
183 }
184
185 // Set the bound constraint mask appropriately.
186 bound_constraint_mask[constraint_index] = true;
187 }
188 return {bound_constraint_mask, decision_var_indices_to_bounds,
189 conflicting_bound_indices};
190}
191
204template <typename Derived>
205 requires(static_cast<bool>(Eigen::DenseBase<Derived>::IsVectorAtCompileTime))
206void project_onto_bounds(
207 Eigen::DenseBase<Derived>& x,
208 std::span<const std::pair<typename Eigen::DenseBase<Derived>::Scalar,
209 typename Eigen::DenseBase<Derived>::Scalar>>
210 decision_variable_indices_to_bounds,
211 const typename Eigen::DenseBase<Derived>::Scalar κ_1 =
212 typename Eigen::DenseBase<Derived>::Scalar(1e-2),
213 const typename Eigen::DenseBase<Derived>::Scalar κ_2 =
214 typename Eigen::DenseBase<Derived>::Scalar(1e-2)) {
215 using Scalar = typename Eigen::DenseBase<Derived>::Scalar;
216
217 using std::abs;
218 using std::isfinite;
219
220 slp_assert(κ_1 > Scalar(0) && κ_2 > Scalar(0) && κ_2 < Scalar(0.5));
221
222 Eigen::Index decision_variable_idx = 0;
223 for (const auto& [lower, upper] : decision_variable_indices_to_bounds) {
224 Scalar& x_i = x[decision_variable_idx++];
225
226 // We assume that bound infeasibility is handled elsewhere.
227 slp_assert(lower <= upper);
228
229 // See section 3.6 of [2]
230 if (isfinite(lower) && isfinite(upper)) {
231 auto p_L = std::min(κ_1 * std::max(Scalar(1), abs(lower)),
232 κ_2 * (upper - lower));
233 auto p_U = std::min(κ_1 * std::max(Scalar(1), abs(upper)),
234 κ_2 * (upper - lower));
235 x_i = std::min(std::max(lower + p_L, x_i), upper - p_U);
236 } else if (isfinite(lower)) {
237 x_i = std::max(x_i, lower + κ_1 * std::max(Scalar(1), abs(lower)));
238 } else if (isfinite(upper)) {
239 x_i = std::min(x_i, upper - κ_1 * std::max(Scalar(1), abs(upper)));
240 }
241 }
242}
243} // namespace slp
Definition intrusive_shared_ptr.hpp:27
Definition bounds.hpp:27
gch::small_vector< std::pair< Scalar, Scalar > > bounds
The tightest bounds on each decision variable.
Definition bounds.hpp:32
gch::small_vector< std::pair< Eigen::Index, Eigen::Index > > conflicting_bound_indices
Definition bounds.hpp:37
Eigen::ArrayX< bool > bound_constraint_mask
Which constraints, if any, are bound constraints.
Definition bounds.hpp:29