OR-Tools  9.0
entering_variable.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_ENTERING_VARIABLE_H_
15 #define OR_TOOLS_GLOP_ENTERING_VARIABLE_H_
16 
17 #include "absl/random/bit_gen_ref.h"
22 #include "ortools/glop/status.h"
27 #include "ortools/util/bitset.h"
28 #include "ortools/util/stats.h"
29 
30 #if !SWIG
31 
32 namespace operations_research {
33 namespace glop {
34 
35 // This class contains the dual algorithms that choose the entering column (i.e.
36 // variable) during a dual simplex iteration. That is the dual ratio test.
37 //
38 // Terminology:
39 // - The entering edge is the edge we are following during a simplex step,
40 // and we call "direction" the reverse of this edge restricted to the
41 // basic variables, i.e. the right inverse of the entering column.
43  public:
44  // Takes references to the linear program data we need.
45  EnteringVariable(const VariablesInfo& variables_info, absl::BitGenRef random,
46  ReducedCosts* reduced_costs);
47 
48  // Dual optimization phase (i.e. phase II) ratio test.
49  // Returns the index of the entering column given that we want to move along
50  // the "update" row vector in the direction given by the sign of
51  // cost_variation. Computes the smallest step that keeps the dual feasibility
52  // for all the columns.
53  ABSL_MUST_USE_RESULT Status DualChooseEnteringColumn(
54  bool nothing_to_recompute, const UpdateRow& update_row,
55  Fractional cost_variation, std::vector<ColIndex>* bound_flip_candidates,
56  ColIndex* entering_col);
57 
58  // Dual feasibility phase (i.e. phase I) ratio test.
59  // Similar to the optimization phase test, but allows a step that increases
60  // the infeasibility of an already infeasible column. The step magnitude is
61  // the one that minimize the sum of infeasibilities when applied.
62  ABSL_MUST_USE_RESULT Status DualPhaseIChooseEnteringColumn(
63  bool nothing_to_recompute, const UpdateRow& update_row,
64  Fractional cost_variation, ColIndex* entering_col);
65 
66  // Sets the parameters.
67  void SetParameters(const GlopParameters& parameters);
68 
69  // Stats related functions.
70  std::string StatString() const { return stats_.StatString(); }
71 
72  // Deterministic time used by some of the functions of this class.
73  //
74  // TODO(user): Be exhausitive and more precise.
75  double DeterministicTime() const {
76  return DeterministicTimeForFpOperations(num_operations_);
77  }
78 
79  private:
80  // Problem data that should be updated from outside.
81  const VariablesInfo& variables_info_;
82 
83  absl::BitGenRef random_;
84  ReducedCosts* reduced_costs_;
85 
86  // Internal data.
87  GlopParameters parameters_;
88 
89  // Stats.
90  struct Stats : public StatsGroup {
91  Stats()
92  : StatsGroup("EnteringVariable"),
93  num_perfect_ties("num_perfect_ties", this) {}
94  IntegerDistribution num_perfect_ties;
95  };
96  Stats stats_;
97 
98  // Temporary vector used to hold the best entering column candidates that are
99  // tied using the current choosing criteria. We actually only store the tied
100  // candidate #2, #3, ...; because the first tied candidate is remembered
101  // anyway.
102  std::vector<ColIndex> equivalent_entering_choices_;
103 
104  // Store a column with its update coefficient and ratio.
105  // This is used during the dual phase I & II ratio tests.
106  struct ColWithRatio {
107  ColWithRatio(ColIndex _col, Fractional reduced_cost, Fractional coeff_m)
108  : col(_col), ratio(reduced_cost / coeff_m), coeff_magnitude(coeff_m) {}
109 
110  // Returns false if "this" is before "other" in a priority queue.
111  bool operator<(const ColWithRatio& other) const {
112  if (ratio == other.ratio) {
113  if (coeff_magnitude == other.coeff_magnitude) {
114  return col > other.col;
115  }
116  return coeff_magnitude < other.coeff_magnitude;
117  }
118  return ratio > other.ratio;
119  }
120 
121  ColIndex col;
124  };
125 
126  // Temporary vector used to hold breakpoints.
127  std::vector<ColWithRatio> breakpoints_;
128 
129  // Counter for the deterministic time.
130  int64_t num_operations_ = 0;
131 
132  DISALLOW_COPY_AND_ASSIGN(EnteringVariable);
133 };
134 
135 } // namespace glop
136 } // namespace operations_research
137 
138 #endif // SWIG
139 #endif // OR_TOOLS_GLOP_ENTERING_VARIABLE_H_
EnteringVariable(const VariablesInfo &variables_info, absl::BitGenRef random, ReducedCosts *reduced_costs)
ABSL_MUST_USE_RESULT Status DualPhaseIChooseEnteringColumn(bool nothing_to_recompute, const UpdateRow &update_row, Fractional cost_variation, ColIndex *entering_col)
void SetParameters(const GlopParameters &parameters)
ABSL_MUST_USE_RESULT Status DualChooseEnteringColumn(bool nothing_to_recompute, const UpdateRow &update_row, Fractional cost_variation, std::vector< ColIndex > *bound_flip_candidates, ColIndex *entering_col)
SatParameters parameters
ColIndex col
Definition: markowitz.cc:183
static double DeterministicTimeForFpOperations(int64_t n)
Definition: lp_types.h:380
Collection of objects used to extend the Constraint Solver library.
Fractional coeff_magnitude
Fractional ratio