Google OR-Tools v9.11
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-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 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/strings/string_view.h"
53#include "ortools/base/macros.h"
54#include "ortools/base/types.h"
56
57namespace operations_research {
58
59// Solomon parser class.
61 public:
63
64#ifndef SWIG
65 SolomonParser(const SolomonParser&) = delete;
66 const SolomonParser& operator=(const SolomonParser&) = delete;
67#endif
68
69 // Loading an instance. Both methods return false in case of failure to read
70 // the instance. Loading a new instance clears the previously loaded instance.
71
72 // Loads instance from a file.
73 bool LoadFile(const std::string& file_name);
74 // Loads instance from a file contained in a zipped archive; the archive can
75 // contain multiple files.
76 bool LoadFile(absl::string_view file_name, const std::string& archive_name);
77
78 // Returns the name of the instance being solved.
79 const std::string& name() const { return name_; }
80 // Returns the index of the depot.
81 int Depot() const { return 0; }
82 // Returns the number of nodes in the current routing problem.
83 int NumberOfNodes() const { return coordinates_.size(); }
84 // Returns the maximum number of vehicles to use.
85 int NumberOfVehicles() const { return vehicles_; }
86 // Returns the coordinates of the nodes in the current routing problem.
87 const std::vector<Coordinates2<int64_t>>& coordinates() const {
88 return coordinates_;
89 }
90 // Returns the capacity of the vehicles.
91 int64_t capacity() const { return capacity_; }
92 // Returns the demand of the nodes in the current routing problem.
93 const std::vector<int64_t>& demands() const { return demands_; }
94 // Returns the time windows of the nodes in the current routing problem.
95 const std::vector<SimpleTimeWindow<int64_t>>& time_windows() const {
96 return time_windows_;
97 }
98 // Returns the service times of the nodes in the current routing problem.
99 const std::vector<int64_t>& service_times() const { return service_times_; }
100 // Returns the distance between two nodes.
101 double GetDistance(int from, int to) const {
102 const Coordinates2<int64_t>& from_coords = coordinates_[from];
103 const Coordinates2<int64_t>& to_coords = coordinates_[to];
104 const double xd = from_coords.x - to_coords.x;
105 const double yd = from_coords.y - to_coords.y;
106 return sqrt(xd * xd + yd * yd);
107 }
108 // Returns the travel time between two nodes.
109 double GetTravelTime(int from, int to) const {
110 return service_times_[from] + GetDistance(from, to);
111 }
112
113 private:
114 enum Section { UNKNOWN, NAME, VEHICLE, CUSTOMER };
115
116 // Parsing
117 void Initialize();
118 bool ParseFile(const std::string& file_name);
119
120 // Parsing data
121 const std::map<std::string, Section> sections_;
122 Section section_;
123 int to_read_;
124 // Input data
125 // Using int64_t to represent different dimension values of the problem:
126 // - demand and vehicle capacity,
127 // - distances and node coordinates,
128 // - time, including time window values and service times.
129 std::string name_;
130 int vehicles_;
131 std::vector<Coordinates2<int64_t>> coordinates_;
132 int64_t capacity_;
133 std::vector<int64_t> demands_;
134 std::vector<SimpleTimeWindow<int64_t>> time_windows_;
135 std::vector<int64_t> service_times_;
136};
137} // namespace operations_research
138
139#endif // OR_TOOLS_ROUTING_PARSERS_SOLOMON_PARSER_H_
double GetTravelTime(int from, int to) const
Returns the travel time between two nodes.
SolomonParser(const SolomonParser &)=delete
int NumberOfNodes() const
Returns the number of nodes in the current routing problem.
const std::vector< int64_t > & demands() const
Returns the demand of the nodes in the current routing problem.
int NumberOfVehicles() const
Returns the maximum number of vehicles to use.
const std::string & name() const
Returns the name of the instance being solved.
int64_t capacity() const
Returns the capacity of the vehicles.
const SolomonParser & operator=(const SolomonParser &)=delete
int Depot() const
Returns the index of the depot.
const std::vector< int64_t > & service_times() const
Returns the service times of the nodes in the current routing problem.
double GetDistance(int from, int to) const
Returns the distance between two nodes.
bool LoadFile(const std::string &file_name)
Loads instance from a file.
const std::vector< Coordinates2< int64_t > > & coordinates() const
Returns the coordinates of the nodes in the current routing problem.
const std::vector< SimpleTimeWindow< int64_t > > & time_windows() const
Returns the time windows of the nodes in the current routing problem.
In SWIG mode, we don't want anything besides these top-level includes.
trees with all degrees equal to
Real-world coordinates.