OR-Tools  9.6
initial_basis.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_INITIAL_BASIS_H_
15 #define OR_TOOLS_GLOP_INITIAL_BASIS_H_
16 
17 #include <vector>
18 
21 #include "ortools/lp_data/sparse.h"
22 
23 namespace operations_research {
24 namespace glop {
25 
26 // This class implements two initial basis algorithms. The idea is to replace as
27 // much as possible the columns of B that correspond to fixed slack variables
28 // with some column of A in order to have more freedom in the values the basic
29 // variables can take.
30 //
31 // The first algorithm is Bixby's initial basis algorithm, described in the
32 // paper below. It considers the columns of A in a particular order (the ones
33 // with more freedom first) and adds the current column to the basis if it keeps
34 // B almost triangular and with coefficients close to 1.0 on the diagonal for
35 // good numerical stability.
36 //
37 // Robert E. Bixby, "Implementing the Simplex Method: The Initial Basis"
38 // ORSA Jounal on Computing, Vol. 4, No. 3, Summer 1992.
39 // http://joc.journal.informs.org/content/4/3/267.abstract
40 //
41 // The second algorithm is is similar to the "advanced initial basis" that GLPK
42 // uses by default. It adds columns one by one to the basis B while keeping it
43 // triangular (not almost triangular as in Bixby's algorithm). The next
44 // column to add is chosen amongst the set of possible candidates using a
45 // heuristic similar to the one used by Bixby.
46 class InitialBasis {
47  public:
48  // Takes references to the linear program data we need.
49  InitialBasis(const CompactSparseMatrix& compact_matrix,
50  const DenseRow& objective, const DenseRow& lower_bound,
51  const DenseRow& upper_bound,
52  const VariableTypeRow& variable_type);
53 
54  // Completes the entries of the given basis that are equal to kInvalidCol with
55  // one of the first num_cols columns of A using Bixby's algorithm.
56  //
57  // Important: For this function, the matrix must be scaled such that the
58  // maximum absolute value in each column is 1.0.
59  void CompleteBixbyBasis(ColIndex num_cols, RowToColMapping* basis);
60 
61  // Similar to CompleteBixbyBasis() but completes the basis into a triangular
62  // one. This function usually produces better initial bases. The dual version
63  // just restricts the possible entering columns to the ones with a cost of 0.0
64  // in order to always start with the all-zeros vector of dual values.
65  //
66  // Returns false if an error occurred during the algorithm (numerically
67  // instable basis).
68  void CompleteTriangularPrimalBasis(ColIndex num_cols, RowToColMapping* basis);
69  void CompleteTriangularDualBasis(ColIndex num_cols, RowToColMapping* basis);
70 
71  // Use Maros's LTSF crash from the book "Computational Techniques of the
72  // Simplex Method". Unlike the other crashes this does not use the initial
73  // content of the basis parameter.
74  void GetPrimalMarosBasis(ColIndex num_cols, RowToColMapping* basis);
75  void GetDualMarosBasis(ColIndex num_cols, RowToColMapping* basis);
76 
77  // Visible for testing. Computes a list of candidate column indices out of the
78  // fist num_candidate_columns of A and sorts them using the
79  // bixby_column_comparator_. This also fills max_scaled_abs_cost_.
80  void ComputeCandidates(ColIndex num_cols, std::vector<ColIndex>* candidates);
81 
82  private:
83  // Internal implementation of the Primal/Dual CompleteTriangularBasis().
84  template <bool only_allow_zero_cost_column>
85  void CompleteTriangularBasis(ColIndex num_cols, RowToColMapping* basis);
86 
87  template <bool only_allow_zero_cost_column>
88  void GetMarosBasis(ColIndex num_cols, RowToColMapping* basis);
89 
90  // Returns an integer representing the order (the lower the better)
91  // between column categories (known as C2, C3 or C4 in the paper).
92  // Also returns a greater index for fixed columns.
93  int GetColumnCategory(ColIndex col) const;
94 
95  // Row and column priorities for Maros crash.
96  int GetMarosPriority(RowIndex row) const;
97  int GetMarosPriority(ColIndex col) const;
98 
99  // Returns the penalty (the lower the better) of a column. This is 'q_j' for a
100  // column 'j' in the paper.
101  Fractional GetColumnPenalty(ColIndex col) const;
102 
103  // Maximum scaled absolute value of the objective for the columns which are
104  // entering candidates. This is used by GetColumnPenalty().
105  Fractional max_scaled_abs_cost_;
106 
107  // Comparator used to sort column indices according to their penalty.
108  // Lower is better.
109  struct BixbyColumnComparator {
110  explicit BixbyColumnComparator(const InitialBasis& initial_basis)
111  : initial_basis_(initial_basis) {}
112  bool operator()(ColIndex col_a, ColIndex col_b) const;
113  const InitialBasis& initial_basis_;
114  } bixby_column_comparator_;
115 
116  // Comparator used by CompleteTriangularBasis(). Note that this one is meant
117  // to be used by a priority queue, so higher is better.
118  struct TriangularColumnComparator {
119  explicit TriangularColumnComparator(const InitialBasis& initial_basis)
120  : initial_basis_(initial_basis) {}
121  bool operator()(ColIndex col_a, ColIndex col_b) const;
122  const InitialBasis& initial_basis_;
123  } triangular_column_comparator_;
124 
125  const CompactSparseMatrix& compact_matrix_;
126  const DenseRow& objective_;
127  const DenseRow& lower_bound_;
128  const DenseRow& upper_bound_;
129  const VariableTypeRow& variable_type_;
130 
131  DISALLOW_COPY_AND_ASSIGN(InitialBasis);
132 };
133 
134 } // namespace glop
135 } // namespace operations_research
136 
137 #endif // OR_TOOLS_GLOP_INITIAL_BASIS_H_
void CompleteTriangularPrimalBasis(ColIndex num_cols, RowToColMapping *basis)
void CompleteTriangularDualBasis(ColIndex num_cols, RowToColMapping *basis)
InitialBasis(const CompactSparseMatrix &compact_matrix, const DenseRow &objective, const DenseRow &lower_bound, const DenseRow &upper_bound, const VariableTypeRow &variable_type)
void CompleteBixbyBasis(ColIndex num_cols, RowToColMapping *basis)
void GetDualMarosBasis(ColIndex num_cols, RowToColMapping *basis)
void GetPrimalMarosBasis(ColIndex num_cols, RowToColMapping *basis)
void ComputeCandidates(ColIndex num_cols, std::vector< ColIndex > *candidates)
ColIndex col
Definition: markowitz.cc:186
RowIndex row
Definition: markowitz.cc:185
Collection of objects used to extend the Constraint Solver library.
IntVar * upper_bound
Definition: routing.cc:1087
IntVar * lower_bound
Definition: routing.cc:1086