Google OR-Tools v9.12
a fast and portable software suite for combinatorial optimization
Loading...
Searching...
No Matches
update_row.cc
Go to the documentation of this file.
1// Copyright 2010-2025 Google LLC
2// Licensed under the Apache License, Version 2.0 (the "License");
3// you may not use this file except in compliance with the License.
4// You may obtain a copy of the License at
5//
6// http://www.apache.org/licenses/LICENSE-2.0
7//
8// Unless required by applicable law or agreed to in writing, software
9// distributed under the License is distributed on an "AS IS" BASIS,
10// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11// See the License for the specific language governing permissions and
12// limitations under the License.
13
15
16#include <cstdlib>
17#include <string>
18
19#include "absl/log/check.h"
20#include "absl/types/span.h"
23#include "ortools/glop/parameters.pb.h"
29#include "ortools/util/stats.h"
30
31namespace operations_research {
32namespace glop {
33
35 const CompactSparseMatrix& transposed_matrix,
36 const VariablesInfo& variables_info,
37 const RowToColMapping& basis,
38 const BasisFactorization& basis_factorization)
39 : matrix_(matrix),
40 transposed_matrix_(transposed_matrix),
41 variables_info_(variables_info),
42 basis_(basis),
43 basis_factorization_(basis_factorization),
44 unit_row_left_inverse_(),
45 non_zero_position_list_(),
46 non_zero_position_set_(),
47 coefficient_(),
48 num_operations_(0),
49 parameters_(),
50 stats_() {}
51
53 SCOPED_TIME_STAT(&stats_);
54 left_inverse_computed_for_ = kInvalidRow;
55 update_row_computed_for_ = kInvalidRow;
56}
57
59 return unit_row_left_inverse_;
60}
61
63 RowIndex leaving_row) {
64 Invalidate();
65 basis_factorization_.TemporaryLeftSolveForUnitRow(RowToColIndex(leaving_row),
66 &unit_row_left_inverse_);
67 return unit_row_left_inverse_;
68}
69
70void UpdateRow::ComputeUnitRowLeftInverse(RowIndex leaving_row) {
71 if (left_inverse_computed_for_ == leaving_row) return;
72 left_inverse_computed_for_ = leaving_row;
73 SCOPED_TIME_STAT(&stats_);
74
75 basis_factorization_.LeftSolveForUnitRow(RowToColIndex(leaving_row),
76 &unit_row_left_inverse_);
77
78 // TODO(user): Refactorize if the estimated accuracy is above a threshold.
79 IF_STATS_ENABLED(stats_.unit_row_left_inverse_accuracy.Add(
80 matrix_.ColumnScalarProduct(basis_[leaving_row],
81 unit_row_left_inverse_.values) -
82 1.0));
83 IF_STATS_ENABLED(stats_.unit_row_left_inverse_density.Add(
84 Density(unit_row_left_inverse_.values)));
85}
86
87void UpdateRow::ComputeUpdateRow(RowIndex leaving_row) {
88 if (update_row_computed_for_ == leaving_row) return;
89 update_row_computed_for_ = leaving_row;
90 ComputeUnitRowLeftInverse(leaving_row);
91 SCOPED_TIME_STAT(&stats_);
92
93 if (parameters_.use_transposed_matrix()) {
94 // Number of entries that ComputeUpdatesRowWise() will need to look at.
95 EntryIndex num_row_wise_entries(0);
96
97 // Because we are about to do an expensive matrix-vector product, we make
98 // sure we drop small entries in the vector for the row-wise algorithm. We
99 // also computes its non-zeros to simplify the code below.
100 //
101 // TODO(user): So far we didn't generalize the use of drop tolerances
102 // everywhere in the solver, so we make sure to not modify
103 // unit_row_left_inverse_ that is also used elsewhere. However, because of
104 // that, we will not get the exact same result depending on the algortihm
105 // used below because the ComputeUpdatesColumnWise() will still use these
106 // small entries (no complexity changes).
107 const Fractional drop_tolerance = parameters_.drop_tolerance();
108 unit_row_left_inverse_filtered_non_zeros_.clear();
109 const auto matrix_view = transposed_matrix_.view();
110 if (unit_row_left_inverse_.non_zeros.empty()) {
111 const ColIndex size = unit_row_left_inverse_.values.size();
112 const auto values = unit_row_left_inverse_.values.view();
113 for (ColIndex col(0); col < size; ++col) {
114 if (std::abs(values[col]) > drop_tolerance) {
115 unit_row_left_inverse_filtered_non_zeros_.push_back(col);
116 num_row_wise_entries += matrix_view.ColumnNumEntries(col);
117 }
118 }
119 } else {
120 for (const auto e : unit_row_left_inverse_) {
121 if (std::abs(e.coefficient()) > drop_tolerance) {
122 unit_row_left_inverse_filtered_non_zeros_.push_back(e.column());
123 num_row_wise_entries += matrix_view.ColumnNumEntries(e.column());
124 }
125 }
126 }
127
128 // The case of size 1 happens often enough to deserve special code.
129 //
130 // TODO(user): The impact is not as high as I hopped though, so not too
131 // important.
132 if (unit_row_left_inverse_filtered_non_zeros_.size() == 1) {
133 ComputeUpdatesForSingleRow(
134 unit_row_left_inverse_filtered_non_zeros_.front());
135 num_operations_ += num_row_wise_entries.value();
136 IF_STATS_ENABLED(stats_.update_row_density.Add(
137 static_cast<double>(num_non_zeros_) /
138 static_cast<double>(matrix_.num_cols().value())));
139 return;
140 }
141
142 // Number of entries that ComputeUpdatesColumnWise() will need to look at.
143 const EntryIndex num_col_wise_entries =
144 variables_info_.GetNumEntriesInRelevantColumns();
145
146 // Note that the thresholds were chosen (more or less) from the result of
147 // the microbenchmark tests of this file in September 2013.
148 // TODO(user): automate the computation of these constants at run-time?
149 const double row_wise = static_cast<double>(num_row_wise_entries.value());
150 if (row_wise < 0.5 * static_cast<double>(num_col_wise_entries.value())) {
151 if (row_wise < 1.1 * static_cast<double>(matrix_.num_cols().value())) {
152 ComputeUpdatesRowWiseHypersparse();
153
154 // We use a multiplicative factor because these entries are often widely
155 // spread in memory. There is also some overhead to each fp operations.
156 num_operations_ +=
157 5 * num_row_wise_entries.value() + matrix_.num_cols().value() / 64;
158 } else {
159 ComputeUpdatesRowWise();
160 num_operations_ +=
161 num_row_wise_entries.value() + matrix_.num_rows().value();
162 }
163 } else {
164 ComputeUpdatesColumnWise();
165 num_operations_ +=
166 num_col_wise_entries.value() + matrix_.num_cols().value();
167 }
168 } else {
169 ComputeUpdatesColumnWise();
170 num_operations_ +=
171 variables_info_.GetNumEntriesInRelevantColumns().value() +
172 matrix_.num_cols().value();
173 }
174 IF_STATS_ENABLED(stats_.update_row_density.Add(
175 static_cast<double>(num_non_zeros_) /
176 static_cast<double>(matrix_.num_cols().value())));
177}
178
180 const std::string& algorithm) {
181 unit_row_left_inverse_.values = lhs;
182 ComputeNonZeros(lhs, &unit_row_left_inverse_filtered_non_zeros_);
183 if (algorithm == "column") {
184 ComputeUpdatesColumnWise();
185 } else if (algorithm == "row") {
186 ComputeUpdatesRowWise();
187 } else if (algorithm == "row_hypersparse") {
188 ComputeUpdatesRowWiseHypersparse();
189 } else {
190 LOG(DFATAL) << "Unknown algorithm in ComputeUpdateRowForBenchmark(): '"
191 << algorithm << "'";
192 }
193}
194
195const DenseRow& UpdateRow::GetCoefficients() const { return coefficient_; }
196
197absl::Span<const ColIndex> UpdateRow::GetNonZeroPositions() const {
198 return absl::MakeSpan(non_zero_position_list_.data(), num_non_zeros_);
199}
200
201void UpdateRow::SetParameters(const GlopParameters& parameters) {
202 parameters_ = parameters;
203}
204
205// This is optimized for the case when the total number of entries is about
206// the same as, or greater than, the number of columns.
207void UpdateRow::ComputeUpdatesRowWise() {
208 SCOPED_TIME_STAT(&stats_);
209 coefficient_.AssignToZero(matrix_.num_cols());
210 const auto output_coeffs = coefficient_.view();
211 const auto view = transposed_matrix_.view();
212 for (const ColIndex col : unit_row_left_inverse_filtered_non_zeros_) {
213 const Fractional multiplier = unit_row_left_inverse_[col];
214 for (const EntryIndex i : view.Column(col)) {
215 const ColIndex pos = RowToColIndex(view.EntryRow(i));
216 output_coeffs[pos] += multiplier * view.EntryCoefficient(i);
217 }
218 }
219
220 non_zero_position_list_.resize(matrix_.num_cols().value());
221 auto* non_zeros = non_zero_position_list_.data();
222 const Fractional drop_tolerance = parameters_.drop_tolerance();
223 for (const ColIndex col : variables_info_.GetIsRelevantBitRow()) {
224 if (std::abs(output_coeffs[col]) > drop_tolerance) {
225 *non_zeros++ = col;
226 }
227 }
228 num_non_zeros_ = non_zeros - non_zero_position_list_.data();
229}
230
231// This is optimized for the case when the total number of entries is smaller
232// than the number of columns.
233void UpdateRow::ComputeUpdatesRowWiseHypersparse() {
234 SCOPED_TIME_STAT(&stats_);
235 const ColIndex num_cols = matrix_.num_cols();
236 non_zero_position_set_.ClearAndResize(num_cols);
237 coefficient_.resize(num_cols, 0.0);
238
239 const auto output_coeffs = coefficient_.view();
240 const auto view = transposed_matrix_.view();
241 const auto nz_set = non_zero_position_set_.const_view();
242 for (const ColIndex col : unit_row_left_inverse_filtered_non_zeros_) {
243 const Fractional multiplier = unit_row_left_inverse_[col];
244 for (const EntryIndex i : view.Column(col)) {
245 const ColIndex pos = RowToColIndex(view.EntryRow(i));
246 const Fractional v = multiplier * view.EntryCoefficient(i);
247 if (!nz_set[pos]) {
248 // Note that we could create the non_zero_position_list_ here, but we
249 // prefer to keep the non-zero positions sorted, so using the bitset is
250 // a good alternative. Of course if the solution is really really
251 // sparse, then sorting non_zero_position_list_ will be faster.
252 output_coeffs[pos] = v;
253 non_zero_position_set_.Set(pos);
254 } else {
255 output_coeffs[pos] += v;
256 }
257 }
258 }
259
260 // Only keep in non_zero_position_set_ the relevant positions.
261 non_zero_position_set_.Intersection(variables_info_.GetIsRelevantBitRow());
262 non_zero_position_list_.resize(matrix_.num_cols().value());
263 auto* non_zeros = non_zero_position_list_.data();
264 const Fractional drop_tolerance = parameters_.drop_tolerance();
265 for (const ColIndex col : non_zero_position_set_) {
266 // TODO(user): Since the solution is really sparse, maybe storing the
267 // non-zero coefficients contiguously in a vector is better than keeping
268 // them as they are. Note however that we will iterate only twice on the
269 // update row coefficients during an iteration.
270 if (std::abs(output_coeffs[col]) > drop_tolerance) {
271 *non_zeros++ = col;
272 }
273 }
274 num_non_zeros_ = non_zeros - non_zero_position_list_.data();
275}
276
277void UpdateRow::ComputeUpdatesForSingleRow(ColIndex row_as_col) {
278 coefficient_.resize(matrix_.num_cols(), 0.0);
279 non_zero_position_list_.resize(matrix_.num_cols().value());
280 auto* non_zeros = non_zero_position_list_.data();
281
282 const DenseBitRow& is_relevant = variables_info_.GetIsRelevantBitRow();
283 const Fractional drop_tolerance = parameters_.drop_tolerance();
284 const Fractional multiplier = unit_row_left_inverse_[row_as_col];
285 const auto output_coeffs = coefficient_.view();
286 const auto view = transposed_matrix_.view();
287 for (const EntryIndex i : view.Column(row_as_col)) {
288 const ColIndex pos = RowToColIndex(view.EntryRow(i));
289 if (!is_relevant[pos]) continue;
290
291 const Fractional v = multiplier * view.EntryCoefficient(i);
292 if (std::abs(v) > drop_tolerance) {
293 output_coeffs[pos] = v;
294 *non_zeros++ = pos;
295 }
296 }
297 num_non_zeros_ = non_zeros - non_zero_position_list_.data();
298}
299
300void UpdateRow::ComputeUpdatesColumnWise() {
301 SCOPED_TIME_STAT(&stats_);
302
303 coefficient_.resize(matrix_.num_cols(), 0.0);
304 non_zero_position_list_.resize(matrix_.num_cols().value());
305 auto* non_zeros = non_zero_position_list_.data();
306
307 const Fractional drop_tolerance = parameters_.drop_tolerance();
308 const auto output_coeffs = coefficient_.view();
309 const auto view = matrix_.view();
310 const auto unit_row_left_inverse = unit_row_left_inverse_.values.const_view();
311 for (const ColIndex col : variables_info_.GetIsRelevantBitRow()) {
312 // Coefficient of the column right inverse on the 'leaving_row'.
313 const Fractional coeff =
314 view.ColumnScalarProduct(col, unit_row_left_inverse);
315
316 // Nothing to do if 'coeff' is (almost) zero which does happen due to
317 // sparsity. Note that it shouldn't be too bad to use a non-zero drop
318 // tolerance here because even if we introduce some precision issues, the
319 // quantities updated by this update row will eventually be recomputed.
320 if (std::abs(coeff) > drop_tolerance) {
321 *non_zeros++ = col;
322 output_coeffs[col] = coeff;
323 }
324 }
325 num_non_zeros_ = non_zeros - non_zero_position_list_.data();
326}
327
328// Note that we use the same algo as ComputeUpdatesColumnWise() here. The
329// others version might be faster, but this is called at most once per solve, so
330// it shouldn't be too bad.
331void UpdateRow::ComputeFullUpdateRow(RowIndex leaving_row,
332 DenseRow* output) const {
333 CHECK_EQ(leaving_row, left_inverse_computed_for_);
334
335 const ColIndex num_cols = matrix_.num_cols();
336 output->AssignToZero(num_cols);
337
338 // Fills the only position at one in the basic columns.
339 (*output)[basis_[leaving_row]] = 1.0;
340
341 // Fills the non-basic column.
342 const Fractional drop_tolerance = parameters_.drop_tolerance();
343 const auto view = matrix_.view();
344 const auto unit_row_left_inverse = unit_row_left_inverse_.values.const_view();
345 for (const ColIndex col : variables_info_.GetNotBasicBitRow()) {
346 const Fractional coeff =
347 view.ColumnScalarProduct(col, unit_row_left_inverse);
348 if (std::abs(coeff) > drop_tolerance) {
349 (*output)[col] = coeff;
350 }
351 }
352}
353
354} // namespace glop
355} // namespace operations_research
const DenseRow & GetCoefficients() const
const ScatteredRow & ComputeAndGetUnitRowLeftInverse(RowIndex leaving_row)
Definition update_row.cc:62
void SetParameters(const GlopParameters &parameters)
Sets the algorithm parameters.
void ComputeFullUpdateRow(RowIndex leaving_row, DenseRow *output) const
const ScatteredRow & GetUnitRowLeftInverse() const
Definition update_row.cc:58
void ComputeUnitRowLeftInverse(RowIndex leaving_row)
Definition update_row.cc:70
void ComputeUpdateRow(RowIndex leaving_row)
Definition update_row.cc:87
UpdateRow(const CompactSparseMatrix &matrix, const CompactSparseMatrix &transposed_matrix, const VariablesInfo &variables_info, const RowToColMapping &basis, const BasisFactorization &basis_factorization)
Takes references to the linear program data we need.
Definition update_row.cc:34
void ComputeUpdateRowForBenchmark(const DenseRow &lhs, const std::string &algorithm)
absl::Span< const ColIndex > GetNonZeroPositions() const
const DenseBitRow & GetIsRelevantBitRow() const
double Density(const DenseRow &row)
Definition lp_utils.cc:176
StrictITIVector< RowIndex, ColIndex > RowToColMapping
Definition lp_types.h:394
void ComputeNonZeros(const StrictITIVector< IndexType, Fractional > &input, std::vector< IndexType > *non_zeros)
Computes the positions of the non-zeros of a dense vector.
Definition lp_utils.h:216
Bitset64< ColIndex > DenseBitRow
Row of bits.
Definition lp_types.h:375
ColIndex RowToColIndex(RowIndex row)
Get the ColIndex corresponding to the column # row.
Definition lp_types.h:54
constexpr RowIndex kInvalidRow(-1)
StrictITIVector< ColIndex, Fractional > DenseRow
Row-vector types. Row-vector types are indexed by a column index.
Definition lp_types.h:351
In SWIG mode, we don't want anything besides these top-level includes.
#define IF_STATS_ENABLED(instructions)
Definition stats.h:417
#define SCOPED_TIME_STAT(stats)
Definition stats.h:418