OR-Tools  9.6
dual_edge_norms.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_DUAL_EDGE_NORMS_H_
15 #define OR_TOOLS_GLOP_DUAL_EDGE_NORMS_H_
16 
17 #include <string>
18 
20 #include "ortools/glop/parameters.pb.h"
25 #include "ortools/util/stats.h"
26 
27 namespace operations_research {
28 namespace glop {
29 
30 // This class maintains the dual edge squared norms to be used in the
31 // dual steepest edge pricing. The dual edge u_i associated with a basic
32 // variable of row index i is such that u_i.B = e_i where e_i is the unit row
33 // vector with a 1.0 at position i and B the current basis. We call such vector
34 // u_i an unit row left inverse, and it can be computed by
35 //
36 // basis_factorization.LeftSolveForUnitRow(i, &u_i);
37 //
38 // Instead of computing each ||u_i|| at every iteration, it is more efficient to
39 // update them incrementally for each basis pivot applied to B. See the code or
40 // the papers below for details:
41 //
42 // J.J. Forrest, D. Goldfarb, "Steepest-edge simplex algorithms for linear
43 // programming", Mathematical Programming 57 (1992) 341-374, North-Holland.
44 // http://www.springerlink.com/content/q645w3t2q229m248/
45 //
46 // Achim Koberstein, "The dual simplex method, techniques for a fast and stable
47 // implementation", PhD, Paderborn, Univ., 2005.
48 // http://digital.ub.uni-paderborn.de/hs/download/pdf/3885?originalFilename=true
50  public:
51  // Takes references to the linear program data we need.
52  explicit DualEdgeNorms(const BasisFactorization& basis_factorization);
53 
54  // Clears, i.e. reset the object to its initial value. This will trigger a
55  // full norm recomputation on the next GetEdgeSquaredNorms().
56  void Clear();
57 
58  // When we just add new constraints to the matrix and use an incremental
59  // solve, we do not need to recompute the norm of the old rows, and the norm
60  // of the new ones can be just set to 1 as long as we use identity columns for
61  // these.
62  void ResizeOnNewRows(RowIndex new_size);
63 
64  // If this is true, then the caller must re-factorize the basis before the
65  // next call to GetEdgeSquaredNorms(). This is because the latter will
66  // recompute the norms from scratch and therefore needs a hightened precision
67  // and speed. This also indicates if GetEdgeSquaredNorms() will trigger a
68  // recomputation.
69  bool NeedsBasisRefactorization() const;
70 
71  // Returns the dual edge squared norms. This is only valid if the caller
72  // properly called UpdateBeforeBasisPivot() before each basis pivot, or just
73  // called Clear().
75 
76  // Updates the norms if the columns of the basis where permuted.
77  void UpdateDataOnBasisPermutation(const ColumnPermutation& col_perm);
78 
79  // Computes exactly the norm of the given leaving row, and returns true if it
80  // is good enough compared to our current norm. In both case update the
81  // current norm with its precise version and decide if we should recompute
82  // norms on the next GetEdgeSquaredNorms().
83  bool TestPrecision(RowIndex leaving_row,
84  const ScatteredRow& unit_row_left_inverse);
85 
86  // Updates the norms just before a basis pivot is applied:
87  // - The column at leaving_row will leave the basis and the column at
88  // entering_col will enter it.
89  // - direction is the right inverse of the entering column.
90  // - unit_row_left_inverse is the left inverse of the unit row with index
91  // given by the leaving_row. This is also the leaving dual edge.
92  void UpdateBeforeBasisPivot(ColIndex entering_col, RowIndex leaving_row,
93  const ScatteredColumn& direction,
94  const ScatteredRow& unit_row_left_inverse);
95 
96  // Sets the algorithm parameters.
97  void SetParameters(const GlopParameters& parameters) {
98  parameters_ = parameters;
99  }
100 
101  // Stats related functions.
102  std::string StatString() const { return stats_.StatString(); }
103 
104  private:
105  // Recomputes the dual edge squared norms from scratch with maximum precision.
106  // The matrix must have been refactorized before because we will do a lot of
107  // inversions. See NeedsBasisRefactorization(). This is checked in debug mode.
108  void ComputeEdgeSquaredNorms();
109 
110  // Computes the vector tau needed to update the norms using a right solve:
111  // B.tau = (u_i)^T, u_i.B = e_i for i = leaving_row.
112  const DenseColumn& ComputeTau(const ScatteredColumn& unit_row_left_inverse);
113 
114  // Statistics.
115  struct Stats : public StatsGroup {
116  Stats()
117  : StatsGroup("DualEdgeNorms"),
118  tau_density("tau_density", this),
119  edge_norms_accuracy("edge_norms_accuracy", this),
120  lower_bounded_norms("lower_bounded_norms", this) {}
121  RatioDistribution tau_density;
122  DoubleDistribution edge_norms_accuracy;
123  IntegerDistribution lower_bounded_norms;
124  };
125  Stats stats_;
126 
127  // Parameters.
128  GlopParameters parameters_;
129 
130  // Problem data that should be updated from outside.
131  const BasisFactorization& basis_factorization_;
132 
133  // The dual edge norms.
134  DenseColumn edge_squared_norms_;
135  DenseColumn tmp_edge_squared_norms_;
136 
137  // Whether we should recompute the norm from scratch.
138  bool recompute_edge_squared_norms_;
139 
140  DISALLOW_COPY_AND_ASSIGN(DualEdgeNorms);
141 };
142 
143 } // namespace glop
144 } // namespace operations_research
145 
146 #endif // OR_TOOLS_GLOP_DUAL_EDGE_NORMS_H_
void UpdateBeforeBasisPivot(ColIndex entering_col, RowIndex leaving_row, const ScatteredColumn &direction, const ScatteredRow &unit_row_left_inverse)
void UpdateDataOnBasisPermutation(const ColumnPermutation &col_perm)
bool TestPrecision(RowIndex leaving_row, const ScatteredRow &unit_row_left_inverse)
DualEdgeNorms(const BasisFactorization &basis_factorization)
void SetParameters(const GlopParameters &parameters)
SatParameters parameters
StrictITIVector< RowIndex, Fractional > DenseColumn
Definition: lp_types.h:370
Collection of objects used to extend the Constraint Solver library.