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

241 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": [
"# futoshiki"
]
},
{
"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/futoshiki.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/futoshiki.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",
" Futoshiki problem in Google CP Solver.\n",
"\n",
" From http://en.wikipedia.org/wiki/Futoshiki\n",
" '''\n",
" The puzzle is played on a square grid, such as 5 x 5. The objective\n",
" is to place the numbers 1 to 5 (or whatever the dimensions are)\n",
" such that each row, and column contains each of the digits 1 to 5.\n",
" Some digits may be given at the start. In addition, inequality\n",
" constraints are also initially specifed between some of the squares,\n",
" such that one must be higher or lower than its neighbour. These\n",
" constraints must be honoured as the grid is filled out.\n",
" '''\n",
"\n",
" Also see\n",
" http://www.guardian.co.uk/world/2006/sep/30/japan.estheraddley\n",
"\n",
"\n",
" This Google CP Solver model is inspired by the Minion/Tailor\n",
" example futoshiki.eprime.\n",
"\n",
"\n",
" Compare with the following models:\n",
" * MiniZinc: http://hakank.org/minizinc/futoshiki.mzn\n",
" * ECLiPSe: http://hakank.org/eclipse/futoshiki.ecl\n",
" * Gecode: http://hakank.org/gecode/futoshiki.cpp\n",
" * SICStus: http://hakank.org/sicstus/futoshiki.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": [
"from ortools.constraint_solver import pywrapcp\n",
"\n",
"\n",
"def main(values, lt):\n",
"\n",
" # Create the solver.\n",
" solver = pywrapcp.Solver(\"Futoshiki problem\")\n",
"\n",
" #\n",
" # data\n",
" #\n",
" size = len(values)\n",
" RANGE = list(range(size))\n",
" NUMQD = list(range(len(lt)))\n",
"\n",
" #\n",
" # variables\n",
" #\n",
" field = {}\n",
" for i in RANGE:\n",
" for j in RANGE:\n",
" field[i, j] = solver.IntVar(1, size, \"field[%i,%i]\" % (i, j))\n",
" field_flat = [field[i, j] for i in RANGE for j in RANGE]\n",
"\n",
" #\n",
" # constraints\n",
" #\n",
" # set initial values\n",
" for row in RANGE:\n",
" for col in RANGE:\n",
" if values[row][col] > 0:\n",
" solver.Add(field[row, col] == values[row][col])\n",
"\n",
" # all rows have to be different\n",
" for row in RANGE:\n",
" solver.Add(solver.AllDifferent([field[row, col] for col in RANGE]))\n",
"\n",
" # all columns have to be different\n",
" for col in RANGE:\n",
" solver.Add(solver.AllDifferent([field[row, col] for row in RANGE]))\n",
"\n",
" # all < constraints are satisfied\n",
" # Also: make 0-based\n",
" for i in NUMQD:\n",
" solver.Add(\n",
" field[lt[i][0] - 1, lt[i][1] - 1] < field[lt[i][2] - 1, lt[i][3] - 1])\n",
"\n",
" #\n",
" # search and result\n",
" #\n",
" db = solver.Phase(field_flat, solver.CHOOSE_FIRST_UNBOUND,\n",
" solver.ASSIGN_MIN_VALUE)\n",
"\n",
" solver.NewSearch(db)\n",
"\n",
" num_solutions = 0\n",
" while solver.NextSolution():\n",
" num_solutions += 1\n",
" for i in RANGE:\n",
" for j in RANGE:\n",
" print(field[i, j].Value(), end=\" \")\n",
" print()\n",
" print()\n",
"\n",
" solver.EndSearch()\n",
"\n",
" print(\"num_solutions:\", num_solutions)\n",
" print(\"failures:\", solver.Failures())\n",
" print(\"branches:\", solver.Branches())\n",
" print(\"WallTime:\", solver.WallTime())\n",
"\n",
"\n",
"#\n",
"# Example from Tailor model futoshiki.param/futoshiki.param\n",
"# Solution:\n",
"# 5 1 3 2 4\n",
"# 1 4 2 5 3\n",
"# 2 3 1 4 5\n",
"# 3 5 4 1 2\n",
"# 4 2 5 3 1\n",
"#\n",
"# Futoshiki instance, by Andras Salamon\n",
"# specify the numbers in the grid\n",
"#\n",
"values1 = [[0, 0, 3, 2, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0],\n",
" [0, 0, 0, 0, 0]]\n",
"\n",
"# [i1,j1, i2,j2] requires that values[i1,j1] < values[i2,j2]\n",
"# Note: 1-based\n",
"lt1 = [[1, 2, 1, 1], [1, 4, 1, 5], [2, 3, 1, 3], [3, 3, 2, 3], [3, 4, 2, 4],\n",
" [2, 5, 3, 5], [3, 2, 4, 2], [4, 4, 4, 3], [5, 2, 5, 1], [5, 4, 5, 3],\n",
" [5, 5, 4, 5]]\n",
"\n",
"#\n",
"# Example from http://en.wikipedia.org/wiki/Futoshiki\n",
"# Solution:\n",
"# 5 4 3 2 1\n",
"# 4 3 1 5 2\n",
"# 2 1 4 3 5\n",
"# 3 5 2 1 4\n",
"# 1 2 5 4 3\n",
"#\n",
"values2 = [[0, 0, 0, 0, 0], [4, 0, 0, 0, 2], [0, 0, 4, 0, 0], [0, 0, 0, 0, 4],\n",
" [0, 0, 0, 0, 0]]\n",
"\n",
"# Note: 1-based\n",
"lt2 = [[1, 2, 1, 1], [1, 4, 1, 3], [1, 5, 1, 4], [4, 4, 4, 5], [5, 1, 5, 2],\n",
" [5, 2, 5, 3]]\n",
"\n",
"print(\"Problem 1\")\n",
"main(values1, lt1)\n",
"print(\"\\nProblem 2\")\n",
"main(values2, lt2)\n",
"\n"
]
}
],
"metadata": {
"language_info": {
"name": "python"
}
},
"nbformat": 4,
"nbformat_minor": 5
}