OR-Tools  9.0
variable_values.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_VARIABLE_VALUES_H_
15 #define OR_TOOLS_GLOP_VARIABLE_VALUES_H_
16 
19 #include "ortools/glop/pricing.h"
23 #include "ortools/util/stats.h"
24 
25 namespace operations_research {
26 namespace glop {
27 
28 // Class holding all the variable values and responsible for updating them. The
29 // variable values 'x' are such that 'A.x = 0' where A is the linear program
30 // matrix. This is because slack variables with bounds corresponding to the
31 // constraints bounds were added to the linear program matrix A.
32 //
33 // Some remarks:
34 // - For convenience, the variable values are stored in a DenseRow and indexed
35 // by ColIndex, like the variables and the columns of A.
36 // - During the dual-simplex, all non-basic variable values are at their exact
37 // bounds or exactly at 0.0 for a free variable.
38 // - During the primal-simplex, the non-basic variable values may not be exactly
39 // at their bounds because of bound-shifting during degenerate simplex
40 // pivoting which is implemented by not setting the variable values exactly at
41 // their bounds to have a lower primal residual error.
43  public:
44  VariableValues(const GlopParameters& parameters,
45  const CompactSparseMatrix& matrix,
46  const RowToColMapping& basis,
47  const VariablesInfo& variables_info,
48  const BasisFactorization& basis_factorization,
49  DualEdgeNorms* dual_edge_norms,
50  DynamicMaximum<RowIndex>* dual_prices);
51 
52  // Getters for the variable values.
53  const Fractional Get(ColIndex col) const { return variable_values_[col]; }
54  const DenseRow& GetDenseRow() const { return variable_values_; }
55 
56  // Sets the value of a non-basic variable to the exact value implied by its
57  // current status. Note that the basic variable values are NOT updated by this
58  // function and it is up to the client to call RecomputeBasicVariableValues().
60 
61  // Calls SetNonBasicVariableValueFromStatus() on all non-basic variables.
63 
64  // Recomputes the value of the basic variables from the non-basic ones knowing
65  // that the linear program matrix A times the variable values vector must be
66  // zero. It is better to call this when the basis is refactorized. This
67  // is checked in debug mode.
69 
70  // Computes the infinity norm of A.x where A is the linear_program matrix and
71  // x is the variable values column.
73 
74  // Computes the maximum bound error for all the variables, defined as the
75  // distance of the current value of the variable to its interval
76  // [lower bound, upper bound]. The infeasibility is thus equal to 0.0 if the
77  // current value falls within the bounds, to the distance to lower_bound
78  // (resp. upper_bound), if the current value is below (resp. above)
79  // lower_bound (resp. upper_bound).
82 
83  // Updates the variable during a simplex pivot:
84  // - step * direction is substracted from the basic variables value.
85  // - step is added to the entering column value.
86  void UpdateOnPivoting(const ScatteredColumn& direction, ColIndex entering_col,
87  Fractional step);
88 
89  // Batch version of SetNonBasicVariableValueFromStatus(). This function also
90  // updates the basic variable values and infeasibility statuses if
91  // update_basic_variables is true. The update is done in an incremental way
92  // and is thus more efficient than calling afterwards
93  // RecomputeBasicVariableValues() and RecomputeDualPrices().
94  void UpdateGivenNonBasicVariables(const std::vector<ColIndex>& cols_to_update,
95  bool update_basic_variables);
96 
97  // Functions dealing with the primal-infeasible basic variables. A basic
98  // variable is primal-infeasible if its infeasibility is stricly greater than
99  // the primal feasibility tolerance. These are exactly the dual "prices" and
100  // are just used during the dual simplex.
101  //
102  // This information is only available after a call to RecomputeDualPrices()
103  // and has to be kept in sync by calling UpdateDualPrices() for the rows that
104  // changed values.
105  void RecomputeDualPrices();
106  void UpdateDualPrices(const std::vector<RowIndex>& row);
107 
108  // The primal phase I objective is related to the primal infeasible
109  // information above. The cost of a basic column will be 1 if the variable is
110  // above its upper bound by strictly more than the primal tolerance, and -1 if
111  // it is lower than its lower bound by strictly less than the same tolerance.
112  //
113  // Returns true iff some cost changed.
114  template <typename Rows>
115  bool UpdatePrimalPhaseICosts(const Rows& rows, DenseRow* objective);
116 
117  // Sets the variable value of a given column.
118  void Set(ColIndex col, Fractional value) { variable_values_[col] = value; }
119 
120  // Parameters and stats functions.
121  std::string StatString() const { return stats_.StatString(); }
122 
123  private:
124  // It is important that the infeasibility is always computed in the same
125  // way. So the code should always use these functions that returns a positive
126  // value when the variable is out of bounds.
127  Fractional GetUpperBoundInfeasibility(ColIndex col) const {
128  return variable_values_[col] -
129  variables_info_.GetVariableUpperBounds()[col];
130  }
131  Fractional GetLowerBoundInfeasibility(ColIndex col) const {
132  return variables_info_.GetVariableLowerBounds()[col] -
133  variable_values_[col];
134  }
135 
136  // Input problem data.
137  const GlopParameters& parameters_;
138  const CompactSparseMatrix& matrix_;
139  const RowToColMapping& basis_;
140  const VariablesInfo& variables_info_;
141  const BasisFactorization& basis_factorization_;
142 
143  // The dual prices are a normalized version of the primal infeasibility.
144  DualEdgeNorms* dual_edge_norms_;
145  DynamicMaximum<RowIndex>* dual_prices_;
146 
147  // Values of the variables.
148  DenseRow variable_values_;
149 
150  mutable StatsGroup stats_;
151  mutable ScatteredColumn scratchpad_;
152 
153  // A temporary scattered column that is always reset to all zero after use.
154  ScatteredColumn initially_all_zero_scratchpad_;
155 
156  DISALLOW_COPY_AND_ASSIGN(VariableValues);
157 };
158 
159 template <typename Rows>
161  DenseRow* objective) {
162  SCOPED_TIME_STAT(&stats_);
163  bool changed = false;
164  const Fractional tolerance = parameters_.primal_feasibility_tolerance();
165  for (const RowIndex row : rows) {
166  const ColIndex col = basis_[row];
167  Fractional new_cost = 0.0;
168  if (GetUpperBoundInfeasibility(col) > tolerance) {
169  new_cost = 1.0;
170  } else if (GetLowerBoundInfeasibility(col) > tolerance) {
171  new_cost = -1.0;
172  }
173  if (new_cost != (*objective)[col]) {
174  changed = true;
175  (*objective)[col] = new_cost;
176  }
177  }
178  return changed;
179 }
180 
181 } // namespace glop
182 } // namespace operations_research
183 
184 #endif // OR_TOOLS_GLOP_VARIABLE_VALUES_H_
std::string StatString() const
Definition: stats.cc:71
void Set(ColIndex col, Fractional value)
void UpdateGivenNonBasicVariables(const std::vector< ColIndex > &cols_to_update, bool update_basic_variables)
void UpdateDualPrices(const std::vector< RowIndex > &row)
void UpdateOnPivoting(const ScatteredColumn &direction, ColIndex entering_col, Fractional step)
VariableValues(const GlopParameters &parameters, const CompactSparseMatrix &matrix, const RowToColMapping &basis, const VariablesInfo &variables_info, const BasisFactorization &basis_factorization, DualEdgeNorms *dual_edge_norms, DynamicMaximum< RowIndex > *dual_prices)
const Fractional Get(ColIndex col) const
bool UpdatePrimalPhaseICosts(const Rows &rows, DenseRow *objective)
const DenseRow & GetVariableUpperBounds() const
const DenseRow & GetVariableLowerBounds() const
SatParameters parameters
int64_t value
ColIndex col
Definition: markowitz.cc:183
RowIndex row
Definition: markowitz.cc:182
StrictITIVector< ColIndex, Fractional > DenseRow
Definition: lp_types.h:300
StrictITIVector< RowIndex, ColIndex > RowToColMapping
Definition: lp_types.h:343
Collection of objects used to extend the Constraint Solver library.
#define SCOPED_TIME_STAT(stats)
Definition: stats.h:438