OR-Tools  9.6
update_row.h
Go to the documentation of this file.
1 // Copyright 2010-2022 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_UPDATE_ROW_H_
15 #define OR_TOOLS_GLOP_UPDATE_ROW_H_
16 
17 #include <cstdint>
18 #include <string>
19 #include <vector>
20 
22 #include "ortools/glop/parameters.pb.h"
26 #include "ortools/util/stats.h"
27 
28 namespace operations_research {
29 namespace glop {
30 
31 // During a simplex iteration, when the basis 'leaving_row' has been
32 // selected, one of the main quantities needed in the primal or dual simplex
33 // algorithm is called the update row.
34 //
35 // By definition, update_row[col] is the coefficient at position
36 // 'leaving_row' in the current basis of the column 'col' of the matrix A.
37 //
38 // One efficient way to compute it is to compute the left inverse by B of the
39 // unit vector with a one at the given leaving_row, and then to take the
40 // scalar product of this left inverse with all the columns of A:
41 // update_row[col] = (unit_{leaving_row} . B^{-1}) . A_col
42 class UpdateRow {
43  public:
44  // Takes references to the linear program data we need.
45  UpdateRow(const CompactSparseMatrix& matrix,
46  const CompactSparseMatrix& transposed_matrix,
47  const VariablesInfo& variables_info, const RowToColMapping& basis,
48  const BasisFactorization& basis_factorization);
49 
50  // Invalidates the current update row and unit_row_left_inverse so the next
51  // call to ComputeUpdateRow() will recompute everything and not just return
52  // right away.
53  void Invalidate();
54 
55  // Computes the left inverse of the given unit row, and stores it in
56  // unit_row_left_inverse_. The result is computed only once if leaving_row do
57  // not change, this until the next Invalidate() call.
58  void ComputeUnitRowLeftInverse(RowIndex leaving_row);
59 
60  // Computes the relevant coefficients (See GetIsRelevantBitRow() in
61  // VariablesInfo) of the update row. The result is only computed once
62  // if leaving_row do not change, this until the next Invalidate() call.
63  void ComputeUpdateRow(RowIndex leaving_row);
64 
65  // Returns the left inverse of the unit row as computed by the last call to
66  // ComputeUpdateRow().
67  const ScatteredRow& GetUnitRowLeftInverse() const;
68 
69  // Returns true if ComputeUpdateRow() was called since the last Invalidate().
70  const bool IsComputedFor(RowIndex leaving_row) const {
71  return update_row_computed_for_ == leaving_row;
72  }
73 
74  // Returns the update coefficients and non-zero positions corresponding to the
75  // last call to ComputeUpdateRow().
76  //
77  // TODO(user): Consider returning a packed vector of coefficient parallel to
78  // GetNonZeroPositions() instead. It should be fast to compute and iteration
79  // later should be quicker.
80  const DenseRow& GetCoefficients() const;
81  const ColIndexVector& GetNonZeroPositions() const;
82  const Fractional GetCoefficient(ColIndex col) const {
83  return coefficient_[col];
84  }
85 
86  // Computes the update row including all position and fill output with it.
87  // We only use this when ComputeUnitRowLeftInverse() has already been called
88  // and we CHECK that.
89  void ComputeFullUpdateRow(RowIndex leaving_row, DenseRow* output) const;
90 
91  // Sets the algorithm parameters.
92  void SetParameters(const GlopParameters& parameters);
93 
94  // Returns statistics about this class as a string.
95  std::string StatString() const { return stats_.StatString(); }
96 
97  // Only used for testing.
98  // Computes as the update row the product 'lhs' times the linear program
99  // matrix given at construction. Only the relevant columns matter (see
100  // VariablesInfo) and 'algorithm' can be one of "column", "row" or
101  // "row_hypersparse".
102  void ComputeUpdateRowForBenchmark(const DenseRow& lhs,
103  const std::string& algorithm);
104 
105  // Deterministic time used by the scalar product computation of this class.
106  double DeterministicTime() const {
107  return DeterministicTimeForFpOperations(num_operations_);
108  }
109 
110  // This returns the asked unit row left inverse. It temporarily invalidate
111  // the class state by calling Invalidate().
112  const ScatteredRow& ComputeAndGetUnitRowLeftInverse(RowIndex leaving_row);
113 
114  private:
115  // ComputeUpdateRow() does the common work and calls one of these functions
116  // depending on the situation.
117  void ComputeUpdatesRowWise();
118  void ComputeUpdatesRowWiseHypersparse();
119  void ComputeUpdatesColumnWise();
120  void ComputeUpdatesForSingleRow(ColIndex row_as_col);
121 
122  // Problem data that should be updated from outside.
123  const CompactSparseMatrix& matrix_;
124  const CompactSparseMatrix& transposed_matrix_;
125  const VariablesInfo& variables_info_;
126  const RowToColMapping& basis_;
127  const BasisFactorization& basis_factorization_;
128 
129  // Left inverse by B of a unit row. Its scalar product with a column 'a' of A
130  // gives the value of the right inverse of 'a' on the 'leaving_row'.
131  ScatteredRow unit_row_left_inverse_;
132 
133  // The non-zeros of unit_row_left_inverse_ above the drop tolerance.
134  std::vector<ColIndex> unit_row_left_inverse_filtered_non_zeros_;
135 
136  // Holds the current update row data.
137  // Note that non_zero_position_set_ is not always up to date.
138  ColIndexVector non_zero_position_list_;
139  DenseBitRow non_zero_position_set_;
140  DenseRow coefficient_;
141 
142  // Boolean used to avoid recomputing many times the same thing.
143  bool compute_update_row_;
144  RowIndex left_inverse_computed_for_ = kInvalidRow;
145  RowIndex update_row_computed_for_ = kInvalidRow;
146 
147  // Statistics about this class.
148  struct Stats : public StatsGroup {
149  Stats()
150  : StatsGroup("UpdateRow"),
151  unit_row_left_inverse_density("unit_row_left_inverse_density", this),
152  unit_row_left_inverse_accuracy("unit_row_left_inverse_accuracy",
153  this),
154  update_row_density("update_row_density", this) {}
155  RatioDistribution unit_row_left_inverse_density;
156  DoubleDistribution unit_row_left_inverse_accuracy;
157  RatioDistribution update_row_density;
158  };
159 
160  // Track the number of basic floating point multiplication.
161  // Used by DeterministicTime().
162  int64_t num_operations_;
163 
164  // Glop standard classes.
165  GlopParameters parameters_;
166  Stats stats_;
167 
168  DISALLOW_COPY_AND_ASSIGN(UpdateRow);
169 };
170 
171 } // namespace glop
172 } // namespace operations_research
173 
174 #endif // OR_TOOLS_GLOP_UPDATE_ROW_H_
const ScatteredRow & GetUnitRowLeftInverse() const
Definition: update_row.cc:47
const ScatteredRow & ComputeAndGetUnitRowLeftInverse(RowIndex leaving_row)
Definition: update_row.cc:51
const DenseRow & GetCoefficients() const
Definition: update_row.cc:183
void ComputeUpdateRowForBenchmark(const DenseRow &lhs, const std::string &algorithm)
Definition: update_row.cc:167
void ComputeUnitRowLeftInverse(RowIndex leaving_row)
Definition: update_row.cc:59
UpdateRow(const CompactSparseMatrix &matrix, const CompactSparseMatrix &transposed_matrix, const VariablesInfo &variables_info, const RowToColMapping &basis, const BasisFactorization &basis_factorization)
Definition: update_row.cc:23
const bool IsComputedFor(RowIndex leaving_row) const
Definition: update_row.h:70
void ComputeFullUpdateRow(RowIndex leaving_row, DenseRow *output) const
Definition: update_row.cc:310
const Fractional GetCoefficient(ColIndex col) const
Definition: update_row.h:82
void ComputeUpdateRow(RowIndex leaving_row)
Definition: update_row.cc:76
void SetParameters(const GlopParameters &parameters)
Definition: update_row.cc:189
const ColIndexVector & GetNonZeroPositions() const
Definition: update_row.cc:185
std::string StatString() const
Definition: update_row.h:95
SatParameters parameters
ColIndex col
Definition: markowitz.cc:186
std::vector< ColIndex > ColIndexVector
Definition: lp_types.h:350
constexpr RowIndex kInvalidRow(-1)
static double DeterministicTimeForFpOperations(int64_t n)
Definition: lp_types.h:421
Collection of objects used to extend the Constraint Solver library.