131 lines
5.0 KiB
Plaintext
131 lines
5.0 KiB
Plaintext
{
|
|
"cells": [
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"# Copyright 2010-2018 Google LLC\n",
|
|
"# Licensed under the Apache License, Version 2.0 (the \"License\");\n",
|
|
"# you may not use this file except in compliance with the License.\n",
|
|
"# You may obtain a copy of the License at\n",
|
|
"#\n",
|
|
"# http://www.apache.org/licenses/LICENSE-2.0\n",
|
|
"#\n",
|
|
"# Unless required by applicable law or agreed to in writing, software\n",
|
|
"# distributed under the License is distributed on an \"AS IS\" BASIS,\n",
|
|
"# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n",
|
|
"# See the License for the specific language governing permissions and\n",
|
|
"# limitations under the License.\n",
|
|
"# [START program]\n",
|
|
"\"\"\"Vehicles Routing Problem (VRP).\"\"\"\n",
|
|
"\n",
|
|
"# [START import]\n",
|
|
"from __future__ import print_function\n",
|
|
"from ortools.constraint_solver import routing_enums_pb2\n",
|
|
"from ortools.constraint_solver import pywrapcp\n",
|
|
"# [END import]\n",
|
|
"\n",
|
|
"# [START solution_printer]\n",
|
|
"def print_solution(manager, routing, solution):\n",
|
|
" \"\"\"Prints solution on console.\"\"\"\n",
|
|
" 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",
|
|
" route_distance = 0\n",
|
|
" while not routing.IsEnd(index):\n",
|
|
" plan_output += ' {} -> '.format(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",
|
|
" 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",
|
|
"# [END solution_printer]\n",
|
|
"\n",
|
|
"\n",
|
|
"\"\"\"Solve the CVRP problem.\"\"\"\n",
|
|
"# Instantiate the data problem.\n",
|
|
"# [START data]\n",
|
|
"num_locations = 20;\n",
|
|
"num_vehicles = 5;\n",
|
|
"depot = 0;\n",
|
|
"# [END data]\n",
|
|
"\n",
|
|
"# Create the routing index manager.\n",
|
|
"# [START index_manager]\n",
|
|
"manager = pywrapcp.RoutingIndexManager(\n",
|
|
" num_locations, num_vehicles, depot)\n",
|
|
"# [END index_manager]\n",
|
|
"\n",
|
|
"# Create Routing Model.\n",
|
|
"# [START routing_model]\n",
|
|
"routing = pywrapcp.RoutingModel(manager)\n",
|
|
"\n",
|
|
"# [END routing_model]\n",
|
|
"\n",
|
|
"# Create and register a transit callback.\n",
|
|
"# [START transit_callback]\n",
|
|
"def distance_callback(from_index, to_index):\n",
|
|
" \"\"\"Returns the distance between the two nodes.\"\"\"\n",
|
|
" # Convert from routing variable Index to distance matrix NodeIndex.\n",
|
|
" from_node = manager.IndexToNode(from_index)\n",
|
|
" to_node = manager.IndexToNode(to_index)\n",
|
|
" return 1\n",
|
|
"\n",
|
|
"transit_callback_index = routing.RegisterTransitCallback(distance_callback)\n",
|
|
"# [END transit_callback]\n",
|
|
"\n",
|
|
"# Define cost of each arc.\n",
|
|
"# [START arc_cost]\n",
|
|
"routing.SetArcCostEvaluatorOfAllVehicles(transit_callback_index)\n",
|
|
"# [END arc_cost]\n",
|
|
"\n",
|
|
"# Add Distance constraint.\n",
|
|
"# [START distance_constraint]\n",
|
|
"dimension_name = 'Distance'\n",
|
|
"routing.AddDimension(\n",
|
|
" transit_callback_index,\n",
|
|
" 0, # no slack\n",
|
|
" 3000, # vehicle maximum travel distance\n",
|
|
" True, # start cumul to zero\n",
|
|
" dimension_name)\n",
|
|
"distance_dimension = routing.GetDimensionOrDie(dimension_name)\n",
|
|
"distance_dimension.SetGlobalSpanCostCoefficient(100)\n",
|
|
"# [END distance_constraint]\n",
|
|
"\n",
|
|
"# Setting first solution heuristic.\n",
|
|
"# [START parameters]\n",
|
|
"search_parameters = pywrapcp.DefaultRoutingSearchParameters()\n",
|
|
"search_parameters.first_solution_strategy = (\n",
|
|
" routing_enums_pb2.FirstSolutionStrategy.PATH_CHEAPEST_ARC)\n",
|
|
"search_parameters.local_search_metaheuristic = (\n",
|
|
" routing_enums_pb2.LocalSearchMetaheuristic.GUIDED_LOCAL_SEARCH)\n",
|
|
"search_parameters.log_search = True\n",
|
|
"search_parameters.time_limit.FromSeconds(10)\n",
|
|
"# [END parameters]\n",
|
|
"\n",
|
|
"# Solve the problem.\n",
|
|
"# [START solve]\n",
|
|
"solution = routing.SolveWithParameters(search_parameters)\n",
|
|
"# [END solve]\n",
|
|
"\n",
|
|
"# Print solution on console.\n",
|
|
"# [START print_solution]\n",
|
|
"if solution:\n",
|
|
" print_solution(manager, routing, solution)\n",
|
|
"# [END print_solution]\n",
|
|
"\n"
|
|
]
|
|
}
|
|
],
|
|
"metadata": {},
|
|
"nbformat": 4,
|
|
"nbformat_minor": 4
|
|
}
|