OR-Tools  9.6
solomon_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 <cstdint>
17 #include <memory>
18 #include <string>
19 #include <vector>
20 
21 #include "absl/strings/match.h"
22 #include "absl/strings/str_split.h"
24 #include "ortools/base/logging.h"
25 #include "ortools/base/map_util.h"
26 #include "ortools/base/numbers.h"
27 #include "ortools/base/path.h"
28 #include "ortools/base/zipfile.h"
30 
31 namespace operations_research {
32 
34  : sections_({{"VEHICLE", VEHICLE}, {"CUSTOMER", CUSTOMER}}) {
35  Initialize();
36 }
37 
38 bool SolomonParser::LoadFile(const std::string& file_name) {
39  Initialize();
40  return ParseFile(file_name);
41 }
42 
43 bool SolomonParser::LoadFile(const std::string& file_name,
44  const std::string& archive_name) {
45  Initialize();
46  if (!absl::StartsWith(archive_name, "/")) {
47  return false;
48  }
49  const std::string fake_zip_path = "/zip" + archive_name;
50  std::shared_ptr<zipfile::ZipArchive> fake_zip_closer(
51  zipfile::OpenZipArchive(archive_name));
52  if (nullptr == fake_zip_closer) return false;
53  const std::string zip_filename = file::JoinPath(fake_zip_path, file_name);
54  return ParseFile(zip_filename);
55 }
56 
57 void SolomonParser::Initialize() {
58  name_.clear();
59  vehicles_ = 0;
60  coordinates_.clear();
61  capacity_ = 0;
62  demands_.clear();
63  time_windows_.clear();
64  service_times_.clear();
65  section_ = NAME;
66  to_read_ = 1;
67 }
68 
69 bool SolomonParser::ParseFile(const std::string& file_name) {
70  for (const std::string& line :
72  const std::vector<std::string> words =
73  absl::StrSplit(line, absl::ByAnyChar(" :\t"), absl::SkipEmpty());
74  // Skip blank lines
75  if (words.empty()) continue;
76  if (to_read_ > 0) {
77  switch (section_) {
78  case NAME: {
79  name_ = words[0];
80  break;
81  }
82  case VEHICLE: {
83  if (to_read_ == 1) {
84  if (words.size() != 2) return false;
85  vehicles_ = strings::ParseLeadingInt32Value(words[0], -1);
86  if (vehicles_ < 0) return false;
87  capacity_ = strings::ParseLeadingInt32Value(words[1], -1);
88  if (capacity_ < 0) return false;
89  }
90  break;
91  }
92  case CUSTOMER: {
93  if (to_read_ < 2) {
94  std::vector<int64_t> values;
95  for (int i = 1; i < words.size(); ++i) {
96  const int64_t value =
97  strings::ParseLeadingInt64Value(words[i], -1);
98  if (value < 0) return false;
99  values.push_back(value);
100  }
101  coordinates_.push_back({values[0], values[1]});
102  demands_.push_back(values[2]);
103  time_windows_.push_back({values[3], values[4]});
104  service_times_.push_back(values[5]);
105  ++to_read_;
106  }
107  break;
108  }
109  default: {
110  LOG(ERROR) << "Reading data outside section";
111  return false;
112  }
113  }
114  --to_read_;
115  } else { // New section
116  section_ = gtl::FindWithDefault(sections_, words[0], UNKNOWN);
117  switch (section_) {
118  case VEHICLE: {
119  // Two rows: header and data.
120  to_read_ = 2;
121  break;
122  }
123  case CUSTOMER: {
124  to_read_ = 2;
125  break;
126  }
127  default: {
128  LOG(ERROR) << "Unknown section: " << section_;
129  return false;
130  }
131  }
132  }
133  }
134  return section_ == CUSTOMER;
135 }
136 
137 } // namespace operations_research
bool LoadFile(const std::string &file_name)
int64_t value
std::string JoinPath(absl::string_view path1, absl::string_view path2)
Definition: path.cc:25
const Collection::value_type::second_type & FindWithDefault(const Collection &collection, const typename Collection::value_type::first_type &key, const typename Collection::value_type::second_type &value)
Definition: map_util.h:29
Collection of objects used to extend the Constraint Solver library.
int64_t ParseLeadingInt64Value(const char *str, int64_t deflt)
Definition: numbers.cc:161
int32_t ParseLeadingInt32Value(const char *str, int32_t deflt)
Definition: numbers.cc:58
std::shared_ptr< ZipArchive > OpenZipArchive(absl::string_view path, const ZipFileOptions &options)
Definition: zipfile.cc:32
int line
Definition: parse_proto.cc:31