Google OR-Tools v9.11
a fast and portable software suite for combinatorial optimization
Loading...
Searching...
No Matches
binpacking_2d_parser.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 <string>
17#include <vector>
18
19#include "absl/strings/numbers.h"
20#include "absl/strings/str_split.h"
21#include "absl/strings/string_view.h"
24
25namespace operations_research {
26namespace packing {
27
29 : num_dimensions_(-1),
30 load_status_(NOT_STARTED),
31 num_items_(0),
32 instances_seen_(0) {}
33
34bool BinPacking2dParser::Load2BPFile(absl::string_view file_name,
35 int instance) {
36 if (load_status_ != NOT_STARTED) {
37 return false;
38 }
39
40 num_dimensions_ = 2;
41
42 for (const std::string& line : FileLines(file_name)) {
43 ProcessNew2BpLine(line, instance);
44 if (load_status_ == PARSING_FINISHED) {
45 break;
46 }
47 }
48 return num_items_ == problem_.items_size() && num_items_ > 0;
49}
50
51void BinPacking2dParser::ProcessNew2BpLine(const std::string& line,
52 int instance) {
53 const std::vector<std::string> words =
54 absl::StrSplit(line, absl::ByAnyChar(" :\t\r"), absl::SkipEmpty());
55 if (words.size() == 3 && words[1] == "PROBLEM" && words[2] == "CLASS") {
56 // New instance starting.
57 instances_seen_++;
58 if (load_status_ == NOT_STARTED && instances_seen_ == instance) {
59 load_status_ = INSTANCE_FOUND;
60 } else if (instances_seen_ > instance) {
61 load_status_ = PARSING_FINISHED;
62 }
63 }
64
65 if (load_status_ == INSTANCE_FOUND) {
66 if (words.empty()) {
67 return;
68 } else if (words.size() == 2 || words[2] == "H(I),W(I),I=1,...,N") {
69 // Reading an item.
70 CHECK_NE(num_items_, 0);
71 CHECK_LT(problem_.items_size(), num_items_);
72 MultipleDimensionsBinPackingItem* item = problem_.add_items();
73 MultipleDimensionsBinPackingShape* shape = item->add_shapes();
74 int64_t dim;
75 CHECK(absl::SimpleAtoi(words[0], &dim));
76 shape->add_dimensions(dim);
77 CHECK(absl::SimpleAtoi(words[1], &dim));
78 shape->add_dimensions(dim);
79 item->set_value(1);
80 } else if (words[1] == "N.") { // Reading the number of item.
81 CHECK(absl::SimpleAtoi(words[0], &num_items_));
82 } else if (words[2] == "RELATIVE") {
83 // Just double checking.
84 int local_instance;
85 CHECK(absl::SimpleAtoi(words[0], &local_instance));
86 CHECK_EQ(local_instance, (instance - 1) % 10 + 1);
87 } else if (words[2] == "HBIN,WBIN") {
88 MultipleDimensionsBinPackingShape* box_shape =
89 problem_.mutable_box_shape();
90 int64_t dim;
91 CHECK(absl::SimpleAtoi(words[0], &dim));
92 box_shape->add_dimensions(dim);
93 CHECK(absl::SimpleAtoi(words[1], &dim));
94 box_shape->add_dimensions(dim);
95 }
96 }
97}
98
99} // namespace packing
100} // namespace operations_research
bool Load2BPFile(absl::string_view file_name, int instance)
In SWIG mode, we don't want anything besides these top-level includes.
int line