Files
ortools-clone/examples/notebook/contrib/coins_grid.ipynb
2020-11-18 11:44:51 +01:00

191 lines
6.7 KiB
Plaintext

{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"##### Copyright 2020 Google LLC."
]
},
{
"cell_type": "markdown",
"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",
"metadata": {},
"source": [
"# coins_grid"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<table align=\"left\">\n",
"<td>\n",
"<a href=\"https://colab.research.google.com/github/google/or-tools/blob/master/examples/notebook/contrib/coins_grid.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/coins_grid.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",
"metadata": {},
"source": [
"First, you must install [ortools](https://pypi.org/project/ortools/) package in this colab."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"!pip install ortools"
]
},
{
"cell_type": "code",
"execution_count": null,
"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",
"\"\"\"\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",
"\n",
"from __future__ import print_function\n",
"import sys\n",
"from ortools.constraint_solver import pywrapcp\n",
"\n",
"\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"
]
}
],
"metadata": {},
"nbformat": 4,
"nbformat_minor": 4
}