170 lines
5.8 KiB
Plaintext
170 lines
5.8 KiB
Plaintext
{
|
|
"cells": [
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "58e307f8",
|
|
"metadata": {},
|
|
"source": [
|
|
"##### Copyright 2021 Google LLC."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "e54fadbd",
|
|
"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": "6231fc22",
|
|
"metadata": {},
|
|
"source": [
|
|
"# simple_routing_program"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "53c76b90",
|
|
"metadata": {},
|
|
"source": [
|
|
"<table align=\"left\">\n",
|
|
"<td>\n",
|
|
"<a href=\"https://colab.research.google.com/github/google/or-tools/blob/master/examples/notebook/constraint_solver/simple_routing_program.ipynb\"><img src=\"https://raw.githubusercontent.com/google/or-tools/master/tools/colab_32px.png\"/>Run in Google Colab</a>\n",
|
|
"</td>\n",
|
|
"<td>\n",
|
|
"<a href=\"https://github.com/google/or-tools/blob/master/ortools/constraint_solver/samples/simple_routing_program.py\"><img src=\"https://raw.githubusercontent.com/google/or-tools/master/tools/github_32px.png\"/>View source on GitHub</a>\n",
|
|
"</td>\n",
|
|
"</table>"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "14872edb",
|
|
"metadata": {},
|
|
"source": [
|
|
"First, you must install [ortools](https://pypi.org/project/ortools/) package in this colab."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "5ce7a297",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"!pip install ortools"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "d34ffa99",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"#!/usr/bin/env python3\n",
|
|
"# Copyright 2010-2021 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",
|
|
"\"\"\"Vehicle Routing example.\"\"\"\n",
|
|
"\n",
|
|
"# [START import]\n",
|
|
"from ortools.constraint_solver import routing_enums_pb2\n",
|
|
"from ortools.constraint_solver import pywrapcp\n",
|
|
"# [END import]\n",
|
|
"\n",
|
|
"\n",
|
|
"\"\"\"Entry point of the program.\"\"\"\n",
|
|
"# Instantiate the data problem.\n",
|
|
"# [START data]\n",
|
|
"num_locations = 5\n",
|
|
"num_vehicles = 1\n",
|
|
"depot = 0\n",
|
|
"# [END data]\n",
|
|
"\n",
|
|
"# Create the routing index manager.\n",
|
|
"# [START index_manager]\n",
|
|
"manager = pywrapcp.RoutingIndexManager(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 absolute difference between the two nodes.\"\"\"\n",
|
|
" # Convert from routing variable Index to user NodeIndex.\n",
|
|
" from_node = int(manager.IndexToNode(from_index))\n",
|
|
" to_node = int(manager.IndexToNode(to_index))\n",
|
|
" return abs(to_node - from_node)\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",
|
|
"# 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) # pylint: disable=no-member\n",
|
|
"# [END parameters]\n",
|
|
"\n",
|
|
"# Solve the problem.\n",
|
|
"# [START solve]\n",
|
|
"assignment = routing.SolveWithParameters(search_parameters)\n",
|
|
"# [END solve]\n",
|
|
"\n",
|
|
"# Print solution on console.\n",
|
|
"# [START print_solution]\n",
|
|
"print('Objective: {}'.format(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",
|
|
" 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",
|
|
"print(plan_output)\n",
|
|
"# [END print_solution]\n",
|
|
"\n"
|
|
]
|
|
}
|
|
],
|
|
"metadata": {},
|
|
"nbformat": 4,
|
|
"nbformat_minor": 5
|
|
}
|