180 lines
5.4 KiB
Plaintext
180 lines
5.4 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": [
|
|
"# magic_square"
|
|
]
|
|
},
|
|
{
|
|
"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/magic_square.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/magic_square.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",
|
|
"\n",
|
|
" Magic squares in Google CP Solver.\n",
|
|
"\n",
|
|
" Magic square problem.\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",
|
|
"import sys\n",
|
|
"from ortools.constraint_solver import pywrapcp\n",
|
|
"\n",
|
|
"\n",
|
|
"# Create the solver.\n",
|
|
"solver = pywrapcp.Solver(\"n-queens\")\n",
|
|
"\n",
|
|
"#\n",
|
|
"# data\n",
|
|
"#\n",
|
|
"\n",
|
|
"#\n",
|
|
"# declare variables\n",
|
|
"#\n",
|
|
"x = {}\n",
|
|
"for i in range(n):\n",
|
|
" for j in range(n):\n",
|
|
" x[(i, j)] = solver.IntVar(1, n * n, \"x(%i,%i)\" % (i, j))\n",
|
|
"x_flat = [x[(i, j)] for i in range(n) for j in range(n)]\n",
|
|
"\n",
|
|
"# the sum\n",
|
|
"# s = ( n * (n*n + 1)) / 2\n",
|
|
"s = solver.IntVar(1, n * n * n, \"s\")\n",
|
|
"\n",
|
|
"#\n",
|
|
"# constraints\n",
|
|
"#\n",
|
|
"# solver.Add(s == ( n * (n*n + 1)) / 2)\n",
|
|
"\n",
|
|
"solver.Add(solver.AllDifferent(x_flat))\n",
|
|
"\n",
|
|
"[solver.Add(solver.Sum([x[(i, j)] for j in range(n)]) == s) for i in range(n)]\n",
|
|
"[solver.Add(solver.Sum([x[(i, j)] for i in range(n)]) == s) for j in range(n)]\n",
|
|
"\n",
|
|
"solver.Add(solver.Sum([x[(i, i)] for i in range(n)]) == s) # diag 1\n",
|
|
"solver.Add(solver.Sum([x[(i, n - i - 1)] for i in range(n)]) == s) # diag 2\n",
|
|
"\n",
|
|
"# symmetry breaking\n",
|
|
"# solver.Add(x[(0,0)] == 1)\n",
|
|
"\n",
|
|
"#\n",
|
|
"# solution and search\n",
|
|
"#\n",
|
|
"solution = solver.Assignment()\n",
|
|
"solution.Add(x_flat)\n",
|
|
"solution.Add(s)\n",
|
|
"\n",
|
|
"# db: DecisionBuilder\n",
|
|
"db = solver.Phase(\n",
|
|
" x_flat,\n",
|
|
" # solver.INT_VAR_DEFAULT,\n",
|
|
" solver.CHOOSE_FIRST_UNBOUND,\n",
|
|
" # solver.CHOOSE_MIN_SIZE_LOWEST_MAX,\n",
|
|
"\n",
|
|
" # solver.ASSIGN_MIN_VALUE\n",
|
|
" solver.ASSIGN_CENTER_VALUE)\n",
|
|
"\n",
|
|
"solver.NewSearch(db)\n",
|
|
"num_solutions = 0\n",
|
|
"while solver.NextSolution():\n",
|
|
" print(\"s:\", s.Value())\n",
|
|
" for i in range(n):\n",
|
|
" for j in range(n):\n",
|
|
" print(\"%2i\" % x[(i, j)].Value(), end=\" \")\n",
|
|
" print()\n",
|
|
"\n",
|
|
" print()\n",
|
|
" num_solutions += 1\n",
|
|
" if num_solutions > limit:\n",
|
|
" break\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 = 4\n",
|
|
"limit=100\n"
|
|
]
|
|
}
|
|
],
|
|
"metadata": {},
|
|
"nbformat": 4,
|
|
"nbformat_minor": 4
|
|
}
|