Google OR-Tools v9.11
a fast and portable software suite for combinatorial optimization
Loading...
Searching...
No Matches
lilim_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 <limits>
18#include <memory>
19#include <optional>
20#include <string>
21#include <vector>
22
23#include "absl/strings/match.h"
24#include "absl/strings/str_split.h"
25#include "absl/strings/string_view.h"
26#include "ortools/base/file.h"
29#include "ortools/base/path.h"
32
33namespace operations_research {
34
35bool LiLimParser::LoadFile(absl::string_view file_name) {
36 Initialize();
37 return ParseFile(file_name);
38}
39
40bool LiLimParser::LoadFile(absl::string_view file_name,
41 absl::string_view archive_name) {
42 Initialize();
43 if (!absl::StartsWith(archive_name, "/")) {
44 return false;
45 }
46 std::shared_ptr<zipfile::ZipArchive> zip_archive(
47 zipfile::OpenZipArchive(archive_name));
48 if (nullptr == zip_archive) {
49 return false;
50 }
51 return ParseFile(file::JoinPath("/zip", archive_name, file_name));
52}
53
54std::optional<int> LiLimParser::GetDelivery(int node) const {
55 const int delivery = deliveries_[node];
56 if (delivery == 0) return std::nullopt;
57 return delivery;
58}
59
60std::optional<int> LiLimParser::GetPickup(int node) const {
61 const int pickup = pickups_[node];
62 if (pickup == 0) return std::nullopt;
63 return pickup;
64}
65
66void LiLimParser::Initialize() {
67 vehicles_ = 0;
68 coordinates_.clear();
69 pickups_.clear();
70 deliveries_.clear();
71 capacity_ = 0;
72 speed_ = 0;
73 demands_.clear();
74 time_windows_.clear();
75 service_times_.clear();
76}
77
78#define PARSE_AND_RETURN_IF_NEGATIVE(str, dest_type, dest) \
79 dest_type dest = strings::ParseLeadingInt32Value(str, -1); \
80 do { \
81 if (dest < 0) return false; \
82 } while (false)
83
84bool LiLimParser::ParseFile(absl::string_view file_name) {
85 const int64_t kInvalidDemand = std::numeric_limits<int64_t>::min();
86 bool vehicles_initialized = false;
87 File* file = nullptr;
88 if (!file::Open(file_name, "r", &file, file::Defaults()).ok()) {
89 return false;
90 }
91 for (const std::string& line :
93 const std::vector<std::string> words =
94 absl::StrSplit(line, absl::ByAnyChar(" :\t"), absl::SkipEmpty());
95 // Skip blank lines
96 if (words.empty()) continue;
97 if (!vehicles_initialized) {
98 if (words.size() != 3) return false;
99 PARSE_AND_RETURN_IF_NEGATIVE(words[0], , vehicles_);
100 PARSE_AND_RETURN_IF_NEGATIVE(words[1], , capacity_);
101 PARSE_AND_RETURN_IF_NEGATIVE(words[2], , speed_);
102 vehicles_initialized = true;
103 continue;
104 }
105 // Reading node data.
106 if (words.size() != 9) return false;
107 PARSE_AND_RETURN_IF_NEGATIVE(words[1], const int64_t, x);
108 PARSE_AND_RETURN_IF_NEGATIVE(words[2], const int64_t, y);
109 coordinates_.push_back({x, y});
110 const int64_t demand =
111 strings::ParseLeadingInt64Value(words[3], kInvalidDemand);
112 if (demand == kInvalidDemand) return false;
113 demands_.push_back(demand);
114 PARSE_AND_RETURN_IF_NEGATIVE(words[4], const int64_t, open_time);
115 PARSE_AND_RETURN_IF_NEGATIVE(words[5], const int64_t, close_time);
116 time_windows_.push_back({open_time, close_time});
117 PARSE_AND_RETURN_IF_NEGATIVE(words[6], const int64_t, service_time);
118 service_times_.push_back(service_time);
119 PARSE_AND_RETURN_IF_NEGATIVE(words[7], const int64_t, pickup);
120 pickups_.push_back(pickup);
121 PARSE_AND_RETURN_IF_NEGATIVE(words[8], const int64_t, delivery);
122 deliveries_.push_back(delivery);
123 }
124 return vehicles_initialized;
125}
126
127#undef PARSE_AND_RETURN_IF_NEGATIVE
128
129} // namespace operations_research
IntegerValue y
Definition file.h:30
bool LoadFile(absl::string_view file_name)
Loads instance from a file.
std::optional< int > GetDelivery(int node) const
Returns the delivery of a pickup.
std::optional< int > GetPickup(int node) const
Returns the pickup of a delivery.
#define PARSE_AND_RETURN_IF_NEGATIVE(str, dest_type, dest)
Definition file.cc:169
absl::Status Open(absl::string_view filename, absl::string_view mode, File **f, Options options)
As of 2016-01, these methods can only be used with flags = file::Defaults().
Definition file.cc:170
std::string JoinPath()
Definition path.h:82
Options Defaults()
Definition file.h:109
In SWIG mode, we don't want anything besides these top-level includes.
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
const Variable x
Definition qp_tests.cc:127
int64_t demand
Definition resource.cc:126