21#include "absl/strings/ascii.h"
22#include "absl/strings/match.h"
23#include "absl/strings/str_join.h"
24#include "absl/strings/str_split.h"
25#include "absl/strings/string_view.h"
37 : sections_({{
"VEHICLE", VEHICLE}, {
"CUSTOMER", CUSTOMER}}) {
43 return ParseFile(file_name);
47 const std::string& archive_name) {
49 if (!absl::StartsWith(archive_name,
"/")) {
52 const std::string fake_zip_path =
"/zip" + archive_name;
53 std::shared_ptr<zipfile::ZipArchive> fake_zip_closer(
55 if (
nullptr == fake_zip_closer)
return false;
56 const std::string zip_filename =
file::JoinPath(fake_zip_path, file_name);
57 return ParseFile(zip_filename);
60void SolomonParser::Initialize() {
66 time_windows_.clear();
67 service_times_.clear();
72bool SolomonParser::ParseFile(absl::string_view file_name) {
73 for (
const std::string& line :
75 const std::vector<std::string> words =
76 absl::StrSplit(line, absl::ByAnyChar(
" :\t"), absl::SkipEmpty());
78 if (words.empty())
continue;
87 if (words.size() != 2)
return false;
89 if (vehicles_ < 0)
return false;
91 if (capacity_ < 0)
return false;
97 std::vector<int64_t> values;
98 for (
int i = 1;
i < words.size(); ++
i) {
101 if (value < 0)
return false;
102 values.push_back(value);
104 coordinates_.push_back({values[0], values[1]});
105 demands_.push_back(values[2]);
106 time_windows_.push_back({values[3], values[4]});
107 service_times_.push_back(values[5]);
113 LOG(ERROR) <<
"Reading data outside section";
131 LOG(ERROR) <<
"Unknown section: " << section_;
137 return section_ == CUSTOMER;
144 return ParseFile(file_name);
147void SolomonSolutionParser::Initialize() {
152bool SolomonSolutionParser::ParseFile(absl::string_view file_name) {
153 bool success =
false;
154 for (
const std::string& line :
157 const std::vector<std::string> words =
158 absl::StrSplit(line,
':', absl::SkipEmpty());
160 if (words.empty())
continue;
161 std::string key = words[0];
162 std::string value = words.size() > 1
163 ? absl::StrJoin(words.begin() + 1, words.end(),
":")
165 if (!RE2::FullMatch(key,
"Route\\s*(\\d+)\\s*")) {
166 absl::StripAsciiWhitespace(&key);
167 absl::StripAsciiWhitespace(&value);
168 key_values_[key] = value;
173 routes_.push_back(std::vector<int>());
174 for (
const auto item :
175 absl::StrSplit(value, absl::ByAnyChar(
" \t"), absl::SkipEmpty())) {
bool LoadFile(absl::string_view file_name)
Loads instance from a file.
bool LoadFile(absl::string_view file_name)
const MapUtilMappedT< Collection > & FindWithDefault(const Collection &collection, const KeyType &key, const MapUtilMappedT< Collection > &value)
Common utilities for parsing routing instances.
int32_t ParseLeadingInt32Value(const char *str, int32_t deflt)
int64_t ParseLeadingInt64Value(const char *str, int64_t deflt)
std::shared_ptr< ZipArchive > OpenZipArchive(absl::string_view path, const ZipFileOptions &options)