OR-Tools  9.6
pdtsp_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 <functional>
17 #include <string>
18 #include <vector>
19 
20 #include "absl/strings/str_split.h"
21 #include "ortools/base/gzipfile.h"
22 #include "ortools/base/mathutil.h"
23 #include "ortools/base/numbers.h"
24 #include "ortools/base/path.h"
25 #include "ortools/base/strtoint.h"
27 
28 namespace operations_research {
29 namespace {
30 
31 using absl::ByAnyChar;
32 
33 File* OpenReadOnly(const std::string& file_name) {
34  File* file = nullptr;
35  if (file::Open(file_name, "r", &file, file::Defaults()).ok() &&
36  file::Extension(file_name) == "gz") {
37  file = GZipFileReader(file_name, file, TAKE_OWNERSHIP);
38  }
39  return file;
40 }
41 } // namespace
42 
43 PdTspParser::PdTspParser() : section_(SIZE_SECTION) {}
44 
45 bool PdTspParser::LoadFile(const std::string& file_name) {
46  for (const std::string& line :
48  ProcessNewLine(line);
49  }
50  return true;
51 }
52 
53 std::function<int64_t(int, int)> PdTspParser::Distances() const {
54  std::function<int64_t(int, int)> distances = [this](int from, int to) {
55  const double xd = x_[from] - x_[to];
56  const double yd = y_[from] - y_[to];
57  const double d = sqrt(xd * xd + yd * yd);
58  return MathUtil::FastInt64Round(d);
59  };
60  return distances;
61 }
62 
63 void PdTspParser::ProcessNewLine(const std::string& line) {
64  const std::vector<std::string> words =
65  absl::StrSplit(line, ByAnyChar(" :\t"), absl::SkipEmpty());
66  if (!words.empty()) {
67  switch (section_) {
68  case SIZE_SECTION: {
69  const int size = atoi64(words[0]);
70  x_.resize(size, 0);
71  y_.resize(size, 0);
72  deliveries_.resize(size, -1);
73  section_ = DEPOT_SECTION;
74  break;
75  }
76  case DEPOT_SECTION:
77  depot_ = atoi64(words[0]) - 1;
78  x_[depot_] = atoi64(words[1]);
79  y_[depot_] = atoi64(words[2]);
80  deliveries_[depot_] = -1;
81  section_ = NODE_SECTION;
82  break;
83  case NODE_SECTION: {
84  const int kEof = -999;
85  const int id = atoi64(words[0]) - 1;
86  if (id + 1 == kEof) {
87  section_ = EOF_SECTION;
88  } else {
89  x_[id] = atoi64(words[1]);
90  y_[id] = atoi64(words[2]);
91  const bool is_pickup = atoi64(words[3]) == 0;
92  if (is_pickup) {
93  deliveries_[id] = atoi64(words[4]) - 1;
94  }
95  }
96  break;
97  }
98  case EOF_SECTION:
99  break;
100  default:
101  break;
102  }
103  }
104 }
105 
106 } // namespace operations_research
Definition: base/file.h:33
static int64_t FastInt64Round(double x)
Definition: mathutil.h:138
bool LoadFile(const std::string &file_name)
Definition: pdtsp_parser.cc:45
std::function< int64_t(int, int)> Distances() const
Definition: pdtsp_parser.cc:53
File * GZipFileReader(const absl::string_view name, File *file, Ownership ownership, AppendedStreams appended_streams)
Definition: gzipfile.cc:25
@ TAKE_OWNERSHIP
Definition: gzipfile.h:34
Options Defaults()
Definition: base/file.h:123
absl::Status Open(const absl::string_view &filename, const absl::string_view &mode, File **f, int flags)
Definition: base/file.cc:143
absl::string_view Extension(absl::string_view path)
Definition: path.cc:133
Collection of objects used to extend the Constraint Solver library.
int64_t atoi64(const std::string &word)
Definition: strtoint.h:58
int line
Definition: parse_proto.cc:31