176 lines
6.4 KiB
Plaintext
176 lines
6.4 KiB
Plaintext
{
|
|
"cells": [
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "google",
|
|
"metadata": {},
|
|
"source": [
|
|
"##### Copyright 2022 Google LLC."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "apache",
|
|
"metadata": {},
|
|
"source": [
|
|
"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"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "basename",
|
|
"metadata": {},
|
|
"source": [
|
|
"# tsp_cities"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "link",
|
|
"metadata": {},
|
|
"source": [
|
|
"<table align=\"left\">\n",
|
|
"<td>\n",
|
|
"<a href=\"https://colab.research.google.com/github/google/or-tools/blob/main/examples/notebook/constraint_solver/tsp_cities.ipynb\"><img src=\"https://raw.githubusercontent.com/google/or-tools/main/tools/colab_32px.png\"/>Run in Google Colab</a>\n",
|
|
"</td>\n",
|
|
"<td>\n",
|
|
"<a href=\"https://github.com/google/or-tools/blob/main/ortools/constraint_solver/samples/tsp_cities.py\"><img src=\"https://raw.githubusercontent.com/google/or-tools/main/tools/github_32px.png\"/>View source on GitHub</a>\n",
|
|
"</td>\n",
|
|
"</table>"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "doc",
|
|
"metadata": {},
|
|
"source": [
|
|
"First, you must install [ortools](https://pypi.org/project/ortools/) package in this colab."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "install",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"!pip install ortools"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "description",
|
|
"metadata": {},
|
|
"source": [
|
|
"\n",
|
|
"Simple Travelling Salesperson Problem (TSP) between cities.\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "code",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"from ortools.constraint_solver import routing_enums_pb2\n",
|
|
"from ortools.constraint_solver import pywrapcp\n",
|
|
"\n",
|
|
"\n",
|
|
"def create_data_model():\n",
|
|
" \"\"\"Stores the data for the problem.\"\"\"\n",
|
|
" data = {}\n",
|
|
" data['distance_matrix'] = [\n",
|
|
" [0, 2451, 713, 1018, 1631, 1374, 2408, 213, 2571, 875, 1420, 2145, 1972],\n",
|
|
" [2451, 0, 1745, 1524, 831, 1240, 959, 2596, 403, 1589, 1374, 357, 579],\n",
|
|
" [713, 1745, 0, 355, 920, 803, 1737, 851, 1858, 262, 940, 1453, 1260],\n",
|
|
" [1018, 1524, 355, 0, 700, 862, 1395, 1123, 1584, 466, 1056, 1280, 987],\n",
|
|
" [1631, 831, 920, 700, 0, 663, 1021, 1769, 949, 796, 879, 586, 371],\n",
|
|
" [1374, 1240, 803, 862, 663, 0, 1681, 1551, 1765, 547, 225, 887, 999],\n",
|
|
" [2408, 959, 1737, 1395, 1021, 1681, 0, 2493, 678, 1724, 1891, 1114, 701],\n",
|
|
" [213, 2596, 851, 1123, 1769, 1551, 2493, 0, 2699, 1038, 1605, 2300, 2099],\n",
|
|
" [2571, 403, 1858, 1584, 949, 1765, 678, 2699, 0, 1744, 1645, 653, 600],\n",
|
|
" [875, 1589, 262, 466, 796, 547, 1724, 1038, 1744, 0, 679, 1272, 1162],\n",
|
|
" [1420, 1374, 940, 1056, 879, 225, 1891, 1605, 1645, 679, 0, 1017, 1200],\n",
|
|
" [2145, 357, 1453, 1280, 586, 887, 1114, 2300, 653, 1272, 1017, 0, 504],\n",
|
|
" [1972, 579, 1260, 987, 371, 999, 701, 2099, 600, 1162, 1200, 504, 0],\n",
|
|
" ] # yapf: disable\n",
|
|
" data['num_vehicles'] = 1\n",
|
|
" data['depot'] = 0\n",
|
|
" return data\n",
|
|
"\n",
|
|
"\n",
|
|
"def print_solution(manager, routing, solution):\n",
|
|
" \"\"\"Prints solution on console.\"\"\"\n",
|
|
" print('Objective: {} miles'.format(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",
|
|
" 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",
|
|
" print(plan_output)\n",
|
|
" plan_output += 'Route distance: {}miles\\n'.format(route_distance)\n",
|
|
"\n",
|
|
"\n",
|
|
"def main():\n",
|
|
" \"\"\"Entry point of the program.\"\"\"\n",
|
|
" # Instantiate the data problem.\n",
|
|
" data = create_data_model()\n",
|
|
"\n",
|
|
" # Create the routing index manager.\n",
|
|
" manager = pywrapcp.RoutingIndexManager(len(data['distance_matrix']),\n",
|
|
" data['num_vehicles'], data['depot'])\n",
|
|
"\n",
|
|
" # Create Routing Model.\n",
|
|
" routing = pywrapcp.RoutingModel(manager)\n",
|
|
"\n",
|
|
"\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 data['distance_matrix'][from_node][to_node]\n",
|
|
"\n",
|
|
" transit_callback_index = routing.RegisterTransitCallback(distance_callback)\n",
|
|
"\n",
|
|
" # Define cost of each arc.\n",
|
|
" routing.SetArcCostEvaluatorOfAllVehicles(transit_callback_index)\n",
|
|
"\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",
|
|
" # Solve the problem.\n",
|
|
" solution = routing.SolveWithParameters(search_parameters)\n",
|
|
"\n",
|
|
" # Print solution on console.\n",
|
|
" if solution:\n",
|
|
" print_solution(manager, routing, solution)\n",
|
|
"\n",
|
|
"\n",
|
|
"main()\n",
|
|
"\n"
|
|
]
|
|
}
|
|
],
|
|
"metadata": {},
|
|
"nbformat": 4,
|
|
"nbformat_minor": 5
|
|
}
|