OR-Tools  8.0
lp_data_utils.h
Go to the documentation of this file.
1 // Copyright 2010-2018 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 OR_TOOLS_LP_DATA_LP_DATA_UTILS_H_
18 #define OR_TOOLS_LP_DATA_LP_DATA_UTILS_H_
19 
24 
25 namespace operations_research {
26 namespace glop {
27 
28 // For all constraints in linear_program, if the constraint has a slack
29 // variable, change its value in *values so that the constraints itself is
30 // satisfied.
31 // Note that this obviously won't always imply that the bounds of the slack
32 // variable itself will be satisfied.
33 // The code assumes (and DCHECKs) that all constraints with a slack variable
34 // have their upper and lower bounds both set to 0. This is ensured by
35 // LinearProgram::AddSlackVariablesWhereNecessary().
36 void ComputeSlackVariablesValues(const LinearProgram& linear_program,
37  DenseRow* values);
38 
39 // This is separated from LinearProgram class because of a cyclic dependency
40 // when scaling as an LP.
41 void Scale(LinearProgram* lp, SparseMatrixScaler* scaler,
42  GlopParameters::ScalingAlgorithm scaling_method);
43 
44 // A convenience method for above providing a default algorithm for callers that
45 // don't specify one.
46 void Scale(LinearProgram* lp, SparseMatrixScaler* scaler);
47 
48 // Class to facilitate the conversion between an original "unscaled" LP problem
49 // and its scaled version. It is easy to get the direction wrong, so it make
50 // sense to have a single place where all the scaling formulas are kept.
52  public:
53  // Scale the given LP.
54  void Scale(LinearProgram* lp);
55 
56  // Clear all scaling coefficients.
57  void Clear();
58 
59  // A variable value in the original domain must be multiplied by this factor
60  // to be in the scaled domain.
61  Fractional VariableScalingFactor(ColIndex col) const;
62 
63  // Transforms corresponding value from the scaled domain to the original one.
68 
69  // Unscale a row vector v such that v.B = unit_row. When basis_col is the
70  // index of the Column that correspond to the unit position in matrix B.
71  void UnscaleUnitRowLeftSolve(ColIndex basis_col,
72  ScatteredRow* left_inverse) const;
73 
74  // Unscale a col vector v such that B.c = matrix_column_col.
75  void UnscaleColumnRightSolve(const RowToColMapping& basis, ColIndex col,
76  ScatteredColumn* right_inverse) const;
77 
78  // Visible for testing. All objective coefficients of the original LP where
79  // multiplied by this factor. Nothing else changed.
80  Fractional BoundsScalingFactor() const { return bound_scaling_factor_; }
81 
82  // Visible for testing. All variable/constraint bounds of the original LP
83  // where multiplied by this factor. Nothing else changed.
85  return objective_scaling_factor_;
86  }
87 
88  private:
89  SparseMatrixScaler scaler_;
90  Fractional bound_scaling_factor_ = 1.0;
91  Fractional objective_scaling_factor_ = 1.0;
92 };
93 
94 } // namespace glop
95 } // namespace operations_research
96 
97 #endif // OR_TOOLS_LP_DATA_LP_DATA_UTILS_H_
operations_research::glop::LpScalingHelper::ObjectiveScalingFactor
Fractional ObjectiveScalingFactor() const
Definition: lp_data_utils.h:84
operations_research::glop::DenseRow
StrictITIVector< ColIndex, Fractional > DenseRow
Definition: lp_types.h:299
operations_research::glop::LpScalingHelper::BoundsScalingFactor
Fractional BoundsScalingFactor() const
Definition: lp_data_utils.h:80
operations_research::glop::Scale
void Scale(LinearProgram *lp, SparseMatrixScaler *scaler)
Definition: lp_data_utils.cc:52
operations_research::glop::LpScalingHelper::UnscaleUnitRowLeftSolve
void UnscaleUnitRowLeftSolve(ColIndex basis_col, ScatteredRow *left_inverse) const
Definition: lp_data_utils.cc:120
operations_research::glop::LpScalingHelper::Scale
void Scale(LinearProgram *lp)
Definition: lp_data_utils.cc:76
lp_data.h
operations_research::glop::LpScalingHelper::UnscaleConstraintActivity
Fractional UnscaleConstraintActivity(RowIndex row, Fractional value) const
Definition: lp_data_utils.cc:114
value
int64 value
Definition: demon_profiler.cc:43
operations_research
The vehicle routing library lets one model and solve generic vehicle routing problems ranging from th...
Definition: dense_doubly_linked_list.h:21
matrix_scaler.h
operations_research::glop::LpScalingHelper::Clear
void Clear()
Definition: lp_data_utils.cc:84
operations_research::glop::LpScalingHelper::UnscaleDualValue
Fractional UnscaleDualValue(RowIndex row, Fractional value) const
Definition: lp_data_utils.cc:108
operations_research::glop::Fractional
double Fractional
Definition: lp_types.h:77
operations_research::glop::ComputeSlackVariablesValues
void ComputeSlackVariablesValues(const LinearProgram &linear_program, DenseRow *values)
Definition: lp_data_utils.cc:19
operations_research::glop::LpScalingHelper::UnscaleColumnRightSolve
void UnscaleColumnRightSolve(const RowToColMapping &basis, ColIndex col, ScatteredColumn *right_inverse) const
Definition: lp_data_utils.cc:139
operations_research::glop::LpScalingHelper
Definition: lp_data_utils.h:51
operations_research::glop::StrictITIVector< RowIndex, ColIndex >
operations_research::glop::LpScalingHelper::UnscaleReducedCost
Fractional UnscaleReducedCost(ColIndex col, Fractional value) const
Definition: lp_data_utils.cc:102
operations_research::glop::ScatteredRow
Definition: scattered_vector.h:193
operations_research::glop::SparseMatrixScaler
Definition: matrix_scaler.h:79
operations_research::glop::LpScalingHelper::UnscaleVariableValue
Fractional UnscaleVariableValue(ColIndex col, Fractional value) const
Definition: lp_data_utils.cc:96
operations_research::glop::LpScalingHelper::VariableScalingFactor
Fractional VariableScalingFactor(ColIndex col) const
Definition: lp_data_utils.cc:90
col
ColIndex col
Definition: markowitz.cc:176
row
RowIndex row
Definition: markowitz.cc:175
operations_research::glop::LinearProgram
Definition: lp_data.h:55
operations_research::glop::ScatteredColumn
Definition: scattered_vector.h:192
lp_types.h
parameters.pb.h