Files
ortools-clone/examples/notebook/contrib/lectures.ipynb
2022-06-27 15:42:26 +02:00

199 lines
5.5 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": [
"# lectures"
]
},
{
"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/lectures.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/lectures.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",
" Lectures problem in Google CP Solver.\n",
"\n",
" Biggs: Discrete Mathematics (2nd ed), page 187.\n",
" '''\n",
" Suppose we wish to schedule six one-hour lectures, v1, v2, v3, v4, v5, v6.\n",
" Among the the potential audience there are people who wish to hear both\n",
"\n",
" - v1 and v2\n",
" - v1 and v4\n",
" - v3 and v5\n",
" - v2 and v6\n",
" - v4 and v5\n",
" - v5 and v6\n",
" - v1 and v6\n",
"\n",
" How many hours are necessary in order that the lectures can be given\n",
" without clashes?\n",
" '''\n",
"\n",
" Compare with the following models:\n",
" * MiniZinc: http://www.hakank.org/minizinc/lectures.mzn\n",
" * SICstus: http://hakank.org/sicstus/lectures.pl\n",
" * ECLiPSe: http://hakank.org/eclipse/lectures.ecl\n",
" * Gecode: http://hakank.org/gecode/lectures.cpp\n",
"\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"
]
},
{
"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():\n",
"\n",
" # Create the solver.\n",
" solver = pywrapcp.Solver('Lectures')\n",
"\n",
" #\n",
" # data\n",
" #\n",
"\n",
" #\n",
" # The schedule requirements:\n",
" # lecture a cannot be held at the same time as b\n",
" # Note: 1-based\n",
" g = [[1, 2], [1, 4], [3, 5], [2, 6], [4, 5], [5, 6], [1, 6]]\n",
"\n",
" # number of nodes\n",
" n = 6\n",
"\n",
" # number of edges\n",
" edges = len(g)\n",
"\n",
" #\n",
" # declare variables\n",
" #\n",
" v = [solver.IntVar(0, n - 1, 'v[%i]' % i) for i in range(n)]\n",
"\n",
" # maximum color, to minimize\n",
" # Note: since Python is 0-based, the\n",
" # number of colors is +1\n",
" max_c = solver.IntVar(0, n - 1, 'max_c')\n",
"\n",
" #\n",
" # constraints\n",
" #\n",
" solver.Add(max_c == solver.Max(v))\n",
"\n",
" # ensure that there are no clashes\n",
" # also, adjust to 0-base\n",
" for i in range(edges):\n",
" solver.Add(v[g[i][0] - 1] != v[g[i][1] - 1])\n",
"\n",
" # symmetry breaking:\n",
" # - v0 has the color 0,\n",
" # - v1 has either color 0 or 1\n",
" solver.Add(v[0] == 0)\n",
" solver.Add(v[1] <= 1)\n",
"\n",
" # objective\n",
" objective = solver.Minimize(max_c, 1)\n",
"\n",
" #\n",
" # solution and search\n",
" #\n",
" db = solver.Phase(v, solver.CHOOSE_MIN_SIZE_LOWEST_MIN,\n",
" solver.ASSIGN_CENTER_VALUE)\n",
"\n",
" solver.NewSearch(db, [objective])\n",
"\n",
" num_solutions = 0\n",
" while solver.NextSolution():\n",
" num_solutions += 1\n",
" print('max_c:', max_c.Value() + 1, 'colors')\n",
" print('v:', [v[i].Value() for i in range(n)])\n",
" print()\n",
"\n",
" print('num_solutions:', num_solutions)\n",
" print('failures:', solver.Failures())\n",
" print('branches:', solver.Branches())\n",
" print('WallTime:', solver.WallTime(), 'ms')\n",
"\n",
"\n",
"main()\n",
"\n"
]
}
],
"metadata": {},
"nbformat": 4,
"nbformat_minor": 5
}