191 lines
5.7 KiB
Plaintext
191 lines
5.7 KiB
Plaintext
{
|
|
"cells": [
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "google",
|
|
"metadata": {},
|
|
"source": [
|
|
"##### Copyright 2025 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": [
|
|
"# bus_schedule"
|
|
]
|
|
},
|
|
{
|
|
"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/contrib/bus_schedule.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/examples/contrib/bus_schedule.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",
|
|
"\n",
|
|
" Bus scheduling in Google CP Solver.\n",
|
|
"\n",
|
|
"\n",
|
|
" Problem from Taha \"Introduction to Operations Research\", page 58.\n",
|
|
"\n",
|
|
" This is a slightly more general model than Taha's.\n",
|
|
"\n",
|
|
" Compare with the following models:\n",
|
|
" * MiniZinc: http://www.hakank.org/minizinc/bus_scheduling.mzn\n",
|
|
" * Comet : http://www.hakank.org/comet/bus_schedule.co\n",
|
|
" * ECLiPSe : http://www.hakank.org/eclipse/bus_schedule.ecl\n",
|
|
" * Gecode : http://www.hakank.org/gecode/bus_schedule.cpp\n",
|
|
" * Tailor/Essence' : http://www.hakank.org/tailor/bus_schedule.eprime\n",
|
|
" * SICStus: http://hakank.org/sicstus/bus_schedule.pl\n",
|
|
"\n",
|
|
" This model was created by Hakan Kjellerstrand (hakank@gmail.com)\n",
|
|
" Also see my other Google CP Solver models:\n",
|
|
" http://www.hakank.org/google_or_tools/\n",
|
|
"\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "code",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"import sys\n",
|
|
"from ortools.constraint_solver import pywrapcp\n",
|
|
"\n",
|
|
"\n",
|
|
"def main(num_buses_check=0):\n",
|
|
"\n",
|
|
" # Create the solver.\n",
|
|
" solver = pywrapcp.Solver(\"Bus scheduling\")\n",
|
|
"\n",
|
|
" # data\n",
|
|
" time_slots = 6\n",
|
|
" demands = [8, 10, 7, 12, 4, 4]\n",
|
|
" max_num = sum(demands)\n",
|
|
"\n",
|
|
" # declare variables\n",
|
|
" x = [solver.IntVar(0, max_num, \"x%i\" % i) for i in range(time_slots)]\n",
|
|
" num_buses = solver.IntVar(0, max_num, \"num_buses\")\n",
|
|
"\n",
|
|
" #\n",
|
|
" # constraints\n",
|
|
" #\n",
|
|
" solver.Add(num_buses == solver.Sum(x))\n",
|
|
"\n",
|
|
" # Meet the demands for this and the next time slot\n",
|
|
" for i in range(time_slots - 1):\n",
|
|
" solver.Add(x[i] + x[i + 1] >= demands[i])\n",
|
|
"\n",
|
|
" # The demand \"around the clock\"\n",
|
|
" solver.Add(x[time_slots - 1] + x[0] == demands[time_slots - 1])\n",
|
|
"\n",
|
|
" if num_buses_check > 0:\n",
|
|
" solver.Add(num_buses == num_buses_check)\n",
|
|
"\n",
|
|
" #\n",
|
|
" # solution and search\n",
|
|
" #\n",
|
|
" solution = solver.Assignment()\n",
|
|
" solution.Add(x)\n",
|
|
" solution.Add(num_buses)\n",
|
|
"\n",
|
|
" collector = solver.AllSolutionCollector(solution)\n",
|
|
" cargs = [collector]\n",
|
|
"\n",
|
|
" # objective\n",
|
|
" if num_buses_check == 0:\n",
|
|
" objective = solver.Minimize(num_buses, 1)\n",
|
|
" cargs.extend([objective])\n",
|
|
"\n",
|
|
" solver.Solve(\n",
|
|
" solver.Phase(x, solver.CHOOSE_FIRST_UNBOUND, solver.ASSIGN_MIN_VALUE),\n",
|
|
" cargs)\n",
|
|
"\n",
|
|
" num_solutions = collector.SolutionCount()\n",
|
|
" num_buses_check_value = 0\n",
|
|
" for s in range(num_solutions):\n",
|
|
" print(\"x:\", [collector.Value(s, x[i]) for i in range(len(x))], end=\" \")\n",
|
|
" num_buses_check_value = collector.Value(s, num_buses)\n",
|
|
" print(\" num_buses:\", num_buses_check_value)\n",
|
|
"\n",
|
|
" print()\n",
|
|
" print(\"num_solutions:\", num_solutions)\n",
|
|
" print(\"failures:\", solver.Failures())\n",
|
|
" print(\"branches:\", solver.Branches())\n",
|
|
" print(\"WallTime:\", solver.WallTime())\n",
|
|
" print()\n",
|
|
" if num_buses_check == 0:\n",
|
|
" return num_buses_check_value\n",
|
|
"\n",
|
|
"\n",
|
|
"print(\"Check for minimun number of buses\")\n",
|
|
"num_buses_check = main()\n",
|
|
"print(\"... got \", num_buses_check, \"buses\")\n",
|
|
"print(\"All solutions:\")\n",
|
|
"main(num_buses_check)\n",
|
|
"\n"
|
|
]
|
|
}
|
|
],
|
|
"metadata": {
|
|
"language_info": {
|
|
"name": "python"
|
|
}
|
|
},
|
|
"nbformat": 4,
|
|
"nbformat_minor": 5
|
|
}
|