forked from google/or-tools
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathset_cover_reader.cc
138 lines (123 loc) · 4.26 KB
/
set_cover_reader.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
// Copyright 2010-2024 Google LLC
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "ortools/algorithms/set_cover_reader.h"
#include <cctype>
#include <cstddef>
#include <cstdint>
#include "absl/log/check.h"
#include "absl/strings/numbers.h"
#include "absl/strings/string_view.h"
#include "ortools/algorithms/set_cover_model.h"
#include "ortools/base/file.h"
#include "ortools/base/logging.h"
#include "ortools/base/options.h"
#include "ortools/util/filelineiter.h"
namespace operations_research {
class SetCoverReader {
public:
explicit SetCoverReader(File* file);
absl::string_view GetNextToken();
double ParseNextDouble();
int64_t ParseNextInteger();
private:
size_t SkipBlanks(size_t pos) const;
size_t SkipNonBlanks(size_t pos) const;
FileLineIterator line_iter_;
absl::string_view line_;
int start_pos_;
int end_pos_;
};
SetCoverReader::SetCoverReader(File* file)
: line_iter_(file, FileLineIterator::REMOVE_INLINE_CR |
FileLineIterator::REMOVE_BLANK_LINES),
start_pos_(0),
end_pos_(0) {
line_ = *line_iter_;
}
size_t SetCoverReader::SkipBlanks(size_t pos) const {
const size_t size = line_.size();
for (; pos < size && std::isspace(line_[pos]); ++pos) {
}
return pos;
}
size_t SetCoverReader::SkipNonBlanks(size_t pos) const {
const size_t size = line_.size();
for (; pos < size && !std::isspace(line_[pos]); ++pos) {
}
return pos;
}
absl::string_view SetCoverReader::GetNextToken() {
const size_t size = line_.size();
start_pos_ = SkipBlanks(end_pos_);
if (start_pos_ >= size) {
++line_iter_;
line_ = *line_iter_;
start_pos_ = SkipBlanks(0);
}
end_pos_ = SkipNonBlanks(start_pos_);
return line_.substr(start_pos_, end_pos_ - start_pos_);
}
double SetCoverReader::ParseNextDouble() {
double value = 0;
CHECK(absl::SimpleAtod(GetNextToken(), &value));
return value;
}
int64_t SetCoverReader::ParseNextInteger() {
int64_t value = 0;
CHECK(absl::SimpleAtoi(GetNextToken(), &value));
return value;
}
SetCoverModel ReadBeasleySetCoverProblem(absl::string_view filename) {
SetCoverModel model;
File* file(file::OpenOrDie(filename, "r", file::Defaults()));
SetCoverReader reader(file);
const ElementIndex num_rows(reader.ParseNextInteger());
const SubsetIndex num_cols(reader.ParseNextInteger());
model.ReserveNumSubsets(num_cols.value());
for (SubsetIndex subset : SubsetRange(num_cols)) {
const double cost(reader.ParseNextDouble());
model.SetSubsetCost(subset.value(), cost);
}
for (ElementIndex element : ElementRange(num_rows)) {
const RowEntryIndex row_size(reader.ParseNextInteger());
for (RowEntryIndex entry(0); entry < row_size; ++entry) {
const int subset(reader.ParseNextInteger() - 1);
model.AddElementToSubset(element.value(), subset);
}
}
file->Close(file::Defaults()).IgnoreError();
model.CreateSparseRowView();
return model;
}
SetCoverModel ReadRailSetCoverProblem(absl::string_view filename) {
SetCoverModel model;
File* file(file::OpenOrDie(filename, "r", file::Defaults()));
SetCoverReader reader(file);
const ElementIndex num_rows(reader.ParseNextInteger());
const BaseInt num_cols(reader.ParseNextInteger());
model.ReserveNumSubsets(num_cols);
for (int i(0); i < num_cols; ++i) {
const double cost(reader.ParseNextDouble());
model.SetSubsetCost(i, cost);
const ColumnEntryIndex column_size(reader.ParseNextInteger());
model.ReserveNumElementsInSubset(i, column_size.value());
for (const ColumnEntryIndex _ : ColumnEntryRange(column_size)) {
const ElementIndex element(reader.ParseNextInteger() - 1);
model.AddElementToSubset(element.value(), i);
}
}
file->Close(file::Defaults()).IgnoreError();
model.CreateSparseRowView();
return model;
}
} // namespace operations_research