OR-Tools  9.6
vector_bin_packing_parser.cc
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 
15 
16 #include <cstdint>
17 #include <string>
18 #include <vector>
19 
20 #include "absl/strings/numbers.h"
21 #include "absl/strings/str_split.h"
22 #include "ortools/packing/vector_bin_packing.pb.h"
24 
25 namespace operations_research {
26 namespace packing {
27 namespace vbp {
28 
29 bool VbpParser::ParseFile(const std::string& data_filename) {
30  vbp_.Clear();
31 
32  load_status_ = DIMENSION_SECTION;
33  for (const std::string& line : FileLines(data_filename)) {
34  if (load_status_ == ERROR_FOUND) break;
35  ProcessLine(line);
36  }
37 
38  // Checks status.
39  if (load_status_ == ERROR_FOUND) {
40  LOG(INFO) << vbp_;
41  return false;
42  }
43  return vbp_.item_size() == num_declared_items_;
44 }
45 
46 void VbpParser::ReportError(const std::string& line) {
47  LOG(ERROR) << "Error: status = " << load_status_ << ", line = " << line;
48  load_status_ = ERROR_FOUND;
49 }
50 
51 void VbpParser::ProcessLine(const std::string& line) {
52  const std::vector<std::string> words =
53  absl::StrSplit(line, absl::ByAnyChar(" :\t\r"), absl::SkipEmpty());
54 
55  if (words.empty()) return;
56 
57  switch (load_status_) {
58  case NOT_STARTED: {
59  LOG(FATAL) << "Should not be here";
60  }
61  case DIMENSION_SECTION: {
62  if (words.size() != 1) {
63  ReportError(line);
64  return;
65  }
66  num_resources_ = strtoint32(words[0]);
67  load_status_ = BIN_SECTION;
68  break;
69  }
70  case BIN_SECTION: {
71  if (words.size() != num_resources_) {
72  ReportError(line);
73  return;
74  }
75  for (const std::string& dim_str : words) {
76  vbp_.add_resource_capacity(strtoint64(dim_str));
77  }
78  load_status_ = NUMBER_OF_ITEMS_SECTION;
79  break;
80  }
81  case NUMBER_OF_ITEMS_SECTION: {
82  if (words.size() != 1) {
83  ReportError(line);
84  return;
85  }
86  num_declared_items_ = strtoint32(words[0]);
87  load_status_ = ITEM_SECTION;
88  break;
89  }
90  case ITEM_SECTION: {
91  if (words.size() != num_resources_ + 1) {
92  ReportError(line);
93  return;
94  }
95  Item* const item = vbp_.add_item();
96  for (int i = 0; i < num_resources_; ++i) {
97  item->add_resource_usage(strtoint64(words[i]));
98  }
99  item->set_num_copies(strtoint32(words[num_resources_]));
100  item->set_max_number_of_copies_per_bin(item->num_copies());
101  break;
102  }
103  case ERROR_FOUND: {
104  break;
105  }
106  }
107 }
108 
109 int VbpParser::strtoint32(const std::string& word) {
110  int result;
111  CHECK(absl::SimpleAtoi(word, &result));
112  return result;
113 }
114 
115 int64_t VbpParser::strtoint64(const std::string& word) {
116  int64_t result;
117  CHECK(absl::SimpleAtoi(word, &result));
118  return result;
119 }
120 
121 } // namespace vbp
122 } // namespace packing
123 } // namespace operations_research
bool ParseFile(const std::string &data_filename)
Collection of objects used to extend the Constraint Solver library.
int line
Definition: parse_proto.cc:31