1 // Copyright 2010-2021 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 <vector>
18
19 #include "ortools/constraint_solver/routing.h"
20 #include "ortools/constraint_solver/routing_enums.pb.h"
21 #include "ortools/constraint_solver/routing_index_manager.h"
22 #include "ortools/constraint_solver/routing_parameters.h"
23 // [END import]
24
25 namespace operations_research {
26 // [START data_model]
27 struct DataModel {
28 const std::vector<std::vector<int64_t>> distance_matrix{
29 {0, 548, 776, 696, 582, 274, 502, 194, 308, 194, 536, 502, 388, 354, 468,
30 776, 662},
31 {548, 0, 684, 308, 194, 502, 730, 354, 696, 742, 1084, 594, 480, 674,
32 1016, 868, 1210},
33 {776, 684, 0, 992, 878, 502, 274, 810, 468, 742, 400, 1278, 1164, 1130,
34 788, 1552, 754},
35 {696, 308, 992, 0, 114, 650, 878, 502, 844, 890, 1232, 514, 628, 822,
36 1164, 560, 1358},
37 {582, 194, 878, 114, 0, 536, 764, 388, 730, 776, 1118, 400, 514, 708,
38 1050, 674, 1244},
39 {274, 502, 502, 650, 536, 0, 228, 308, 194, 240, 582, 776, 662, 628, 514,
40 1050, 708},
41 {502, 730, 274, 878, 764, 228, 0, 536, 194, 468, 354, 1004, 890, 856, 514,
42 1278, 480},
43 {194, 354, 810, 502, 388, 308, 536, 0, 342, 388, 730, 468, 354, 320, 662,
44 742, 856},
45 {308, 696, 468, 844, 730, 194, 194, 342, 0, 274, 388, 810, 696, 662, 320,
46 1084, 514},
47 {194, 742, 742, 890, 776, 240, 468, 388, 274, 0, 342, 536, 422, 388, 274,
48 810, 468},
49 {536, 1084, 400, 1232, 1118, 582, 354, 730, 388, 342, 0, 878, 764, 730,
50 388, 1152, 354},
51 {502, 594, 1278, 514, 400, 776, 1004, 468, 810, 536, 878, 0, 114, 308,
52 650, 274, 844},
53 {388, 480, 1164, 628, 514, 662, 890, 354, 696, 422, 764, 114, 0, 194, 536,
54 388, 730},
55 {354, 674, 1130, 822, 708, 628, 856, 320, 662, 388, 730, 308, 194, 0, 342,
56 422, 536},
57 {468, 1016, 788, 1164, 1050, 514, 514, 662, 320, 274, 388, 650, 536, 342,
58 0, 764, 194},
59 {776, 868, 1552, 560, 674, 1050, 1278, 742, 1084, 810, 1152, 274, 388,
60 422, 764, 0, 798},
61 {662, 1210, 754, 1358, 1244, 708, 480, 856, 514, 468, 354, 844, 730, 536,
62 194, 798, 0},
63 };
64 const int num_vehicles = 4;
65 const RoutingIndexManager::NodeIndex depot{0};
66 };
67 // [END data_model]
68
69 // [START solution_printer]
70 //! @brief Print the solution.
71 //! @param[in] data Data of the problem.
72 //! @param[in] manager Index manager used.
73 //! @param[in] routing Routing solver used.
74 //! @param[in] solution Solution found by the solver.
PrintSolution(const DataModel & data,const RoutingIndexManager & manager,const RoutingModel & routing,const Assignment & solution)75 void PrintSolution(const DataModel& data, const RoutingIndexManager& manager,
76 const RoutingModel& routing, const Assignment& solution) {
77 LOG(INFO) << "Objective: " << solution.ObjectiveValue();
78 int64_t total_distance{0};
79 for (int vehicle_id = 0; vehicle_id < data.num_vehicles; ++vehicle_id) {
80 int64_t index = routing.Start(vehicle_id);
81 LOG(INFO) << "Route for Vehicle " << vehicle_id << ":";
82 int64_t distance{0};
83 std::stringstream route;
84 while (routing.IsEnd(index) == false) {
85 route << manager.IndexToNode(index).value() << " -> ";
86 int64_t previous_index = index;
87 index = solution.Value(routing.NextVar(index));
88 distance += routing.GetArcCostForVehicle(previous_index, index,
89 int64_t{vehicle_id});
90 }
91 LOG(INFO) << route.str() << manager.IndexToNode(index).value();
92 LOG(INFO) << "Distance of the route: " << distance << "m";
93 total_distance += distance;
94 }
95 LOG(INFO) << "Total distance of all routes: " << total_distance << "m";
96 LOG(INFO) << "";
97 LOG(INFO) << "Advanced usage:";
98 LOG(INFO) << "Problem solved in " << routing.solver()->wall_time() << "ms";
99 }
100 // [END solution_printer]
101
Vrp()102 void Vrp() {
103 // Instantiate the data problem.
104 // [START data]
105 DataModel data;
106 // [END data]
107
108 // Create Routing Index Manager
109 // [START index_manager]
110 RoutingIndexManager manager(data.distance_matrix.size(), data.num_vehicles,
111 data.depot);
112 // [END index_manager]
113
114 // Create Routing Model.
115 // [START routing_model]
116 RoutingModel routing(manager);
117 // [END routing_model]
118
119 // Create and register a transit callback.
120 // [START transit_callback]
121 const int transit_callback_index = routing.RegisterTransitCallback(
122 [&data, &manager](int64_t from_index, int64_t to_index) -> int64_t {
123 // Convert from routing variable Index to distance matrix NodeIndex.
124 auto from_node = manager.IndexToNode(from_index).value();
125 auto to_node = manager.IndexToNode(to_index).value();
126 return data.distance_matrix[from_node][to_node];
127 });
128 // [END transit_callback]
129
130 // Define cost of each arc.
131 // [START arc_cost]
132 routing.SetArcCostEvaluatorOfAllVehicles(transit_callback_index);
133 // [END arc_cost]
134
135 // Setting first solution heuristic.
136 // [START parameters]
137 RoutingSearchParameters searchParameters = DefaultRoutingSearchParameters();
138 searchParameters.set_first_solution_strategy(
139 FirstSolutionStrategy::PATH_CHEAPEST_ARC);
140 // [END parameters]
141
142 // Solve the problem.
143 // [START solve]
144 const Assignment* solution = routing.SolveWithParameters(searchParameters);
145 // [END solve]
146
147 // Print solution on console.
148 // [START print_solution]
149 PrintSolution(data, manager, routing, *solution);
150 // [END print_solution]
151 }
152 } // namespace operations_research
153
main(int argc,char ** argv)154 int main(int argc, char** argv) {
155 operations_research::Vrp();
156 return EXIT_SUCCESS;
157 }
158 // [END program]
159