206 lines
6.6 KiB
Plaintext
206 lines
6.6 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": [
|
|
"# coins_grid"
|
|
]
|
|
},
|
|
{
|
|
"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/coins_grid.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/coins_grid.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",
|
|
" Coins grid problem in Google CP Solver.\n",
|
|
"\n",
|
|
" Problem from\n",
|
|
" Tony Hurlimann: \"A coin puzzle - SVOR-contest 2007\"\n",
|
|
" http://www.svor.ch/competitions/competition2007/AsroContestSolution.pdf\n",
|
|
" '''\n",
|
|
" In a quadratic grid (or a larger chessboard) with 31x31 cells, one should\n",
|
|
" place coins in such a way that the following conditions are fulfilled:\n",
|
|
" 1. In each row exactly 14 coins must be placed.\n",
|
|
" 2. In each column exactly 14 coins must be placed.\n",
|
|
" 3. The sum of the quadratic horizontal distance from the main diagonal\n",
|
|
" of all cells containing a coin must be as small as possible.\n",
|
|
" 4. In each cell at most one coin can be placed.\n",
|
|
" The description says to place 14x31 = 434 coins on the chessboard each row\n",
|
|
" containing 14 coins and each column also containing 14 coins.\n",
|
|
" '''\n",
|
|
"\n",
|
|
" Cf the LPL model:\n",
|
|
" http://diuflx71.unifr.ch/lpl/GetModel?name=/puzzles/coin\n",
|
|
"\n",
|
|
" Note: Laurent Perron helped me to improve this model.\n",
|
|
"\n",
|
|
" Compare with the following models:\n",
|
|
" * Tailor/Essence': http://hakank.org/tailor/coins_grid.eprime\n",
|
|
" * MiniZinc: http://hakank.org/minizinc/coins_grid.mzn\n",
|
|
" * SICStus: http://hakank.org/sicstus/coins_grid.pl\n",
|
|
" * Zinc: http://hakank.org/minizinc/coins_grid.zinc\n",
|
|
" * Choco: http://hakank.org/choco/CoinsGrid.java\n",
|
|
" * Comet: http://hakank.org/comet/coins_grid.co\n",
|
|
" * ECLiPSe: http://hakank.org/eclipse/coins_grid.ecl\n",
|
|
" * Gecode: http://hakank.org/gecode/coins_grid.cpp\n",
|
|
" * Gecode/R: http://hakank.org/gecode_r/coins_grid.rb\n",
|
|
" * JaCoP: http://hakank.org/JaCoP/CoinsGrid.java\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",
|
|
"\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(n, c):\n",
|
|
" # Create the solver.\n",
|
|
" solver = pywrapcp.Solver(\"Coins grid\")\n",
|
|
" # data\n",
|
|
"\n",
|
|
" print(\"n: \", n)\n",
|
|
" print(\"c: \", c)\n",
|
|
"\n",
|
|
" # declare variables\n",
|
|
" x = {}\n",
|
|
" for i in range(n):\n",
|
|
" for j in range(n):\n",
|
|
" x[(i, j)] = solver.BoolVar(\"x %i %i\" % (i, j))\n",
|
|
"\n",
|
|
" #\n",
|
|
" # constraints\n",
|
|
" #\n",
|
|
"\n",
|
|
" # sum rows/columns == c\n",
|
|
" for i in range(n):\n",
|
|
" solver.Add(solver.SumEquality([x[(i, j)] for j in range(n)], c)) # sum rows\n",
|
|
" solver.Add(solver.SumEquality([x[(j, i)] for j in range(n)], c)) # sum cols\n",
|
|
"\n",
|
|
" # quadratic horizonal distance var\n",
|
|
" objective_var = solver.Sum(\n",
|
|
" [x[(i, j)] * (i - j) * (i - j) for i in range(n) for j in range(n)])\n",
|
|
"\n",
|
|
" # objective\n",
|
|
" objective = solver.Minimize(objective_var, 1)\n",
|
|
"\n",
|
|
" #\n",
|
|
" # solution and search\n",
|
|
" #\n",
|
|
" solution = solver.Assignment()\n",
|
|
" solution.Add([x[(i, j)] for i in range(n) for j in range(n)])\n",
|
|
" solution.AddObjective(objective_var)\n",
|
|
"\n",
|
|
" # last solutions\n",
|
|
" collector = solver.LastSolutionCollector(solution)\n",
|
|
" search_log = solver.SearchLog(1000000, objective_var)\n",
|
|
" restart = solver.ConstantRestart(300)\n",
|
|
" solver.Solve(\n",
|
|
" solver.Phase([x[(i, j)] for i in range(n) for j in range(n)],\n",
|
|
" solver.CHOOSE_RANDOM, solver.ASSIGN_MAX_VALUE),\n",
|
|
" [collector, search_log, objective])\n",
|
|
"\n",
|
|
" print(\"objective:\", collector.ObjectiveValue(0))\n",
|
|
" for i in range(n):\n",
|
|
" for j in range(n):\n",
|
|
" print(collector.Value(0, x[(i, j)]), end=\" \")\n",
|
|
" print()\n",
|
|
" print()\n",
|
|
"\n",
|
|
" print(\"failures:\", solver.Failures())\n",
|
|
" print(\"branches:\", solver.Branches())\n",
|
|
" print(\"WallTime:\", solver.WallTime())\n",
|
|
"\n",
|
|
"\n",
|
|
"# data\n",
|
|
"n = 5 # the grid size\n",
|
|
"c = 2 # number of coins per row/column\n",
|
|
"if len(sys.argv) > 1:\n",
|
|
" n = int(sys.argv[1])\n",
|
|
"if len(sys.argv) > 2:\n",
|
|
" c = int(sys.argv[2])\n",
|
|
"\n",
|
|
"main(n, c)\n",
|
|
"\n"
|
|
]
|
|
}
|
|
],
|
|
"metadata": {
|
|
"language_info": {
|
|
"name": "python"
|
|
}
|
|
},
|
|
"nbformat": 4,
|
|
"nbformat_minor": 5
|
|
}
|