Google OR-Tools v9.11
a fast and portable software suite for combinatorial optimization
Loading...
Searching...
No Matches
set_cover_reader.cc
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
15
16#include <cctype>
17#include <cstddef>
18#include <cstdint>
19
20#include "absl/log/check.h"
21#include "absl/strings/numbers.h"
22#include "absl/strings/string_view.h"
24#include "ortools/base/file.h"
28
29namespace operations_research {
30
32 public:
33 explicit SetCoverReader(File* file);
34 absl::string_view GetNextToken();
35 double ParseNextDouble();
36 int64_t ParseNextInteger();
37
38 private:
39 size_t SkipBlanks(size_t pos) const;
40 size_t SkipNonBlanks(size_t pos) const;
41 FileLineIterator line_iter_;
42 absl::string_view line_;
43 int start_pos_;
44 int end_pos_;
45};
46
48 : line_iter_(file, FileLineIterator::REMOVE_INLINE_CR |
49 FileLineIterator::REMOVE_BLANK_LINES),
50 start_pos_(0),
51 end_pos_(0) {
52 line_ = *line_iter_;
53}
54
55size_t SetCoverReader::SkipBlanks(size_t pos) const {
56 const size_t size = line_.size();
57 for (; pos < size && std::isspace(line_[pos]); ++pos) {
58 }
59 return pos;
60}
61
62size_t SetCoverReader::SkipNonBlanks(size_t pos) const {
63 const size_t size = line_.size();
64 for (; pos < size && !std::isspace(line_[pos]); ++pos) {
65 }
66 return pos;
67}
68
69absl::string_view SetCoverReader::GetNextToken() {
70 const size_t size = line_.size();
71 start_pos_ = SkipBlanks(end_pos_);
72 if (start_pos_ >= size) {
73 ++line_iter_;
74 line_ = *line_iter_;
75 start_pos_ = SkipBlanks(0);
76 }
77 end_pos_ = SkipNonBlanks(start_pos_);
78 return line_.substr(start_pos_, end_pos_ - start_pos_);
79}
80
82 double value = 0;
83 CHECK(absl::SimpleAtod(GetNextToken(), &value));
84 return value;
85}
86
88 int64_t value = 0;
89 CHECK(absl::SimpleAtoi(GetNextToken(), &value));
90 return value;
91}
92
93SetCoverModel ReadBeasleySetCoverProblem(absl::string_view filename) {
95 File* file(file::OpenOrDie(filename, "r", file::Defaults()));
96 SetCoverReader reader(file);
97 const ElementIndex num_rows(reader.ParseNextInteger());
98 const SubsetIndex num_cols(reader.ParseNextInteger());
99 model.ReserveNumSubsets(num_cols.value());
100 for (SubsetIndex subset : SubsetRange(num_cols)) {
101 const double cost(reader.ParseNextDouble());
102 model.SetSubsetCost(subset.value(), cost);
103 }
104 for (ElementIndex element : ElementRange(num_rows)) {
105 const RowEntryIndex row_size(reader.ParseNextInteger());
106 for (RowEntryIndex entry(0); entry < row_size; ++entry) {
107 const int subset(reader.ParseNextInteger() - 1);
108 model.AddElementToSubset(element.value(), subset);
109 }
110 }
111 file->Close(file::Defaults()).IgnoreError();
112 return model;
113}
114
115SetCoverModel ReadRailSetCoverProblem(absl::string_view filename) {
117 File* file(file::OpenOrDie(filename, "r", file::Defaults()));
118 SetCoverReader reader(file);
119 const ElementIndex num_rows(reader.ParseNextInteger());
120 const int num_cols(reader.ParseNextInteger());
121 model.ReserveNumSubsets(num_cols);
122 for (int i(0); i < num_cols; ++i) {
123 const double cost(reader.ParseNextDouble());
124 model.SetSubsetCost(i, cost);
125 const ColumnEntryIndex column_size(reader.ParseNextInteger());
126 model.ReserveNumElementsInSubset(i, column_size.value());
127 for (const ColumnEntryIndex _ : ColumnEntryRange(column_size)) {
128 const ElementIndex element(reader.ParseNextInteger() - 1);
129 model.AddElementToSubset(element.value(), i);
130 }
131 }
132 file->Close(file::Defaults()).IgnoreError();
133 return model;
134}
135
136} // namespace operations_research
IntegerValue size
Implements the minimum interface for a range-based for loop iterator.
Definition file.h:30
Main class for describing a weighted set-covering problem.
int64_t value
GRBmodel * model
Definition file.cc:169
Options Defaults()
Definition file.h:109
File * OpenOrDie(absl::string_view filename, absl::string_view mode, Options options)
Definition file.cc:182
In SWIG mode, we don't want anything besides these top-level includes.
SetCoverModel ReadBeasleySetCoverProblem(absl::string_view filename)
util_intops::StrongIntRange< SubsetIndex > SubsetRange
util_intops::StrongIntRange< ColumnEntryIndex > ColumnEntryRange
SetCoverModel ReadRailSetCoverProblem(absl::string_view filename)
util_intops::StrongIntRange< ElementIndex > ElementRange