Google OR-Tools v9.12
a fast and portable software suite for combinatorial optimization
Loading...
Searching...
No Matches
lilim_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 Li&Lim PDPTW (pickup and delivery problems with time windows)
15// instances.
16// The goal is to find routes starting and end at a depot which visit a set of
17// nodes. Nodes are grouped in pairs of pickup and delivery nodes. The pickup
18// node of each pair has to be performed before the delivery node and both nodes
19// have to be on the same route. The objective is first to minimize the number
20// of routes and then to minimize the total distance traveled, distances being
21// measured with the Euclidean distance between nodes.
22// Routes are subject to two other types of constraints:
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// or delivered by the vehicle).
27//
28// The format of the data is the following:
29// - one row to describe vehicles (which are all identical):
30// <number of vehicles> <vehicle capacity> <speed>
31// - followed by a row per node:
32// <node id> <x> <y> <demand> <ready time> <due date> <service time> <pickup
33// index> <delivery index>
34//
35// Node 0 corresponds to the depot. For pickup nodes, pickup index is 0, and
36// delivery index gives the index of the corresponding delivery node. For
37// delivery tasks, delivery index is 0, and pickup index gives the index of the
38// corresponding pickup node. The value of travel time is equal to the value of
39// distance.
40
41#ifndef OR_TOOLS_ROUTING_PARSERS_LILIM_PARSER_H_
42#define OR_TOOLS_ROUTING_PARSERS_LILIM_PARSER_H_
43
44#include <math.h>
45
46#include <cstdint>
47#include <optional>
48#include <string>
49#include <vector>
50
51#include "absl/strings/string_view.h"
53
55
56// Li&Lim parser class
58 public:
59 LiLimParser() = default;
60#ifndef SWIG
62 const LiLimParser& operator=(const LiLimParser&) = delete;
63#endif
64
65 // Loading an instance. Both methods return false in case of failure to read
66 // the instance. Loading a new instance clears the previously loaded instance.
67
68 // Loads instance from a file.
69 bool LoadFile(absl::string_view file_name);
70 // Loads instance from a file contained in a zipped archive; the archive can
71 // contain multiple files.
72 bool LoadFile(absl::string_view file_name, absl::string_view archive_name);
73
74 // Returns the index of the depot.
75 int Depot() const { return 0; }
76 // Returns the number of nodes in the current routing problem.
77 int NumberOfNodes() const { return coordinates_.size(); }
78 // Returns the maximum number of vehicles to use.
79 int NumberOfVehicles() const { return vehicles_; }
80 // Returns the coordinates of the nodes in the current routing problem.
81 const std::vector<Coordinates2<int64_t>>& coordinates() const {
82 return coordinates_;
83 }
84 // Returns the delivery of a pickup.
85 std::optional<int> GetDelivery(int node) const;
86 // Returns the pickup of a delivery.
87 std::optional<int> GetPickup(int node) const;
88 // Returns the capacity of the vehicles.
89 int64_t capacity() const { return capacity_; }
90 // Returns the demand of the nodes in the current routing problem.
91 const std::vector<int64_t>& demands() const { return demands_; }
92 // Returns the time windows of the nodes in the current routing problem.
93 const std::vector<SimpleTimeWindow<int64_t>>& time_windows() const {
94 return time_windows_;
95 }
96 // Returns the service times of the nodes in the current routing problem.
97 const std::vector<int64_t>& service_times() const { return service_times_; }
98 // Returns the distance between two nodes.
99 double GetDistance(int from, int to) const {
100 const Coordinates2<int64_t>& from_coords = coordinates_[from];
101 const Coordinates2<int64_t>& to_coords = coordinates_[to];
102 const double xd = from_coords.x - to_coords.x;
103 const double yd = from_coords.y - to_coords.y;
104 return sqrt(xd * xd + yd * yd);
105 }
106 // Returns the travel time between two nodes.
107 double GetTravelTime(int from, int to) const {
108 return service_times_[from] + GetDistance(from, to);
109 }
110
111 private:
112 void Initialize();
113 bool ParseFile(absl::string_view file_name);
114
115 int vehicles_ = 0;
116 std::vector<Coordinates2<int64_t>> coordinates_;
117 std::vector<int> pickups_;
118 std::vector<int> deliveries_;
119 int64_t capacity_ = 0;
120 int64_t speed_ = 0;
121 std::vector<int64_t> demands_;
122 std::vector<SimpleTimeWindow<int64_t>> time_windows_;
123 std::vector<int64_t> service_times_;
124};
125} // namespace operations_research::routing
126
127#endif // OR_TOOLS_ROUTING_PARSERS_LILIM_PARSER_H_
double GetTravelTime(int from, int to) const
Returns the travel time between two nodes.
std::optional< int > GetPickup(int node) const
Returns the pickup of a delivery.
const std::vector< SimpleTimeWindow< int64_t > > & time_windows() const
Returns the time windows 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 LiLimParser & operator=(const LiLimParser &)=delete
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.
int64_t capacity() const
Returns the capacity of the vehicles.
const std::vector< int64_t > & service_times() const
Returns the service times of the nodes in the current routing problem.
int NumberOfNodes() const
Returns the number of nodes in the current routing problem.
bool LoadFile(absl::string_view 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.
std::optional< int > GetDelivery(int node) const
Returns the delivery of a pickup.
Common utilities for parsing routing instances.
trees with all degrees equal to