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