OR-Tools  9.1
markowitz.h
Go to the documentation of this file.
1// Copyright 2010-2021 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
14// LU decomposition algorithm of a sparse matrix B with Markowitz pivot
15// selection strategy. The algorithm constructs a lower matrix L, upper matrix
16// U, row permutation P and a column permutation Q such that L.U = P.B.Q^{-1}.
17//
18// The current algorithm is a mix of ideas that can be found in the literature
19// and of some optimizations tailored for its use in a revised simplex algorithm
20// (like a fast processing of the singleton columns present in B). It constructs
21// L and U column by column from left to right.
22//
23// A key concept is the one of the residual matrix which is the bottom right
24// square submatrix that still needs to be factorized during the classical
25// Gaussian elimination. The algorithm maintains the non-zero pattern of its
26// rows and its row/column degrees.
27//
28// At each step, a number of columns equal to 'markowitz_zlatev_parameter' are
29// chosen as candidates from the residual matrix. They are the ones with minimal
30// residual column degree. They can be found easily because the columns of the
31// residual matrix are kept in a priority queue.
32//
33// We compute the numerical value of these residual columns like in a
34// left-looking algorithm by solving a sparse lower-triangular system with the
35// current L constructed so far. Note that this step is highly optimized for
36// sparsity and we reuse the computations done in the previous steps (if the
37// candidate column was already considered before). As a by-product, we also
38// get the corresponding column of U.
39//
40// Among the entries of these columns, a pivot is chosen such that the product:
41// (num_column_entries - 1) * (num_row_entries - 1)
42// is minimized. Only the pivots with a magnitude greater than
43// 'lu_factorization_pivot_threshold' times the maximum magnitude of the
44// corresponding residual column are considered for stability reasons.
45//
46// Once the pivot is chosen, the residual column divided by the pivot becomes a
47// column of L, and the non-zero pattern of the new residual submatrix is
48// updated by subtracting the outer product of this pivot column times the pivot
49// row. The product minimized above is thus an upper bound of the number of
50// fill-in created during a step.
51//
52// References:
53//
54// J. R. Gilbert and T. Peierls, "Sparse partial pivoting in time proportional
55// to arithmetic operations," SIAM J. Sci. Statist. Comput., 9 (1988): 862-874.
56//
57// I.S. Duff, A.M. Erisman and J.K. Reid, "Direct Methods for Sparse Matrices",
58// Clarendon, Oxford, UK, 1987, ISBN 0-19-853421-3,
59// http://www.amazon.com/dp/0198534213
60//
61// T.A. Davis, "Direct methods for Sparse Linear Systems", SIAM, Philadelphia,
62// 2006, ISBN-13: 978-0-898716-13, http://www.amazon.com/dp/0898716136
63//
64// TODO(user): Determine whether any of these would bring any benefit:
65// - S.C. Eisenstat and J.W.H. Liu, "The theory of elimination trees for
66// sparse unsymmetric matrices," SIAM J. Matrix Anal. Appl., 26:686-705,
67// January 2005
68// - S.C. Eisenstat and J.W.H. Liu. "Algorithmic aspects of elimination trees
69// for sparse unsymmetric matrices," SIAM J. Matrix Anal. Appl.,
70// 29:1363-1381, January 2008.
71// - http://perso.ens-lyon.fr/~bucar/papers/kauc.pdf
72
73#ifndef OR_TOOLS_GLOP_MARKOWITZ_H_
74#define OR_TOOLS_GLOP_MARKOWITZ_H_
75
76#include <cstdint>
77#include <queue>
78
79#include "absl/container/inlined_vector.h"
83#include "ortools/glop/status.h"
88#include "ortools/util/stats.h"
89
90namespace operations_research {
91namespace glop {
92
93// Holds the non-zero positions (by row) and column/row degree of the residual
94// matrix during the Gaussian elimination.
95//
96// During each step of Gaussian elimination, a row and a column will be
97// "removed" from the residual matrix. Note however that the row and column
98// indices of the non-removed part do not change, so the residual matrix at a
99// given step will only correspond to a subset of the initial indices.
101 public:
103
104 // Releases the memory used by this class.
105 void Clear();
106
107 // Resets the pattern to the one of an empty square matrix of the given size.
108 void Reset(RowIndex num_rows, ColIndex num_cols);
109
110 // Resets the pattern to the one of the given matrix but only for the
111 // rows/columns whose given permutation is kInvalidRow or kInvalidCol.
112 // This also fills the singleton columns/rows with the corresponding entries.
114 const RowPermutation& row_perm,
115 const ColumnPermutation& col_perm,
116 std::vector<ColIndex>* singleton_columns,
117 std::vector<RowIndex>* singleton_rows);
118
119 // Adds a non-zero entry to the matrix. There should be no duplicates.
120 void AddEntry(RowIndex row, ColIndex col);
121
122 // Marks the given pivot row and column as deleted.
123 // This is called at each step of the Gaussian elimination on the pivot.
124 void DeleteRowAndColumn(RowIndex pivot_row, ColIndex pivot_col);
125
126 // Decreases the degree of a row/column. This is the basic operation used to
127 // keep the correct degree after a call to DeleteRowAndColumn(). This is
128 // because row_non_zero_[row] is only lazily cleaned.
129 int32_t DecreaseRowDegree(RowIndex row);
130 int32_t DecreaseColDegree(ColIndex col);
131
132 // Returns true if the column has been deleted by DeleteRowAndColumn().
133 bool IsColumnDeleted(ColIndex col) const;
134
135 // Removes from the corresponding row_non_zero_[row] the columns that have
136 // been previously deleted by DeleteRowAndColumn().
137 void RemoveDeletedColumnsFromRow(RowIndex row);
138
139 // Returns the first non-deleted column index from this row or kInvalidCol if
140 // none can be found.
141 ColIndex GetFirstNonDeletedColumnFromRow(RowIndex row) const;
142
143 // Performs a generic Gaussian update of the residual matrix:
144 // - DeleteRowAndColumn() must already have been called.
145 // - The non-zero pattern is augmented (set union) by the one of the
146 // outer product of the pivot column and row.
147 //
148 // Important: as a small optimization, this function does not call
149 // DecreaseRowDegree() on the row in the pivot column. This has to be done by
150 // the client.
151 void Update(RowIndex pivot_row, ColIndex pivot_col,
152 const SparseColumn& column);
153
154 // Returns the degree (i.e. the number of non-zeros) of the given column.
155 // This is only valid for the column indices still in the residual matrix.
156 int32_t ColDegree(ColIndex col) const {
157 DCHECK(!deleted_columns_[col]);
158 return col_degree_[col];
159 }
160
161 // Returns the degree (i.e. the number of non-zeros) of the given row.
162 // This is only valid for the row indices still in the residual matrix.
163 int32_t RowDegree(RowIndex row) const { return row_degree_[row]; }
164
165 // Returns the set of non-zeros of the given row (unsorted).
166 // Call RemoveDeletedColumnsFromRow(row) to clean the row first.
167 // This is only valid for the row indices still in the residual matrix.
168 const absl::InlinedVector<ColIndex, 6>& RowNonZero(RowIndex row) const {
169 return row_non_zero_[row];
170 }
171
172 private:
173 // Augments the non-zero pattern of the given row by taking its union with the
174 // non-zero pattern of the given pivot_row.
175 void MergeInto(RowIndex pivot_row, RowIndex row);
176
177 // Different version of MergeInto() that works only if the non-zeros position
178 // of each row are sorted in increasing order. The output will also be sorted.
179 //
180 // TODO(user): This is currently not used but about the same speed as the
181 // non-sorted version. Investigate more.
182 void MergeIntoSorted(RowIndex pivot_row, RowIndex row);
183
184 // Using InlinedVector helps because we usually have many rows with just a few
185 // non-zeros. Note that on a 64 bits computer we get exactly 6 inlined int32_t
186 // elements without extra space, and the size of the inlined vector is 4 times
187 // 64 bits.
188 //
189 // TODO(user): We could be even more efficient since a size of int32_t is
190 // enough for us and we could store in common the inlined/not-inlined size.
194 DenseBooleanRow deleted_columns_;
195 DenseBooleanRow bool_scratchpad_;
196 std::vector<ColIndex> col_scratchpad_;
197 ColIndex num_non_deleted_columns_;
198
199 DISALLOW_COPY_AND_ASSIGN(MatrixNonZeroPattern);
200};
201
202// Adjustable priority queue of columns. Pop() returns a column with the
203// smallest degree first (degree = number of entries in the column).
204// Empty columns (i.e. with degree 0) are not stored in the queue.
206 public:
208
209 // Releases the memory used by this class.
210 void Clear();
211
212 // Clears the queue and prepares it to store up to num_cols column indices
213 // with a degree from 1 to max_degree included.
214 void Reset(int32_t max_degree, ColIndex num_cols);
215
216 // Changes the degree of a column and make sure it is in the queue. The degree
217 // must be non-negative (>= 0) and at most equal to the value of num_cols used
218 // in Reset(). A degree of zero will remove the column from the queue.
219 void PushOrAdjust(ColIndex col, int32_t degree);
220
221 // Removes the column index with higher priority from the queue and returns
222 // it. Returns kInvalidCol if the queue is empty.
223 ColIndex Pop();
224
225 private:
228 std::vector<std::vector<ColIndex>> col_by_degree_;
229 int32_t min_degree_;
230 DISALLOW_COPY_AND_ASSIGN(ColumnPriorityQueue);
231};
232
233// Contains a set of columns indexed by ColIndex. This is like a SparseMatrix
234// but this class is optimized for the case where only a small subset of columns
235// is needed at the same time (like it is the case in our LU algorithm). It
236// reuses the memory of the columns that are no longer needed.
238 public:
240
241 // Resets the repository to num_cols empty columns.
242 void Reset(ColIndex num_cols);
243
244 // Returns the column with given index.
245 const SparseColumn& column(ColIndex col) const;
246
247 // Gets the mutable column with given column index. The returned vector
248 // address is only valid until the next call to mutable_column().
250
251 // Clears the column with given index and releases its memory to the common
252 // memory pool that is used to create new mutable_column() on demand.
253 void ClearAndReleaseColumn(ColIndex col);
254
255 // Reverts this class to its initial state. This releases the memory of the
256 // columns that were used but not the memory of this class member (this should
257 // be fine).
258 void Clear();
259
260 private:
261 // mutable_column(col) is stored in columns_[mapping_[col]].
262 // The columns_ that can be reused have their index stored in free_columns_.
263 const SparseColumn empty_column_;
265 std::vector<int> free_columns_;
266 std::vector<SparseColumn> columns_;
267 DISALLOW_COPY_AND_ASSIGN(SparseMatrixWithReusableColumnMemory);
268};
269
270// The class that computes either the actual L.U decomposition, or the
271// permutation P and Q such that P.B.Q^{-1} will have a sparse L.U
272// decomposition.
274 public:
276
277 // Computes the full factorization with P, Q, L and U.
278 //
279 // If the matrix is singular, the returned status will indicate it and the
280 // permutation (col_perm) will contain a maximum non-singular set of columns
281 // of the matrix. Moreover, by adding singleton columns with a one at the rows
282 // such that 'row_perm[row] == kInvalidRow', then the matrix will be
283 // non-singular.
284 ABSL_MUST_USE_RESULT Status
285 ComputeLU(const CompactSparseMatrixView& basis_matrix,
286 RowPermutation* row_perm, ColumnPermutation* col_perm,
287 TriangularMatrix* lower, TriangularMatrix* upper);
288
289 // Only computes P and Q^{-1}, L and U can be computed later from these
290 // permutations using another algorithm (for instance left-looking L.U). This
291 // may be faster than computing the full L and U at the same time but the
292 // current implementation is not optimized for this.
293 //
294 // It behaves the same as ComputeLU() for singular matrices.
295 //
296 // This function also works with a non-square matrix. It will return a set of
297 // independent columns of maximum size. If all the given columns are
298 // independent, the returned Status will be OK.
299 ABSL_MUST_USE_RESULT Status ComputeRowAndColumnPermutation(
300 const CompactSparseMatrixView& basis_matrix, RowPermutation* row_perm,
301 ColumnPermutation* col_perm);
302
303 // Releases the memory used by this class.
304 void Clear();
305
306 // Returns an estimate of the time spent in the last factorization.
308
309 // Returns a string containing the statistics for this class.
310 std::string StatString() const { return stats_.StatString(); }
311
312 // Sets the current parameters.
314 parameters_ = parameters;
315 }
316
317 private:
318 // Statistics about this class.
319 struct Stats : public StatsGroup {
320 Stats()
321 : StatsGroup("Markowitz"),
322 basis_singleton_column_ratio("basis_singleton_column_ratio", this),
323 basis_residual_singleton_column_ratio(
324 "basis_residual_singleton_column_ratio", this),
325 pivots_without_fill_in_ratio("pivots_without_fill_in_ratio", this),
326 degree_two_pivot_columns("degree_two_pivot_columns", this) {}
327 RatioDistribution basis_singleton_column_ratio;
328 RatioDistribution basis_residual_singleton_column_ratio;
329 RatioDistribution pivots_without_fill_in_ratio;
330 RatioDistribution degree_two_pivot_columns;
331 };
332 Stats stats_;
333
334 // Fast track for singleton columns of the matrix. Fills a part of the row and
335 // column permutation that move these columns in order to form an identity
336 // sub-matrix on the upper left.
337 //
338 // Note(user): Linear programming bases usually have a resonable percentage of
339 // slack columns in them, so this gives a big speedup.
340 void ExtractSingletonColumns(const CompactSparseMatrixView& basis_matrix,
341 RowPermutation* row_perm,
342 ColumnPermutation* col_perm, int* index);
343
344 // Fast track for columns that form a triangular matrix. This does not find
345 // all of them, but because the column are ordered in the same way they were
346 // ordered at the end of the previous factorization, this is likely to find
347 // quite a few.
348 //
349 // The main gain here is that it avoids taking these columns into account in
350 // InitializeResidualMatrix() and later in RemoveRowFromResidualMatrix().
351 void ExtractResidualSingletonColumns(
352 const CompactSparseMatrixView& basis_matrix, RowPermutation* row_perm,
353 ColumnPermutation* col_perm, int* index);
354
355 // Helper function for determining if a column is a residual singleton column.
356 // If it is, RowIndex* row contains the index of the single residual edge.
357 bool IsResidualSingletonColumn(const ColumnView& column,
358 const RowPermutation& row_perm, RowIndex* row);
359
360 // Returns the column of the current residual matrix with an index 'col' in
361 // the initial matrix. We compute it by solving a linear system with the
362 // current lower_ and the last computed column 'col' of a previous residual
363 // matrix. This uses the same algorithm as a left-looking factorization (see
364 // lu_factorization.h for more details).
365 const SparseColumn& ComputeColumn(const RowPermutation& row_perm,
366 ColIndex col);
367
368 // Finds an entry in the residual matrix with a low Markowitz score and a high
369 // enough magnitude. Returns its Markowitz score and updates the given
370 // pointers.
371 //
372 // We use the strategy of Zlatev, "On some pivotal strategies in Gaussian
373 // elimination by sparse technique" (1980). SIAM J. Numer. Anal. 17 18-30. It
374 // consists of looking for the best pivot in only a few columns (usually 3
375 // or 4) amongst the ones which have the lowest number of entries.
376 //
377 // Amongst the pivots with a minimum Markowitz number, we choose the one
378 // with highest magnitude. This doesn't apply to pivots with a 0 Markowitz
379 // number because all such pivots will have to be taken at some point anyway.
380 int64_t FindPivot(const RowPermutation& row_perm,
381 const ColumnPermutation& col_perm, RowIndex* pivot_row,
382 ColIndex* pivot_col, Fractional* pivot_coefficient);
383
384 // Updates the degree of a given column in the internal structure of the
385 // class.
386 void UpdateDegree(ColIndex col, int degree);
387
388 // Removes all the coefficients in the residual matrix that are on the given
389 // row or column. In both cases, the pivot row or column is ignored.
390 void RemoveRowFromResidualMatrix(RowIndex pivot_row, ColIndex pivot_col);
391 void RemoveColumnFromResidualMatrix(RowIndex pivot_row, ColIndex pivot_col);
392
393 // Updates the residual matrix given the pivot position. This is needed if the
394 // pivot row and pivot column both have more than one entry. Otherwise, the
395 // residual matrix can be updated more efficiently by calling one of the
396 // Remove...() functions above.
397 void UpdateResidualMatrix(RowIndex pivot_row, ColIndex pivot_col);
398
399 // Pointer to the matrix to factorize.
400 CompactSparseMatrixView const* basis_matrix_;
401
402 // These matrices are transformed during the algorithm into the final L and U
403 // matrices modulo some row and column permutations. Note that the columns of
404 // these matrices stay in the initial order.
405 SparseMatrixWithReusableColumnMemory permuted_lower_;
406 SparseMatrixWithReusableColumnMemory permuted_upper_;
407
408 // These matrices will hold the final L and U. The are created columns by
409 // columns from left to right, and at the end, their rows are permuted by
410 // ComputeLU() to become triangular.
411 TriangularMatrix lower_;
412 TriangularMatrix upper_;
413
414 // The columns of permuted_lower_ for which we do need a call to
415 // PermutedLowerSparseSolve(). This speeds up ComputeColumn().
416 DenseBooleanRow permuted_lower_column_needs_solve_;
417
418 // Contains the non-zero positions of the current residual matrix (the
419 // lower-right square matrix that gets smaller by one row and column at each
420 // Gaussian elimination step).
421 MatrixNonZeroPattern residual_matrix_non_zero_;
422
423 // Data structure to access the columns by increasing degree.
424 ColumnPriorityQueue col_by_degree_;
425
426 // True as long as only singleton columns of the residual matrix are used.
427 bool contains_only_singleton_columns_;
428
429 // Boolean used to know when col_by_degree_ become useful.
430 bool is_col_by_degree_initialized_;
431
432 // FindPivot() needs to look at the first entries of col_by_degree_, it
433 // temporary put them here before pushing them back to col_by_degree_.
434 std::vector<ColIndex> examined_col_;
435
436 // Singleton column indices are kept here rather than in col_by_degree_ to
437 // optimize the algorithm: as long as this or singleton_row_ are not empty,
438 // col_by_degree_ do not need to be initialized nor updated.
439 std::vector<ColIndex> singleton_column_;
440
441 // List of singleton row indices.
442 std::vector<RowIndex> singleton_row_;
443
444 // Proto holding all the parameters of this algorithm.
445 GlopParameters parameters_;
446
447 // Number of floating point operations of the last factorization.
448 int64_t num_fp_operations_;
449
450 DISALLOW_COPY_AND_ASSIGN(Markowitz);
451};
452
453} // namespace glop
454} // namespace operations_research
455
456#endif // OR_TOOLS_GLOP_MARKOWITZ_H_
#define DCHECK(condition)
Definition: base/logging.h:885
void Reset(int32_t max_degree, ColIndex num_cols)
Definition: markowitz.cc:815
void PushOrAdjust(ColIndex col, int32_t degree)
Definition: markowitz.cc:823
double DeterministicTimeOfLastFactorization() const
Definition: markowitz.cc:553
ABSL_MUST_USE_RESULT Status ComputeLU(const CompactSparseMatrixView &basis_matrix, RowPermutation *row_perm, ColumnPermutation *col_perm, TriangularMatrix *lower, TriangularMatrix *upper)
Definition: markowitz.cc:149
void SetParameters(const GlopParameters &parameters)
Definition: markowitz.h:313
std::string StatString() const
Definition: markowitz.h:310
ABSL_MUST_USE_RESULT Status ComputeRowAndColumnPermutation(const CompactSparseMatrixView &basis_matrix, RowPermutation *row_perm, ColumnPermutation *col_perm)
Definition: markowitz.cc:27
const absl::InlinedVector< ColIndex, 6 > & RowNonZero(RowIndex row) const
Definition: markowitz.h:168
void DeleteRowAndColumn(RowIndex pivot_row, ColIndex pivot_col)
Definition: markowitz.cc:642
int32_t RowDegree(RowIndex row) const
Definition: markowitz.h:163
void AddEntry(RowIndex row, ColIndex col)
Definition: markowitz.cc:628
void Reset(RowIndex num_rows, ColIndex num_cols)
Definition: markowitz.cc:566
void Update(RowIndex pivot_row, ColIndex pivot_col, const SparseColumn &column)
Definition: markowitz.cc:678
int32_t ColDegree(ColIndex col) const
Definition: markowitz.h:156
ColIndex GetFirstNonDeletedColumnFromRow(RowIndex row) const
Definition: markowitz.cc:670
void InitializeFromMatrixSubset(const CompactSparseMatrixView &basis_matrix, const RowPermutation &row_perm, const ColumnPermutation &col_perm, std::vector< ColIndex > *singleton_columns, std::vector< RowIndex > *singleton_rows)
Definition: markowitz.cc:576
const SparseColumn & column(ColIndex col) const
Definition: markowitz.cc:870
SatParameters parameters
ColIndex col
Definition: markowitz.cc:183
RowIndex row
Definition: markowitz.cc:182
Permutation< ColIndex > ColumnPermutation
StrictITIVector< ColIndex, bool > DenseBooleanRow
Definition: lp_types.h:306
Permutation< RowIndex > RowPermutation
Collection of objects used to extend the Constraint Solver library.
int index
Definition: pack.cc:509