Files
ortools-clone/examples/notebook/contrib/photo_problem.ipynb
2025-02-04 18:04:03 +01:00

235 lines
7.2 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": [
"# photo_problem"
]
},
{
"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/photo_problem.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/photo_problem.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",
" Photo problem in Google CP Solver.\n",
"\n",
" Problem statement from Mozart/Oz tutorial:\n",
" http://www.mozart-oz.org/home/doc/fdt/node37.html#section.reified.photo\n",
" '''\n",
" Betty, Chris, Donald, Fred, Gary, Mary, and Paul want to align in one\n",
" row for taking a photo. Some of them have preferences next to whom\n",
" they want to stand:\n",
"\n",
" 1. Betty wants to stand next to Gary and Mary.\n",
" 2. Chris wants to stand next to Betty and Gary.\n",
" 3. Fred wants to stand next to Mary and Donald.\n",
" 4. Paul wants to stand next to Fred and Donald.\n",
"\n",
" Obviously, it is impossible to satisfy all preferences. Can you find\n",
" an alignment that maximizes the number of satisfied preferences?\n",
" '''\n",
"\n",
" Oz solution:\n",
" 6 # alignment(betty:5 chris:6 donald:1 fred:3 gary:7 mary:4 paul:2)\n",
" [5, 6, 1, 3, 7, 4, 2]\n",
"\n",
"\n",
" Compare with the following models:\n",
" * MiniZinc: http://www.hakank.org/minizinc/photo_hkj.mzn\n",
" * Comet: http://hakank.org/comet/photo_problem.co\n",
" * SICStus: http://hakank.org/sicstus/photo_problem.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"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "code",
"metadata": {},
"outputs": [],
"source": [
"import sys\n",
"\n",
"from ortools.constraint_solver import pywrapcp\n",
"\n",
"\n",
"def main(show_all_max=0):\n",
"\n",
" # Create the solver.\n",
" solver = pywrapcp.Solver(\"Photo problem\")\n",
"\n",
" #\n",
" # data\n",
" #\n",
" persons = [\"Betty\", \"Chris\", \"Donald\", \"Fred\", \"Gary\", \"Mary\", \"Paul\"]\n",
" n = len(persons)\n",
" preferences = [\n",
" # 0 1 2 3 4 5 6\n",
" # B C D F G M P\n",
" [0, 0, 0, 0, 1, 1, 0], # Betty 0\n",
" [1, 0, 0, 0, 1, 0, 0], # Chris 1\n",
" [0, 0, 0, 0, 0, 0, 0], # Donald 2\n",
" [0, 0, 1, 0, 0, 1, 0], # Fred 3\n",
" [0, 0, 0, 0, 0, 0, 0], # Gary 4\n",
" [0, 0, 0, 0, 0, 0, 0], # Mary 5\n",
" [0, 0, 1, 1, 0, 0, 0] # Paul 6\n",
" ]\n",
"\n",
" print(\"\"\"Preferences:\n",
" 1. Betty wants to stand next to Gary and Mary.\n",
" 2. Chris wants to stand next to Betty and Gary.\n",
" 3. Fred wants to stand next to Mary and Donald.\n",
" 4. Paul wants to stand next to Fred and Donald.\n",
" \"\"\")\n",
"\n",
" #\n",
" # declare variables\n",
" #\n",
" positions = [solver.IntVar(0, n - 1, \"positions[%i]\" % i) for i in range(n)]\n",
"\n",
" # successful preferences\n",
" z = solver.IntVar(0, n * n, \"z\")\n",
"\n",
" #\n",
" # constraints\n",
" #\n",
" solver.Add(solver.AllDifferent(positions))\n",
"\n",
" # calculate all the successful preferences\n",
" b = [\n",
" solver.IsEqualCstVar(abs(positions[i] - positions[j]), 1)\n",
" for i in range(n)\n",
" for j in range(n)\n",
" if preferences[i][j] == 1\n",
" ]\n",
" solver.Add(z == solver.Sum(b))\n",
"\n",
" #\n",
" # Symmetry breaking (from the Oz page):\n",
" # Fred is somewhere left of Betty\n",
" solver.Add(positions[3] < positions[0])\n",
"\n",
" # objective\n",
" objective = solver.Maximize(z, 1)\n",
" if show_all_max != 0:\n",
" print(\"Showing all maximum solutions (z == 6).\\n\")\n",
" solver.Add(z == 6)\n",
"\n",
" #\n",
" # search and result\n",
" #\n",
" db = solver.Phase(positions, solver.CHOOSE_FIRST_UNBOUND,\n",
" solver.ASSIGN_MAX_VALUE)\n",
"\n",
" if show_all_max == 0:\n",
" solver.NewSearch(db, [objective])\n",
" else:\n",
" solver.NewSearch(db)\n",
"\n",
" num_solutions = 0\n",
" while solver.NextSolution():\n",
" print(\"z:\", z.Value())\n",
" p = [positions[i].Value() for i in range(n)]\n",
"\n",
" print(\" \".join(\n",
" [persons[j] for i in range(n) for j in range(n) if p[j] == i]))\n",
" print(\"Successful preferences:\")\n",
" for i in range(n):\n",
" for j in range(n):\n",
" if preferences[i][j] == 1 and abs(p[i] - p[j]) == 1:\n",
" print(\"\\t\", persons[i], persons[j])\n",
" print()\n",
" num_solutions += 1\n",
"\n",
" solver.EndSearch()\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",
"\n",
"\n",
"show_all_max = 0 # show all maximal solutions\n",
"if len(sys.argv) > 1:\n",
" show_all_max = 1\n",
"main(show_all_max)\n",
"\n"
]
}
],
"metadata": {
"language_info": {
"name": "python"
}
},
"nbformat": 4,
"nbformat_minor": 5
}