Google OR-Tools v9.11
a fast and portable software suite for combinatorial optimization
Loading...
Searching...
No Matches
solomon_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 <memory>
18#include <string>
19#include <vector>
20
21#include "absl/strings/match.h"
22#include "absl/strings/str_split.h"
23#include "absl/strings/string_view.h"
27#include "ortools/base/path.h"
28#include "ortools/base/types.h"
31
32namespace operations_research {
33
35 : sections_({{"VEHICLE", VEHICLE}, {"CUSTOMER", CUSTOMER}}) {
36 Initialize();
37}
38
39bool SolomonParser::LoadFile(const std::string& file_name) {
40 Initialize();
41 return ParseFile(file_name);
42}
43
44bool SolomonParser::LoadFile(absl::string_view file_name,
45 const std::string& archive_name) {
46 Initialize();
47 if (!absl::StartsWith(archive_name, "/")) {
48 return false;
49 }
50 const std::string fake_zip_path = "/zip" + archive_name;
51 std::shared_ptr<zipfile::ZipArchive> fake_zip_closer(
52 zipfile::OpenZipArchive(archive_name));
53 if (nullptr == fake_zip_closer) return false;
54 const std::string zip_filename = file::JoinPath(fake_zip_path, file_name);
55 return ParseFile(zip_filename);
56}
57
58void SolomonParser::Initialize() {
59 name_.clear();
60 vehicles_ = 0;
61 coordinates_.clear();
62 capacity_ = 0;
63 demands_.clear();
64 time_windows_.clear();
65 service_times_.clear();
66 section_ = NAME;
67 to_read_ = 1;
68}
69
70bool SolomonParser::ParseFile(const std::string& file_name) {
71 for (const std::string& line :
73 const std::vector<std::string> words =
74 absl::StrSplit(line, absl::ByAnyChar(" :\t"), absl::SkipEmpty());
75 // Skip blank lines
76 if (words.empty()) continue;
77 if (to_read_ > 0) {
78 switch (section_) {
79 case NAME: {
80 name_ = words[0];
81 break;
82 }
83 case VEHICLE: {
84 if (to_read_ == 1) {
85 if (words.size() != 2) return false;
86 vehicles_ = strings::ParseLeadingInt32Value(words[0], -1);
87 if (vehicles_ < 0) return false;
88 capacity_ = strings::ParseLeadingInt32Value(words[1], -1);
89 if (capacity_ < 0) return false;
90 }
91 break;
92 }
93 case CUSTOMER: {
94 if (to_read_ < 2) {
95 std::vector<int64_t> values;
96 for (int i = 1; i < words.size(); ++i) {
97 const int64_t value =
99 if (value < 0) return false;
100 values.push_back(value);
101 }
102 coordinates_.push_back({values[0], values[1]});
103 demands_.push_back(values[2]);
104 time_windows_.push_back({values[3], values[4]});
105 service_times_.push_back(values[5]);
106 ++to_read_;
107 }
108 break;
109 }
110 default: {
111 LOG(ERROR) << "Reading data outside section";
112 return false;
113 }
114 }
115 --to_read_;
116 } else { // New section
117 section_ = gtl::FindWithDefault(sections_, words[0], UNKNOWN);
118 switch (section_) {
119 case VEHICLE: {
120 // Two rows: header and data.
121 to_read_ = 2;
122 break;
123 }
124 case CUSTOMER: {
125 to_read_ = 2;
126 break;
127 }
128 default: {
129 LOG(ERROR) << "Unknown section: " << section_;
130 return false;
131 }
132 }
133 }
134 }
135 return section_ == CUSTOMER;
136}
137
138} // namespace operations_research
bool LoadFile(const std::string &file_name)
Loads instance from a file.
int64_t value
std::string JoinPath()
Definition path.h:82
const MapUtilMappedT< Collection > & FindWithDefault(const Collection &collection, const KeyType &key, const MapUtilMappedT< Collection > &value)
Definition map_util.h:36
In SWIG mode, we don't want anything besides these top-level includes.
int32_t ParseLeadingInt32Value(const char *str, int32_t deflt)
Definition numbers.cc:60
int64_t ParseLeadingInt64Value(const char *str, int64_t deflt)
Definition numbers.cc:167
std::shared_ptr< ZipArchive > OpenZipArchive(absl::string_view path, const ZipFileOptions &options)
Definition zipfile.cc:32
int line