OR-Tools  9.6
solomon_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 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_SOLOMON_PARSER_H_
43 #define OR_TOOLS_ROUTING_SOLOMON_PARSER_H_
44 
45 #include <math.h>
46 
47 #include <cstdint>
48 #include <map>
49 #include <string>
50 #include <vector>
51 
53 #include "ortools/base/macros.h"
55 
56 namespace operations_research {
57 
58 // Solomon parser class.
60  public:
61  SolomonParser();
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(const std::string& file_name);
73  // Loads instance from a file contained in a zipped archive; the archive can
74  // contain multiple files.
75  bool LoadFile(const std::string& 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(const std::string& 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 } // namespace operations_research
137 
138 #endif // OR_TOOLS_ROUTING_SOLOMON_PARSER_H_
const std::vector< int64_t > & demands() const
const std::vector< Coordinates2< int64_t > > & coordinates() const
const SolomonParser & operator=(const SolomonParser &)=delete
bool LoadFile(const std::string &file_name)
const std::vector< int64_t > & service_times() const
const std::vector< SimpleTimeWindow< int64_t > > & time_windows() const
double GetDistance(int from, int to) const
SolomonParser(const SolomonParser &)=delete
const std::string & name() const
double GetTravelTime(int from, int to) const
Collection of objects used to extend the Constraint Solver library.