update notebooks
This commit is contained in:
@@ -80,7 +80,6 @@
|
||||
" http://en.wikipedia.org/wiki/Vehicle_routing_problem.\n",
|
||||
"\n",
|
||||
" Distances are in meters.\n",
|
||||
"\n",
|
||||
"\n"
|
||||
]
|
||||
},
|
||||
@@ -141,8 +140,8 @@
|
||||
"#######################\n",
|
||||
"def manhattan_distance(position_1, position_2):\n",
|
||||
" \"\"\"Computes the Manhattan distance between two points\"\"\"\n",
|
||||
" return (\n",
|
||||
" abs(position_1[0] - position_2[0]) + abs(position_1[1] - position_2[1]))\n",
|
||||
" return (abs(position_1[0] - position_2[0]) +\n",
|
||||
" abs(position_1[1] - position_2[1]))\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"def create_distance_evaluator(data):\n",
|
||||
@@ -193,33 +192,31 @@
|
||||
"###########\n",
|
||||
"def print_solution(data, routing, manager, assignment): # pylint:disable=too-many-locals\n",
|
||||
" \"\"\"Prints assignment on console\"\"\"\n",
|
||||
" print('Objective: {}'.format(assignment.ObjectiveValue()))\n",
|
||||
" print(f'Objective: {assignment.ObjectiveValue()}')\n",
|
||||
" total_distance = 0\n",
|
||||
" total_load = 0\n",
|
||||
" capacity_dimension = routing.GetDimensionOrDie('Capacity')\n",
|
||||
" for vehicle_id in range(data['num_vehicles']):\n",
|
||||
" index = routing.Start(vehicle_id)\n",
|
||||
" plan_output = 'Route for vehicle {}:\\n'.format(vehicle_id)\n",
|
||||
" plan_output = f'Route for vehicle {vehicle_id}:\\n'\n",
|
||||
" distance = 0\n",
|
||||
" while not routing.IsEnd(index):\n",
|
||||
" load_var = capacity_dimension.CumulVar(index)\n",
|
||||
" plan_output += ' {} Load({}) -> '.format(\n",
|
||||
" manager.IndexToNode(index), assignment.Value(load_var))\n",
|
||||
" plan_output += (f' {manager.IndexToNode(index)} '\n",
|
||||
" f'Load({assignment.Value(load_var)}) -> ')\n",
|
||||
" previous_index = index\n",
|
||||
" index = assignment.Value(routing.NextVar(index))\n",
|
||||
" distance += routing.GetArcCostForVehicle(previous_index, index,\n",
|
||||
" vehicle_id)\n",
|
||||
" load_var = capacity_dimension.CumulVar(index)\n",
|
||||
" plan_output += ' {0} Load({1})\\n'.format(\n",
|
||||
" manager.IndexToNode(index), assignment.Value(load_var))\n",
|
||||
" plan_output += 'Distance of the route: {}m\\n'.format(distance)\n",
|
||||
" plan_output += 'Load of the route: {}\\n'.format(\n",
|
||||
" assignment.Value(load_var))\n",
|
||||
" plan_output += f' {manager.IndexToNode(index)} Load({assignment.Value(load_var)})\\n'\n",
|
||||
" plan_output += f'Distance of the route: {distance}m\\n'\n",
|
||||
" plan_output += f'Load of the route: {assignment.Value(load_var)}\\n'\n",
|
||||
" print(plan_output)\n",
|
||||
" total_distance += distance\n",
|
||||
" total_load += assignment.Value(load_var)\n",
|
||||
" print('Total Distance of all routes: {}m'.format(total_distance))\n",
|
||||
" print('Total Load of all routes: {}'.format(total_load))\n",
|
||||
" print(f'Total Distance of all routes: {total_distance}m')\n",
|
||||
" print(f'Total Load of all routes: {total_load}')\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"########\n",
|
||||
|
||||
@@ -99,7 +99,6 @@
|
||||
" new nodes introduced, to avoid schedules having spurious transits through\n",
|
||||
" those new nodes unless it's necessary to reload. This consideration is taken\n",
|
||||
" into account in `create_distance_evaluator()`.\n",
|
||||
"\n",
|
||||
"\n"
|
||||
]
|
||||
},
|
||||
@@ -230,6 +229,7 @@
|
||||
"\n",
|
||||
"def add_distance_dimension(routing, manager, data, distance_evaluator_index):\n",
|
||||
" \"\"\"Add Global Span constraint\"\"\"\n",
|
||||
" del manager\n",
|
||||
" distance = 'Distance'\n",
|
||||
" routing.AddDimension(\n",
|
||||
" distance_evaluator_index,\n",
|
||||
@@ -294,7 +294,8 @@
|
||||
" travel_time = 0\n",
|
||||
" else:\n",
|
||||
" travel_time = manhattan_distance(\n",
|
||||
" data['locations'][from_node], data['locations'][to_node]) / data['vehicle_speed']\n",
|
||||
" data['locations'][from_node],\n",
|
||||
" data['locations'][to_node]) / data['vehicle_speed']\n",
|
||||
" return travel_time\n",
|
||||
"\n",
|
||||
" _total_time = {}\n",
|
||||
@@ -306,8 +307,8 @@
|
||||
" _total_time[from_node][to_node] = 0\n",
|
||||
" else:\n",
|
||||
" _total_time[from_node][to_node] = int(\n",
|
||||
" service_time(data, from_node) + travel_time(\n",
|
||||
" data, from_node, to_node))\n",
|
||||
" service_time(data, from_node) +\n",
|
||||
" travel_time(data, from_node, to_node))\n",
|
||||
"\n",
|
||||
" def time_evaluator(manager, from_node, to_node):\n",
|
||||
" \"\"\"Returns the total time between the two nodes\"\"\"\n",
|
||||
@@ -377,20 +378,21 @@
|
||||
" while not routing.IsEnd(index):\n",
|
||||
" load_var = capacity_dimension.CumulVar(index)\n",
|
||||
" time_var = time_dimension.CumulVar(index)\n",
|
||||
" plan_output += ' {0} Load({1}) Time({2},{3}) ->'.format(\n",
|
||||
" manager.IndexToNode(index),\n",
|
||||
" assignment.Value(load_var),\n",
|
||||
" assignment.Min(time_var), assignment.Max(time_var))\n",
|
||||
" plan_output += (\n",
|
||||
" f' {manager.IndexToNode(index)} '\n",
|
||||
" f'Load({assignment.Value(load_var)}) '\n",
|
||||
" f'Time({assignment.Min(time_var)},{assignment.Max(time_var)}) ->'\n",
|
||||
" )\n",
|
||||
" previous_index = index\n",
|
||||
" index = assignment.Value(routing.NextVar(index))\n",
|
||||
" distance += routing.GetArcCostForVehicle(previous_index, index,\n",
|
||||
" vehicle_id)\n",
|
||||
" load_var = capacity_dimension.CumulVar(index)\n",
|
||||
" time_var = time_dimension.CumulVar(index)\n",
|
||||
" plan_output += ' {0} Load({1}) Time({2},{3})\\n'.format(\n",
|
||||
" manager.IndexToNode(index),\n",
|
||||
" assignment.Value(load_var),\n",
|
||||
" assignment.Min(time_var), assignment.Max(time_var))\n",
|
||||
" plan_output += (\n",
|
||||
" f' {manager.IndexToNode(index)} '\n",
|
||||
" f'Load({assignment.Value(load_var)}) '\n",
|
||||
" f'Time({assignment.Min(time_var)},{assignment.Max(time_var)})\\n')\n",
|
||||
" plan_output += f'Distance of the route: {distance}m\\n'\n",
|
||||
" plan_output += f'Load of the route: {assignment.Value(load_var)}\\n'\n",
|
||||
" plan_output += f'Time of the route: {assignment.Value(time_var)}min\\n'\n",
|
||||
@@ -398,9 +400,9 @@
|
||||
" total_distance += distance\n",
|
||||
" total_load += assignment.Value(load_var)\n",
|
||||
" total_time += assignment.Value(time_var)\n",
|
||||
" print('Total Distance of all routes: {}m'.format(total_distance))\n",
|
||||
" print('Total Load of all routes: {}'.format(total_load))\n",
|
||||
" print('Total Time of all routes: {}min'.format(total_time))\n",
|
||||
" print(f'Total Distance of all routes: {total_distance}m')\n",
|
||||
" print(f'Total Load of all routes: {total_load}')\n",
|
||||
" print(f'Total Time of all routes: {total_time}min')\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"########\n",
|
||||
|
||||
@@ -148,8 +148,8 @@
|
||||
"#######################\n",
|
||||
"def manhattan_distance(position_1, position_2):\n",
|
||||
" \"\"\"Computes the Manhattan distance between two points\"\"\"\n",
|
||||
" return (\n",
|
||||
" abs(position_1[0] - position_2[0]) + abs(position_1[1] - position_2[1]))\n",
|
||||
" return (abs(position_1[0] - position_2[0]) +\n",
|
||||
" abs(position_1[1] - position_2[1]))\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"def create_distance_evaluator(data):\n",
|
||||
@@ -207,8 +207,9 @@
|
||||
" if from_node == to_node:\n",
|
||||
" travel_time = 0\n",
|
||||
" else:\n",
|
||||
" travel_time = manhattan_distance(data['locations'][from_node], data[\n",
|
||||
" 'locations'][to_node]) / data['vehicle_speed']\n",
|
||||
" travel_time = manhattan_distance(\n",
|
||||
" data['locations'][from_node],\n",
|
||||
" data['locations'][to_node]) / data['vehicle_speed']\n",
|
||||
" return travel_time\n",
|
||||
"\n",
|
||||
" _total_time = {}\n",
|
||||
@@ -220,8 +221,8 @@
|
||||
" _total_time[from_node][to_node] = 0\n",
|
||||
" else:\n",
|
||||
" _total_time[from_node][to_node] = int(\n",
|
||||
" service_time(data, from_node) + travel_time(\n",
|
||||
" data, from_node, to_node))\n",
|
||||
" service_time(data, from_node) +\n",
|
||||
" travel_time(data, from_node, to_node))\n",
|
||||
"\n",
|
||||
" def time_evaluator(manager, from_node, to_node):\n",
|
||||
" \"\"\"Returns the total time between the two nodes\"\"\"\n",
|
||||
@@ -271,18 +272,18 @@
|
||||
" total_time = 0\n",
|
||||
" for vehicle_id in range(manager.GetNumberOfVehicles()):\n",
|
||||
" index = routing.Start(vehicle_id)\n",
|
||||
" plan_output = 'Route for vehicle {}:\\n'.format(vehicle_id)\n",
|
||||
" plan_output = f'Route for vehicle {vehicle_id}:\\n'\n",
|
||||
" distance = 0\n",
|
||||
" while not routing.IsEnd(index):\n",
|
||||
" load_var = capacity_dimension.CumulVar(index)\n",
|
||||
" time_var = time_dimension.CumulVar(index)\n",
|
||||
" slack_var = time_dimension.SlackVar(index)\n",
|
||||
" plan_output += ' {0} Load({1}) Time({2},{3}) Slack({4},{5}) ->'.format(\n",
|
||||
" manager.IndexToNode(index),\n",
|
||||
" assignment.Value(load_var),\n",
|
||||
" assignment.Min(time_var),\n",
|
||||
" assignment.Max(time_var),\n",
|
||||
" assignment.Min(slack_var), assignment.Max(slack_var))\n",
|
||||
" plan_output += (\n",
|
||||
" f' {manager.IndexToNode(index)} '\n",
|
||||
" f'Load({assignment.Value(load_var)}) '\n",
|
||||
" f'Time({assignment.Min(time_var)},{assignment.Max(time_var)}) '\n",
|
||||
" f'Slack({assignment.Min(slack_var)},{assignment.Max(slack_var)}) ->'\n",
|
||||
" )\n",
|
||||
" previous_index = index\n",
|
||||
" index = assignment.Value(routing.NextVar(index))\n",
|
||||
" distance += routing.GetArcCostForVehicle(previous_index, index,\n",
|
||||
@@ -290,22 +291,20 @@
|
||||
" load_var = capacity_dimension.CumulVar(index)\n",
|
||||
" time_var = time_dimension.CumulVar(index)\n",
|
||||
" slack_var = time_dimension.SlackVar(index)\n",
|
||||
" plan_output += ' {0} Load({1}) Time({2},{3})\\n'.format(\n",
|
||||
" manager.IndexToNode(index),\n",
|
||||
" assignment.Value(load_var),\n",
|
||||
" assignment.Min(time_var), assignment.Max(time_var))\n",
|
||||
" plan_output += 'Distance of the route: {0}m\\n'.format(distance)\n",
|
||||
" plan_output += 'Load of the route: {}\\n'.format(\n",
|
||||
" assignment.Value(load_var))\n",
|
||||
" plan_output += 'Time of the route: {}\\n'.format(\n",
|
||||
" assignment.Value(time_var))\n",
|
||||
" plan_output += (\n",
|
||||
" f' {manager.IndexToNode(index)} '\n",
|
||||
" f'Load({assignment.Value(load_var)}) '\n",
|
||||
" f'Time({assignment.Min(time_var)},{assignment.Max(time_var)})\\n')\n",
|
||||
" plan_output += f'Distance of the route: {distance}m\\n'\n",
|
||||
" plan_output += f'Load of the route: {assignment.Value(load_var)}\\n'\n",
|
||||
" plan_output += f'Time of the route: {assignment.Value(time_var)}\\n'\n",
|
||||
" print(plan_output)\n",
|
||||
" total_distance += distance\n",
|
||||
" total_load += assignment.Value(load_var)\n",
|
||||
" total_time += assignment.Value(time_var)\n",
|
||||
" print('Total Distance of all routes: {0}m'.format(total_distance))\n",
|
||||
" print('Total Load of all routes: {}'.format(total_load))\n",
|
||||
" print('Total Time of all routes: {0}min'.format(total_time))\n",
|
||||
" print(f'Total Distance of all routes: {total_distance}m')\n",
|
||||
" print(f'Total Load of all routes: {total_load}')\n",
|
||||
" print(f'Total Time of all routes: {total_time}min')\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"def main():\n",
|
||||
|
||||
@@ -265,18 +265,17 @@
|
||||
"\n",
|
||||
"def print_solution(data, manager, routing, assignment): # pylint:disable=too-many-locals\n",
|
||||
" \"\"\"Prints assignment on console.\"\"\"\n",
|
||||
" print('Objective: {}'.format(assignment.ObjectiveValue()))\n",
|
||||
" print(f'Objective: {assignment.ObjectiveValue()}')\n",
|
||||
"\n",
|
||||
" print('Breaks:')\n",
|
||||
" intervals = assignment.IntervalVarContainer()\n",
|
||||
" for i in range(intervals.Size()):\n",
|
||||
" brk = intervals.Element(i)\n",
|
||||
" if brk.PerformedValue() == 1:\n",
|
||||
" print('{}: Start({}) Duration({})'.format(brk.Var().Name(),\n",
|
||||
" brk.StartValue(),\n",
|
||||
" brk.DurationValue()))\n",
|
||||
" print(f'{brk.Var().Name()}:'\n",
|
||||
" f' Start({brk.StartValue()}) Duration({brk.DurationValue()})')\n",
|
||||
" else:\n",
|
||||
" print('{}: Unperformed'.format(brk.Var().Name()))\n",
|
||||
" print(f'{brk.Var().Name()}: Unperformed')\n",
|
||||
"\n",
|
||||
" total_distance = 0\n",
|
||||
" total_load = 0\n",
|
||||
@@ -285,37 +284,40 @@
|
||||
" time_dimension = routing.GetDimensionOrDie('Time')\n",
|
||||
" for vehicle_id in range(data['num_vehicles']):\n",
|
||||
" index = routing.Start(vehicle_id)\n",
|
||||
" plan_output = 'Route for vehicle {}:\\n'.format(vehicle_id)\n",
|
||||
" plan_output = f'Route for vehicle {vehicle_id}:\\n'\n",
|
||||
" distance = 0\n",
|
||||
" while not routing.IsEnd(index):\n",
|
||||
" load_var = capacity_dimension.CumulVar(index)\n",
|
||||
" time_var = time_dimension.CumulVar(index)\n",
|
||||
" slack_var = time_dimension.SlackVar(index)\n",
|
||||
" plan_output += ' {0} Load({1}) Time({2},{3}) Slack({4},{5}) ->'.format(\n",
|
||||
" manager.IndexToNode(index), assignment.Value(load_var),\n",
|
||||
" assignment.Min(time_var), assignment.Max(time_var),\n",
|
||||
" assignment.Min(slack_var), assignment.Max(slack_var))\n",
|
||||
" node = manager.IndexToNode(index)\n",
|
||||
" plan_output += (\n",
|
||||
" f' {node}'\n",
|
||||
" f' Load({assignment.Value(load_var)})'\n",
|
||||
" f' Time({assignment.Min(time_var)}, {assignment.Max(time_var)})'\n",
|
||||
" f' Slack({assignment.Min(slack_var)}, {assignment.Max(slack_var)})'\n",
|
||||
" ' ->')\n",
|
||||
" previous_index = index\n",
|
||||
" index = assignment.Value(routing.NextVar(index))\n",
|
||||
" distance += routing.GetArcCostForVehicle(previous_index, index,\n",
|
||||
" vehicle_id)\n",
|
||||
" load_var = capacity_dimension.CumulVar(index)\n",
|
||||
" time_var = time_dimension.CumulVar(index)\n",
|
||||
" plan_output += ' {0} Load({1}) Time({2},{3})\\n'.format(\n",
|
||||
" manager.IndexToNode(index), assignment.Value(load_var),\n",
|
||||
" assignment.Min(time_var), assignment.Max(time_var))\n",
|
||||
" plan_output += 'Distance of the route: {0}m\\n'.format(distance)\n",
|
||||
" plan_output += 'Load of the route: {}\\n'.format(\n",
|
||||
" assignment.Value(load_var))\n",
|
||||
" plan_output += 'Time of the route: {}\\n'.format(\n",
|
||||
" assignment.Value(time_var))\n",
|
||||
" node = manager.IndexToNode(index)\n",
|
||||
" plan_output += (\n",
|
||||
" f' {node}'\n",
|
||||
" f' Load({assignment.Value(load_var)})'\n",
|
||||
" f' Time({assignment.Min(time_var)}, {assignment.Max(time_var)})\\n')\n",
|
||||
" plan_output += f'Distance of the route: {distance}m\\n'\n",
|
||||
" plan_output += f'Load of the route: {assignment.Value(load_var)}\\n'\n",
|
||||
" plan_output += f'Time of the route: {assignment.Value(time_var)}\\n'\n",
|
||||
" print(plan_output)\n",
|
||||
" total_distance += distance\n",
|
||||
" total_load += assignment.Value(load_var)\n",
|
||||
" total_time += assignment.Value(time_var)\n",
|
||||
" print('Total Distance of all routes: {0}m'.format(total_distance))\n",
|
||||
" print('Total Load of all routes: {}'.format(total_load))\n",
|
||||
" print('Total Time of all routes: {0}min'.format(total_time))\n",
|
||||
" print(f'Total Distance of all routes: {total_distance}m')\n",
|
||||
" print(f'Total Load of all routes: {total_load}')\n",
|
||||
" print(f'Total Time of all routes: {total_time}min')\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"def main():\n",
|
||||
|
||||
@@ -110,16 +110,16 @@
|
||||
" solver.NewSearch(decision_builder)\n",
|
||||
" while solver.NextSolution():\n",
|
||||
" count += 1\n",
|
||||
" solution = 'Solution {}:\\n'.format(count)\n",
|
||||
" solution = f'Solution {count}:\\n'\n",
|
||||
" for var in [x, y, z]:\n",
|
||||
" solution += ' {} = {}'.format(var.Name(), var.Value())\n",
|
||||
" solution += f' {var.Name()} = {var.Value()}'\n",
|
||||
" print(solution)\n",
|
||||
" solver.EndSearch()\n",
|
||||
" print('Number of solutions found: ', count)\n",
|
||||
" print(f'Number of solutions found: {count}')\n",
|
||||
"\n",
|
||||
" print('Advanced usage:')\n",
|
||||
" print('Problem solved in ', solver.WallTime(), 'ms')\n",
|
||||
" print('Memory usage: ', pywrapcp.Solver.MemoryUsage(), 'bytes')\n",
|
||||
" print(f'Problem solved in {solver.WallTime()}ms')\n",
|
||||
" print(f'Memory usage: {pywrapcp.Solver.MemoryUsage()}bytes')\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"main()\n",
|
||||
|
||||
@@ -123,17 +123,17 @@
|
||||
" assignment = routing.SolveWithParameters(search_parameters)\n",
|
||||
"\n",
|
||||
" # Print solution on console.\n",
|
||||
" print('Objective: {}'.format(assignment.ObjectiveValue()))\n",
|
||||
" print(f'Objective: {assignment.ObjectiveValue()}')\n",
|
||||
" index = routing.Start(0)\n",
|
||||
" plan_output = 'Route for vehicle 0:\\n'\n",
|
||||
" route_distance = 0\n",
|
||||
" while not routing.IsEnd(index):\n",
|
||||
" plan_output += '{} -> '.format(manager.IndexToNode(index))\n",
|
||||
" plan_output += f'{manager.IndexToNode(index)} -> '\n",
|
||||
" previous_index = index\n",
|
||||
" index = assignment.Value(routing.NextVar(index))\n",
|
||||
" route_distance += routing.GetArcCostForVehicle(previous_index, index, 0)\n",
|
||||
" plan_output += '{}\\n'.format(manager.IndexToNode(index))\n",
|
||||
" plan_output += 'Distance of the route: {}m\\n'.format(route_distance)\n",
|
||||
" plan_output += f'{manager.IndexToNode(index)}\\n'\n",
|
||||
" plan_output += f'Distance of the route: {route_distance}m\\n'\n",
|
||||
" print(plan_output)\n",
|
||||
"\n",
|
||||
"\n",
|
||||
|
||||
@@ -139,17 +139,17 @@
|
||||
"\n",
|
||||
"def print_solution(manager, routing, assignment):\n",
|
||||
" \"\"\"Prints assignment on console.\"\"\"\n",
|
||||
" print('Objective: {}'.format(assignment.ObjectiveValue()))\n",
|
||||
" print(f'Objective: {assignment.ObjectiveValue()}')\n",
|
||||
" index = routing.Start(0)\n",
|
||||
" plan_output = 'Route for vehicle 0:\\n'\n",
|
||||
" route_distance = 0\n",
|
||||
" while not routing.IsEnd(index):\n",
|
||||
" plan_output += ' {} ->'.format(manager.IndexToNode(index))\n",
|
||||
" plan_output += f' {manager.IndexToNode(index)} ->'\n",
|
||||
" previous_index = index\n",
|
||||
" index = assignment.Value(routing.NextVar(index))\n",
|
||||
" route_distance += routing.GetArcCostForVehicle(previous_index, index, 0)\n",
|
||||
" plan_output += ' {}\\n'.format(manager.IndexToNode(index))\n",
|
||||
" plan_output += 'Distance of the route: {}m\\n'.format(route_distance)\n",
|
||||
" plan_output += f' {manager.IndexToNode(index)}\\n'\n",
|
||||
" plan_output += f'Distance of the route: {route_distance}m\\n'\n",
|
||||
" print(plan_output)\n",
|
||||
"\n",
|
||||
"\n",
|
||||
|
||||
@@ -164,18 +164,18 @@
|
||||
"\n",
|
||||
"def print_solution(manager, routing, solution):\n",
|
||||
" \"\"\"Prints solution on console.\"\"\"\n",
|
||||
" print('Objective: {}'.format(solution.ObjectiveValue()))\n",
|
||||
" print(f'Objective: {solution.ObjectiveValue()}')\n",
|
||||
" index = routing.Start(0)\n",
|
||||
" plan_output = 'Route:\\n'\n",
|
||||
" route_distance = 0\n",
|
||||
" while not routing.IsEnd(index):\n",
|
||||
" plan_output += ' {} ->'.format(manager.IndexToNode(index))\n",
|
||||
" plan_output += f' {manager.IndexToNode(index)} ->'\n",
|
||||
" previous_index = index\n",
|
||||
" index = solution.Value(routing.NextVar(index))\n",
|
||||
" route_distance += routing.GetArcCostForVehicle(previous_index, index, 0)\n",
|
||||
" plan_output += ' {}\\n'.format(manager.IndexToNode(index))\n",
|
||||
" plan_output += f' {manager.IndexToNode(index)}\\n'\n",
|
||||
" print(plan_output)\n",
|
||||
" plan_output += 'Objective: {}m\\n'.format(route_distance)\n",
|
||||
" plan_output += f'Objective: {route_distance}m\\n'\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"def main():\n",
|
||||
|
||||
@@ -112,18 +112,18 @@
|
||||
"\n",
|
||||
"def print_solution(manager, routing, solution):\n",
|
||||
" \"\"\"Prints solution on console.\"\"\"\n",
|
||||
" print('Objective: {} miles'.format(solution.ObjectiveValue()))\n",
|
||||
" print(f'Objective: {solution.ObjectiveValue()} miles')\n",
|
||||
" index = routing.Start(0)\n",
|
||||
" plan_output = 'Route for vehicle 0:\\n'\n",
|
||||
" route_distance = 0\n",
|
||||
" while not routing.IsEnd(index):\n",
|
||||
" plan_output += ' {} ->'.format(manager.IndexToNode(index))\n",
|
||||
" plan_output += f' {manager.IndexToNode(index)} ->'\n",
|
||||
" previous_index = index\n",
|
||||
" index = solution.Value(routing.NextVar(index))\n",
|
||||
" route_distance += routing.GetArcCostForVehicle(previous_index, index, 0)\n",
|
||||
" plan_output += ' {}\\n'.format(manager.IndexToNode(index))\n",
|
||||
" plan_output += f' {manager.IndexToNode(index)}\\n'\n",
|
||||
" print(plan_output)\n",
|
||||
" plan_output += 'Route distance: {}miles\\n'.format(route_distance)\n",
|
||||
" plan_output += f'Route distance: {route_distance}miles\\n'\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"def main():\n",
|
||||
|
||||
@@ -167,17 +167,17 @@
|
||||
"\n",
|
||||
"def print_solution(manager, routing, solution):\n",
|
||||
" \"\"\"Prints solution on console.\"\"\"\n",
|
||||
" print('Objective: {}'.format(solution.ObjectiveValue()))\n",
|
||||
" print(f'Objective: {solution.ObjectiveValue()}')\n",
|
||||
" index = routing.Start(0)\n",
|
||||
" plan_output = 'Route for vehicle 0:\\n'\n",
|
||||
" route_distance = 0\n",
|
||||
" while not routing.IsEnd(index):\n",
|
||||
" plan_output += ' {} ->'.format(manager.IndexToNode(index))\n",
|
||||
" plan_output += f' {manager.IndexToNode(index)} ->'\n",
|
||||
" previous_index = index\n",
|
||||
" index = solution.Value(routing.NextVar(index))\n",
|
||||
" route_distance += routing.GetArcCostForVehicle(previous_index, index, 0)\n",
|
||||
" plan_output += ' {}\\n'.format(manager.IndexToNode(index))\n",
|
||||
" plan_output += 'Distance of the route: {}m\\n'.format(route_distance)\n",
|
||||
" plan_output += f' {manager.IndexToNode(index)}\\n'\n",
|
||||
" plan_output += f'Distance of the route: {route_distance}m\\n'\n",
|
||||
" print(plan_output)\n",
|
||||
"\n",
|
||||
"\n",
|
||||
|
||||
@@ -179,19 +179,19 @@
|
||||
" total_distance = 0\n",
|
||||
" for vehicle_id in range(data['num_vehicles']):\n",
|
||||
" index = routing.Start(vehicle_id)\n",
|
||||
" plan_output = 'Route for vehicle {}:\\n'.format(vehicle_id)\n",
|
||||
" plan_output = f'Route for vehicle {vehicle_id}:\\n'\n",
|
||||
" route_distance = 0\n",
|
||||
" while not routing.IsEnd(index):\n",
|
||||
" plan_output += ' {} ->'.format(manager.IndexToNode(index))\n",
|
||||
" plan_output += f' {manager.IndexToNode(index)} ->'\n",
|
||||
" previous_index = index\n",
|
||||
" index = solution.Value(routing.NextVar(index))\n",
|
||||
" route_distance += routing.GetArcCostForVehicle(\n",
|
||||
" previous_index, index, vehicle_id)\n",
|
||||
" plan_output += ' {}\\n'.format(manager.IndexToNode(index))\n",
|
||||
" plan_output += 'Distance of the route: {}m\\n'.format(route_distance)\n",
|
||||
" plan_output += f' {manager.IndexToNode(index)}\\n'\n",
|
||||
" plan_output += f'Distance of the route: {route_distance}m\\n'\n",
|
||||
" print(plan_output)\n",
|
||||
" total_distance += route_distance\n",
|
||||
" print('Total Distance of all routes: {}m'.format(total_distance))\n",
|
||||
" print(f'Total Distance of all routes: {total_distance}m')\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"\n",
|
||||
|
||||
@@ -168,10 +168,8 @@
|
||||
" data = create_data_model()\n",
|
||||
"\n",
|
||||
" # Create the routing index manager.\n",
|
||||
" manager = pywrapcp.RoutingIndexManager(\n",
|
||||
" len(data['time_matrix']),\n",
|
||||
" data['num_vehicles'],\n",
|
||||
" data['depot'])\n",
|
||||
" manager = pywrapcp.RoutingIndexManager(len(data['time_matrix']),\n",
|
||||
" data['num_vehicles'], data['depot'])\n",
|
||||
"\n",
|
||||
" # Create Routing Model.\n",
|
||||
" routing = pywrapcp.RoutingModel(manager)\n",
|
||||
@@ -216,13 +214,15 @@
|
||||
" # Add a break lasting 5 minutes, start between 25 and 45 minutes after route start\n",
|
||||
" for v in range(manager.GetNumberOfVehicles()):\n",
|
||||
" start_var = time_dimension.CumulVar(routing.Start(v))\n",
|
||||
" break_start = routing.solver().Sum([routing.solver().IntVar(25, 45), start_var])\n",
|
||||
" break_start = routing.solver().Sum(\n",
|
||||
" [routing.solver().IntVar(25, 45), start_var])\n",
|
||||
"\n",
|
||||
" break_intervals = [\n",
|
||||
" routing.solver().FixedDurationIntervalVar(\n",
|
||||
" break_start, 5, 'Break for vehicle {}'.format(v))\n",
|
||||
" routing.solver().FixedDurationIntervalVar(break_start, 5,\n",
|
||||
" f'Break for vehicle {v}')\n",
|
||||
" ]\n",
|
||||
" time_dimension.SetBreakIntervalsOfVehicle(break_intervals, v, node_visit_transit)\n",
|
||||
" time_dimension.SetBreakIntervalsOfVehicle(break_intervals, v,\n",
|
||||
" node_visit_transit)\n",
|
||||
"\n",
|
||||
" # Setting first solution heuristic.\n",
|
||||
" search_parameters = pywrapcp.DefaultRoutingSearchParameters()\n",
|
||||
|
||||
@@ -174,26 +174,25 @@
|
||||
" total_load = 0\n",
|
||||
" for vehicle_id in range(data['num_vehicles']):\n",
|
||||
" index = routing.Start(vehicle_id)\n",
|
||||
" plan_output = 'Route for vehicle {}:\\n'.format(vehicle_id)\n",
|
||||
" plan_output = f'Route for vehicle {vehicle_id}:\\n'\n",
|
||||
" route_distance = 0\n",
|
||||
" route_load = 0\n",
|
||||
" while not routing.IsEnd(index):\n",
|
||||
" node_index = manager.IndexToNode(index)\n",
|
||||
" route_load += data['demands'][node_index]\n",
|
||||
" plan_output += ' {0} Load({1}) -> '.format(node_index, route_load)\n",
|
||||
" plan_output += f' {node_index} Load({route_load}) -> '\n",
|
||||
" previous_index = index\n",
|
||||
" index = solution.Value(routing.NextVar(index))\n",
|
||||
" route_distance += routing.GetArcCostForVehicle(\n",
|
||||
" previous_index, index, vehicle_id)\n",
|
||||
" plan_output += ' {0} Load({1})\\n'.format(manager.IndexToNode(index),\n",
|
||||
" route_load)\n",
|
||||
" plan_output += 'Distance of the route: {}m\\n'.format(route_distance)\n",
|
||||
" plan_output += 'Load of the route: {}\\n'.format(route_load)\n",
|
||||
" plan_output += f' {manager.IndexToNode(index)} Load({route_load})\\n'\n",
|
||||
" plan_output += f'Distance of the route: {route_distance}m\\n'\n",
|
||||
" plan_output += f'Load of the route: {route_load}\\n'\n",
|
||||
" print(plan_output)\n",
|
||||
" total_distance += route_distance\n",
|
||||
" total_load += route_load\n",
|
||||
" print('Total distance of all routes: {}m'.format(total_distance))\n",
|
||||
" print('Total load of all routes: {}'.format(total_load))\n",
|
||||
" print(f'Total distance of all routes: {total_distance}m')\n",
|
||||
" print(f'Total load of all routes: {total_load}')\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"def main():\n",
|
||||
|
||||
@@ -176,33 +176,32 @@
|
||||
" if routing.IsStart(node) or routing.IsEnd(node):\n",
|
||||
" continue\n",
|
||||
" if assignment.Value(routing.NextVar(node)) == node:\n",
|
||||
" dropped_nodes += ' {}'.format(manager.IndexToNode(node))\n",
|
||||
" dropped_nodes += f' {manager.IndexToNode(node)}'\n",
|
||||
" print(dropped_nodes)\n",
|
||||
" # Display routes\n",
|
||||
" total_distance = 0\n",
|
||||
" total_load = 0\n",
|
||||
" for vehicle_id in range(data['num_vehicles']):\n",
|
||||
" index = routing.Start(vehicle_id)\n",
|
||||
" plan_output = 'Route for vehicle {}:\\n'.format(vehicle_id)\n",
|
||||
" plan_output = f'Route for vehicle {vehicle_id}:\\n'\n",
|
||||
" route_distance = 0\n",
|
||||
" route_load = 0\n",
|
||||
" while not routing.IsEnd(index):\n",
|
||||
" node_index = manager.IndexToNode(index)\n",
|
||||
" route_load += data['demands'][node_index]\n",
|
||||
" plan_output += ' {0} Load({1}) -> '.format(node_index, route_load)\n",
|
||||
" plan_output += f' {node_index} Load({route_load}) -> '\n",
|
||||
" previous_index = index\n",
|
||||
" index = assignment.Value(routing.NextVar(index))\n",
|
||||
" route_distance += routing.GetArcCostForVehicle(\n",
|
||||
" previous_index, index, vehicle_id)\n",
|
||||
" plan_output += ' {0} Load({1})\\n'.format(manager.IndexToNode(index),\n",
|
||||
" route_load)\n",
|
||||
" plan_output += 'Distance of the route: {}m\\n'.format(route_distance)\n",
|
||||
" plan_output += 'Load of the route: {}\\n'.format(route_load)\n",
|
||||
" plan_output += f' {manager.IndexToNode(index)} Load({route_load})\\n'\n",
|
||||
" plan_output += f'Distance of the route: {route_distance}m\\n'\n",
|
||||
" plan_output += f'Load of the route: {route_load}\\n'\n",
|
||||
" print(plan_output)\n",
|
||||
" total_distance += route_distance\n",
|
||||
" total_load += route_load\n",
|
||||
" print('Total Distance of all routes: {}m'.format(total_distance))\n",
|
||||
" print('Total Load of all routes: {}'.format(total_load))\n",
|
||||
" print(f'Total Distance of all routes: {total_distance}m')\n",
|
||||
" print(f'Total Load of all routes: {total_load}')\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"def main():\n",
|
||||
|
||||
@@ -179,19 +179,19 @@
|
||||
" max_route_distance = 0\n",
|
||||
" for vehicle_id in range(data['num_vehicles']):\n",
|
||||
" index = routing.Start(vehicle_id)\n",
|
||||
" plan_output = 'Route for vehicle {}:\\n'.format(vehicle_id)\n",
|
||||
" plan_output = f'Route for vehicle {vehicle_id}:\\n'\n",
|
||||
" route_distance = 0\n",
|
||||
" while not routing.IsEnd(index):\n",
|
||||
" plan_output += ' {} -> '.format(manager.IndexToNode(index))\n",
|
||||
" plan_output += f' {manager.IndexToNode(index)} -> '\n",
|
||||
" previous_index = index\n",
|
||||
" index = solution.Value(routing.NextVar(index))\n",
|
||||
" route_distance += routing.GetArcCostForVehicle(\n",
|
||||
" previous_index, index, vehicle_id)\n",
|
||||
" plan_output += '{}\\n'.format(manager.IndexToNode(index))\n",
|
||||
" plan_output += 'Distance of the route: {}m\\n'.format(route_distance)\n",
|
||||
" plan_output += f'{manager.IndexToNode(index)}\\n'\n",
|
||||
" plan_output += f'Distance of the route: {route_distance}m\\n'\n",
|
||||
" print(plan_output)\n",
|
||||
" max_route_distance = max(route_distance, max_route_distance)\n",
|
||||
" print('Maximum of the route distances: {}m'.format(max_route_distance))\n",
|
||||
" print(f'Maximum of the route distances: {max_route_distance}m')\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"\n",
|
||||
|
||||
@@ -177,19 +177,19 @@
|
||||
" max_route_distance = 0\n",
|
||||
" for vehicle_id in range(data['num_vehicles']):\n",
|
||||
" index = routing.Start(vehicle_id)\n",
|
||||
" plan_output = 'Route for vehicle {}:\\n'.format(vehicle_id)\n",
|
||||
" plan_output = f'Route for vehicle {vehicle_id}:\\n'\n",
|
||||
" route_distance = 0\n",
|
||||
" while not routing.IsEnd(index):\n",
|
||||
" plan_output += ' {} -> '.format(manager.IndexToNode(index))\n",
|
||||
" plan_output += f' {manager.IndexToNode(index)} -> '\n",
|
||||
" previous_index = index\n",
|
||||
" index = solution.Value(routing.NextVar(index))\n",
|
||||
" route_distance += routing.GetArcCostForVehicle(\n",
|
||||
" previous_index, index, vehicle_id)\n",
|
||||
" plan_output += '{}\\n'.format(manager.IndexToNode(index))\n",
|
||||
" plan_output += 'Distance of the route: {}m\\n'.format(route_distance)\n",
|
||||
" plan_output += f'{manager.IndexToNode(index)}\\n'\n",
|
||||
" plan_output += f'Distance of the route: {route_distance}m\\n'\n",
|
||||
" print(plan_output)\n",
|
||||
" max_route_distance = max(route_distance, max_route_distance)\n",
|
||||
" print('Maximum of the route distances: {}m'.format(max_route_distance))\n",
|
||||
" print(f'Maximum of the route distances: {max_route_distance}m')\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"\n",
|
||||
|
||||
@@ -109,42 +109,42 @@
|
||||
"\n",
|
||||
" # Need 11 X and 13 Y\n",
|
||||
" data['providers_x'] = [\n",
|
||||
" 0, # start\n",
|
||||
" -11, # end\n",
|
||||
" 2, # X supply 1\n",
|
||||
" 2, # X supply 2\n",
|
||||
" 4, # X supply 3\n",
|
||||
" 4, # X supply 4\n",
|
||||
" 4, # X supply 5\n",
|
||||
" 5, # X supply 6\n",
|
||||
" 1, # X/Y supply 1\n",
|
||||
" 2, # X/Y supply 2\n",
|
||||
" 2, # X/Y supply 3\n",
|
||||
" 0, # Y supply 1\n",
|
||||
" 0, # Y supply 2\n",
|
||||
" 0, # Y supply 3\n",
|
||||
" 0, # Y supply 4\n",
|
||||
" 0, # Y supply 5\n",
|
||||
" 0, # Y supply 6\n",
|
||||
" 0, # start\n",
|
||||
" -11, # end\n",
|
||||
" 2, # X supply 1\n",
|
||||
" 2, # X supply 2\n",
|
||||
" 4, # X supply 3\n",
|
||||
" 4, # X supply 4\n",
|
||||
" 4, # X supply 5\n",
|
||||
" 5, # X supply 6\n",
|
||||
" 1, # X/Y supply 1\n",
|
||||
" 2, # X/Y supply 2\n",
|
||||
" 2, # X/Y supply 3\n",
|
||||
" 0, # Y supply 1\n",
|
||||
" 0, # Y supply 2\n",
|
||||
" 0, # Y supply 3\n",
|
||||
" 0, # Y supply 4\n",
|
||||
" 0, # Y supply 5\n",
|
||||
" 0, # Y supply 6\n",
|
||||
" ]\n",
|
||||
" data['providers_y'] = [\n",
|
||||
" 0, # start\n",
|
||||
" -13, # ends\n",
|
||||
" 0, # X supply 1\n",
|
||||
" 0, # X supply 2\n",
|
||||
" 0, # X supply 3\n",
|
||||
" 0, # X supply 4\n",
|
||||
" 0, # X supply 5\n",
|
||||
" 0, # X supply 6\n",
|
||||
" 3, # X/Y supply 1\n",
|
||||
" 2, # X/Y supply 2\n",
|
||||
" 1, # X/Y supply 3\n",
|
||||
" 3, # Y supply 1\n",
|
||||
" 3, # Y supply 2\n",
|
||||
" 3, # Y supply 3\n",
|
||||
" 3, # Y supply 4\n",
|
||||
" 3, # Y supply 5\n",
|
||||
" 5, # Y supply 6\n",
|
||||
" 0, # start\n",
|
||||
" -13, # ends\n",
|
||||
" 0, # X supply 1\n",
|
||||
" 0, # X supply 2\n",
|
||||
" 0, # X supply 3\n",
|
||||
" 0, # X supply 4\n",
|
||||
" 0, # X supply 5\n",
|
||||
" 0, # X supply 6\n",
|
||||
" 3, # X/Y supply 1\n",
|
||||
" 2, # X/Y supply 2\n",
|
||||
" 1, # X/Y supply 3\n",
|
||||
" 3, # Y supply 1\n",
|
||||
" 3, # Y supply 2\n",
|
||||
" 3, # Y supply 3\n",
|
||||
" 3, # Y supply 4\n",
|
||||
" 3, # Y supply 5\n",
|
||||
" 5, # Y supply 6\n",
|
||||
" ]\n",
|
||||
" data['vehicle_capacities_x'] = [15] * data['num_vehicles']\n",
|
||||
" data['vehicle_capacities_y'] = [15] * data['num_vehicles']\n",
|
||||
@@ -156,28 +156,28 @@
|
||||
" 468, 776, 662\n",
|
||||
" ],\n",
|
||||
" [\n",
|
||||
" 548, 0, 684, 308, 194, 502, 730, 354, 696, 742, 1084, 594, 480,\n",
|
||||
" 674, 1016, 868, 1210\n",
|
||||
" 548, 0, 684, 308, 194, 502, 730, 354, 696, 742, 1084, 594, 480, 674,\n",
|
||||
" 1016, 868, 1210\n",
|
||||
" ],\n",
|
||||
" [\n",
|
||||
" 776, 684, 0, 992, 878, 502, 274, 810, 468, 742, 400, 1278, 1164,\n",
|
||||
" 1130, 788, 1552, 754\n",
|
||||
" ],\n",
|
||||
" [\n",
|
||||
" 696, 308, 992, 0, 114, 650, 878, 502, 844, 890, 1232, 514, 628,\n",
|
||||
" 822, 1164, 560, 1358\n",
|
||||
" 696, 308, 992, 0, 114, 650, 878, 502, 844, 890, 1232, 514, 628, 822,\n",
|
||||
" 1164, 560, 1358\n",
|
||||
" ],\n",
|
||||
" [\n",
|
||||
" 582, 194, 878, 114, 0, 536, 764, 388, 730, 776, 1118, 400, 514,\n",
|
||||
" 708, 1050, 674, 1244\n",
|
||||
" 582, 194, 878, 114, 0, 536, 764, 388, 730, 776, 1118, 400, 514, 708,\n",
|
||||
" 1050, 674, 1244\n",
|
||||
" ],\n",
|
||||
" [\n",
|
||||
" 274, 502, 502, 650, 536, 0, 228, 308, 194, 240, 582, 776, 662, 628,\n",
|
||||
" 514, 1050, 708\n",
|
||||
" ],\n",
|
||||
" [\n",
|
||||
" 502, 730, 274, 878, 764, 228, 0, 536, 194, 468, 354, 1004, 890,\n",
|
||||
" 856, 514, 1278, 480\n",
|
||||
" 502, 730, 274, 878, 764, 228, 0, 536, 194, 468, 354, 1004, 890, 856,\n",
|
||||
" 514, 1278, 480\n",
|
||||
" ],\n",
|
||||
" [\n",
|
||||
" 194, 354, 810, 502, 388, 308, 536, 0, 342, 388, 730, 468, 354, 320,\n",
|
||||
@@ -200,12 +200,12 @@
|
||||
" 308, 650, 274, 844\n",
|
||||
" ],\n",
|
||||
" [\n",
|
||||
" 388, 480, 1164, 628, 514, 662, 890, 354, 696, 422, 764, 114, 0,\n",
|
||||
" 194, 536, 388, 730\n",
|
||||
" 388, 480, 1164, 628, 514, 662, 890, 354, 696, 422, 764, 114, 0, 194,\n",
|
||||
" 536, 388, 730\n",
|
||||
" ],\n",
|
||||
" [\n",
|
||||
" 354, 674, 1130, 822, 708, 628, 856, 320, 662, 388, 730, 308, 194,\n",
|
||||
" 0, 342, 422, 536\n",
|
||||
" 354, 674, 1130, 822, 708, 628, 856, 320, 662, 388, 730, 308, 194, 0,\n",
|
||||
" 342, 422, 536\n",
|
||||
" ],\n",
|
||||
" [\n",
|
||||
" 468, 1016, 788, 1164, 1050, 514, 514, 662, 320, 274, 388, 650, 536,\n",
|
||||
@@ -276,8 +276,8 @@
|
||||
"\n",
|
||||
" # Create the routing index manager.\n",
|
||||
" manager = pywrapcp.RoutingIndexManager(len(data['distance_matrix']),\n",
|
||||
" data['num_vehicles'],\n",
|
||||
" data['starts'], data['ends'])\n",
|
||||
" data['num_vehicles'], data['starts'],\n",
|
||||
" data['ends'])\n",
|
||||
"\n",
|
||||
" # Create Routing Model.\n",
|
||||
" routing = pywrapcp.RoutingModel(manager)\n",
|
||||
@@ -308,6 +308,7 @@
|
||||
" # Minimize the longest road\n",
|
||||
" distance_dimension.SetGlobalSpanCostCoefficient(100)\n",
|
||||
"\n",
|
||||
"\n",
|
||||
" # Add Capacity constraint.\n",
|
||||
" def demand_callback_x(from_index):\n",
|
||||
" \"\"\"Returns the demand of the node.\"\"\"\n",
|
||||
@@ -315,7 +316,8 @@
|
||||
" from_node = manager.IndexToNode(from_index)\n",
|
||||
" return data['providers_x'][from_node]\n",
|
||||
"\n",
|
||||
" demand_callback_x_index = routing.RegisterUnaryTransitCallback(demand_callback_x)\n",
|
||||
" demand_callback_x_index = routing.RegisterUnaryTransitCallback(\n",
|
||||
" demand_callback_x)\n",
|
||||
" routing.AddDimensionWithVehicleCapacity(\n",
|
||||
" demand_callback_x_index,\n",
|
||||
" 0, # null capacity slack\n",
|
||||
@@ -329,7 +331,8 @@
|
||||
" from_node = manager.IndexToNode(from_index)\n",
|
||||
" return data['providers_y'][from_node]\n",
|
||||
"\n",
|
||||
" demand_callback_y_index = routing.RegisterUnaryTransitCallback(demand_callback_y)\n",
|
||||
" demand_callback_y_index = routing.RegisterUnaryTransitCallback(\n",
|
||||
" demand_callback_y)\n",
|
||||
" routing.AddDimensionWithVehicleCapacity(\n",
|
||||
" demand_callback_y_index,\n",
|
||||
" 0, # null capacity slack\n",
|
||||
@@ -346,8 +349,12 @@
|
||||
" ends.append(routing.End(v))\n",
|
||||
"\n",
|
||||
" node_end = data['ends'][0]\n",
|
||||
" solver.Add(solver.Sum([load_x_dim.CumulVar(l) for l in ends]) >= -data['providers_x'][node_end])\n",
|
||||
" solver.Add(solver.Sum([load_y_dim.CumulVar(l) for l in ends]) >= -data['providers_y'][node_end])\n",
|
||||
" solver.Add(\n",
|
||||
" solver.Sum([load_x_dim.CumulVar(l)\n",
|
||||
" for l in ends]) >= -data['providers_x'][node_end])\n",
|
||||
" solver.Add(\n",
|
||||
" solver.Sum([load_y_dim.CumulVar(l)\n",
|
||||
" for l in ends]) >= -data['providers_y'][node_end])\n",
|
||||
" #solver.Add(load_y_dim.CumulVar(end) >= -data['providers_y'][node_end])\n",
|
||||
"\n",
|
||||
" # Allow to freely drop any nodes.\n",
|
||||
|
||||
@@ -199,30 +199,31 @@
|
||||
"\n",
|
||||
" for vehicle_id in range(data['num_vehicles']):\n",
|
||||
" index = routing.Start(vehicle_id)\n",
|
||||
" plan_output = 'Route for vehicle {}:\\n'.format(vehicle_id)\n",
|
||||
" plan_output = f'Route for vehicle {vehicle_id}:\\n'\n",
|
||||
" route_distance = 0\n",
|
||||
" while not routing.IsEnd(index):\n",
|
||||
" one_var = dim_one.CumulVar(index)\n",
|
||||
" one_slack_var = dim_one.SlackVar(index)\n",
|
||||
" two_var = dim_two.CumulVar(index)\n",
|
||||
" two_slack_var = dim_two.SlackVar(index)\n",
|
||||
" plan_output += ' N:{0} one:({1},{2}) two:({3},{4}) -> '.format(\n",
|
||||
" manager.IndexToNode(index), solution.Value(one_var),\n",
|
||||
" solution.Value(one_slack_var), solution.Value(two_var),\n",
|
||||
" solution.Value(two_slack_var))\n",
|
||||
" plan_output += (\n",
|
||||
" f' N:{manager.IndexToNode(index)}'\n",
|
||||
" f' one:({solution.Value(one_var)}, {solution.Value(one_slack_var)})'\n",
|
||||
" f' two:({solution.Value(two_var)}, {solution.Value(two_slack_var)})'\n",
|
||||
" ' -> ')\n",
|
||||
" previous_index = index\n",
|
||||
" index = solution.Value(routing.NextVar(index))\n",
|
||||
" route_distance += routing.GetArcCostForVehicle(\n",
|
||||
" previous_index, index, vehicle_id)\n",
|
||||
" one_var = dim_one.CumulVar(index)\n",
|
||||
" two_var = dim_two.CumulVar(index)\n",
|
||||
" plan_output += 'N:{0} one:{1} two:{2}\\n'.format(\n",
|
||||
" manager.IndexToNode(index), solution.Value(one_var),\n",
|
||||
" solution.Value(two_var))\n",
|
||||
" plan_output += 'Distance of the route: {}m\\n'.format(route_distance)\n",
|
||||
" plan_output += (f'N:{manager.IndexToNode(index)}'\n",
|
||||
" f' one:{solution.Value(one_var)}'\n",
|
||||
" f' two:{solution.Value(two_var)}\\n')\n",
|
||||
" plan_output += f'Distance of the route: {route_distance}m\\n'\n",
|
||||
" print(plan_output)\n",
|
||||
" max_route_distance = max(route_distance, max_route_distance)\n",
|
||||
" print('Maximum of the route distances: {}m'.format(max_route_distance))\n",
|
||||
" print(f'Maximum of the route distances: {max_route_distance}m')\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"\n",
|
||||
|
||||
@@ -135,9 +135,13 @@
|
||||
" print(v_ends)\n",
|
||||
"\n",
|
||||
" print('\\nNodes:')\n",
|
||||
" print('| locations | manager.GetNumberOfNodes | manager.GetNumberOfIndices | routing.nodes | routing.Size |')\n",
|
||||
" print(\n",
|
||||
" '| locations | manager.GetNumberOfNodes | manager.GetNumberOfIndices | routing.nodes | routing.Size |'\n",
|
||||
" )\n",
|
||||
" print('|---|---|---|---|---|')\n",
|
||||
" print(f'| {locations} | {manager.GetNumberOfNodes()} | {manager.GetNumberOfIndices()} | {routing.nodes()} | {routing.Size()} |')\n",
|
||||
" print(\n",
|
||||
" f'| {locations} | {manager.GetNumberOfNodes()} | {manager.GetNumberOfIndices()} | {routing.nodes()} | {routing.Size()} |'\n",
|
||||
" )\n",
|
||||
"\n",
|
||||
" print('\\nLocations:')\n",
|
||||
" print('| node | index | routing.IsStart | routing.IsEnd |')\n",
|
||||
@@ -151,16 +155,22 @@
|
||||
" )\n",
|
||||
"\n",
|
||||
" print('\\nStart/End:')\n",
|
||||
" print('| vehicle | Start/end | node | index | routing.IsStart | routing.IsEnd |')\n",
|
||||
" print(\n",
|
||||
" '| vehicle | Start/end | node | index | routing.IsStart | routing.IsEnd |'\n",
|
||||
" )\n",
|
||||
" print('|---|---|---|---|---|---|')\n",
|
||||
" for v in range(manager.GetNumberOfVehicles()):\n",
|
||||
" start_index = routing.Start(v)\n",
|
||||
" start_node = manager.IndexToNode(start_index)\n",
|
||||
" print(f'| {v} | start | {start_node} | {start_index} | {routing.IsStart(start_index)} | {routing.IsEnd(start_index)} |')\n",
|
||||
" print(\n",
|
||||
" f'| {v} | start | {start_node} | {start_index} | {routing.IsStart(start_index)} | {routing.IsEnd(start_index)} |'\n",
|
||||
" )\n",
|
||||
" for v in range(manager.GetNumberOfVehicles()):\n",
|
||||
" end_index = routing.End(v)\n",
|
||||
" end_node = manager.IndexToNode(end_index)\n",
|
||||
" print(f'| {v} | end | {end_node} | {end_index} | {routing.IsStart(end_index)} | {routing.IsEnd(end_index)} |')\n",
|
||||
" print(\n",
|
||||
" f'| {v} | end | {end_node} | {end_index} | {routing.IsStart(end_index)} | {routing.IsEnd(end_index)} |'\n",
|
||||
" )\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"main()\n",
|
||||
|
||||
@@ -181,19 +181,19 @@
|
||||
" total_distance = 0\n",
|
||||
" for vehicle_id in range(data['num_vehicles']):\n",
|
||||
" index = routing.Start(vehicle_id)\n",
|
||||
" plan_output = 'Route for vehicle {}:\\n'.format(vehicle_id)\n",
|
||||
" plan_output = f'Route for vehicle {vehicle_id}:\\n'\n",
|
||||
" route_distance = 0\n",
|
||||
" while not routing.IsEnd(index):\n",
|
||||
" plan_output += ' {} -> '.format(manager.IndexToNode(index))\n",
|
||||
" plan_output += f' {manager.IndexToNode(index)} -> '\n",
|
||||
" previous_index = index\n",
|
||||
" index = solution.Value(routing.NextVar(index))\n",
|
||||
" route_distance += routing.GetArcCostForVehicle(\n",
|
||||
" previous_index, index, vehicle_id)\n",
|
||||
" plan_output += '{}\\n'.format(manager.IndexToNode(index))\n",
|
||||
" plan_output += 'Distance of the route: {}m\\n'.format(route_distance)\n",
|
||||
" plan_output += f'{manager.IndexToNode(index)}\\n'\n",
|
||||
" plan_output += f'Distance of the route: {route_distance}m\\n'\n",
|
||||
" print(plan_output)\n",
|
||||
" total_distance += route_distance\n",
|
||||
" print('Total Distance of all routes: {}m'.format(total_distance))\n",
|
||||
" print(f'Total Distance of all routes: {total_distance}m')\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"def main():\n",
|
||||
|
||||
@@ -181,19 +181,19 @@
|
||||
" total_distance = 0\n",
|
||||
" for vehicle_id in range(data['num_vehicles']):\n",
|
||||
" index = routing.Start(vehicle_id)\n",
|
||||
" plan_output = 'Route for vehicle {}:\\n'.format(vehicle_id)\n",
|
||||
" plan_output = f'Route for vehicle {vehicle_id}:\\n'\n",
|
||||
" route_distance = 0\n",
|
||||
" while not routing.IsEnd(index):\n",
|
||||
" plan_output += ' {} -> '.format(manager.IndexToNode(index))\n",
|
||||
" plan_output += f' {manager.IndexToNode(index)} -> '\n",
|
||||
" previous_index = index\n",
|
||||
" index = assignment.Value(routing.NextVar(index))\n",
|
||||
" route_distance += routing.GetArcCostForVehicle(\n",
|
||||
" previous_index, index, vehicle_id)\n",
|
||||
" plan_output += '{}\\n'.format(manager.IndexToNode(index))\n",
|
||||
" plan_output += 'Distance of the route: {}m\\n'.format(route_distance)\n",
|
||||
" plan_output += f'{manager.IndexToNode(index)}\\n'\n",
|
||||
" plan_output += f'Distance of the route: {route_distance}m\\n'\n",
|
||||
" print(plan_output)\n",
|
||||
" total_distance += route_distance\n",
|
||||
" print('Total Distance of all routes: {}m'.format(total_distance))\n",
|
||||
" print(f'Total Distance of all routes: {total_distance}m')\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"def main():\n",
|
||||
|
||||
@@ -181,19 +181,19 @@
|
||||
" total_distance = 0\n",
|
||||
" for vehicle_id in range(data['num_vehicles']):\n",
|
||||
" index = routing.Start(vehicle_id)\n",
|
||||
" plan_output = 'Route for vehicle {}:\\n'.format(vehicle_id)\n",
|
||||
" plan_output = f'Route for vehicle {vehicle_id}:\\n'\n",
|
||||
" route_distance = 0\n",
|
||||
" while not routing.IsEnd(index):\n",
|
||||
" plan_output += ' {} -> '.format(manager.IndexToNode(index))\n",
|
||||
" plan_output += f' {manager.IndexToNode(index)} -> '\n",
|
||||
" previous_index = index\n",
|
||||
" index = assignment.Value(routing.NextVar(index))\n",
|
||||
" route_distance += routing.GetArcCostForVehicle(\n",
|
||||
" previous_index, index, vehicle_id)\n",
|
||||
" plan_output += '{}\\n'.format(manager.IndexToNode(index))\n",
|
||||
" plan_output += 'Distance of the route: {}m\\n'.format(route_distance)\n",
|
||||
" plan_output += f'{manager.IndexToNode(index)}\\n'\n",
|
||||
" plan_output += f'Distance of the route: {route_distance}m\\n'\n",
|
||||
" print(plan_output)\n",
|
||||
" total_distance += route_distance\n",
|
||||
" print('Total Distance of all routes: {}m'.format(total_distance))\n",
|
||||
" print(f'Total Distance of all routes: {total_distance}m')\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"def main():\n",
|
||||
|
||||
@@ -143,22 +143,22 @@
|
||||
" total_time = 0\n",
|
||||
" for vehicle_id in range(data['num_vehicles']):\n",
|
||||
" index = routing.Start(vehicle_id)\n",
|
||||
" plan_output = 'Route for vehicle {}:\\n'.format(vehicle_id)\n",
|
||||
" plan_output = f'Route for vehicle {vehicle_id}:\\n'\n",
|
||||
" while not routing.IsEnd(index):\n",
|
||||
" time_var = time_dimension.CumulVar(index)\n",
|
||||
" plan_output += '{0} Time({1},{2}) -> '.format(\n",
|
||||
" manager.IndexToNode(index), solution.Min(time_var),\n",
|
||||
" solution.Max(time_var))\n",
|
||||
" plan_output += (\n",
|
||||
" f'{manager.IndexToNode(index)}'\n",
|
||||
" f' Time({solution.Min(time_var)}, {solution.Max(time_var)})'\n",
|
||||
" ' -> ')\n",
|
||||
" index = solution.Value(routing.NextVar(index))\n",
|
||||
" time_var = time_dimension.CumulVar(index)\n",
|
||||
" plan_output += '{0} Time({1},{2})\\n'.format(manager.IndexToNode(index),\n",
|
||||
" solution.Min(time_var),\n",
|
||||
" solution.Max(time_var))\n",
|
||||
" plan_output += 'Time of the route: {}min\\n'.format(\n",
|
||||
" solution.Min(time_var))\n",
|
||||
" plan_output += (\n",
|
||||
" f'{manager.IndexToNode(index)}'\n",
|
||||
" f' Time({solution.Min(time_var)},{solution.Max(time_var)})\\n')\n",
|
||||
" plan_output += f'Time of the route: {solution.Min(time_var)}min\\n'\n",
|
||||
" print(plan_output)\n",
|
||||
" total_time += solution.Min(time_var)\n",
|
||||
" print('Total time of all routes: {}min'.format(total_time))\n",
|
||||
" print(f'Total time of all routes: {total_time}min')\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"def main():\n",
|
||||
|
||||
@@ -172,19 +172,19 @@
|
||||
" max_route_distance = 0\n",
|
||||
" for vehicle_id in range(data['num_vehicles']):\n",
|
||||
" index = routing.Start(vehicle_id)\n",
|
||||
" plan_output = 'Route for vehicle {}:\\n'.format(vehicle_id)\n",
|
||||
" plan_output = f'Route for vehicle {vehicle_id}:\\n'\n",
|
||||
" route_distance = 0\n",
|
||||
" while not routing.IsEnd(index):\n",
|
||||
" plan_output += ' {} -> '.format(manager.IndexToNode(index))\n",
|
||||
" plan_output += f' {manager.IndexToNode(index)} -> '\n",
|
||||
" previous_index = index\n",
|
||||
" index = solution.Value(routing.NextVar(index))\n",
|
||||
" route_distance += routing.GetArcCostForVehicle(\n",
|
||||
" previous_index, index, vehicle_id)\n",
|
||||
" plan_output += '{}\\n'.format(manager.IndexToNode(index))\n",
|
||||
" plan_output += 'Distance of the route: {}m\\n'.format(route_distance)\n",
|
||||
" plan_output += f'{manager.IndexToNode(index)}\\n'\n",
|
||||
" plan_output += f'Distance of the route: {route_distance}m\\n'\n",
|
||||
" print(plan_output)\n",
|
||||
" max_route_distance = max(route_distance, max_route_distance)\n",
|
||||
" print('Maximum of the route distances: {}m'.format(max_route_distance))\n",
|
||||
" print(f'Maximum of the route distances: {max_route_distance}m')\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"def main():\n",
|
||||
|
||||
@@ -140,22 +140,22 @@
|
||||
" total_time = 0\n",
|
||||
" for vehicle_id in range(data['num_vehicles']):\n",
|
||||
" index = routing.Start(vehicle_id)\n",
|
||||
" plan_output = 'Route for vehicle {}:\\n'.format(vehicle_id)\n",
|
||||
" plan_output = f'Route for vehicle {vehicle_id}:\\n'\n",
|
||||
" while not routing.IsEnd(index):\n",
|
||||
" time_var = time_dimension.CumulVar(index)\n",
|
||||
" plan_output += '{0} Time({1},{2}) -> '.format(\n",
|
||||
" manager.IndexToNode(index), solution.Min(time_var),\n",
|
||||
" solution.Max(time_var))\n",
|
||||
" plan_output += (\n",
|
||||
" f'{manager.IndexToNode(index)}'\n",
|
||||
" f' Time({solution.Min(time_var)},{solution.Max(time_var)})'\n",
|
||||
" ' -> ')\n",
|
||||
" index = solution.Value(routing.NextVar(index))\n",
|
||||
" time_var = time_dimension.CumulVar(index)\n",
|
||||
" plan_output += '{0} Time({1},{2})\\n'.format(manager.IndexToNode(index),\n",
|
||||
" solution.Min(time_var),\n",
|
||||
" solution.Max(time_var))\n",
|
||||
" plan_output += 'Time of the route: {}min\\n'.format(\n",
|
||||
" solution.Min(time_var))\n",
|
||||
" plan_output += (\n",
|
||||
" f'{manager.IndexToNode(index)}'\n",
|
||||
" f' Time({solution.Min(time_var)},{solution.Max(time_var)})\\n')\n",
|
||||
" plan_output += f'Time of the route: {solution.Min(time_var)}min\\n'\n",
|
||||
" print(plan_output)\n",
|
||||
" total_time += solution.Min(time_var)\n",
|
||||
" print('Total time of all routes: {}min'.format(total_time))\n",
|
||||
" print(f'Total time of all routes: {total_time}min')\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"def main():\n",
|
||||
|
||||
@@ -139,12 +139,12 @@
|
||||
" if assignment.Value(routing.NextVar(index)) == index:\n",
|
||||
" node = manager.IndexToNode(index)\n",
|
||||
" if node > 16:\n",
|
||||
" original = node\n",
|
||||
" while original > 16:\n",
|
||||
" original = original - 16\n",
|
||||
" dropped_nodes += f' {node}({original})'\n",
|
||||
" original = node\n",
|
||||
" while original > 16:\n",
|
||||
" original = original - 16\n",
|
||||
" dropped_nodes += f' {node}({original})'\n",
|
||||
" else:\n",
|
||||
" dropped_nodes += f' {node}'\n",
|
||||
" dropped_nodes += f' {node}'\n",
|
||||
" print(dropped_nodes)\n",
|
||||
" # Display routes\n",
|
||||
" time_dimension = routing.GetDimensionOrDie('Time')\n",
|
||||
@@ -157,12 +157,12 @@
|
||||
" time_var = time_dimension.CumulVar(index)\n",
|
||||
" node = manager.IndexToNode(index)\n",
|
||||
" if node > 16:\n",
|
||||
" original = node\n",
|
||||
" while original > 16:\n",
|
||||
" original = original - 16\n",
|
||||
" plan_output += f'{node}({original})'\n",
|
||||
" original = node\n",
|
||||
" while original > 16:\n",
|
||||
" original = original - 16\n",
|
||||
" plan_output += f'{node}({original})'\n",
|
||||
" else:\n",
|
||||
" plan_output += f'{node}'\n",
|
||||
" plan_output += f'{node}'\n",
|
||||
" plan_output += f' Time:{assignment.Value(time_var)} -> '\n",
|
||||
" if start_time == 0:\n",
|
||||
" start_time = assignment.Value(time_var)\n",
|
||||
@@ -185,9 +185,9 @@
|
||||
"\n",
|
||||
" # Create the routing index manager.\n",
|
||||
" manager = pywrapcp.RoutingIndexManager(\n",
|
||||
" 1 + 16*4, # number of locations\n",
|
||||
" data['num_vehicles'],\n",
|
||||
" data['depot'])\n",
|
||||
" 1 + 16 * 4, # number of locations\n",
|
||||
" data['num_vehicles'],\n",
|
||||
" data['depot'])\n",
|
||||
"\n",
|
||||
" # Create Routing Model.\n",
|
||||
" routing = pywrapcp.RoutingModel(manager)\n",
|
||||
@@ -202,9 +202,9 @@
|
||||
" # since our matrix is 17x17 map duplicated node to original one to\n",
|
||||
" # retrieve the travel time\n",
|
||||
" while from_node > 16:\n",
|
||||
" from_node = from_node - 16;\n",
|
||||
" from_node = from_node - 16\n",
|
||||
" while to_node > 16:\n",
|
||||
" to_node = to_node - 16;\n",
|
||||
" to_node = to_node - 16\n",
|
||||
" # add service of 25min for each location (except depot)\n",
|
||||
" service_time = 0\n",
|
||||
" if from_node != data['depot']:\n",
|
||||
@@ -235,35 +235,35 @@
|
||||
" routing.VehicleVar(index_0).SetValues([-1, 0])\n",
|
||||
"\n",
|
||||
" # Vehicle 1 location TW: [11am, 1pm]\n",
|
||||
" index_1 = manager.NodeToIndex(location_idx+16*1)\n",
|
||||
" index_1 = manager.NodeToIndex(location_idx + 16 * 1)\n",
|
||||
" time_dimension.CumulVar(index_1).SetRange(660, 780)\n",
|
||||
" routing.VehicleVar(index_1).SetValues([-1, 1])\n",
|
||||
"\n",
|
||||
" # Vehicle 2 location TW: [1pm, 3pm]\n",
|
||||
" index_2 = manager.NodeToIndex(location_idx+16*2)\n",
|
||||
" index_2 = manager.NodeToIndex(location_idx + 16 * 2)\n",
|
||||
" time_dimension.CumulVar(index_2).SetRange(780, 900)\n",
|
||||
" routing.VehicleVar(index_2).SetValues([-1, 2])\n",
|
||||
"\n",
|
||||
" # Vehicle 3 location TW: [3pm, 5pm]\n",
|
||||
" index_3 = manager.NodeToIndex(location_idx+16*3)\n",
|
||||
" index_3 = manager.NodeToIndex(location_idx + 16 * 3)\n",
|
||||
" time_dimension.CumulVar(index_3).SetRange(900, 1020)\n",
|
||||
" routing.VehicleVar(index_3).SetValues([-1, 3])\n",
|
||||
"\n",
|
||||
" # Add Disjunction so only one node among duplicate is visited\n",
|
||||
" penalty = 100_000 # Give solver strong incentive to visit one node\n",
|
||||
" penalty = 100_000 # Give solver strong incentive to visit one node\n",
|
||||
" routing.AddDisjunction([index_0, index_1, index_2, index_3], penalty, 1)\n",
|
||||
"\n",
|
||||
" # Add time window constraints for each vehicle start node.\n",
|
||||
" depot_idx = data['depot']\n",
|
||||
" for vehicle_id in range(data['num_vehicles']):\n",
|
||||
" index = routing.Start(vehicle_id)\n",
|
||||
" time_dimension.CumulVar(index).SetRange(480, 1020) # (8am, 5pm)\n",
|
||||
" time_dimension.CumulVar(index).SetRange(480, 1020) # (8am, 5pm)\n",
|
||||
"\n",
|
||||
" # Add time window constraints for each vehicle end node.\n",
|
||||
" depot_idx = data['depot']\n",
|
||||
" for vehicle_id in range(data['num_vehicles']):\n",
|
||||
" index = routing.End(vehicle_id)\n",
|
||||
" time_dimension.CumulVar(index).SetRange(480, 1020) # (8am, 5pm)\n",
|
||||
" time_dimension.CumulVar(index).SetRange(480, 1020) # (8am, 5pm)\n",
|
||||
"\n",
|
||||
" # Instantiate route start and end times to produce feasible times.\n",
|
||||
" for i in range(data['num_vehicles']):\n",
|
||||
|
||||
@@ -146,8 +146,8 @@
|
||||
" plan_output += f'Distance of the route: {route_distance}m\\n'\n",
|
||||
" total_distance += route_distance\n",
|
||||
" print(plan_output)\n",
|
||||
" print('Total distance of all routes: {}m'.format(total_distance))\n",
|
||||
" print('Total token of all routes: {}'.format(total_token))\n",
|
||||
" print(f'Total distance of all routes: {total_distance}m')\n",
|
||||
" print(f'Total token of all routes: {total_token}')\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"def main():\n",
|
||||
|
||||
@@ -93,19 +93,19 @@
|
||||
" max_route_distance = 0\n",
|
||||
" for vehicle_id in range(manager.GetNumberOfVehicles()):\n",
|
||||
" index = routing.Start(vehicle_id)\n",
|
||||
" plan_output = 'Route for vehicle {}:\\n'.format(vehicle_id)\n",
|
||||
" plan_output = f'Route for vehicle {vehicle_id}:\\n'\n",
|
||||
" route_distance = 0\n",
|
||||
" while not routing.IsEnd(index):\n",
|
||||
" plan_output += ' {} -> '.format(manager.IndexToNode(index))\n",
|
||||
" plan_output += f' {manager.IndexToNode(index)} -> '\n",
|
||||
" previous_index = index\n",
|
||||
" index = solution.Value(routing.NextVar(index))\n",
|
||||
" route_distance += routing.GetArcCostForVehicle(\n",
|
||||
" previous_index, index, vehicle_id)\n",
|
||||
" plan_output += '{}\\n'.format(manager.IndexToNode(index))\n",
|
||||
" plan_output += 'Distance of the route: {}m\\n'.format(route_distance)\n",
|
||||
" plan_output += f'{manager.IndexToNode(index)}\\n'\n",
|
||||
" plan_output += f'Distance of the route: {route_distance}m\\n'\n",
|
||||
" print(plan_output)\n",
|
||||
" max_route_distance = max(route_distance, max_route_distance)\n",
|
||||
" print('Maximum of the route distances: {}m'.format(max_route_distance))\n",
|
||||
" print(f'Maximum of the route distances: {max_route_distance}m')\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"def main():\n",
|
||||
|
||||
@@ -73,14 +73,14 @@
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"\n",
|
||||
"Simple Vehicles Routing Problem (VRP).\n",
|
||||
"Simple Vehicle Routing Problem (VRP).\n",
|
||||
"\n",
|
||||
" This is a sample using the routing library python wrapper to solve a VRP\n",
|
||||
" problem.\n",
|
||||
" A description of the problem can be found here:\n",
|
||||
" http://en.wikipedia.org/wiki/Vehicle_routing_problem.\n",
|
||||
"This is a sample using the routing library Python wrapper to solve a VRP\n",
|
||||
"instance.\n",
|
||||
"A description of the problem can be found here:\n",
|
||||
"http://en.wikipedia.org/wiki/Vehicle_routing_problem.\n",
|
||||
"\n",
|
||||
" Distances are in meters.\n",
|
||||
"Distances are in meters.\n",
|
||||
"\n"
|
||||
]
|
||||
},
|
||||
@@ -137,19 +137,19 @@
|
||||
" total_distance = 0\n",
|
||||
" for vehicle_id in range(data['num_vehicles']):\n",
|
||||
" index = routing.Start(vehicle_id)\n",
|
||||
" plan_output = 'Route for vehicle {}:\\n'.format(vehicle_id)\n",
|
||||
" plan_output = f'Route for vehicle {vehicle_id}:\\n'\n",
|
||||
" route_distance = 0\n",
|
||||
" while not routing.IsEnd(index):\n",
|
||||
" plan_output += ' {} ->'.format(manager.IndexToNode(index))\n",
|
||||
" plan_output += f' {manager.IndexToNode(index)} ->'\n",
|
||||
" previous_index = index\n",
|
||||
" index = assignment.Value(routing.NextVar(index))\n",
|
||||
" route_distance += routing.GetArcCostForVehicle(\n",
|
||||
" previous_index, index, vehicle_id)\n",
|
||||
" plan_output += ' {}\\n'.format(manager.IndexToNode(index))\n",
|
||||
" plan_output += 'Distance of the route: {}m\\n'.format(route_distance)\n",
|
||||
" plan_output += f' {manager.IndexToNode(index)}\\n'\n",
|
||||
" plan_output += f'Distance of the route: {route_distance}m\\n'\n",
|
||||
" print(plan_output)\n",
|
||||
" total_distance += route_distance\n",
|
||||
" print('Total Distance of all routes: {}m'.format(total_distance))\n",
|
||||
" print(f'Total Distance of all routes: {total_distance}m')\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"\n",
|
||||
|
||||
@@ -149,9 +149,9 @@
|
||||
" end_time = cumul_data[i][j][1]\n",
|
||||
" route_str += ' -> ' + str(route[j]) + \\\n",
|
||||
" ' Time(' + str(start_time) + ', ' + str(end_time) + ')'\n",
|
||||
" route_str += '\\n Route time: {} min\\n\\n'.format(start_time)\n",
|
||||
" route_str += f'\\n Route time: {start_time}min\\n\\n'\n",
|
||||
" total_time += cumul_data[i][len(route) - 1][0]\n",
|
||||
" route_str += 'Total time: {} min'.format(total_time)\n",
|
||||
" route_str += f'Total time: {total_time}min'\n",
|
||||
" print(route_str)\n",
|
||||
"\n",
|
||||
"\n",
|
||||
|
||||
@@ -105,7 +105,7 @@
|
||||
" # Variables\n",
|
||||
" # x[i, j] is an array of 0-1 variables, which will be 1\n",
|
||||
" # if worker i is assigned to task j.\n",
|
||||
" x = model.new_bool_var_array(shape=[num_workers, num_tasks], name='x')\n",
|
||||
" x = model.new_bool_var_array(shape=[num_workers, num_tasks], name='x') # pytype: disable=wrong-arg-types # numpy-scalars\n",
|
||||
"\n",
|
||||
" # Constraints\n",
|
||||
" # Each worker is assigned to at most 1 task.\n",
|
||||
|
||||
Reference in New Issue
Block a user