Google OR-Tools v9.15
a fast and portable software suite for combinatorial optimization
Loading...
Searching...
No Matches
vrp_drop_nodes.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
21#include "google/protobuf/duration.pb.h"
28// [END import]
29
30namespace operations_research {
31// [START data_model]
32struct DataModel {
33 const std::vector<std::vector<int64_t>> distance_matrix{
34 {0, 548, 776, 696, 582, 274, 502, 194, 308, 194, 536, 502, 388, 354, 468,
35 776, 662},
36 {548, 0, 684, 308, 194, 502, 730, 354, 696, 742, 1084, 594, 480, 674,
37 1016, 868, 1210},
38 {776, 684, 0, 992, 878, 502, 274, 810, 468, 742, 400, 1278, 1164, 1130,
39 788, 1552, 754},
40 {696, 308, 992, 0, 114, 650, 878, 502, 844, 890, 1232, 514, 628, 822,
41 1164, 560, 1358},
42 {582, 194, 878, 114, 0, 536, 764, 388, 730, 776, 1118, 400, 514, 708,
43 1050, 674, 1244},
44 {274, 502, 502, 650, 536, 0, 228, 308, 194, 240, 582, 776, 662, 628, 514,
45 1050, 708},
46 {502, 730, 274, 878, 764, 228, 0, 536, 194, 468, 354, 1004, 890, 856, 514,
47 1278, 480},
48 {194, 354, 810, 502, 388, 308, 536, 0, 342, 388, 730, 468, 354, 320, 662,
49 742, 856},
50 {308, 696, 468, 844, 730, 194, 194, 342, 0, 274, 388, 810, 696, 662, 320,
51 1084, 514},
52 {194, 742, 742, 890, 776, 240, 468, 388, 274, 0, 342, 536, 422, 388, 274,
53 810, 468},
54 {536, 1084, 400, 1232, 1118, 582, 354, 730, 388, 342, 0, 878, 764, 730,
55 388, 1152, 354},
56 {502, 594, 1278, 514, 400, 776, 1004, 468, 810, 536, 878, 0, 114, 308,
57 650, 274, 844},
58 {388, 480, 1164, 628, 514, 662, 890, 354, 696, 422, 764, 114, 0, 194, 536,
59 388, 730},
60 {354, 674, 1130, 822, 708, 628, 856, 320, 662, 388, 730, 308, 194, 0, 342,
61 422, 536},
62 {468, 1016, 788, 1164, 1050, 514, 514, 662, 320, 274, 388, 650, 536, 342,
63 0, 764, 194},
64 {776, 868, 1552, 560, 674, 1050, 1278, 742, 1084, 810, 1152, 274, 388,
65 422, 764, 0, 798},
66 {662, 1210, 754, 1358, 1244, 708, 480, 856, 514, 468, 354, 844, 730, 536,
67 194, 798, 0},
68 };
69 // [START demands_capacities]
70 const std::vector<int64_t> demands{
71 0, 1, 1, 3, 6, 3, 6, 8, 8, 1, 2, 1, 2, 6, 6, 8, 8,
72 };
73 const std::vector<int64_t> vehicle_capacities{15, 15, 15, 15};
74 // [END demands_capacities]
75 const int num_vehicles = 4;
76 const RoutingIndexManager::NodeIndex depot{0};
77};
78// [END data_model]
79
80// [START solution_printer]
86void PrintSolution(const DataModel& data, const RoutingIndexManager& manager,
87 const RoutingModel& routing, const Assignment& solution) {
88 // Display dropped nodes.
89 std::ostringstream dropped_nodes;
90 for (int64_t node = 0; node < routing.Size(); ++node) {
91 if (routing.IsStart(node) || routing.IsEnd(node)) continue;
92 if (solution.Value(routing.NextVar(node)) == node) {
93 dropped_nodes << " " << manager.IndexToNode(node).value();
94 }
95 }
96 LOG(INFO) << "Dropped nodes:" << dropped_nodes.str();
97 // Display routes
98 int64_t total_distance{0};
99 int64_t total_load{0};
100 for (int vehicle_id = 0; vehicle_id < data.num_vehicles; ++vehicle_id) {
101 if (!routing.IsVehicleUsed(solution, vehicle_id)) {
102 continue;
103 }
104 int64_t index = routing.Start(vehicle_id);
105 LOG(INFO) << "Route for Vehicle " << vehicle_id << ":";
106 int64_t route_distance{0};
107 int64_t route_load{0};
108 std::ostringstream route;
109 while (!routing.IsEnd(index)) {
110 const int node_index = manager.IndexToNode(index).value();
111 route_load += data.demands[node_index];
112 route << node_index << " Load(" << route_load << ") -> ";
113 const 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 LOG(INFO) << "Load of the route: " << route_load;
121 total_distance += route_distance;
122 total_load += route_load;
123 }
124 LOG(INFO) << "Total distance of all routes: " << total_distance << "m";
125 LOG(INFO) << "Total load of all routes: " << total_load;
126 LOG(INFO) << "";
127 LOG(INFO) << "Advanced usage:";
128 LOG(INFO) << "Problem solved in " << routing.solver()->wall_time() << "ms";
129}
130// [END solution_printer]
131
132void VrpDropNodes() {
133 // Instantiate the data problem.
134 // [START data]
135 DataModel data;
136 // [END data]
137
138 // Create Routing Index Manager
139 // [START index_manager]
140 RoutingIndexManager manager(data.distance_matrix.size(), data.num_vehicles,
141 data.depot);
142 // [END index_manager]
143
144 // Create Routing Model.
145 // [START routing_model]
146 RoutingModel routing(manager);
147 // [END routing_model]
148
149 // Create and register a transit callback.
150 // [START transit_callback]
151 const int transit_callback_index = routing.RegisterTransitCallback(
152 [&data, &manager](const int64_t from_index,
153 const int64_t to_index) -> int64_t {
154 // Convert from routing variable Index to distance matrix NodeIndex.
155 const int from_node = manager.IndexToNode(from_index).value();
156 const int to_node = manager.IndexToNode(to_index).value();
157 return data.distance_matrix[from_node][to_node];
158 });
159 // [END transit_callback]
160
161 // Define cost of each arc.
162 // [START arc_cost]
163 routing.SetArcCostEvaluatorOfAllVehicles(transit_callback_index);
164 // [END arc_cost]
165
166 // Add Capacity constraint.
167 // [START capacity_constraint]
168 const int demand_callback_index = routing.RegisterUnaryTransitCallback(
169 [&data, &manager](const int64_t from_index) -> int64_t {
170 // Convert from routing variable Index to demand NodeIndex.
171 const int from_node = manager.IndexToNode(from_index).value();
172 return data.demands[from_node];
173 });
174 routing.AddDimensionWithVehicleCapacity(
175 demand_callback_index, // transit callback index
176 int64_t{0}, // null capacity slack
177 data.vehicle_capacities, // vehicle maximum capacities
178 true, // start cumul to zero
179 "Capacity");
180 // Allow to drop nodes.
181 int64_t penalty{1000};
182 for (int i = 1; i < data.distance_matrix.size(); ++i) {
183 routing.AddDisjunction(
184 {manager.NodeToIndex(RoutingIndexManager::NodeIndex(i))}, penalty);
185 }
186 // [END capacity_constraint]
187
188 // Setting first solution heuristic.
189 // [START parameters]
190 RoutingSearchParameters search_parameters = DefaultRoutingSearchParameters();
191 search_parameters.set_first_solution_strategy(
193 search_parameters.set_local_search_metaheuristic(
195 search_parameters.mutable_time_limit()->set_seconds(1);
196 // [END parameters]
197
198 // Solve the problem.
199 // [START solve]
200 const Assignment* solution = routing.SolveWithParameters(search_parameters);
201 // [END solve]
202
203 // Print solution on console.
204 // [START print_solution]
205 PrintSolution(data, manager, routing, *solution);
206 // [END print_solution]
207}
208} // namespace operations_research
209
210int main(int /*argc*/, char* /*argv*/[]) {
211 operations_research::VrpDropNodes();
212 return EXIT_SUCCESS;
213}
214// [END program]
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...