Google OR-Tools v9.11
a fast and portable software suite for combinatorial optimization
Loading...
Searching...
No Matches
pdtsp_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 <functional>
17#include <string>
18#include <vector>
19
20#include "absl/strings/str_split.h"
21#include "absl/strings/string_view.h"
25#include "ortools/base/path.h"
28
29namespace operations_research {
30namespace {
31
32using absl::ByAnyChar;
33
34File* OpenReadOnly(absl::string_view file_name) {
35 File* file = nullptr;
36 if (file::Open(file_name, "r", &file, file::Defaults()).ok() &&
37 file::Extension(file_name) == "gz") {
39 }
40 return file;
41}
42} // namespace
43
44PdTspParser::PdTspParser() : section_(SIZE_SECTION) {}
45
46bool PdTspParser::LoadFile(const std::string& file_name) {
47 for (const std::string& line :
49 ProcessNewLine(line);
50 }
51 return true;
52}
53
54std::function<int64_t(int, int)> PdTspParser::Distances() const {
55 std::function<int64_t(int, int)> distances = [this](int from, int to) {
56 const double xd = x_[from] - x_[to];
57 const double yd = y_[from] - y_[to];
58 const double d = sqrt(xd * xd + yd * yd);
60 };
61 return distances;
62}
63
64void PdTspParser::ProcessNewLine(const std::string& line) {
65 const std::vector<std::string> words =
66 absl::StrSplit(line, ByAnyChar(" :\t"), absl::SkipEmpty());
67 if (!words.empty()) {
68 switch (section_) {
69 case SIZE_SECTION: {
70 const int size = atoi64(words[0]);
71 x_.resize(size, 0);
72 y_.resize(size, 0);
73 deliveries_.resize(size, -1);
74 section_ = DEPOT_SECTION;
75 break;
76 }
77 case DEPOT_SECTION:
78 depot_ = atoi64(words[0]) - 1;
79 x_[depot_] = atoi64(words[1]);
80 y_[depot_] = atoi64(words[2]);
81 deliveries_[depot_] = -1;
82 section_ = NODE_SECTION;
83 break;
84 case NODE_SECTION: {
85 const int kEof = -999;
86 const int id = atoi64(words[0]) - 1;
87 if (id + 1 == kEof) {
88 section_ = EOF_SECTION;
89 } else {
90 x_[id] = atoi64(words[1]);
91 y_[id] = atoi64(words[2]);
92 const bool is_pickup = atoi64(words[3]) == 0;
93 if (is_pickup) {
94 deliveries_[id] = atoi64(words[4]) - 1;
95 }
96 }
97 break;
98 }
99 case EOF_SECTION:
100 break;
101 default:
102 break;
103 }
104 }
105}
106
107} // namespace operations_research
IntegerValue size
@ TAKE_OWNERSHIP
Definition basictypes.h:22
Definition file.h:30
static int64_t FastInt64Round(double x)
Definition mathutil.h:272
std::function< int64_t(int, int)> Distances() const
Returns a function returning distances between nodes.
bool LoadFile(const std::string &file_name)
Loads and parse a PDTSP from a given file.
File * GZipFileReader(const absl::string_view name, File *file, Ownership ownership, AppendedStreams appended_streams)
public entry points
Definition gzipfile.cc:25
Definition file.cc:169
absl::string_view Extension(absl::string_view path)
Definition path.cc:133
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
Options Defaults()
Definition file.h:109
In SWIG mode, we don't want anything besides these top-level includes.
int64_t atoi64(absl::string_view word)
Definition strtoint.h:57
trees with all degrees equal to
int line