OR-Tools  9.6
binpacking_2d_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 <string>
17 #include <vector>
18 
19 #include "absl/strings/numbers.h"
20 #include "absl/strings/str_split.h"
21 #include "ortools/base/logging.h"
23 
24 namespace operations_research {
25 namespace packing {
26 
28  : num_dimensions_(-1),
29  load_status_(NOT_STARTED),
30  num_items_(0),
31  instances_seen_(0) {}
32 
33 bool BinPacking2dParser::Load2BPFile(const std::string& file_name,
34  int instance) {
35  if (load_status_ != NOT_STARTED) {
36  return false;
37  }
38 
39  num_dimensions_ = 2;
40 
41  for (const std::string& line : FileLines(file_name)) {
42  ProcessNew2BpLine(line, instance);
43  if (load_status_ == PARSING_FINISHED) {
44  break;
45  }
46  }
47  return num_items_ == problem_.items_size() && num_items_ > 0;
48 }
49 
50 void BinPacking2dParser::ProcessNew2BpLine(const std::string& line,
51  int instance) {
52  const std::vector<std::string> words =
53  absl::StrSplit(line, absl::ByAnyChar(" :\t\r"), absl::SkipEmpty());
54  if (words.size() == 3 && words[1] == "PROBLEM" && words[2] == "CLASS") {
55  // New instance starting.
56  instances_seen_++;
57  if (load_status_ == NOT_STARTED && instances_seen_ == instance) {
58  load_status_ = INSTANCE_FOUND;
59  } else if (instances_seen_ > instance) {
60  load_status_ = PARSING_FINISHED;
61  }
62  }
63 
64  if (load_status_ == INSTANCE_FOUND) {
65  if (words.empty()) {
66  return;
67  } else if (words.size() == 2 || words[2] == "H(I),W(I),I=1,...,N") {
68  // Reading an item.
69  CHECK_NE(num_items_, 0);
70  CHECK_LT(problem_.items_size(), num_items_);
71  MultipleDimensionsBinPackingItem* item = problem_.add_items();
72  MultipleDimensionsBinPackingShape* shape = item->add_shapes();
73  int64_t dim;
74  CHECK(absl::SimpleAtoi(words[0], &dim));
75  shape->add_dimensions(dim);
76  CHECK(absl::SimpleAtoi(words[1], &dim));
77  shape->add_dimensions(dim);
78  item->set_value(1);
79  } else if (words[1] == "N.") { // Reading the number of item.
80  CHECK(absl::SimpleAtoi(words[0], &num_items_));
81  } else if (words[2] == "RELATIVE") {
82  // Just double checking.
83  int local_instance;
84  CHECK(absl::SimpleAtoi(words[0], &local_instance));
85  CHECK_EQ(local_instance, (instance - 1) % 10 + 1);
86  } else if (words[2] == "HBIN,WBIN") {
87  MultipleDimensionsBinPackingShape* box_shape =
88  problem_.mutable_box_shape();
89  int64_t dim;
90  CHECK(absl::SimpleAtoi(words[0], &dim));
91  box_shape->add_dimensions(dim);
92  CHECK(absl::SimpleAtoi(words[1], &dim));
93  box_shape->add_dimensions(dim);
94  }
95  }
96 }
97 
98 } // namespace packing
99 } // namespace operations_research
bool Load2BPFile(const std::string &file_name, int instance)
Collection of objects used to extend the Constraint Solver library.
int line
Definition: parse_proto.cc:31