217 lines
6.9 KiB
Plaintext
217 lines
6.9 KiB
Plaintext
{
|
|
"cells": [
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "google",
|
|
"metadata": {},
|
|
"source": [
|
|
"##### Copyright 2021 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": [
|
|
"# strimko2"
|
|
]
|
|
},
|
|
{
|
|
"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/strimko2.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/strimko2.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": "code",
|
|
"execution_count": null,
|
|
"id": "code",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"# Copyright 2010 Hakan Kjellerstrand hakank@gmail.com\n",
|
|
"#\n",
|
|
"# 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",
|
|
"\"\"\"Strimko problem in Google CP Solver.\n",
|
|
"\n",
|
|
" From\n",
|
|
" 360: A New Twist on Latin Squares\n",
|
|
" http://threesixty360.wordpress.com/2009/08/04/a-new-twist-on-latin-squares/\n",
|
|
" '''\n",
|
|
" The idea is simple: each row and column of an nxn grid must contain\n",
|
|
" the number 1, 2, ... n exactly once (that is, the grid must form a\n",
|
|
" Latin square), and each \"stream\" (connected path in the grid) must\n",
|
|
" also contain the numbers 1, 2, ..., n exactly once.\n",
|
|
" '''\n",
|
|
"\n",
|
|
" For more information, see:\n",
|
|
" * http://www.strimko.com/\n",
|
|
" * http://www.strimko.com/rules.htm\n",
|
|
" * http://www.strimko.com/about.htm\n",
|
|
" * http://www.puzzlersparadise.com/Strimko.htm\n",
|
|
"\n",
|
|
" I have blogged about this (using MiniZinc model) in\n",
|
|
" 'Strimko - Latin squares puzzle with \"streams\"'\n",
|
|
" http://www.hakank.org/constraint_programming_blog/2009/08/strimko_latin_squares_puzzle_w_1.html\n",
|
|
"\n",
|
|
" Compare with the following models:\n",
|
|
" * MiniZinc: http://hakank.org/minizinc/strimko2.mzn\n",
|
|
" * ECLiPSe: http://hakank.org/eclipse/strimko2.ecl\n",
|
|
" * SICStus: http://hakank.org/sicstus/strimko2.pl\n",
|
|
" * Gecode: http://hakank.org/gecode/strimko2.cpp\n",
|
|
"\n",
|
|
" This model was created by Hakan Kjellerstrand (hakank@gmail.com)\n",
|
|
" See my other Google CP Solver models: http://www.hakank.org/google_or_tools/\n",
|
|
"\"\"\"\n",
|
|
"import sys\n",
|
|
"\n",
|
|
"from ortools.constraint_solver import pywrapcp\n",
|
|
"\n",
|
|
"\n",
|
|
"\n",
|
|
"# Create the solver.\n",
|
|
"solver = pywrapcp.Solver('Strimko')\n",
|
|
"\n",
|
|
"#\n",
|
|
"# default problem\n",
|
|
"#\n",
|
|
"if streams == '':\n",
|
|
" streams = [[1, 1, 2, 2, 2, 2, 2], [1, 1, 2, 3, 3, 3, 2],\n",
|
|
" [1, 4, 1, 3, 3, 5, 5], [4, 4, 3, 1, 3, 5, 5],\n",
|
|
" [4, 6, 6, 6, 7, 7, 5], [6, 4, 6, 4, 5, 5, 7],\n",
|
|
" [6, 6, 4, 7, 7, 7, 7]]\n",
|
|
"\n",
|
|
" # Note: This is 1-based\n",
|
|
" placed = [[2, 1, 1], [2, 3, 7], [2, 5, 6], [2, 7, 4], [3, 2, 7], [3, 6, 1],\n",
|
|
" [4, 1, 4], [4, 7, 5], [5, 2, 2], [5, 6, 6]]\n",
|
|
"\n",
|
|
"n = len(streams)\n",
|
|
"num_placed = len(placed)\n",
|
|
"\n",
|
|
"print('n:', n)\n",
|
|
"\n",
|
|
"#\n",
|
|
"# variables\n",
|
|
"#\n",
|
|
"\n",
|
|
"x = {}\n",
|
|
"for i in range(n):\n",
|
|
" for j in range(n):\n",
|
|
" x[i, j] = solver.IntVar(1, n, 'x[%i,%i]' % (i, j))\n",
|
|
"\n",
|
|
"x_flat = [x[i, j] for i in range(n) for j in range(n)]\n",
|
|
"\n",
|
|
"#\n",
|
|
"# constraints\n",
|
|
"#\n",
|
|
"\n",
|
|
"# all rows and columns must be unique, i.e. a Latin Square\n",
|
|
"for i in range(n):\n",
|
|
" row = [x[i, j] for j in range(n)]\n",
|
|
" solver.Add(solver.AllDifferent(row))\n",
|
|
"\n",
|
|
" col = [x[j, i] for j in range(n)]\n",
|
|
" solver.Add(solver.AllDifferent(col))\n",
|
|
"\n",
|
|
"#\n",
|
|
"# streams\n",
|
|
"#\n",
|
|
"for s in range(1, n + 1):\n",
|
|
" tmp = [x[i, j] for i in range(n) for j in range(n) if streams[i][j] == s]\n",
|
|
" solver.Add(solver.AllDifferent(tmp))\n",
|
|
"\n",
|
|
"#\n",
|
|
"# placed\n",
|
|
"#\n",
|
|
"for i in range(num_placed):\n",
|
|
" # note: also adjust to 0-based\n",
|
|
" solver.Add(x[placed[i][0] - 1, placed[i][1] - 1] == placed[i][2])\n",
|
|
"\n",
|
|
"#\n",
|
|
"# search and solution\n",
|
|
"#\n",
|
|
"db = solver.Phase(x_flat, solver.INT_VAR_DEFAULT, solver.INT_VALUE_DEFAULT)\n",
|
|
"\n",
|
|
"solver.NewSearch(db)\n",
|
|
"\n",
|
|
"num_solutions = 0\n",
|
|
"while solver.NextSolution():\n",
|
|
" for i in range(n):\n",
|
|
" for j in range(n):\n",
|
|
" print(x[i, j].Value(), end=' ')\n",
|
|
" print()\n",
|
|
"\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(), 'ms')\n",
|
|
"\n"
|
|
]
|
|
}
|
|
],
|
|
"metadata": {},
|
|
"nbformat": 4,
|
|
"nbformat_minor": 5
|
|
}
|