Google OR-Tools v9.15
a fast and portable software suite for combinatorial optimization
Loading...
Searching...
No Matches
vrp_pickup_delivery_lifo.cc

Simple VRP example.

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// [START program]
15// [START import]
16#include <cstdint>
17#include <cstdlib>
18#include <sstream>
19#include <vector>
20
27// [END import]
28
29namespace operations_research {
30// [START data_model]
31struct DataModel {
32 const std::vector<std::vector<int64_t>> distance_matrix{
33 {0, 548, 776, 696, 582, 274, 502, 194, 308, 194, 536, 502, 388, 354, 468,
34 776, 662},
35 {548, 0, 684, 308, 194, 502, 730, 354, 696, 742, 1084, 594, 480, 674,
36 1016, 868, 1210},
37 {776, 684, 0, 992, 878, 502, 274, 810, 468, 742, 400, 1278, 1164, 1130,
38 788, 1552, 754},
39 {696, 308, 992, 0, 114, 650, 878, 502, 844, 890, 1232, 514, 628, 822,
40 1164, 560, 1358},
41 {582, 194, 878, 114, 0, 536, 764, 388, 730, 776, 1118, 400, 514, 708,
42 1050, 674, 1244},
43 {274, 502, 502, 650, 536, 0, 228, 308, 194, 240, 582, 776, 662, 628, 514,
44 1050, 708},
45 {502, 730, 274, 878, 764, 228, 0, 536, 194, 468, 354, 1004, 890, 856, 514,
46 1278, 480},
47 {194, 354, 810, 502, 388, 308, 536, 0, 342, 388, 730, 468, 354, 320, 662,
48 742, 856},
49 {308, 696, 468, 844, 730, 194, 194, 342, 0, 274, 388, 810, 696, 662, 320,
50 1084, 514},
51 {194, 742, 742, 890, 776, 240, 468, 388, 274, 0, 342, 536, 422, 388, 274,
52 810, 468},
53 {536, 1084, 400, 1232, 1118, 582, 354, 730, 388, 342, 0, 878, 764, 730,
54 388, 1152, 354},
55 {502, 594, 1278, 514, 400, 776, 1004, 468, 810, 536, 878, 0, 114, 308,
56 650, 274, 844},
57 {388, 480, 1164, 628, 514, 662, 890, 354, 696, 422, 764, 114, 0, 194, 536,
58 388, 730},
59 {354, 674, 1130, 822, 708, 628, 856, 320, 662, 388, 730, 308, 194, 0, 342,
60 422, 536},
61 {468, 1016, 788, 1164, 1050, 514, 514, 662, 320, 274, 388, 650, 536, 342,
62 0, 764, 194},
63 {776, 868, 1552, 560, 674, 1050, 1278, 742, 1084, 810, 1152, 274, 388,
64 422, 764, 0, 798},
65 {662, 1210, 754, 1358, 1244, 708, 480, 856, 514, 468, 354, 844, 730, 536,
66 194, 798, 0},
67 };
68 // [START pickups_deliveries]
69 const std::vector<std::vector<RoutingIndexManager::NodeIndex>>
70 pickups_deliveries{
71 {RoutingIndexManager::NodeIndex{1},
72 RoutingIndexManager::NodeIndex{6}},
73 {RoutingIndexManager::NodeIndex{2},
74 RoutingIndexManager::NodeIndex{10}},
75 {RoutingIndexManager::NodeIndex{4},
76 RoutingIndexManager::NodeIndex{3}},
77 {RoutingIndexManager::NodeIndex{5},
78 RoutingIndexManager::NodeIndex{9}},
79 {RoutingIndexManager::NodeIndex{7},
80 RoutingIndexManager::NodeIndex{8}},
81 {RoutingIndexManager::NodeIndex{15},
82 RoutingIndexManager::NodeIndex{11}},
83 {RoutingIndexManager::NodeIndex{13},
84 RoutingIndexManager::NodeIndex{12}},
85 {RoutingIndexManager::NodeIndex{16},
86 RoutingIndexManager::NodeIndex{14}},
87 };
88 // [END pickups_deliveries]
89 const int num_vehicles = 4;
90 const RoutingIndexManager::NodeIndex depot{0};
91};
92// [END data_model]
93
94// [START solution_printer]
100void PrintSolution(const DataModel& data, const RoutingIndexManager& manager,
101 const RoutingModel& routing, const Assignment& solution) {
102 int64_t total_distance{0};
103 for (int vehicle_id = 0; vehicle_id < data.num_vehicles; ++vehicle_id) {
104 if (!routing.IsVehicleUsed(solution, vehicle_id)) {
105 continue;
106 }
107 int64_t index = routing.Start(vehicle_id);
108 LOG(INFO) << "Route for Vehicle " << vehicle_id << ":";
109 int64_t route_distance{0};
110 std::stringstream route;
111 while (!routing.IsEnd(index)) {
112 route << manager.IndexToNode(index).value() << " -> ";
113 int64_t previous_index = index;
114 index = solution.Value(routing.NextVar(index));
115 route_distance += routing.GetArcCostForVehicle(previous_index, index,
116 int64_t{vehicle_id});
117 }
118 LOG(INFO) << route.str() << manager.IndexToNode(index).value();
119 LOG(INFO) << "Distance of the route: " << route_distance << "m";
120 total_distance += route_distance;
121 }
122 LOG(INFO) << "Total distance of all routes: " << total_distance << "m";
123 LOG(INFO) << "";
124 LOG(INFO) << "Advanced usage:";
125 LOG(INFO) << "Problem solved in " << routing.solver()->wall_time() << "ms";
126}
127// [END solution_printer]
128
129void VrpGlobalSpan() {
130 // Instantiate the data problem.
131 // [START data]
132 DataModel data;
133 // [END data]
134
135 // Create Routing Index Manager
136 // [START index_manager]
137 RoutingIndexManager manager(data.distance_matrix.size(), data.num_vehicles,
138 data.depot);
139 // [END index_manager]
140
141 // Create Routing Model.
142 // [START routing_model]
143 RoutingModel routing(manager);
144 // [END routing_model]
145
146 // Define cost of each arc.
147 // [START arc_cost]
148 const int transit_callback_index = routing.RegisterTransitCallback(
149 [&data, &manager](const int64_t from_index,
150 const int64_t to_index) -> int64_t {
151 // Convert from routing variable Index to distance matrix NodeIndex.
152 const int from_node = manager.IndexToNode(from_index).value();
153 const int to_node = manager.IndexToNode(to_index).value();
154 return data.distance_matrix[from_node][to_node];
155 });
156 routing.SetArcCostEvaluatorOfAllVehicles(transit_callback_index);
157 // [END arc_cost]
158
159 // Add Distance constraint.
160 // [START distance_constraint]
161 routing.AddDimension(transit_callback_index, // transit callback
162 0, // no slack
163 3000, // vehicle maximum travel distance
164 true, // start cumul to zero
165 "Distance");
166 RoutingDimension* distance_dimension =
167 routing.GetMutableDimension("Distance");
168 distance_dimension->SetGlobalSpanCostCoefficient(100);
169 // [END distance_constraint]
170
171 // Define Transportation Requests.
172 // [START pickup_delivery_constraint]
173 Solver* const solver = routing.solver();
174 for (const auto& request : data.pickups_deliveries) {
175 const int64_t pickup_index = manager.NodeToIndex(request[0]);
176 const int64_t delivery_index = manager.NodeToIndex(request[1]);
177 routing.AddPickupAndDelivery(pickup_index, delivery_index);
178 solver->AddConstraint(solver->MakeEquality(
179 routing.VehicleVar(pickup_index), routing.VehicleVar(delivery_index)));
180 solver->AddConstraint(
181 solver->MakeLessOrEqual(distance_dimension->CumulVar(pickup_index),
182 distance_dimension->CumulVar(delivery_index)));
183 }
184 routing.SetPickupAndDeliveryPolicyOfAllVehicles(
186 // [END pickup_delivery_constraint]
187
188 // Setting first solution heuristic.
189 // [START parameters]
190 RoutingSearchParameters searchParameters = DefaultRoutingSearchParameters();
191 searchParameters.set_first_solution_strategy(
193 // [END parameters]
194
195 // Solve the problem.
196 // [START solve]
197 const Assignment* solution = routing.SolveWithParameters(searchParameters);
198 // [END solve]
199
200 // Print solution on console.
201 // [START print_solution]
202 PrintSolution(data, manager, routing, *solution);
203 // [END print_solution]
204}
205} // namespace operations_research
206
207int main(int /*argc*/, char* /*argv*/[]) {
208 operations_research::VrpGlobalSpan();
209 return EXIT_SUCCESS;
210}
211// [END program]
@ PICKUP_AND_DELIVERY_LIFO
Deliveries must be performed in reverse order of pickups.
Definition routing.h:265
int main(int argc, char **argv)
Definition fz.cc:218
OR-Tools root namespace.
Select next search node to expand Select next item_i to add this new search node to the search Generate a new search node where item_i is not in the knapsack Check validity of this new partial solution(using propagators) - If valid
RoutingSearchParameters DefaultRoutingSearchParameters()
The vehicle routing library lets one model and solve generic vehicle routing problems ranging from th...