Google OR-Tools v9.15
a fast and portable software suite for combinatorial optimization
Loading...
Searching...
No Matches
lp_data_utils.h
Go to the documentation of this file.
1// Copyright 2010-2025 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// Utility helpers for manipulating LinearProgram and other types defined in
15// lp_data.
16
17#ifndef ORTOOLS_LP_DATA_LP_DATA_UTILS_H_
18#define ORTOOLS_LP_DATA_LP_DATA_UTILS_H_
19
25
26namespace operations_research {
27namespace glop {
28
29// This is separated from LinearProgram class because of a cyclic dependency
30// when scaling as an LP.
31void Scale(LinearProgram* lp, SparseMatrixScaler* scaler,
33
34// A convenience method for above providing a default algorithm for callers that
35// don't specify one.
36void Scale(LinearProgram* lp, SparseMatrixScaler* scaler);
37
38// Class to facilitate the conversion between an original "unscaled" LP problem
39// and its scaled version. It is easy to get the direction wrong, so it make
40// sense to have a single place where all the scaling formulas are kept.
42 public:
43 // Clear all scaling coefficients.
44 void Clear();
45
46 // Scale the given LP.
47 void Scale(LinearProgram* lp);
48 void Scale(const GlopParameters& params, LinearProgram* lp);
49 void ConfigureFromFactors(absl::Span<const double> row_factors,
50 absl::Span<const double> col_factors);
51
52 // Transforms value from unscaled domain to the scaled one.
53 Fractional ScaleVariableValue(ColIndex col, Fractional value) const;
54 Fractional ScaleReducedCost(ColIndex col, Fractional value) const;
55 Fractional ScaleDualValue(RowIndex row, Fractional value) const;
56 Fractional ScaleConstraintActivity(RowIndex row, Fractional value) const;
57
58 // Transforms corresponding value from the scaled domain to the original one.
59 Fractional UnscaleVariableValue(ColIndex col, Fractional value) const;
60 Fractional UnscaleReducedCost(ColIndex col, Fractional value) const;
61 Fractional UnscaleDualValue(RowIndex row, Fractional value) const;
62 Fractional UnscaleLeftSolveValue(RowIndex row, Fractional value) const;
63 Fractional UnscaleConstraintActivity(RowIndex row, Fractional value) const;
64
65 // Unscale a row vector v such that v.B = unit_row. When basis_col is the
66 // index of the Column that correspond to the unit position in matrix B.
67 void UnscaleUnitRowLeftSolve(ColIndex basis_col,
68 ScatteredRow* left_inverse) const;
69
70 // Unscale a col vector v such that B.c = matrix_column_col.
71 void UnscaleColumnRightSolve(const RowToColMapping& basis, ColIndex col,
72 ScatteredColumn* right_inverse) const;
73
74 // A variable value in the original domain must be multiplied by this factor
75 // to be in the scaled domain.
76 Fractional VariableScalingFactor(ColIndex col) const;
77
78 // Same as VariableScalingFactor() except that ColIndex greater than the
79 // number of columns will be interpreted as "slack" variable whose scaling
80 // factor depends on the row.
82
83 // Extra scaling function, to scale objective/bounds.
84 void AverageCostScaling(DenseRow* objective);
85 void ContainOneBoundScaling(DenseRow* upper_bounds, DenseRow* lower_bounds);
86
87 // Visible for testing. All variable/constraint bounds of the original LP
88 // where multiplied by this factor. Nothing else changed.
89 Fractional BoundsScalingFactor() const { return bound_scaling_factor_; }
90
91 // Visible for testing. All objective coefficients of the original LP where
92 // multiplied by this factor. Nothing else changed.
94 return objective_scaling_factor_;
95 }
96
97 private:
98 Fractional RowUnscalingFactor(RowIndex row) const {
99 return matrix_is_scaled_ ? row_unscaling_factors_[row] : 1.0;
100 }
101 Fractional ColUnscalingFactor(ColIndex col) const {
102 return matrix_is_scaled_ ? col_unscaling_factors_[col] : 1.0;
103 }
104
105 bool matrix_is_scaled_ = false;
106 DenseColumn row_unscaling_factors_;
107 DenseRow col_unscaling_factors_;
108
109 Fractional bound_scaling_factor_ = 1.0;
110 Fractional objective_scaling_factor_ = 1.0;
111};
112
113} // namespace glop
114} // namespace operations_research
115
116#endif // ORTOOLS_LP_DATA_LP_DATA_UTILS_H_
GlopParameters_ScalingAlgorithm ScalingAlgorithm
void UnscaleUnitRowLeftSolve(ColIndex basis_col, ScatteredRow *left_inverse) const
void AverageCostScaling(DenseRow *objective)
void ConfigureFromFactors(absl::Span< const double > row_factors, absl::Span< const double > col_factors)
Fractional VariableScalingFactorWithSlack(ColIndex col) const
Fractional ScaleVariableValue(ColIndex col, Fractional value) const
Fractional UnscaleDualValue(RowIndex row, Fractional value) const
void ContainOneBoundScaling(DenseRow *upper_bounds, DenseRow *lower_bounds)
Fractional UnscaleConstraintActivity(RowIndex row, Fractional value) const
Fractional UnscaleLeftSolveValue(RowIndex row, Fractional value) const
Fractional ScaleDualValue(RowIndex row, Fractional value) const
Fractional ScaleConstraintActivity(RowIndex row, Fractional value) const
Fractional UnscaleReducedCost(ColIndex col, Fractional value) const
Fractional VariableScalingFactor(ColIndex col) const
Fractional UnscaleVariableValue(ColIndex col, Fractional value) const
Fractional ScaleReducedCost(ColIndex col, Fractional value) const
void UnscaleColumnRightSolve(const RowToColMapping &basis, ColIndex col, ScatteredColumn *right_inverse) const
double Fractional
Definition lp_types.h:81
StrictITIVector< RowIndex, ColIndex > RowToColMapping
Definition lp_types.h:394
void Scale(LinearProgram *lp, SparseMatrixScaler *scaler)
StrictITIVector< RowIndex, Fractional > DenseColumn
Definition lp_types.h:380
StrictITIVector< ColIndex, Fractional > DenseRow
Definition lp_types.h:351
OR-Tools root namespace.