Google OR-Tools: vrp_initial_routes.cc

Simple VRP example.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16#include <algorithm>

17#include <cstdint>

18#include <cstdlib>

19#include <sstream>

20#include <vector>

21

22#include "google/protobuf/duration.pb.h"

29

30

32

33struct DataModel {

34 const std::vector<std::vector<int64_t>> distance_matrix{

35 {0, 548, 776, 696, 582, 274, 502, 194, 308, 194, 536, 502, 388, 354, 468,

36 776, 662},

37 {548, 0, 684, 308, 194, 502, 730, 354, 696, 742, 1084, 594, 480, 674,

38 1016, 868, 1210},

39 {776, 684, 0, 992, 878, 502, 274, 810, 468, 742, 400, 1278, 1164, 1130,

40 788, 1552, 754},

41 {696, 308, 992, 0, 114, 650, 878, 502, 844, 890, 1232, 514, 628, 822,

42 1164, 560, 1358},

43 {582, 194, 878, 114, 0, 536, 764, 388, 730, 776, 1118, 400, 514, 708,

44 1050, 674, 1244},

45 {274, 502, 502, 650, 536, 0, 228, 308, 194, 240, 582, 776, 662, 628, 514,

46 1050, 708},

47 {502, 730, 274, 878, 764, 228, 0, 536, 194, 468, 354, 1004, 890, 856, 514,

48 1278, 480},

49 {194, 354, 810, 502, 388, 308, 536, 0, 342, 388, 730, 468, 354, 320, 662,

50 742, 856},

51 {308, 696, 468, 844, 730, 194, 194, 342, 0, 274, 388, 810, 696, 662, 320,

52 1084, 514},

53 {194, 742, 742, 890, 776, 240, 468, 388, 274, 0, 342, 536, 422, 388, 274,

54 810, 468},

55 {536, 1084, 400, 1232, 1118, 582, 354, 730, 388, 342, 0, 878, 764, 730,

56 388, 1152, 354},

57 {502, 594, 1278, 514, 400, 776, 1004, 468, 810, 536, 878, 0, 114, 308,

58 650, 274, 844},

59 {388, 480, 1164, 628, 514, 662, 890, 354, 696, 422, 764, 114, 0, 194, 536,

60 388, 730},

61 {354, 674, 1130, 822, 708, 628, 856, 320, 662, 388, 730, 308, 194, 0, 342,

62 422, 536},

63 {468, 1016, 788, 1164, 1050, 514, 514, 662, 320, 274, 388, 650, 536, 342,

64 0, 764, 194},

65 {776, 868, 1552, 560, 674, 1050, 1278, 742, 1084, 810, 1152, 274, 388,

66 422, 764, 0, 798},

67 {662, 1210, 754, 1358, 1244, 708, 480, 856, 514, 468, 354, 844, 730, 536,

68 194, 798, 0},

69 };

70

71 const std::vector<std::vector<int64_t>> initial_routes{

72 {8, 16, 14, 13, 12, 11},

73 {3, 4, 9, 10},

74 {15, 1},

75 {7, 5, 2, 6},

76 };

77

78 const int num_vehicles = 4;

79 const RoutingIndexManager::NodeIndex depot{0};

80};

81

82

88

89void PrintSolution(const DataModel& data, const RoutingIndexManager& manager,

90 const RoutingModel& routing, const Assignment& solution) {

91 LOG(INFO) << "Objective: " << solution.ObjectiveValue();

92 int64_t max_route_distance{0};

93 for (int vehicle_id = 0; vehicle_id < data.num_vehicles; ++vehicle_id) {

94 if (!routing.IsVehicleUsed(solution, vehicle_id)) {

95 continue;

96 }

97 int64_t index = routing.Start(vehicle_id);

98 LOG(INFO) << "Route for Vehicle " << vehicle_id << ":";

99 int64_t route_distance{0};

100 std::stringstream route;

101 while (!routing.IsEnd(index)) {

102 route << manager.IndexToNode(index).value() << " -> ";

103 const int64_t previous_index = index;

104 index = solution.Value(routing.NextVar(index));

105 route_distance += routing.GetArcCostForVehicle(previous_index, index,

106 int64_t{vehicle_id});

107 }

108 LOG(INFO) << route.str() << manager.IndexToNode(index).value();

109 LOG(INFO) << "Distance of the route: " << route_distance << "m";

110 max_route_distance = std::max(route_distance, max_route_distance);

111 }

112 LOG(INFO) << "Maximum of the route distances: " << max_route_distance << "m";

113 LOG(INFO) << "";

114 LOG(INFO) << "Problem solved in " << routing.solver()->wall_time() << "ms";

115}

116

117

118void VrpInitialRoutes() {

119

120

121 DataModel data;

122

123

124

125

126 RoutingIndexManager manager(data.distance_matrix.size(), data.num_vehicles,

127 data.depot);

128

129

130

131

132 RoutingModel routing(manager);

133

134

135

136

137 const int transit_callback_index = routing.RegisterTransitCallback(

138 [&data, &manager](const int64_t from_index,

139 const int64_t to_index) -> int64_t {

140

141 const int from_node = manager.IndexToNode(from_index).value();

142 const int to_node = manager.IndexToNode(to_index).value();

143 return data.distance_matrix[from_node][to_node];

144 });

145

146

147

148

149 routing.SetArcCostEvaluatorOfAllVehicles(transit_callback_index);

150

151

152

153

154 routing.AddDimension(transit_callback_index, 0, 3000,

155 true,

156 "Distance");

157 routing.GetMutableDimension("Distance")->SetGlobalSpanCostCoefficient(100);

158

159

160

161

163 searchParameters.set_first_solution_strategy(

165 searchParameters.set_local_search_metaheuristic(

167 searchParameters.mutable_time_limit()->set_seconds(5);

168

169

170

171 routing.CloseModelWithParameters(searchParameters);

172

173

174

175

176 const Assignment* initial_solution =

177 routing.ReadAssignmentFromRoutes(data.initial_routes, true);

178

179 LOG(INFO) << "Initial solution: ";

180 PrintSolution(data, manager, routing, *initial_solution);

181

182

183

184

185 const Assignment* solution = routing.SolveFromAssignmentWithParameters(

186 initial_solution, searchParameters);

187

188

189

190

191 LOG(INFO) << "";

192 LOG(INFO) << "Solution from search: ";

193 PrintSolution(data, manager, routing, *solution);

194

195}

196}

197

198int main(int , char* []) {

199 operations_research::VrpInitialRoutes();

200 return EXIT_SUCCESS;

201}

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...