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

216 lines
6.8 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": [
"# labeled_dice"
]
},
{
"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/labeled_dice.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/labeled_dice.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",
" Labeled dice problem in Google CP Solver.\n",
"\n",
" From Jim Orlin 'Colored letters, labeled dice: a logic puzzle'\n",
" http://jimorlin.wordpress.com/2009/02/17/colored-letters-labeled-dice-a-logic-puzzle/\n",
" '''\n",
" My daughter Jenn bough a puzzle book, and showed me a cute puzzle. There\n",
" are 13 words as follows: BUOY, CAVE, CELT, FLUB, FORK, HEMP, JUDY,\n",
" JUNK, LIMN, QUIP, SWAG, VISA, WISH.\n",
"\n",
" There are 24 different letters that appear in the 13 words. The question\n",
" is: can one assign the 24 letters to 4 different cubes so that the\n",
" four letters of each word appears on different cubes. (There is one\n",
" letter from each word on each cube.) It might be fun for you to try\n",
" it. I'll give a small hint at the end of this post. The puzzle was\n",
" created by Humphrey Dudley.\n",
" '''\n",
"\n",
" Jim Orlin's followup 'Update on Logic Puzzle':\n",
" http://jimorlin.wordpress.com/2009/02/21/update-on-logic-puzzle/\n",
"\n",
"\n",
" Compare with the following models:\n",
" * ECLiPSe: http://hakank.org/eclipse/labeled_dice.ecl\n",
" * Comet : http://www.hakank.org/comet/labeled_dice.co\n",
" * Gecode : http://hakank.org/gecode/labeled_dice.cpp\n",
" * SICStus: http://hakank.org/sicstus/labeled_dice.pl\n",
" * Zinc : http://hakank.org/minizinc/labeled_dice.zinc\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": [
"from ortools.constraint_solver import pywrapcp\n",
"\n",
"\n",
"def main():\n",
"\n",
" # Create the solver.\n",
" solver = pywrapcp.Solver(\"Labeled dice\")\n",
"\n",
" #\n",
" # data\n",
" #\n",
" n = 4\n",
" m = 24\n",
" A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, Y = (\n",
" list(range(m)))\n",
" letters = [\n",
" \"A\", \"B\", \"C\", \"D\", \"E\", \"F\", \"G\", \"H\", \"I\", \"J\", \"K\", \"L\", \"M\", \"N\", \"O\",\n",
" \"P\", \"Q\", \"R\", \"S\", \"T\", \"U\", \"V\", \"W\", \"Y\"\n",
" ]\n",
"\n",
" num_words = 13\n",
" words = [[B, U, O, Y], [C, A, V, E], [C, E, L, T], [F, L, U, B], [F, O, R, K],\n",
" [H, E, M, P], [J, U, D, Y], [J, U, N, K], [L, I, M, N], [Q, U, I, P],\n",
" [S, W, A, G], [V, I, S, A], [W, I, S, H]]\n",
"\n",
" #\n",
" # declare variables\n",
" #\n",
" dice = [solver.IntVar(0, n - 1, \"dice[%i]\" % i) for i in range(m)]\n",
"\n",
" #\n",
" # constraints\n",
" #\n",
"\n",
" # the letters in a word must be on a different die\n",
" for i in range(num_words):\n",
" solver.Add(solver.AllDifferent([dice[words[i][j]] for j in range(n)]))\n",
"\n",
" # there must be exactly 6 letters of each die\n",
" for i in range(n):\n",
" b = [solver.IsEqualCstVar(dice[j], i) for j in range(m)]\n",
" solver.Add(solver.Sum(b) == 6)\n",
"\n",
" #\n",
" # solution and search\n",
" #\n",
" solution = solver.Assignment()\n",
" solution.Add(dice)\n",
"\n",
" db = solver.Phase(dice, solver.CHOOSE_FIRST_UNBOUND, solver.ASSIGN_MIN_VALUE)\n",
"\n",
" #\n",
" # result\n",
" #\n",
" solver.NewSearch(db)\n",
" num_solutions = 0\n",
" while solver.NextSolution():\n",
" num_solutions += 1\n",
" # print \"dice:\", [(letters[i],dice[i].Value()) for i in range(m)]\n",
" for d in range(n):\n",
" print(\"die %i:\" % d, end=\" \")\n",
" for i in range(m):\n",
" if dice[i].Value() == d:\n",
" print(letters[i], end=\" \")\n",
" print()\n",
"\n",
" print(\"The words with the cube label:\")\n",
" for i in range(num_words):\n",
" for j in range(n):\n",
" print(\n",
" \"%s (%i)\" % (letters[words[i][j]], dice[words[i][j]].Value()),\n",
" end=\" \")\n",
" print()\n",
"\n",
" print()\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",
"main()\n",
"\n"
]
}
],
"metadata": {
"language_info": {
"name": "python"
}
},
"nbformat": 4,
"nbformat_minor": 5
}