19#include "absl/log/check.h"
22#include "ortools/glop/parameters.pb.h"
37 : compact_matrix_(compact_matrix),
38 variables_info_(variables_info),
39 basis_factorization_(basis_factorization),
41 recompute_edge_squared_norms_(true),
42 reset_devex_weights_(true),
43 edge_squared_norms_(),
44 matrix_column_norms_(),
46 direction_left_inverse_(),
51 matrix_column_norms_.clear();
52 recompute_edge_squared_norms_ =
true;
53 reset_devex_weights_ =
true;
54 for (
bool* watcher : watchers_) *watcher =
true;
58 if (pricing_rule_ != GlopParameters ::STEEPEST_EDGE)
return false;
59 return recompute_edge_squared_norms_;
63 switch (pricing_rule_) {
64 case GlopParameters::DANTZIG:
66 case GlopParameters::STEEPEST_EDGE:
68 case GlopParameters::DEVEX:
74 if (recompute_edge_squared_norms_) ComputeEdgeSquaredNorms();
75 return edge_squared_norms_;
79 if (reset_devex_weights_) ResetDevexWeights();
80 return devex_weights_;
84 if (matrix_column_norms_.empty()) ComputeMatrixColumnNorms();
85 return matrix_column_norms_;
90 if (!recompute_edge_squared_norms_) {
94 const Fractional old_squared_norm = edge_squared_norms_[entering_col];
96 edge_squared_norms_[entering_col] = precise_squared_norm;
98 const Fractional precise_norm = sqrt(precise_squared_norm);
99 const Fractional estimated_edges_norm_accuracy =
100 (precise_norm - sqrt(old_squared_norm)) / precise_norm;
101 stats_.edges_norm_accuracy.Add(estimated_edges_norm_accuracy);
102 if (std::abs(estimated_edges_norm_accuracy) >
103 parameters_.recompute_edges_norm_threshold()) {
104 VLOG(1) <<
"Recomputing edge norms: " << sqrt(precise_squared_norm)
105 <<
" vs " << sqrt(old_squared_norm);
106 recompute_edge_squared_norms_ =
true;
107 for (
bool* watcher : watchers_) *watcher =
true;
110 if (old_squared_norm < 0.25 * precise_squared_norm) {
111 VLOG(1) <<
"Imprecise norm, reprice. old=" << old_squared_norm
112 <<
" new=" << precise_squared_norm;
120 ColIndex leaving_col,
121 RowIndex leaving_row,
125 DCHECK_NE(entering_col, leaving_col);
126 if (!recompute_edge_squared_norms_) {
128 ComputeDirectionLeftInverse(entering_col, direction);
129 UpdateEdgeSquaredNorms(entering_col, leaving_col, leaving_row,
130 direction.
values, *update_row);
132 if (!reset_devex_weights_) {
135 ++num_devex_updates_since_reset_;
136 if (num_devex_updates_since_reset_ >
137 parameters_.devex_weights_reset_period()) {
138 reset_devex_weights_ =
true;
141 UpdateDevexWeights(entering_col, leaving_col, leaving_row,
142 direction.
values, *update_row);
147void PrimalEdgeNorms::ComputeMatrixColumnNorms() {
150 for (ColIndex col(0); col < compact_matrix_.
num_cols(); ++col) {
156void PrimalEdgeNorms::ComputeEdgeSquaredNorms() {
161 const bool test_limit = (time_limit_ !=
nullptr) &&
172 compact_matrix_.
column(col));
180 recompute_edge_squared_norms_ =
false;
186void PrimalEdgeNorms::ComputeDirectionLeftInverse(
193 const ColIndex size =
RowToColIndex(direction.values.size());
194 const double kThreshold = 0.05 * size.value();
195 if (!direction_left_inverse_.non_zeros.empty() &&
196 (direction_left_inverse_.non_zeros.size() + direction.non_zeros.size() <
199 for (
const auto e : direction) {
200 direction_left_inverse_[
RowToColIndex(e.row())] = e.coefficient();
203 direction_left_inverse_.values =
Transpose(direction.values);
204 direction_left_inverse_.non_zeros.clear();
207 if (direction.non_zeros.size() < kThreshold) {
210 basis_factorization_.LeftSolve(&direction_left_inverse_);
214 compact_matrix_.ColumnScalarProduct(entering_col,
215 direction_left_inverse_.values) -
218 Density(direction_left_inverse_.values)));
228void PrimalEdgeNorms::UpdateEdgeSquaredNorms(ColIndex entering_col,
229 ColIndex leaving_col,
230 RowIndex leaving_row,
238 const Fractional pivot = -direction[leaving_row];
239 DCHECK_NE(pivot, 0.0);
243 const Fractional entering_squared_norm = edge_squared_norms_[entering_col];
245 std::max(1.0, entering_squared_norm /
Square(pivot));
247 int stat_lower_bounded_norms = 0;
249 const auto view = compact_matrix_.view();
250 auto output = edge_squared_norms_.view();
251 const auto direction_left_inverse =
252 direction_left_inverse_.values.const_view();
253 for (
const ColIndex col : update_row.GetNonZeroPositions()) {
254 const Fractional coeff = update_row.GetCoefficient(col);
256 view.ColumnScalarProduct(col, direction_left_inverse);
257 num_operations_ += view.ColumnNumEntries(col).value();
263 coeff * (coeff * leaving_squared_norm + factor * scalar_product);
271 if (output[col] < lower_bound) {
272 output[col] = lower_bound;
273 ++stat_lower_bounded_norms;
276 output[leaving_col] = leaving_squared_norm;
277 stats_.lower_bounded_norms.Add(stat_lower_bounded_norms);
280void PrimalEdgeNorms::UpdateDevexWeights(
281 ColIndex entering_col ,
282 ColIndex leaving_col , RowIndex leaving_row,
289 const Fractional pivot_magnitude = std::abs(direction[leaving_row]);
291 std::max(1.0, entering_norm / pivot_magnitude);
292 for (
const ColIndex col : update_row.GetNonZeroPositions()) {
293 const Fractional coeff = update_row.GetCoefficient(col);
294 const Fractional update_vector_norm = std::abs(coeff) * leaving_norm;
295 devex_weights_[col] =
296 std::max(devex_weights_[col],
Square(update_vector_norm));
298 devex_weights_[leaving_col] =
Square(leaving_norm);
301void PrimalEdgeNorms::ResetDevexWeights() {
303 if (parameters_.initialize_devex_with_column_norms()) {
306 devex_weights_.assign(compact_matrix_.num_cols(), 1.0);
308 num_devex_updates_since_reset_ = 0;
309 reset_devex_weights_ =
false;
bool IsRefactorized() const
Returns true if the factorization was just recomputed.
Fractional RightSolveSquaredNorm(const ColumnView &a) const
EntryIndex NumberOfEntriesInLU() const
EntryIndex num_entries() const
ColIndex num_cols() const
ColumnView column(ColIndex col) const
const DenseRow & GetMatrixColumnNorms()
DenseRow::ConstView GetSquaredNorms()
void UpdateBeforeBasisPivot(ColIndex entering_col, ColIndex leaving_col, RowIndex leaving_row, const ScatteredColumn &direction, UpdateRow *update_row)
const DenseRow & GetDevexWeights()
bool TestEnteringEdgeNormPrecision(ColIndex entering_col, const ScatteredColumn &direction)
PrimalEdgeNorms(const CompactSparseMatrix &compact_matrix, const VariablesInfo &variables_info, const BasisFactorization &basis_factorization)
const DenseRow & GetEdgeSquaredNorms()
bool NeedsBasisRefactorization() const
StrictITISpan< ColIndex, const Fractional > ConstView
ConstView const_view() const
void resize(IntType size)
void ComputeUpdateRow(RowIndex leaving_row)
const DenseBitRow & GetIsRelevantBitRow() const
double Density(const DenseRow &row)
Fractional Square(Fractional f)
const DenseRow & Transpose(const DenseColumn &col)
Similar comment as the other Transpose() implementation above.
ColIndex RowToColIndex(RowIndex row)
Get the ColIndex corresponding to the column # row.
void ClearAndResizeVectorWithNonZeros(IndexType size, ScatteredRowOrCol *v)
Sets a dense vector for which the non zeros are known to be non_zeros.
Fractional PreciseSquaredNorm(const SparseColumn &v)
Fractional SquaredNorm(const SparseColumn &v)
StrictITIVector< RowIndex, Fractional > DenseColumn
Column-vector types. Column-vector types are indexed by a row index.
StrictITIVector< ColIndex, Fractional > DenseRow
Row-vector types. Row-vector types are indexed by a column index.
const ScatteredRow & TransposedView(const ScatteredColumn &c)
In SWIG mode, we don't want anything besides these top-level includes.
#define IF_STATS_ENABLED(instructions)
#define SCOPED_TIME_STAT(stats)
StrictITIVector< Index, Fractional > values
std::vector< Index > non_zeros