Files
ortools-clone/examples/notebook/contrib/hidato.ipynb
2022-04-14 14:31:02 +02:00

291 lines
9.2 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": [
"# hidato"
]
},
{
"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/master/examples/notebook/contrib/hidato.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/examples/contrib/hidato.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": "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",
" Hidato puzzle in Google CP Solver.\n",
"\n",
" http://www.shockwave.com/gamelanding/hidato.jsp\n",
" http://www.hidato.com/\n",
" '''\n",
" Puzzles start semi-filled with numbered tiles.\n",
" The first and last numbers are circled.\n",
" Connect the numbers together to win. Consecutive\n",
" number must touch horizontally, vertically, or\n",
" diagonally.\n",
" '''\n",
"\n",
" Compare with the following models:\n",
" * MiniZinc: http://www.hakank.org/minizinc/hidato.mzn\n",
" * Gecode : http://www.hakank.org/gecode/hidato.cpp\n",
" * Comet : http://www.hakank.org/comet/hidato.co\n",
" * Tailopr/Essence': http://hakank.org/tailor/hidato.eprime\n",
" * ECLiPSe: http://hakank.org/eclipse/hidato.ecl\n",
" * SICStus: http://hakank.org/sicstus/hidato.pl\n",
"\n",
" Note: This model is very slow. Please see Laurent Perron's much faster\n",
" (and more elegant) model: hidato_table.py .\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(r, c):\n",
" # Create the solver.\n",
" solver = pywrapcp.Solver(\"hidato\")\n",
"\n",
" # data\n",
" # Simple problem\n",
" if r == 3 and c == 3:\n",
" puzzle = [[6, 0, 9], [0, 2, 8], [1, 0, 0]]\n",
"\n",
" if r == 7 and c == 7:\n",
" puzzle = [[0, 44, 41, 0, 0, 0, 0], [0, 43, 0, 28, 29, 0, 0],\n",
" [0, 1, 0, 0, 0, 33, 0], [0, 2, 25, 4, 34, 0, 36],\n",
" [49, 16, 0, 23, 0, 0, 0], [0, 19, 0, 0, 12, 7, 0],\n",
" [0, 0, 0, 14, 0, 0, 0]]\n",
"\n",
" # Problems from the book:\n",
" # Gyora Bededek: \"Hidato: 2000 Pure Logic Puzzles\"\n",
"\n",
" # Problem 1 (Practice)\n",
" # r = 5\n",
" # c = r\n",
" # puzzle = [\n",
" # [ 0, 0,20, 0, 0],\n",
" # [ 0, 0, 0,16,18],\n",
" # [22, 0,15, 0, 0],\n",
" # [23, 0, 1,14,11],\n",
" # [ 0,25, 0, 0,12],\n",
" # ]\n",
"\n",
" # Problem 2 (Practice)\n",
" if r == 5 and c == 5:\n",
" puzzle = [\n",
" [0, 0, 0, 0, 14],\n",
" [0, 18, 12, 0, 0],\n",
" [0, 0, 17, 4, 5],\n",
" [0, 0, 7, 0, 0],\n",
" [9, 8, 25, 1, 0],\n",
" ]\n",
"\n",
" # Problem 3 (Beginner)\n",
" if r == 6 and c == 6:\n",
" puzzle = [[0, 26, 0, 0, 0, 18], [0, 0, 27, 0, 0, 19], [31, 23, 0, 0, 14, 0],\n",
" [0, 33, 8, 0, 15, 1], [0, 0, 0, 5, 0, 0], [35, 36, 0, 10, 0, 0]]\n",
"\n",
" # Problem 15 (Intermediate)\n",
" # Note: This takes very long time to solve...\n",
" if r == 8 and c == 8:\n",
" puzzle = [[64, 0, 0, 0, 0, 0, 0, 0], [1, 63, 0, 59, 15, 57, 53, 0],\n",
" [0, 4, 0, 14, 0, 0, 0, 0], [3, 0, 11, 0, 20, 19, 0, 50],\n",
" [0, 0, 0, 0, 22, 0, 48, 40], [9, 0, 0, 32, 23, 0, 0, 41],\n",
" [27, 0, 0, 0, 36, 0, 46, 0], [28, 30, 0, 35, 0, 0, 0, 0]]\n",
"\n",
" print_game(puzzle, r, c)\n",
"\n",
" #\n",
" # declare variables\n",
" #\n",
" x = {}\n",
" for i in range(r):\n",
" for j in range(c):\n",
" x[(i, j)] = solver.IntVar(1, r * c, \"dice(%i,%i)\" % (i, j))\n",
" x_flat = [x[(i, j)] for i in range(r) for j in range(c)]\n",
"\n",
" #\n",
" # constraints\n",
" #\n",
" solver.Add(solver.AllDifferent(x_flat))\n",
"\n",
" #\n",
" # Fill in the clues\n",
" #\n",
" for i in range(r):\n",
" for j in range(c):\n",
" if puzzle[i][j] > 0:\n",
" solver.Add(x[(i, j)] == puzzle[i][j])\n",
"\n",
" # From the numbers k = 1 to r*c-1, find this position,\n",
" # and then the position of k+1\n",
" for k in range(1, r * c):\n",
" i = solver.IntVar(0, r)\n",
" j = solver.IntVar(0, c)\n",
" a = solver.IntVar(-1, 1)\n",
" b = solver.IntVar(-1, 1)\n",
"\n",
" # 1) First: fix \"this\" k\n",
" # 2) and then find the position of the next value (k+1)\n",
" # solver.Add(k == x[(i,j)])\n",
" solver.Add(k == solver.Element(x_flat, i * c + j))\n",
" # solver.Add(k + 1 == x[(i+a,j+b)])\n",
" solver.Add(k + 1 == solver.Element(x_flat, (i + a) * c + (j + b)))\n",
"\n",
" solver.Add(i + a >= 0)\n",
" solver.Add(j + b >= 0)\n",
" solver.Add(i + a < r)\n",
" solver.Add(j + b < c)\n",
"\n",
" # solver.Add(((a != 0) | (b != 0)))\n",
" a_nz = solver.BoolVar()\n",
" b_nz = solver.BoolVar()\n",
" solver.Add(a_nz == solver.IsDifferentCstVar(a, 0))\n",
" solver.Add(b_nz == solver.IsDifferentCstVar(b, 0))\n",
" solver.Add(a_nz + b_nz >= 1)\n",
"\n",
" #\n",
" # solution and search\n",
" #\n",
" solution = solver.Assignment()\n",
" solution.Add(x_flat)\n",
"\n",
" # db: DecisionBuilder\n",
" db = solver.Phase(\n",
" x_flat,\n",
" # solver.INT_VAR_DEFAULT\n",
" # solver.INT_VAR_SIMPLE\n",
" # solver.CHOOSE_RANDOM\n",
" # solver.CHOOSE_MIN_SIZE_LOWEST_MIN\n",
" # solver.CHOOSE_MIN_SIZE_HIGHEST_MIN\n",
" # solver.CHOOSE_MIN_SIZE_LOWEST_MAX\n",
" # solver.CHOOSE_MIN_SIZE_HIGHEST_MAX\n",
" # solver.CHOOSE_PATH\n",
" solver.CHOOSE_FIRST_UNBOUND,\n",
" # solver.INT_VALUE_DEFAULT\n",
" # solver.INT_VALUE_SIMPLE\n",
" # solver.ASSIGN_MAX_VALUE\n",
" # solver.ASSIGN_RANDOM_VALUE\n",
" # solver.ASSIGN_CENTER_VALUE\n",
" solver.ASSIGN_MIN_VALUE)\n",
"\n",
" solver.NewSearch(db)\n",
" num_solutions = 0\n",
" while solver.NextSolution():\n",
" num_solutions += 1\n",
" print(\"\\nSolution:\", num_solutions)\n",
" print_board(x, r, c)\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",
"def print_board(x, rows, cols):\n",
" for i in range(rows):\n",
" for j in range(cols):\n",
" print(\"% 2s\" % x[i, j].Value(), end=\" \")\n",
" print(\"\")\n",
"\n",
"\n",
"def print_game(game, rows, cols):\n",
" for i in range(rows):\n",
" for j in range(cols):\n",
" print(\"% 2s\" % game[i][j], end=\" \")\n",
" print(\"\")\n",
"\n",
"\n",
"# data\n",
"r = 3\n",
"c = r\n",
"if len(sys.argv) > 1:\n",
" r = int(sys.argv[1])\n",
" c = r\n",
"if len(sys.argv) > 2:\n",
" c = int(sys.argv[2])\n",
"main(r, c)\n",
"\n"
]
}
],
"metadata": {},
"nbformat": 4,
"nbformat_minor": 5
}