Google OR-Tools v9.12
a fast and portable software suite for combinatorial optimization
Loading...
Searching...
No Matches
solomon_parser.h
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
14// A parser for "Solomon" instances. The Solomon file library is a set of
15// Capacitated Vehicle Routing Problems with Time Windows created by
16// Pr. Marius Solomon.
17//
18// The goal is to find routes starting and ending at a depot which visit a
19// set of nodes. The objective is first to minimize the number of routes and
20// then to minimize the total distance traveled, distances being measured with
21// the Euclidean distance. There are two types of constraints limiting the
22// route lengths:
23// - time windows restricting the time during which a node can be visited
24// - vehicle capacity which limits the load of the vehicles performing the
25// routes (each node has a corresponding demand which must be picked up
26// by the vehicle).
27//
28// The format of the data is the following:
29//
30// <instance_name>
31// VEHICLE
32// NUMBER CAPACITY
33// <number of nodes> <vehicle capacity>
34// CUSTOMER
35// CUST NO. XCOORD. YCOORD. DEMAND READY TIME DUE DATE SERVICE TIME
36// <node id> <x> <y> <demand> <ready time> <due date> <service time>
37//
38// The parser supports both standard instance files and zipped archives
39// containing multiple instances.
40//
41
42#ifndef OR_TOOLS_ROUTING_PARSERS_SOLOMON_PARSER_H_
43#define OR_TOOLS_ROUTING_PARSERS_SOLOMON_PARSER_H_
44
45#include <math.h>
46
47#include <cstdint>
48#include <map>
49#include <string>
50#include <vector>
51
52#include "absl/container/flat_hash_map.h"
53#include "absl/strings/string_view.h"
55
57
58// Solomon parser class.
60 public:
62
63#ifndef SWIG
64 SolomonParser(const SolomonParser&) = delete;
65 const SolomonParser& operator=(const SolomonParser&) = delete;
66#endif
67
68 // Loading an instance. Both methods return false in case of failure to read
69 // the instance. Loading a new instance clears the previously loaded instance.
70
71 // Loads instance from a file.
72 bool LoadFile(absl::string_view file_name);
73 // Loads instance from a file contained in a zipped archive; the archive can
74 // contain multiple files.
75 bool LoadFile(absl::string_view file_name, const std::string& archive_name);
76
77 // Returns the name of the instance being solved.
78 const std::string& name() const { return name_; }
79 // Returns the index of the depot.
80 int Depot() const { return 0; }
81 // Returns the number of nodes in the current routing problem.
82 int NumberOfNodes() const { return coordinates_.size(); }
83 // Returns the maximum number of vehicles to use.
84 int NumberOfVehicles() const { return vehicles_; }
85 // Returns the coordinates of the nodes in the current routing problem.
86 const std::vector<Coordinates2<int64_t>>& coordinates() const {
87 return coordinates_;
88 }
89 // Returns the capacity of the vehicles.
90 int64_t capacity() const { return capacity_; }
91 // Returns the demand of the nodes in the current routing problem.
92 const std::vector<int64_t>& demands() const { return demands_; }
93 // Returns the time windows of the nodes in the current routing problem.
94 const std::vector<SimpleTimeWindow<int64_t>>& time_windows() const {
95 return time_windows_;
96 }
97 // Returns the service times of the nodes in the current routing problem.
98 const std::vector<int64_t>& service_times() const { return service_times_; }
99 // Returns the distance between two nodes.
100 double GetDistance(int from, int to) const {
101 const Coordinates2<int64_t>& from_coords = coordinates_[from];
102 const Coordinates2<int64_t>& to_coords = coordinates_[to];
103 const double xd = from_coords.x - to_coords.x;
104 const double yd = from_coords.y - to_coords.y;
105 return sqrt(xd * xd + yd * yd);
106 }
107 // Returns the travel time between two nodes.
108 double GetTravelTime(int from, int to) const {
109 return service_times_[from] + GetDistance(from, to);
110 }
111
112 private:
113 enum Section { UNKNOWN, NAME, VEHICLE, CUSTOMER };
114
115 // Parsing
116 void Initialize();
117 bool ParseFile(absl::string_view file_name);
118
119 // Parsing data
120 const std::map<std::string, Section> sections_;
121 Section section_;
122 int to_read_;
123 // Input data
124 // Using int64_t to represent different dimension values of the problem:
125 // - demand and vehicle capacity,
126 // - distances and node coordinates,
127 // - time, including time window values and service times.
128 std::string name_;
129 int vehicles_;
130 std::vector<Coordinates2<int64_t>> coordinates_;
131 int64_t capacity_;
132 std::vector<int64_t> demands_;
133 std::vector<SimpleTimeWindow<int64_t>> time_windows_;
134 std::vector<int64_t> service_times_;
135};
136
137// Solomon solution parser class.
139 public:
141
142#ifndef SWIG
145#endif
146
147 // Loads solution from a file. Returns false in case of failure to read
148 // the file. Loading a new solution clears the previously loaded solution.
149 bool LoadFile(absl::string_view file_name);
150
151 // Returns the number of routes used in the solution.
152 int NumberOfRoutes() const { return routes_.size(); }
153 // Returns the sequence of the ith route, excluding depot nodes.
154 const std::vector<int>& route(int i) const { return routes_[i]; }
155 // Returns the value corresponding to a key. Keys can be (but are not limited
156 // to) "Authors", "Date", "Instance Name", or "Reference". These keys might
157 // vary slightly per instance (refer to the input file).
158 const std::string& GetValueFromKey(absl::string_view key) const {
159 static const std::string* default_value = new std::string{};
160 auto it = key_values_.find(key);
161 if (it != key_values_.end()) return it->second;
162 return *default_value;
163 }
164
165 private:
166 // Parsing
167 void Initialize();
168 bool ParseFile(absl::string_view file_name);
169
170 // Parsing data
171 std::vector<std::vector<int>> routes_;
172 absl::flat_hash_map<std::string, std::string> key_values_;
173};
174
175} // namespace operations_research::routing
176
177#endif // OR_TOOLS_ROUTING_PARSERS_SOLOMON_PARSER_H_
const std::vector< int64_t > & demands() const
Returns the demand of the nodes in the current routing problem.
const std::string & name() const
Returns the name of the instance being solved.
const std::vector< Coordinates2< int64_t > > & coordinates() const
Returns the coordinates of the nodes in the current routing problem.
int Depot() const
Returns the index of the depot.
double GetDistance(int from, int to) const
Returns the distance between two nodes.
const std::vector< SimpleTimeWindow< int64_t > > & time_windows() const
Returns the time windows of the nodes in the current routing problem.
const SolomonParser & operator=(const SolomonParser &)=delete
int64_t capacity() const
Returns the capacity of the vehicles.
SolomonParser(const SolomonParser &)=delete
bool LoadFile(absl::string_view file_name)
Loads instance from a file.
int NumberOfNodes() const
Returns the number of nodes in the current routing problem.
int NumberOfVehicles() const
Returns the maximum number of vehicles to use.
double GetTravelTime(int from, int to) const
Returns the travel time between two nodes.
const std::vector< int64_t > & service_times() const
Returns the service times of the nodes in the current routing problem.
const std::string & GetValueFromKey(absl::string_view key) const
const SolomonSolutionParser & operator=(const SolomonSolutionParser &)=delete
SolomonSolutionParser(const SolomonSolutionParser &)=delete
const std::vector< int > & route(int i) const
Returns the sequence of the ith route, excluding depot nodes.
int NumberOfRoutes() const
Returns the number of routes used in the solution.
Common utilities for parsing routing instances.
trees with all degrees equal to