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