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