OR-Tools  9.6
pdtsp_parser.h
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 
14 // A TSPPD parser used to parse instances of Traveling Salesman Problems with
15 // pickup and delivery constraints. This format was created by Stefan Ropke.
16 // https://link.springer.com/article/10.1007%2Fs10107-008-0234-9
17 
18 #ifndef OR_TOOLS_ROUTING_PDTSP_PARSER_H_
19 #define OR_TOOLS_ROUTING_PDTSP_PARSER_H_
20 
21 #include <functional>
22 #include <string>
23 #include <vector>
24 
26 
27 namespace operations_research {
28 
29 class PdTspParser {
30  public:
31  PdTspParser();
32  ~PdTspParser() = default;
33  // Loads and parse a PDTSP from a given file.
34  bool LoadFile(const std::string& file_name);
35  // Returns the index of the depot.
36  int depot() const { return depot_; }
37  // Returns the number of nodes in the PDTSP.
38  int Size() const { return x_.size(); }
39  // Returns true if the index corresponding to a node is a pickup.
40  bool IsPickup(int index) const { return deliveries_[index] >= 0; }
41  // Returns the delivery corresponding to a pickup.
42  int DeliveryFromPickup(int index) const { return deliveries_[index]; }
43  // Returns a function returning distances between nodes.
44  std::function<int64_t(int, int)> Distances() const;
45 
46  private:
47  enum Sections { SIZE_SECTION, DEPOT_SECTION, NODE_SECTION, EOF_SECTION };
48  void ProcessNewLine(const std::string& line);
49 
50  int depot_;
51  Sections section_;
52  std::vector<double> x_;
53  std::vector<double> y_;
54  std::vector<int> deliveries_;
55 };
56 
57 } // namespace operations_research
58 
59 #endif // OR_TOOLS_ROUTING_PDTSP_PARSER_H_
bool IsPickup(int index) const
Definition: pdtsp_parser.h:40
bool LoadFile(const std::string &file_name)
Definition: pdtsp_parser.cc:45
int DeliveryFromPickup(int index) const
Definition: pdtsp_parser.h:42
std::function< int64_t(int, int)> Distances() const
Definition: pdtsp_parser.cc:53
int index
Collection of objects used to extend the Constraint Solver library.
int line
Definition: parse_proto.cc:31