OR-Tools  9.0
set_covering_parser.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_DATA_SET_COVERING_PARSER_H_
15 #define OR_TOOLS_DATA_SET_COVERING_PARSER_H_
16 
17 #include <cstdint>
18 #include <string>
19 #include <vector>
20 
23 
24 namespace operations_research {
25 namespace scp {
26 
27 // Set covering problem.
28 //
29 // We have a list of subsets of a set. Each subset has a cost. The
30 // goal is to select of solution set of subsets such that (1) all elements
31 // of the set belongs to at least one subset of the solution set, and (2)
32 // the sum of the cost of each subset in the solution set is minimal.
33 //
34 // To follow the standard literature, each element is called a row, and each
35 // subset is called a column.
36 
37 class ScpParser {
38  public:
39  enum Section {
44  ROW,
46  END,
48  };
49 
50  enum Format {
51  // The original scp format of these problem is:
52  //
53  // number of rows (m), number of columns (n)
54  //
55  // the cost of each column c(j),j=1,...,n
56  //
57  // for each row i (i=1,...,m): the number of columns which cover row
58  // i followed by a list of the columns which cover row i.
59  //
60  // The original problems (scp*) from the OR-LIB follow this format.
62  // The railroad format is:
63  // number of rows (m), number of columns (n)
64  //
65  // for each column j (j=1,...,n): the cost of the column, the number
66  // of rows that it covers followed by a list of the rows that it
67  // covers.
68  //
69  // The railroad problems follow this format.
71  // The triplet format is:
72  //
73  // number of rows (m), number of columns (n)
74  //
75  // for each column, the 3 rows it contains. Note that the cost of
76  // each column is 1.
77  //
78  // The Steiner triple covering problems follow this format.
80  // The spp format is:
81  // number of rows (m), number of columns (n)
82  //
83  // for each column j (j=1,...,n): the cost of the column, the number
84  // of rows that it covers followed by a list of the rows that it
85  // covers.
86  //
87  // number of non_zeros
88  //
89  // The set partitioning problems follow this format.
91  };
92 
93  ScpParser();
94 
95  // This will clear the data before importing the file.
96  bool LoadProblem(const std::string& filename, Format format, ScpData* data);
97 
98  private:
99  void ProcessLine(const std::string& line, Format format, ScpData* data);
100  void LogError(const std::string& line, const std::string& error_message);
101  int strtoint32(const std::string& word);
102  int64_t strtoint64(const std::string& word);
103 
104  Section section_;
105  int line_;
106  int remaining_;
107  int current_;
108 };
109 
110 } // namespace scp
111 } // namespace operations_research
112 
113 #endif // OR_TOOLS_DATA_SET_COVERING_PARSER_H_
bool LoadProblem(const std::string &filename, Format format, ScpData *data)
Collection of objects used to extend the Constraint Solver library.