Google OR-Tools v9.11
a fast and portable software suite for combinatorial optimization
Loading...
Searching...
No Matches
entering_variable.h
Go to the documentation of this file.
1// Copyright 2010-2024 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
36namespace operations_research {
37namespace 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 // This type is neither copyable nor movable.
55
56 // Dual optimization phase (i.e. phase II) ratio test.
57 // Returns the index of the entering column given that we want to move along
58 // the "update" row vector in the direction given by the sign of
59 // cost_variation. Computes the smallest step that keeps the dual feasibility
60 // for all the columns.
61 ABSL_MUST_USE_RESULT Status DualChooseEnteringColumn(
62 bool nothing_to_recompute, const UpdateRow& update_row,
63 Fractional cost_variation, std::vector<ColIndex>* bound_flip_candidates,
64 ColIndex* entering_col);
65
66 // Dual feasibility phase (i.e. phase I) ratio test.
67 // Similar to the optimization phase test, but allows a step that increases
68 // the infeasibility of an already infeasible column. The step magnitude is
69 // the one that minimize the sum of infeasibilities when applied.
70 ABSL_MUST_USE_RESULT Status DualPhaseIChooseEnteringColumn(
71 bool nothing_to_recompute, const UpdateRow& update_row,
72 Fractional cost_variation, ColIndex* entering_col);
73
74 // Sets the parameters.
75 void SetParameters(const GlopParameters& parameters);
76
77 // Stats related functions.
78 std::string StatString() const { return stats_.StatString(); }
79
80 // Deterministic time used by some of the functions of this class.
81 //
82 // TODO(user): Be exhausitive and more precise.
83 double DeterministicTime() const {
84 return DeterministicTimeForFpOperations(num_operations_);
85 }
86
87 private:
88 // Problem data that should be updated from outside.
89 const VariablesInfo& variables_info_;
90
91 absl::BitGenRef random_;
92 ReducedCosts* reduced_costs_;
93
94 // Internal data.
95 GlopParameters parameters_;
96
97 // Stats.
98 struct Stats : public StatsGroup {
99 Stats()
100 : StatsGroup("EnteringVariable"),
101 num_perfect_ties("num_perfect_ties", this) {}
102 IntegerDistribution num_perfect_ties;
103 };
104 Stats stats_;
105
106 // Temporary vector used to hold the best entering column candidates that are
107 // tied using the current choosing criteria. We actually only store the tied
108 // candidate #2, #3, ...; because the first tied candidate is remembered
109 // anyway.
110 std::vector<ColIndex> equivalent_entering_choices_;
111
112 // Store a column with its update coefficient and ratio.
113 // This is used during the dual phase I & II ratio tests.
114 struct ColWithRatio {
115 ColWithRatio() = default;
116 ColWithRatio(ColIndex _col, Fractional reduced_cost, Fractional coeff_m)
117 : col(_col), ratio(reduced_cost / coeff_m), coeff_magnitude(coeff_m) {}
118
119 // Returns false if "this" is before "other" in a priority queue.
120 bool operator<(const ColWithRatio& other) const {
121 if (ratio == other.ratio) {
122 if (coeff_magnitude == other.coeff_magnitude) {
123 return col > other.col;
124 }
125 return coeff_magnitude < other.coeff_magnitude;
126 }
127 return ratio > other.ratio;
128 }
129
130 ColIndex col;
131 Fractional ratio;
132 Fractional coeff_magnitude;
133 };
134
135 // Temporary vector used to hold breakpoints.
136 std::vector<ColWithRatio> breakpoints_;
137
138 // Counter for the deterministic time.
139 int64_t num_operations_ = 0;
140};
141
142} // namespace glop
143} // namespace operations_research
144
145#endif // SWIG
146#endif // OR_TOOLS_GLOP_ENTERING_VARIABLE_H_
Statistic on the distribution of a sequence of integers.
Definition stats.h:288
Base class to print a nice summary of a group of statistics.
Definition stats.h:128
std::string StatString() const
Definition stats.cc:77
void SetParameters(const GlopParameters &parameters)
Sets the parameters.
EnteringVariable & operator=(const EnteringVariable &)=delete
std::string StatString() const
Stats related functions.
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)
EnteringVariable(const EnteringVariable &)=delete
This type is neither copyable nor movable.
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)
Takes references to the linear program data we need.
SatParameters parameters
static double DeterministicTimeForFpOperations(int64_t n)
Definition lp_types.h:435
In SWIG mode, we don't want anything besides these top-level includes.