OR-Tools  9.2
lu_factorization.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 #ifndef OR_TOOLS_GLOP_LU_FACTORIZATION_H_
15 #define OR_TOOLS_GLOP_LU_FACTORIZATION_H_
16 
17 #include "ortools/glop/markowitz.h"
19 #include "ortools/glop/status.h"
23 #include "ortools/lp_data/sparse.h"
25 #include "ortools/util/stats.h"
26 
27 namespace operations_research {
28 namespace glop {
29 
30 // An LU-Factorization class encapsulating the LU factorization data and
31 // algorithms. The actual algorithm is in markowitz.h and .cc. This class holds
32 // all the Solve() functions that deal with the permutations and the L and U
33 // factors once they are computed.
35  public:
37 
38  // Returns true if the LuFactorization is a factorization of the identity
39  // matrix. In this state, all the Solve() functions will work for any
40  // vector dimension.
41  bool IsIdentityFactorization() { return is_identity_factorization_; }
42 
43  // Clears internal data structure and reset this class to the factorization
44  // of an identity matrix.
45  void Clear();
46 
47  // Computes an LU-decomposition for a given matrix B. If for some reason,
48  // there was an error, then the factorization is reset to the one of the
49  // identity matrix, and an error is reported.
50  //
51  // Note(user): Since a client must use the result, there is little chance of
52  // it being confused by this revert to identity factorization behavior. The
53  // reason behind it is that this way, calling any public function of this
54  // class will never cause a crash of the program.
55  ABSL_MUST_USE_RESULT Status
56  ComputeFactorization(const CompactSparseMatrixView& compact_matrix);
57 
58  // Given a set of columns, find a maximum linearly independent subset that can
59  // be factorized in a stable way, and complete it into a square matrix using
60  // slack columns. The initial set can have less, more or the same number of
61  // columns as the number of rows.
63  const std::vector<ColIndex>& candidates);
64 
65  // Returns the column permutation used by the LU factorization.
66  const ColumnPermutation& GetColumnPermutation() const { return col_perm_; }
67 
68  // Sets the column permutation to the identity permutation. The idea is that
69  // the column permutation can be incorporated in the basis RowToColMapping,
70  // and once this is done, then a client can call this and effectively remove
71  // the need for a column permutation on each solve.
73  col_perm_.clear();
74  inverse_col_perm_.clear();
75  }
76 
77  // Solves 'B.x = b', x initially contains b, and is replaced by 'B^{-1}.b'.
78  // Since P.B.Q^{-1} = L.U, we have B = P^{-1}.L.U.Q.
79  // 1/ Solve P^{-1}.y = b for y by computing y = P.b,
80  // 2/ solve L.z = y for z,
81  // 3/ solve U.t = z for t,
82  // 4/ finally solve Q.x = t, by computing x = Q^{-1}.t.
83  void RightSolve(DenseColumn* x) const;
84 
85  // Solves 'y.B = r', y initially contains r, and is replaced by r.B^{-1}.
86  // Internally, it takes x = y^T, b = r^T and solves B^T.x = b.
87  // We have P.B.Q^{-1} = P.B.Q^T = L.U, thus (L.U)^T = Q.B^T.P^T.
88  // Therefore B^T = Q^{-1}.U^T.L^T.P^T.P^{-1} = Q^{-1}.U^T.L^T.P
89  // The procedure is thus:
90  // 1/ Solve Q^{-1}.y = b for y, by computing y = Q.b,
91  // 2/ solve U^T.z = y for z,
92  // 3/ solve L^T.t = z for t,
93  // 4/ finally, solve P.x = t for x by computing x = P^{-1}.t.
94  void LeftSolve(DenseRow* y) const;
95 
96  // More fine-grained right/left solve functions that may exploit the initial
97  // non-zeros of the input vector if non-empty. Note that a solve involving L
98  // actually solves P^{-1}.L and a solve involving U actually solves U.Q. To
99  // solve a system with the initial matrix B, one needs to call:
100  // - RightSolveL() and then RightSolveU() for a right solve (B.x = initial x).
101  // - LeftSolveU() and then LeftSolveL() for a left solve (y.B = initial y).
104  void LeftSolveUWithNonZeros(ScatteredRow* y) const;
105 
106  // Specialized version of LeftSolveL() that may exploit the initial non_zeros
107  // of y if it is non empty. Moreover, if result_before_permutation is not
108  // NULL, it might be filled with the result just before row_perm_ is applied
109  // to it and true is returned. If result_before_permutation is not filled,
110  // then false is returned.
112  ScatteredColumn* result_before_permutation) const;
113  void LeftSolveLWithNonZeros(ScatteredRow* y) const;
114 
115  // Specialized version of RightSolveLWithNonZeros() that takes a SparseColumn
116  // or a ScatteredColumn as input. non_zeros will either be cleared or set to
117  // the non zeros of the result. Important: the output x must be of the correct
118  // size and all zero.
119  void RightSolveLForColumnView(const ColumnView& b, ScatteredColumn* x) const;
121  ScatteredColumn* x) const;
122 
123  // Specialized version of RightSolveLWithNonZeros() where x is originally
124  // equal to 'a' permuted by row_perm_. Note that 'a' is only used for DCHECK.
126  ScatteredColumn* x) const;
127 
128  // Specialized version of LeftSolveU() for an unit right-hand side.
129  // non_zeros will either be cleared or set to the non zeros of the results.
130  // It also returns the value of col permuted by Q (which is the position
131  // of the unit-vector rhs in the solve system: y.U = rhs).
132  // Important: the output y must be of the correct size and all zero.
133  ColIndex LeftSolveUForUnitRow(ColIndex col, ScatteredRow* y) const;
134 
135  // Returns the given column of U.
136  // It will only be valid until the next call to GetColumnOfU().
137  const SparseColumn& GetColumnOfU(ColIndex col) const;
138 
139  // Returns the norm of B^{-1}.a
141 
142  // Returns the norm of (B^T)^{-1}.e_row where e is an unit vector.
143  Fractional DualEdgeSquaredNorm(RowIndex row) const;
144 
145  // The fill-in of the LU-factorization is defined as the sum of the number
146  // of entries of both the lower- and upper-triangular matrices L and U minus
147  // the number of entries in the initial matrix B.
148  //
149  // This returns the number of entries in lower + upper as the percentage of
150  // the number of entries in B.
151  double GetFillInPercentage(const CompactSparseMatrixView& matrix) const;
152 
153  // Returns the number of entries in L + U.
154  // If the factorization is the identity, this returns 0.
155  EntryIndex NumberOfEntries() const;
156 
157  // Computes the determinant of the input matrix B.
158  // Since P.B.Q^{-1} = L.U, det(P) * det(B) * det(Q^{-1}) = det(L) * det(U).
159  // det(L) = 1 since L is a lower-triangular matrix with 1 on the diagonal.
160  // det(P) = +1 or -1 (by definition it is the sign of the permutation P)
161  // det(Q^{-1}) = +1 or -1 (the sign of the permutation Q^{-1})
162  // Finally det(U) = product of the diagonal elements of U, since U is an
163  // upper-triangular matrix.
164  // Taking all this into account:
165  // det(B) = sign(P) * sign(Q^{-1}) * prod_i u_ii .
167 
168  // Computes the 1-norm of the inverse of the input matrix B.
169  // For this we iteratively solve B.x = e_j, where e_j is the jth unit vector.
170  // The result of this computation is the jth column of B^-1.
171  // The 1-norm |B| is defined as max_j sum_i |a_ij|
172  // http://en.wikipedia.org/wiki/Matrix_norm
174 
175  // Computes the infinity-norm of the inverse of the input matrix B.
176  // The infinity-norm |B| is defined as max_i sum_j |a_ij|
177  // http://en.wikipedia.org/wiki/Matrix_norm
179 
180  // Computes the condition number of the input matrix B.
181  // For a given norm, this is the matrix norm times the norm of its inverse.
182  //
183  // Note that because the LuFactorization class does not keep the
184  // non-factorized matrix in memory, it needs to be passed to these functions.
185  // It is up to the client to pass exactly the same matrix as the one used
186  // for ComputeFactorization().
187  //
188  // TODO(user): separate this from LuFactorization.
190  const CompactSparseMatrixView& matrix) const;
192  const CompactSparseMatrixView& matrix) const;
194 
195  // Sets the current parameters.
197  parameters_ = parameters;
198  markowitz_.SetParameters(parameters);
199  }
200 
201  // Returns a string containing the statistics for this class.
202  std::string StatString() const {
203  return stats_.StatString() + markowitz_.StatString();
204  }
205 
206  // This is only used for testing and in debug mode.
207  // TODO(user): avoid the matrix conversion by multiplying TriangularMatrix
208  // directly.
209  void ComputeLowerTimesUpper(SparseMatrix* product) const {
210  SparseMatrix temp_lower, temp_upper;
211  lower_.CopyToSparseMatrix(&temp_lower);
212  upper_.CopyToSparseMatrix(&temp_upper);
213  product->PopulateFromProduct(temp_lower, temp_upper);
214  }
215 
216  // Returns the deterministic time of the last factorization.
218 
219  // Visible for testing.
220  const RowPermutation& row_perm() const { return row_perm_; }
222  return inverse_col_perm_;
223  }
224 
225  private:
226  // Statistics about this class.
227  struct Stats : public StatsGroup {
228  Stats()
229  : StatsGroup("LuFactorization"),
230  basis_num_entries("basis_num_entries", this),
231  lu_fill_in("lu_fill_in", this) {}
232  IntegerDistribution basis_num_entries;
233  RatioDistribution lu_fill_in;
234  };
235 
236  // Internal function used in the left solve functions.
237  void LeftSolveScratchpad() const;
238 
239  // Internal function used in the right solve functions
240  template <typename Column>
241  void RightSolveLInternal(const Column& b, ScatteredColumn* x) const;
242 
243  // Fills transpose_upper_ from upper_.
244  void ComputeTransposeUpper();
245 
246  // transpose_lower_ is only needed when we compute dual norms.
247  void ComputeTransposeLower() const;
248 
249  // Computes R = P.B.Q^{-1} - L.U and returns false if the largest magnitude of
250  // the coefficients of P.B.Q^{-1} - L.U is greater than tolerance.
251  bool CheckFactorization(const CompactSparseMatrixView& matrix,
252  Fractional tolerance) const;
253 
254  // Special case where we have nothing to do. This happens at the beginning
255  // when we start the problem with an all-slack basis and gives a good speedup
256  // on really easy problems. It is initially true and set to true each time we
257  // call Clear(). We set it to false if a call to ComputeFactorization()
258  // succeeds.
259  bool is_identity_factorization_;
260 
261  // The triangular factor L and U (and its transpose).
262  TriangularMatrix lower_;
263  TriangularMatrix upper_;
264  TriangularMatrix transpose_upper_;
265 
266  // The transpose of lower_. It is just used by DualEdgeSquaredNorm()
267  // and mutable so it can be lazily initialized.
268  mutable TriangularMatrix transpose_lower_;
269 
270  // The column permutation Q and its inverse Q^{-1} in P.B.Q^{-1} = L.U.
271  ColumnPermutation col_perm_;
272  ColumnPermutation inverse_col_perm_;
273 
274  // The row permutation P and its inverse P^{-1} in P.B.Q^{-1} = L.U.
275  RowPermutation row_perm_;
276  RowPermutation inverse_row_perm_;
277 
278  // Temporary storage used by LeftSolve()/RightSolve().
279  mutable DenseColumn dense_column_scratchpad_;
280 
281  // Temporary storage used by GetColumnOfU().
282  mutable SparseColumn column_of_upper_;
283 
284  // Same as dense_column_scratchpad_ but this vector is always reset to zero by
285  // the functions that use it. non_zero_rows_ is used to track the
286  // non_zero_rows_ position of dense_column_scratchpad_.
287  mutable DenseColumn dense_zero_scratchpad_;
288  mutable std::vector<RowIndex> non_zero_rows_;
289 
290  // Statistics, mutable so const functions can still update it.
291  mutable Stats stats_;
292 
293  // Proto holding all the parameters of this algorithm.
294  GlopParameters parameters_;
295 
296  // The class doing the Markowitz LU factorization.
297  Markowitz markowitz_;
298 
299  DISALLOW_COPY_AND_ASSIGN(LuFactorization);
300 };
301 
302 } // namespace glop
303 } // namespace operations_research
304 #endif // OR_TOOLS_GLOP_LU_FACTORIZATION_H_
RowToColMapping ComputeInitialBasis(const CompactSparseMatrix &matrix, const std::vector< ColIndex > &candidates)
ABSL_MUST_USE_RESULT Status ComputeFactorization(const CompactSparseMatrixView &compact_matrix)
void LeftSolveUWithNonZeros(ScatteredRow *y) const
const ColumnPermutation & GetColumnPermutation() const
Fractional ComputeOneNormConditionNumber(const CompactSparseMatrixView &matrix) const
const SparseColumn & GetColumnOfU(ColIndex col) const
ColIndex col
Definition: markowitz.cc:183
const RowPermutation & row_perm() const
RowIndex row
Definition: markowitz.cc:182
void RightSolveUWithNonZeros(ScatteredColumn *x) const
void SetParameters(const GlopParameters &parameters)
Definition: markowitz.h:313
Permutation< ColIndex > ColumnPermutation
int64_t b
void PopulateFromProduct(const SparseMatrix &a, const SparseMatrix &b)
Definition: sparse.cc:250
Fractional RightSolveSquaredNorm(const ColumnView &a) const
void CopyToSparseMatrix(SparseMatrix *output) const
Definition: sparse.cc:757
StrictITIVector< RowIndex, Fractional > DenseColumn
Definition: lp_types.h:332
Fractional DualEdgeSquaredNorm(RowIndex row) const
const ColumnPermutation & inverse_col_perm() const
bool LeftSolveLWithNonZeros(ScatteredRow *y, ScatteredColumn *result_before_permutation) const
void RightSolveLForColumnView(const ColumnView &b, ScatteredColumn *x) const
void RightSolveLWithPermutedInput(const DenseColumn &a, ScatteredColumn *x) const
void RightSolveLForScatteredColumn(const ScatteredColumn &b, ScatteredColumn *x) const
double GetFillInPercentage(const CompactSparseMatrixView &matrix) const
void ComputeLowerTimesUpper(SparseMatrix *product) const
void RightSolveLWithNonZeros(ScatteredColumn *x) const
Collection of objects used to extend the Constraint Solver library.
void SetParameters(const GlopParameters &parameters)
SatParameters parameters
Permutation< RowIndex > RowPermutation
std::string StatString() const
Definition: markowitz.h:310
Fractional ComputeInfinityNormConditionNumber(const CompactSparseMatrixView &matrix) const
ColIndex LeftSolveUForUnitRow(ColIndex col, ScatteredRow *y) const
int64_t a