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