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