OR-Tools  9.3
primal_edge_norms.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_PRIMAL_EDGE_NORMS_H_
15#define OR_TOOLS_GLOP_PRIMAL_EDGE_NORMS_H_
16
17#include <cstdint>
18
20#include "ortools/glop/parameters.pb.h"
26#include "ortools/util/stats.h"
27
28namespace operations_research {
29namespace glop {
30
31// This class maintains the primal edge squared norms (and other variants) to be
32// used in the primal pricing step. Instead of computing the needed values from
33// scractch at each iteration, it is more efficient to update them incrementally
34// for each basis pivot applied to the simplex basis matrix B.
35//
36// Terminology:
37// - To each non-basic column 'a' of a matrix A, we can associate an "edge" in
38// the kernel of A equal to 1.0 on the index of 'a' and '-B^{-1}.a' on the
39// basic variables.
40// - 'B^{-1}.a' is called the "right inverse" of 'a'.
41// - The entering edge is the edge we are following during a simplex step,
42// and we call "direction" the reverse of this edge restricted to the
43// basic variables, i.e. the right inverse of the entering column.
44//
45// Papers:
46// - D. Goldfarb, J.K. Reid, "A practicable steepest-edge simplex algorithm"
47// Mathematical Programming 12 (1977) 361-371, North-Holland.
48// http://www.springerlink.com/content/g8335137n3j16934/
49// - J.J. Forrest, D. Goldfarb, "Steepest-edge simplex algorithms for linear
50// programming", Mathematical Programming 57 (1992) 341-374, North-Holland.
51// http://www.springerlink.com/content/q645w3t2q229m248/
52// - Ping-Qi Pan "A fast simplex algorithm for linear programming".
53// http://www.optimization-online.org/DB_FILE/2007/10/1805.pdf
54// - Ping-Qi Pan, "Efficient nested pricing in the simplex algorithm",
55// http://www.optimization-online.org/DB_FILE/2007/10/1810.pdf
57 public:
58 // Takes references to the linear program data we need. Note that we assume
59 // that the matrix will never change in our back, but the other references are
60 // supposed to reflect the correct state.
61 PrimalEdgeNorms(const CompactSparseMatrix& compact_matrix,
62 const VariablesInfo& variables_info,
63 const BasisFactorization& basis_factorization);
64
65 // Clears, i.e. resets the object to its initial value. This will trigger
66 // a recomputation for the next Get*() method call.
67 void Clear();
68
69 // If this is true, then the caller must re-factorize the basis before the
70 // next call to GetEdgeSquaredNorms(). This is because the latter will
71 // recompute the norms from scratch and therefore needs a hightened precision
72 // and speed.
73 bool NeedsBasisRefactorization() const;
74
75 // Depending on the SetPricingRule(), this returns one of the "norms" vector
76 // below. Note that all norms are squared.
78
79 // Returns the primal edge squared norms. This is only valid if the caller
80 // properly called UpdateBeforeBasisPivot() before each basis pivot, or if
81 // this is the first call to this function after a Clear(). Note that only the
82 // relevant columns are filled.
84
85 // Returns an approximation of the edges norms "devex".
86 // This is only valid if the caller properly called UpdateBeforeBasisPivot()
87 // before each basis pivot, or if this is the first call to this function
88 // after a Clear().
90
91 // Returns the L2 norms of all the columns of A.
92 // Note that this is currently not cleared by Clear().
94
95 // Compares the current entering edge norm with its precise version (using the
96 // direction that wasn't avaible before) and triggers a full recomputation if
97 // the precision is not good enough (see recompute_edges_norm_threshold in
98 // GlopParameters). As a side effect, this replace the entering_col edge
99 // norm with its precise version.
100 void TestEnteringEdgeNormPrecision(ColIndex entering_col,
101 const ScatteredColumn& direction);
102
103 // Updates any internal data BEFORE the given simplex pivot is applied to B.
104 // Note that no updates are needed in case of a bound flip.
105 // The arguments are in order:
106 // - The index of the entering non-basic column of A.
107 // - The index in B of the leaving basic variable.
108 // - The 'direction', i.e. the right inverse of the entering column.
109 // - The update row (see UpdateRow), which will only be computed if needed.
110 void UpdateBeforeBasisPivot(ColIndex entering_col, ColIndex leaving_col,
111 RowIndex leaving_row,
112 const ScatteredColumn& direction,
113 UpdateRow* update_row);
114
115 // Sets the algorithm parameters.
116 void SetParameters(const GlopParameters& parameters) {
117 parameters_ = parameters;
118 }
119
120 // This changes what GetSquaredNorms() returns.
121 void SetPricingRule(GlopParameters::PricingRule rule) {
122 pricing_rule_ = rule;
123 }
124
125 // Registers a boolean that will be set to true each time the norms are or
126 // will be recomputed. This allows anyone that depends on this to know that it
127 // cannot just assume an incremental changes and needs to updates its data.
128 // Important: UpdateBeforeBasisPivot() will not trigger this.
129 void AddRecomputationWatcher(bool* watcher) { watchers_.push_back(watcher); }
130
131 // Returns a string with statistics about this class.
132 std::string StatString() const { return stats_.StatString(); }
133
134 // Deterministic time used by the scalar product computation of this class.
135 double DeterministicTime() const {
136 return DeterministicTimeForFpOperations(num_operations_);
137 }
138
139 private:
140 // Statistics about this class.
141 struct Stats : public StatsGroup {
142 Stats()
143 : StatsGroup("PrimalEdgeNorms"),
144 direction_left_inverse_density("direction_left_inverse_density",
145 this),
146 direction_left_inverse_accuracy("direction_left_inverse_accuracy",
147 this),
148 edges_norm_accuracy("edges_norm_accuracy", this),
149 lower_bounded_norms("lower_bounded_norms", this) {}
150 RatioDistribution direction_left_inverse_density;
151 DoubleDistribution direction_left_inverse_accuracy;
152 DoubleDistribution edges_norm_accuracy;
153 IntegerDistribution lower_bounded_norms;
154 };
155
156 // Recompute the matrix column L2 norms from scratch.
157 void ComputeMatrixColumnNorms();
158
159 // Recompute the edge squared L2 norms from scratch.
160 void ComputeEdgeSquaredNorms();
161
162 // Compute the left inverse of the direction.
163 // The first argument is there for checking precision.
164 void ComputeDirectionLeftInverse(ColIndex entering_col,
165 const ScatteredColumn& direction);
166
167 // Updates edges_squared_norm_ according to the given pivot.
168 void UpdateEdgeSquaredNorms(ColIndex entering_col, ColIndex leaving_col,
169 RowIndex leaving_row,
170 const DenseColumn& direction,
171 const UpdateRow& update_row);
172
173 // Resets all devex weights to 1.0 .
174 void ResetDevexWeights();
175
176 // Updates devex_weights_ according to the given pivot.
177 void UpdateDevexWeights(ColIndex entering_col, ColIndex leaving_col,
178 RowIndex leaving_row, const DenseColumn& direction,
179 const UpdateRow& update_row);
180
181 // Problem data that should be updated from outside.
182 const CompactSparseMatrix& compact_matrix_;
183 const VariablesInfo& variables_info_;
184 const BasisFactorization& basis_factorization_;
185
186 // Internal data.
187 GlopParameters parameters_;
188 GlopParameters::PricingRule pricing_rule_ = GlopParameters::DANTZIG;
189 Stats stats_;
190
191 // Booleans to control what happens on the next ChooseEnteringColumn() call.
192 bool must_refactorize_basis_;
193 bool recompute_edge_squared_norms_;
194 bool reset_devex_weights_;
195
196 // Norm^2 of the edges of the relevant columns of A.
197 DenseRow edge_squared_norms_;
198
199 // Norm of all the columns of A.
200 DenseRow matrix_column_norms_;
201
202 // Approximation of edges norms "devex".
203 // Denoted by vector 'w' in Pin Qi Pan (1810.pdf section 1.1.4)
204 // At any time, devex_weights_ >= 1.0.
205 DenseRow devex_weights_;
206
207 // Tracks number of updates of the devex weights since we have to reset
208 // them to 1.0 every now and then.
209 int num_devex_updates_since_reset_;
210
211 // Left inverse by B of the 'direction'. This is the transpose of 'v' in the
212 // steepest edge paper. Its scalar product with a column 'a' of A gives the
213 // value of the scalar product of the 'direction' with the right inverse of
214 // 'a'.
215 ScatteredRow direction_left_inverse_;
216
217 // Used by DeterministicTime().
218 int64_t num_operations_;
219
220 // Boolean(s) to set to false when the norms are changed outside of the
221 // UpdateBeforeBasisPivot() function.
222 std::vector<bool*> watchers_;
223
224 DISALLOW_COPY_AND_ASSIGN(PrimalEdgeNorms);
225};
226
227} // namespace glop
228} // namespace operations_research
229
230#endif // OR_TOOLS_GLOP_PRIMAL_EDGE_NORMS_H_
PrimalEdgeNorms(const CompactSparseMatrix &compact_matrix, const VariablesInfo &variables_info, const BasisFactorization &basis_factorization)
void UpdateBeforeBasisPivot(ColIndex entering_col, ColIndex leaving_col, RowIndex leaving_row, const ScatteredColumn &direction, UpdateRow *update_row)
void SetPricingRule(GlopParameters::PricingRule rule)
void TestEnteringEdgeNormPrecision(ColIndex entering_col, const ScatteredColumn &direction)
void SetParameters(const GlopParameters &parameters)
SatParameters parameters
StrictITIVector< ColIndex, Fractional > DenseRow
Definition: lp_types.h:303
StrictITIVector< RowIndex, Fractional > DenseColumn
Definition: lp_types.h:332
static double DeterministicTimeForFpOperations(int64_t n)
Definition: lp_types.h:383
Collection of objects used to extend the Constraint Solver library.