Simple TSP example.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16#include <cstdint>
17#include <cstdlib>
18#include <sstream>
19#include <vector>
20
27
28
30
31struct DataModel {
32 const std::vector<std::vector<int>> 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 const int num_vehicles = 1;
69 const RoutingIndexManager::NodeIndex depot{0};
70};
71
72
73
78void PrintSolution(const RoutingIndexManager& manager,
79 const RoutingModel& routing,
const Assignment&
solution) {
80 LOG(INFO) <<
"Objective: " <<
solution.ObjectiveValue();
81
82 int64_t index = routing.Start(0);
83 LOG(INFO) << "Route for Vehicle 0:";
84 int64_t distance{0};
85 std::stringstream route;
86 while (!routing.IsEnd(index)) {
87 route << manager.IndexToNode(index).value() << " -> ";
88 const int64_t previous_index = index;
89 index =
solution.Value(routing.NextVar(index));
90 distance += routing.GetArcCostForVehicle(previous_index, index, int64_t{0});
91 }
92 LOG(INFO) << route.str() << manager.IndexToNode(index).value();
93 LOG(INFO) << "Distance of the route: " << distance << "m";
94 LOG(INFO) << "";
95 LOG(INFO) << "Advanced usage:";
96 LOG(INFO) << "Problem solved in " << routing.solver()->wall_time() << "ms";
97}
98
99
100void Tsp() {
101
102
103 DataModel data;
104
105
106
107
108 RoutingIndexManager manager(data.distance_matrix.size(), data.num_vehicles,
109 data.depot);
110
111
112
113
114 RoutingModel routing(manager);
115
116
117
118
119 const int transit_callback_index = routing.RegisterTransitCallback(
120 [&data, &manager](const int64_t from_index,
121 const int64_t to_index) -> int64_t {
122
123 const int from_node = manager.IndexToNode(from_index).value();
124 const int to_node = manager.IndexToNode(to_index).value();
125 return data.distance_matrix[from_node][to_node];
126 });
127
128
129
130
131 routing.SetArcCostEvaluatorOfAllVehicles(transit_callback_index);
132
133
134
135
137 searchParameters.set_first_solution_strategy(
139
140
141
142
143 const Assignment*
solution = routing.SolveWithParameters(searchParameters);
144
145
146
147
148 PrintSolution(manager, routing, *
solution);
149
150}
151
152}
153
154int main(
int ,
char* []) {
155 operations_research::Tsp();
156 return EXIT_SUCCESS;
157}
158
static constexpr Value PATH_CHEAPEST_ARC
int main(int argc, char **argv)
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...