Skip to content

Commit

Permalink
update notebooks
Browse files Browse the repository at this point in the history
  • Loading branch information
Mizux committed Apr 30, 2024
1 parent b5917aa commit 32d962c
Show file tree
Hide file tree
Showing 47 changed files with 1,859 additions and 563 deletions.
281 changes: 156 additions & 125 deletions examples/notebook/constraint_solver/cvrp_reload.ipynb

Large diffs are not rendered by default.

20 changes: 12 additions & 8 deletions examples/notebook/constraint_solver/cvrptw_break.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,12 @@
"\n",
"Capacitated Vehicle Routing Problem with Time Windows (CVRPTW).\n",
"\n",
" This is a sample using the routing library python wrapper to solve a CVRPTW\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 CVRPTW\n",
"problem.\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 and time in minutes.\n",
"Distances are in meters and time in minutes.\n",
"\n"
]
},
Expand All @@ -92,8 +92,8 @@
"outputs": [],
"source": [
"import functools\n",
"from ortools.constraint_solver import routing_enums_pb2\n",
"from ortools.constraint_solver import pywrapcp\n",
"from ortools.routing import enums_pb2\n",
"\n",
"\n",
"def create_data_model():\n",
Expand Down Expand Up @@ -386,7 +386,11 @@
" vehicle_break = data[\"breaks\"][v]\n",
" break_intervals[v] = [\n",
" routing.solver().FixedDurationIntervalVar(\n",
" 15, 100, vehicle_break[0], vehicle_break[1], f\"Break for vehicle {v}\"\n",
" 15,\n",
" 100,\n",
" vehicle_break[0],\n",
" vehicle_break[1],\n",
" f\"Break for vehicle {v}\",\n",
" )\n",
" ]\n",
" time_dimension.SetBreakIntervalsOfVehicle(\n",
Expand All @@ -396,7 +400,7 @@
" # Setting first solution heuristic (cheapest addition).\n",
" search_parameters = pywrapcp.DefaultRoutingSearchParameters()\n",
" search_parameters.first_solution_strategy = (\n",
" routing_enums_pb2.FirstSolutionStrategy.PATH_CHEAPEST_ARC\n",
" enums_pb2.FirstSolutionStrategy.PATH_CHEAPEST_ARC\n",
" ) # pylint: disable=no-member\n",
"\n",
" # Solve the problem.\n",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,8 @@
"metadata": {},
"outputs": [],
"source": [
"from ortools.constraint_solver import routing_enums_pb2\n",
"from ortools.constraint_solver import pywrapcp\n",
"from ortools.routing import enums_pb2\n",
"\n",
"\n",
"def main():\n",
Expand Down Expand Up @@ -116,7 +116,7 @@
" # Setting first solution heuristic.\n",
" search_parameters = pywrapcp.DefaultRoutingSearchParameters()\n",
" search_parameters.first_solution_strategy = (\n",
" routing_enums_pb2.FirstSolutionStrategy.PATH_CHEAPEST_ARC\n",
" enums_pb2.FirstSolutionStrategy.PATH_CHEAPEST_ARC\n",
" ) # pylint: disable=no-member\n",
"\n",
" # Solve the problem.\n",
Expand Down
28 changes: 18 additions & 10 deletions examples/notebook/constraint_solver/tsp.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,11 @@
"metadata": {},
"outputs": [],
"source": [
"from ortools.constraint_solver import routing_enums_pb2\n",
"from ortools.constraint_solver import pywrapcp\n",
"from ortools.routing import enums_pb2\n",
"\n",
"FirstSolutionStrategy = enums_pb2.FirstSolutionStrategy\n",
"RoutingSearchStatus = enums_pb2.RoutingSearchStatus\n",
"\n",
"\n",
"def create_data_model():\n",
Expand Down Expand Up @@ -140,16 +143,24 @@
" return distance_callback\n",
"\n",
"\n",
"def print_solution(manager, routing, assignment):\n",
"def print_solution(manager, routing, solution):\n",
" \"\"\"Prints assignment on console.\"\"\"\n",
" print(f\"Objective: {assignment.ObjectiveValue()}\")\n",
" status = routing.status()\n",
" print(f\"Status: {RoutingSearchStatus.Value.Name(status)}\")\n",
" if (\n",
" status != RoutingSearchStatus.ROUTING_OPTIMAL\n",
" and status != RoutingSearchStatus.ROUTING_SUCCESS\n",
" ):\n",
" print(\"No solution found!\")\n",
" return\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 += f\" {manager.IndexToNode(index)} ->\"\n",
" previous_index = index\n",
" index = assignment.Value(routing.NextVar(index))\n",
" index = solution.Value(routing.NextVar(index))\n",
" route_distance += routing.GetArcCostForVehicle(previous_index, index, 0)\n",
" plan_output += f\" {manager.IndexToNode(index)}\\n\"\n",
" plan_output += f\"Distance of the route: {route_distance}m\\n\"\n",
Expand Down Expand Up @@ -178,16 +189,13 @@
"\n",
" # Setting first solution heuristic.\n",
" search_parameters = pywrapcp.DefaultRoutingSearchParameters()\n",
" search_parameters.first_solution_strategy = (\n",
" routing_enums_pb2.FirstSolutionStrategy.PATH_CHEAPEST_ARC\n",
" )\n",
" search_parameters.first_solution_strategy = FirstSolutionStrategy.PATH_CHEAPEST_ARC\n",
"\n",
" # Solve the problem.\n",
" assignment = routing.SolveWithParameters(search_parameters)\n",
" solution = routing.SolveWithParameters(search_parameters)\n",
"\n",
" # Print solution on console.\n",
" if assignment:\n",
" print_solution(manager, routing, assignment)\n",
" print_solution(manager, routing, solution)\n",
"\n",
"\n",
"main()\n",
Expand Down
4 changes: 2 additions & 2 deletions examples/notebook/constraint_solver/tsp_circuit_board.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@
"outputs": [],
"source": [
"import math\n",
"from ortools.constraint_solver import routing_enums_pb2\n",
"from ortools.constraint_solver import pywrapcp\n",
"from ortools.routing import enums_pb2\n",
"\n",
"\n",
"def create_data_model():\n",
Expand Down Expand Up @@ -210,7 +210,7 @@
" # Setting first solution heuristic.\n",
" search_parameters = pywrapcp.DefaultRoutingSearchParameters()\n",
" search_parameters.first_solution_strategy = (\n",
" routing_enums_pb2.FirstSolutionStrategy.PATH_CHEAPEST_ARC\n",
" enums_pb2.FirstSolutionStrategy.PATH_CHEAPEST_ARC\n",
" )\n",
"\n",
" # Solve the problem.\n",
Expand Down
4 changes: 2 additions & 2 deletions examples/notebook/constraint_solver/tsp_cities.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,8 @@
"metadata": {},
"outputs": [],
"source": [
"from ortools.constraint_solver import routing_enums_pb2\n",
"from ortools.constraint_solver import pywrapcp\n",
"from ortools.routing import enums_pb2\n",
"\n",
"\n",
"def create_data_model():\n",
Expand Down Expand Up @@ -155,7 +155,7 @@
" # Setting first solution heuristic.\n",
" search_parameters = pywrapcp.DefaultRoutingSearchParameters()\n",
" search_parameters.first_solution_strategy = (\n",
" routing_enums_pb2.FirstSolutionStrategy.PATH_CHEAPEST_ARC\n",
" enums_pb2.FirstSolutionStrategy.PATH_CHEAPEST_ARC\n",
" )\n",
"\n",
" # Solve the problem.\n",
Expand Down
4 changes: 2 additions & 2 deletions examples/notebook/constraint_solver/tsp_distance_matrix.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,8 @@
"metadata": {},
"outputs": [],
"source": [
"from ortools.constraint_solver import routing_enums_pb2\n",
"from ortools.constraint_solver import pywrapcp\n",
"from ortools.routing import enums_pb2\n",
"\n",
"\n",
"def create_data_model():\n",
Expand Down Expand Up @@ -161,7 +161,7 @@
" # Setting first solution heuristic.\n",
" search_parameters = pywrapcp.DefaultRoutingSearchParameters()\n",
" search_parameters.first_solution_strategy = (\n",
" routing_enums_pb2.FirstSolutionStrategy.PATH_CHEAPEST_ARC\n",
" enums_pb2.FirstSolutionStrategy.PATH_CHEAPEST_ARC\n",
" )\n",
"\n",
" # Solve the problem.\n",
Expand Down
45 changes: 25 additions & 20 deletions examples/notebook/constraint_solver/vrp.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,12 @@
"\n",
"Simple Vehicles 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",
"problem.\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"
]
},
Expand All @@ -91,8 +91,11 @@
"metadata": {},
"outputs": [],
"source": [
"from ortools.constraint_solver import routing_enums_pb2\n",
"from ortools.constraint_solver import pywrapcp\n",
"from ortools.routing import enums_pb2\n",
"\n",
"FirstSolutionStrategy = enums_pb2.FirstSolutionStrategy\n",
"RoutingSearchStatus = enums_pb2.RoutingSearchStatus\n",
"\n",
"\n",
"def create_data_model():\n",
Expand Down Expand Up @@ -124,20 +127,28 @@
" return data\n",
"\n",
"\n",
"def print_solution(data, manager, routing, solution):\n",
" \"\"\"Prints solution on console.\"\"\"\n",
"def print_solution(manager, routing, solution):\n",
" \"\"\"Prints assignment on console.\"\"\"\n",
" status = routing.status()\n",
" print(f\"Status: {RoutingSearchStatus.Value.Name(status)}\")\n",
" if (\n",
" status != RoutingSearchStatus.ROUTING_OPTIMAL\n",
" and status != RoutingSearchStatus.ROUTING_SUCCESS\n",
" ):\n",
" print(\"No solution found!\")\n",
" return\n",
" print(f\"Objective: {solution.ObjectiveValue()}\")\n",
" total_distance = 0\n",
" for vehicle_id in range(data[\"num_vehicles\"]):\n",
" index = routing.Start(vehicle_id)\n",
" plan_output = f\"Route for vehicle {vehicle_id}:\\n\"\n",
" for vehicle_index in range(manager.GetNumberOfVehicles()):\n",
" index = routing.Start(vehicle_index)\n",
" plan_output = f\"Route for vehicle {vehicle_index}:\\n\"\n",
" route_distance = 0\n",
" while not routing.IsEnd(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",
" previous_index, index, vehicle_index\n",
" )\n",
" plan_output += f\" {manager.IndexToNode(index)}\\n\"\n",
" plan_output += f\"Distance of the route: {route_distance}m\\n\"\n",
Expand All @@ -146,7 +157,6 @@
" print(f\"Total Distance of all routes: {total_distance}m\")\n",
"\n",
"\n",
"\n",
"def main():\n",
" \"\"\"Entry point of the program.\"\"\"\n",
" # Instantiate the data problem.\n",
Expand Down Expand Up @@ -175,18 +185,13 @@
"\n",
" # Setting first solution heuristic.\n",
" search_parameters = pywrapcp.DefaultRoutingSearchParameters()\n",
" search_parameters.first_solution_strategy = (\n",
" routing_enums_pb2.FirstSolutionStrategy.PATH_CHEAPEST_ARC\n",
" )\n",
" search_parameters.first_solution_strategy = FirstSolutionStrategy.PATH_CHEAPEST_ARC\n",
"\n",
" # Solve the problem.\n",
" solution = routing.SolveWithParameters(search_parameters)\n",
"\n",
" # Print solution on console.\n",
" if solution:\n",
" print_solution(data, manager, routing, solution)\n",
" else:\n",
" print(\"No solution found !\")\n",
" print_solution(manager, routing, solution)\n",
"\n",
"\n",
"main()\n",
Expand Down
16 changes: 8 additions & 8 deletions examples/notebook/constraint_solver/vrp_breaks.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,12 @@
"\n",
"Vehicle Routing Problem (VRP) with breaks.\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",
"problem.\n",
"A description of the problem can be found here:\n",
"http://en.wikipedia.org/wiki/Vehicle_routing_problem.\n",
"\n",
" Durations are in minutes.\n",
"Durations are in minutes.\n",
"\n"
]
},
Expand All @@ -91,8 +91,8 @@
"metadata": {},
"outputs": [],
"source": [
"from ortools.constraint_solver import routing_enums_pb2\n",
"from ortools.constraint_solver import pywrapcp\n",
"from ortools.routing import enums_pb2\n",
"\n",
"\n",
"def create_data_model():\n",
Expand Down Expand Up @@ -224,10 +224,10 @@
" # Setting first solution heuristic.\n",
" search_parameters = pywrapcp.DefaultRoutingSearchParameters()\n",
" search_parameters.first_solution_strategy = (\n",
" routing_enums_pb2.FirstSolutionStrategy.PATH_CHEAPEST_ARC\n",
" enums_pb2.FirstSolutionStrategy.PATH_CHEAPEST_ARC\n",
" )\n",
" search_parameters.local_search_metaheuristic = (\n",
" routing_enums_pb2.LocalSearchMetaheuristic.GUIDED_LOCAL_SEARCH\n",
" enums_pb2.LocalSearchMetaheuristic.GUIDED_LOCAL_SEARCH\n",
" )\n",
" # search_parameters.log_search = True\n",
" search_parameters.time_limit.FromSeconds(2)\n",
Expand Down

0 comments on commit 32d962c

Please sign in to comment.