Advertisement
Guest User

Untitled

a guest
Apr 20th, 2019
224
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.43 KB | None | 0 0
  1. from __future__ import print_function
  2. from ortools.constraint_solver import routing_enums_pb2
  3. from ortools.constraint_solver import pywrapcp
  4.  
  5.  
  6. def create_data_model():
  7. """Stores the data for the problem."""
  8. data = {}
  9. data['distance_matrix'] = [
  10. [
  11. 0, 548, 776, 696, 582, 274, 502, 194, 308, 194, 536, 502, 388, 354,
  12. 468, 776, 662
  13. ],
  14. [
  15. 548, 0, 684, 308, 194, 502, 730, 354, 696, 742, 1084, 594, 480, 674,
  16. 1016, 868, 1210
  17. ],
  18. [
  19. 776, 684, 0, 992, 878, 502, 274, 810, 468, 742, 400, 1278, 1164,
  20. 1130, 788, 1552, 754
  21. ],
  22. [
  23. 696, 308, 992, 0, 114, 650, 878, 502, 844, 890, 1232, 514, 628, 822,
  24. 1164, 560, 1358
  25. ],
  26. [
  27. 582, 194, 878, 114, 0, 536, 764, 388, 730, 776, 1118, 400, 514, 708,
  28. 1050, 674, 1244
  29. ],
  30. [
  31. 274, 502, 502, 650, 536, 0, 228, 308, 194, 240, 582, 776, 662, 628,
  32. 514, 1050, 708
  33. ],
  34. [
  35. 502, 730, 274, 878, 764, 228, 0, 536, 194, 468, 354, 1004, 890, 856,
  36. 514, 1278, 480
  37. ],
  38. [
  39. 194, 354, 810, 502, 388, 308, 536, 0, 342, 388, 730, 468, 354, 320,
  40. 662, 742, 856
  41. ],
  42. [
  43. 308, 696, 468, 844, 730, 194, 194, 342, 0, 274, 388, 810, 696, 662,
  44. 320, 1084, 514
  45. ],
  46. [
  47. 194, 742, 742, 890, 776, 240, 468, 388, 274, 0, 342, 536, 422, 388,
  48. 274, 810, 468
  49. ],
  50. [
  51. 536, 1084, 400, 1232, 1118, 582, 354, 730, 388, 342, 0, 878, 764,
  52. 730, 388, 1152, 354
  53. ],
  54. [
  55. 502, 594, 1278, 514, 400, 776, 1004, 468, 810, 536, 878, 0, 114,
  56. 308, 650, 274, 844
  57. ],
  58. [
  59. 388, 480, 1164, 628, 514, 662, 890, 354, 696, 422, 764, 114, 0, 194,
  60. 536, 388, 730
  61. ],
  62. [
  63. 354, 674, 1130, 822, 708, 628, 856, 320, 662, 388, 730, 308, 194, 0,
  64. 342, 422, 536
  65. ],
  66. [
  67. 468, 1016, 788, 1164, 1050, 514, 514, 662, 320, 274, 388, 650, 536,
  68. 342, 0, 764, 194
  69. ],
  70. [
  71. 776, 868, 1552, 560, 674, 1050, 1278, 742, 1084, 810, 1152, 274,
  72. 388, 422, 764, 0, 798
  73. ],
  74. [
  75. 662, 1210, 754, 1358, 1244, 708, 480, 856, 514, 468, 354, 844, 730,
  76. 536, 194, 798, 0
  77. ],
  78. ]
  79. data['demands'] = [0, 1, 1, 2, 4, 2, 4, 8, 8, 1, 2, 1, 2, 4, 4, 8, 8]
  80. data['pickups_deliveries'] = [
  81. [1, 6],
  82. [2, 10],
  83. [4, 3],
  84. [5, 9],
  85. [7, 8],
  86. [15, 11],
  87. [13, 12],
  88. [16, 14],
  89. ]
  90. data['vehicle_capacities'] = [15, 15, 15, 15]
  91. data['num_vehicles'] = 4
  92. data['depot'] = 0
  93. return data
  94.  
  95.  
  96. def print_solution(data, manager, routing, assignment):
  97. """Prints assignment on console."""
  98. total_distance = 0
  99. for vehicle_id in range(data['num_vehicles']):
  100. index = routing.Start(vehicle_id)
  101. plan_output = 'Route for vehicle {}:\n'.format(vehicle_id)
  102. route_distance = 0
  103. while not routing.IsEnd(index):
  104. plan_output += ' {} -> '.format(manager.IndexToNode(index))
  105. previous_index = index
  106. index = assignment.Value(routing.NextVar(index))
  107. route_distance += routing.GetArcCostForVehicle(
  108. previous_index, index, vehicle_id)
  109. plan_output += '{}\n'.format(manager.IndexToNode(index))
  110. plan_output += 'Distance of the route: {}m\n'.format(route_distance)
  111. print(plan_output)
  112. total_distance += route_distance
  113. print('Total Distance of all routes: {}m'.format(total_distance))
  114.  
  115.  
  116. def main():
  117. # Instantiate the data problem.
  118. data = create_data_model()
  119.  
  120. # Create the routing index manager.
  121. manager = pywrapcp.RoutingIndexManager(len(data['distance_matrix']), data['num_vehicles'], data['depot'])
  122.  
  123. # Create Routing Model.
  124. routing = pywrapcp.RoutingModel(manager)
  125.  
  126. # Create and register a transit callback.
  127. def distance_callback(from_index, to_index):
  128. """Returns the distance between the two nodes."""
  129. # Convert from routing variable Index to distance matrix NodeIndex.
  130. from_node = manager.IndexToNode(from_index)
  131. to_node = manager.IndexToNode(to_index)
  132. return data['distance_matrix'][from_node][to_node]
  133.  
  134. transit_callback_index = routing.RegisterTransitCallback(distance_callback)
  135. routing.SetArcCostEvaluatorOfAllVehicles(transit_callback_index)
  136.  
  137. # Add Distance constraint.
  138. dimension_name = 'Distance'
  139. routing.AddDimension(
  140. transit_callback_index,
  141. 0, # no slack
  142. 3000, # vehicle maximum travel distance
  143. True, # start cumul to zero
  144. dimension_name)
  145. distance_dimension = routing.GetDimensionOrDie(dimension_name)
  146. distance_dimension.SetGlobalSpanCostCoefficient(100)
  147.  
  148. # Add Capacity constraint.
  149. def demand_callback(from_index):
  150. """Returns the demand of the node."""
  151. # Convert from routing variable Index to demands NodeIndex.
  152. from_node = manager.IndexToNode(from_index)
  153. return data['demands'][from_node]
  154.  
  155. demand_callback_index = routing.RegisterUnaryTransitCallback(demand_callback)
  156. routing.AddDimensionWithVehicleCapacity(
  157. demand_callback_index,
  158. 0, # null capacity slack
  159. data['vehicle_capacities'], # vehicle maximum capacities
  160. True, # start cumul to zero
  161. 'Capacity')
  162.  
  163. # Define Transportation Requests.
  164. for request in data['pickups_deliveries']:
  165. pickup_index = manager.NodeToIndex(request[0])
  166. delivery_index = manager.NodeToIndex(request[1])
  167. routing.AddPickupAndDelivery(pickup_index, delivery_index)
  168. routing.solver().Add(routing.VehicleVar(pickup_index) == routing.VehicleVar(delivery_index))
  169. routing.solver().Add(distance_dimension.CumulVar(pickup_index) <= distance_dimension.CumulVar(delivery_index))
  170.  
  171. # Setting first solution heuristic.
  172. search_parameters = pywrapcp.DefaultRoutingSearchParameters()
  173. search_parameters.first_solution_strategy = (routing_enums_pb2.FirstSolutionStrategy.PATH_CHEAPEST_ARC)
  174. search_parameters.solution_limit = 1
  175. # search_parameters.time_limit.seconds = 300
  176.  
  177. # Solve the problem.
  178. assignment = routing.SolveWithParameters(search_parameters)
  179.  
  180. # Print solution on console.
  181. if assignment:
  182. print_solution(data, manager, routing, assignment)
  183.  
  184.  
  185. if __name__ == '__main__':
  186. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement