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