Google OR-Tools v9.11
a fast and portable software suite for combinatorial optimization
Loading...
Searching...
No Matches
tsplib_parser.h
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
14// A TSPLIB parser. The TSPLIB is a library containing Traveling
15// Salesman Problems and other vehicle routing problems.
16// Limitations:
17// - only TSP and CVRP files are currently supported.
18// - XRAY1, XRAY2 and SPECIAL edge weight types are not supported.
19//
20// Takes as input a data file, potentially gzipped. The data must
21// follow the TSPLIB95 format (described at
22// http://www.iwr.uni-heidelberg.de/groups/comopt/software/TSPLIB95/DOC.PS).
23
24#ifndef OR_TOOLS_ROUTING_PARSERS_TSPLIB_PARSER_H_
25#define OR_TOOLS_ROUTING_PARSERS_TSPLIB_PARSER_H_
26
27#include <functional>
28#include <set>
29#include <string>
30#include <utility>
31#include <vector>
32
33#include "absl/container/flat_hash_map.h"
34#include "absl/types/span.h"
35#include "ortools/base/types.h"
37
38namespace operations_research {
39
40class TspLibParser final {
41 public:
42 // Routing model types (cf. the link above for a description).
44
46 // Loads and parses a routing problem from a given file.
47 bool LoadFile(const std::string& file_name);
48 // Returns the number of nodes in the routing problem stored in a given file.
49 int SizeFromFile(const std::string& file_name) const;
50 // Returns a function returning edge weights between nodes.
51 EdgeWeights GetEdgeWeights() const { return distance_function_; }
52 // Returns the index of the depot.
53 int depot() const { return depot_; }
54 // Returns the number of nodes in the current routing problem.
55 int size() const { return size_; }
56 // Returns the type of the current routing problem.
57 Types type() const { return type_; }
58 // Returns the coordinates of the nodes in the current routing problem (if
59 // they exist).
60 const std::vector<Coordinates3<double>>& coordinates() const {
61 return coords_;
62 }
63 // Returns the capacity of the vehicles in the current routing problem.
64 int64_t capacity() const { return capacity_; }
65 // Returns the maximal distance vehicles can travel.
66 int64_t max_distance() const { return max_distance_; }
67 // Returns the demands (or quantities picked up) at each node.
68 const std::vector<int64_t>& demands() const { return demands_; }
69 // Returns the pairs of nodes corresponding to forced edges (second node is
70 // directly after the first).
71 std::set<std::pair<int, int>> fixed_edges() const { return fixed_edges_; }
72 // Returns edges of the graph on which Hamiltonian cycles need to be built.
73 // Edges are represented as adjacency lists for each node.
74 const std::vector<std::vector<int>>& edges() const { return edges_; }
75 // Returns the name of the current routing model.
76 const std::string& name() const { return name_; }
77 // Returns the comments attached to the data.
78 const std::string& comments() const { return comments_; }
79 // Build a tour output in TSPLIB95 format from a vector of routes, a route
80 // being a sequence of node indices.
81 std::string BuildTourFromRoutes(
82 absl::Span<const std::vector<int>> routes) const;
83
84 private:
85 enum Sections {
86 NAME,
87 TYPE,
88 COMMENT,
89 DIMENSION,
90 DISTANCE,
91 CAPACITY,
92 EDGE_DATA_FORMAT,
93 EDGE_DATA_SECTION,
94 EDGE_WEIGHT_TYPE,
95 EDGE_WEIGHT_FORMAT,
96 EDGE_WEIGHT_SECTION,
97 FIXED_EDGES_SECTION,
98 NODE_COORD_TYPE,
99 DISPLAY_DATA_TYPE,
100 DISPLAY_DATA_SECTION,
101 NODE_COORD_SECTION,
102 DEPOT_SECTION,
103 DEMAND_SECTION,
104 END_OF_FILE,
105 UNDEFINED_SECTION
106 };
107 enum EdgeDataFormat { EDGE_LIST, ADJ_LIST };
108 enum EdgeWeightTypes {
109 EXPLICIT,
110 EUC_2D,
111 EUC_3D,
112 MAX_2D,
113 MAX_3D,
114 MAN_2D,
115 MAN_3D,
116 CEIL_2D,
117 GEO,
118 GEOM,
119 ATT,
120 XRAY1,
121 XRAY2,
122 SPECIAL,
123 UNDEFINED_EDGE_WEIGHT_TYPE
124 };
125 enum EdgeWeightFormats {
126 FUNCTION,
127 FULL_MATRIX,
128 UPPER_ROW,
129 LOWER_ROW,
130 UPPER_DIAG_ROW,
131 LOWER_DIAG_ROW,
132 UPPER_COL,
133 LOWER_COL,
134 UPPER_DIAG_COL,
135 LOWER_DIAG_COL,
136 UNDEFINED_EDGE_WEIGHT_FORMAT
137 };
138
139#ifndef SWIG
140 TspLibParser(const TspLibParser&) = delete;
141 void operator=(const TspLibParser&) = delete;
142#endif
143
144 void ParseExplicitFullMatrix(absl::Span<const std::string> words);
145 void ParseExplicitUpperRow(absl::Span<const std::string> words);
146 void ParseExplicitLowerRow(absl::Span<const std::string> words);
147 void ParseExplicitUpperDiagRow(absl::Span<const std::string> words);
148 void ParseExplicitLowerDiagRow(absl::Span<const std::string> words);
149 void ParseNodeCoord(absl::Span<const std::string> words);
150 void SetUpEdgeWeightSection();
151 void FinalizeEdgeWeights();
152 void ParseSections(absl::Span<const std::string> words);
153 void ProcessNewLine(const std::string& line);
154 void SetExplicitCost(int from, int to, int64_t cost) {
155 if (explicit_costs_.size() != size_ * size_) {
156 explicit_costs_.resize(size_ * size_, 0);
157 }
158 explicit_costs_[from * size_ + to] = cost;
159 }
160
161 // Model data
162 int64_t size_;
163 int64_t capacity_;
164 int64_t max_distance_;
165 std::vector<int64_t> demands_;
166 EdgeWeights distance_function_;
167 std::vector<int64_t> explicit_costs_;
168 std::set<std::pair<int, int>> fixed_edges_;
169 int depot_;
170 std::vector<std::vector<int>> edges_;
171
172 // Parsing data
173 static const absl::flat_hash_map<std::string, Sections>* const kSections;
174 Sections section_;
175 static const absl::flat_hash_map<std::string, Types>* const kTypes;
176 Types type_;
177 static const absl::flat_hash_map<std::string, EdgeDataFormat>* const
178 kEdgeDataFormats;
179 EdgeDataFormat edge_data_format_;
180 static const absl::flat_hash_map<std::string, EdgeWeightTypes>* const
181 kEdgeWeightTypes;
182 EdgeWeightTypes edge_weight_type_;
183 static const absl::flat_hash_map<std::string, EdgeWeightFormats>* const
184 kEdgeWeightFormats;
185 EdgeWeightFormats edge_weight_format_;
186 int edge_row_;
187 int edge_column_;
188 std::vector<Coordinates3<double>> coords_;
189 std::string name_;
190 std::string comments_;
191 int64_t to_read_;
192};
193
194// Class parsing tour (solution) data in TSLIB95 format.
195
196class TspLibTourParser final {
197 public:
199 // Loads and parses a given tour file.
200 bool LoadFile(const std::string& file_name);
201 // Returns a vector corresponding to the sequence of nodes of the tour.
202 const std::vector<int>& tour() const { return tour_; }
203 // Returns the size of the tour.
204 int size() const { return size_; }
205 // Returns the comments attached to the data.
206 const std::string& comments() const { return comments_; }
207
208 private:
209 enum Sections {
210 NAME,
211 TYPE,
212 COMMENT,
213 DIMENSION,
214 TOUR_SECTION,
215 END_OF_FILE,
216 UNDEFINED_SECTION
217 };
218
219#ifndef SWIG
220 TspLibTourParser(const TspLibTourParser&) = delete;
221 void operator=(const TspLibTourParser&) = delete;
222#endif
223
224 void ProcessNewLine(const std::string& line);
225
226 static const absl::flat_hash_map<std::string, Sections>* const kSections;
227 Sections section_;
228 std::string comments_;
229 int64_t size_;
230 std::vector<int> tour_;
231};
232
233// Class parsing tours (solution) data in CVRPlib format.
234
235class CVRPToursParser final {
236 public:
238 // Loads and parses a given tours file.
239 bool LoadFile(const std::string& file_name);
240 // Returns a vector corresponding to the sequence of nodes of tours.
241 const std::vector<std::vector<int>>& tours() const { return tours_; }
242 int64_t cost() const { return cost_; }
243
244 private:
245 void ProcessNewLine(const std::string& line);
246
247 std::vector<std::vector<int>> tours_;
248 int64_t cost_;
249};
250} // namespace operations_research
251
252#endif // OR_TOOLS_ROUTING_PARSERS_TSPLIB_PARSER_H_
Class parsing tours (solution) data in CVRPlib format.
const std::vector< std::vector< int > > & tours() const
Returns a vector corresponding to the sequence of nodes of tours.
bool LoadFile(const std::string &file_name)
Loads and parses a given tours file.
EdgeWeights GetEdgeWeights() const
Returns a function returning edge weights between nodes.
const std::vector< int64_t > & demands() const
Returns the demands (or quantities picked up) at each node.
int depot() const
Returns the index of the depot.
int SizeFromFile(const std::string &file_name) const
Returns the number of nodes in the routing problem stored in a given file.
std::set< std::pair< int, int > > fixed_edges() const
bool LoadFile(const std::string &file_name)
Loads and parses a routing problem from a given file.
int64_t max_distance() const
Returns the maximal distance vehicles can travel.
int64_t capacity() const
Returns the capacity of the vehicles in the current routing problem.
const std::string & name() const
Returns the name of the current routing model.
const std::string & comments() const
Returns the comments attached to the data.
const std::vector< Coordinates3< double > > & coordinates() const
Types type() const
Returns the type of the current routing problem.
std::string BuildTourFromRoutes(absl::Span< const std::vector< int > > routes) const
const std::vector< std::vector< int > > & edges() const
Types
Routing model types (cf. the link above for a description).
int size() const
Returns the number of nodes in the current routing problem.
Class parsing tour (solution) data in TSLIB95 format.
bool LoadFile(const std::string &file_name)
Loads and parses a given tour file.
int size() const
Returns the size of the tour.
const std::vector< int > & tour() const
Returns a vector corresponding to the sequence of nodes of the tour.
const std::string & comments() const
Returns the comments attached to the data.
In SWIG mode, we don't want anything besides these top-level includes.
std::function< int64_t(int, int)> EdgeWeights
Mapping between an edge (given by its tail and its head) and its weight.
trees with all degrees equal to
int line