Google OR-Tools v9.15
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-2025 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 <cmath>
17#include <cstdint>
18#include <functional>
19#include <string>
20#include <vector>
21
22#include "absl/strings/str_split.h"
23#include "absl/strings/string_view.h"
27
29
30PdTspParser::PdTspParser() : section_(SIZE_SECTION) {}
31
32bool PdTspParser::LoadFile(absl::string_view file_name) {
33 for (const std::string& line :
35 ProcessNewLine(line);
36 }
37 return true;
38}
39
40std::function<int64_t(int, int)> PdTspParser::Distances() const {
41 std::function<int64_t(int, int)> distances = [this](int from, int to) {
42 const double xd = x_[from] - x_[to];
43 const double yd = y_[from] - y_[to];
44 const double d = sqrt(xd * xd + yd * yd);
46 };
47 return distances;
48}
49
50void PdTspParser::ProcessNewLine(const std::string& line) {
51 const std::vector<std::string> words =
52 absl::StrSplit(line, absl::ByAnyChar(" :\t"), absl::SkipEmpty());
53 if (!words.empty()) {
54 switch (section_) {
55 case SIZE_SECTION: {
56 const int size = atoi64(words[0]);
57 x_.resize(size, 0);
58 y_.resize(size, 0);
59 deliveries_.resize(size, -1);
60 section_ = DEPOT_SECTION;
61 break;
62 }
63 case DEPOT_SECTION:
64 depot_ = atoi64(words[0]) - 1;
65 x_[depot_] = atoi64(words[1]);
66 y_[depot_] = atoi64(words[2]);
67 deliveries_[depot_] = -1;
68 section_ = NODE_SECTION;
69 break;
70 case NODE_SECTION: {
71 const int kEof = -999;
72 const int id = atoi64(words[0]) - 1;
73 if (id + 1 == kEof) {
74 section_ = EOF_SECTION;
75 } else {
76 x_[id] = atoi64(words[1]);
77 y_[id] = atoi64(words[2]);
78 const bool is_pickup = atoi64(words[3]) == 0;
79 if (is_pickup) {
80 deliveries_[id] = atoi64(words[4]) - 1;
81 }
82 }
83 break;
84 }
85 case EOF_SECTION:
86 break;
87 default:
88 break;
89 }
90 }
91}
92
93} // namespace operations_research::routing
static int64_t FastInt64Round(double x)
Definition mathutil.h:260
std::function< int64_t(int, int)> Distances() const
bool LoadFile(absl::string_view file_name)
int64_t atoi64(absl::string_view word)
Definition strtoint.h:57
trees with all degrees equal to