Files
ortools-clone/examples/notebook/contrib/nqueens3.ipynb
2022-06-27 15:42:26 +02:00

185 lines
5.1 KiB
Plaintext

{
"cells": [
{
"cell_type": "markdown",
"id": "google",
"metadata": {},
"source": [
"##### Copyright 2022 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": [
"# nqueens3"
]
},
{
"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/nqueens3.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/nqueens3.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",
" n-queens problem in Google CP Solver.\n",
"\n",
" N queens problem.\n",
"\n",
" Faster than the previous versions:\n",
" - http://www.hakank.org/gogle_cp_solver/nqueens.py\n",
" - http://www.hakank.org/gogle_cp_solver/nqueens2.py\n",
"\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": [
"import sys\n",
"from ortools.constraint_solver import pywrapcp\n",
"\n",
"\n",
"def main(n=8, num_sol=0, print_sol=1):\n",
" # Create the solver.\n",
" solver = pywrapcp.Solver(\"n-queens\")\n",
"\n",
" #\n",
" # data\n",
" #\n",
" print(\"n:\", n)\n",
" print(\"num_sol:\", num_sol)\n",
" print(\"print_sol:\", print_sol)\n",
"\n",
" # declare variables\n",
" q = [solver.IntVar(0, n - 1, \"x%i\" % i) for i in range(n)]\n",
"\n",
" #\n",
" # constraints\n",
" #\n",
" solver.Add(solver.AllDifferent(q))\n",
" solver.Add(solver.AllDifferent([q[i] + i for i in range(n)]))\n",
" solver.Add(solver.AllDifferent([q[i] - i for i in range(n)]))\n",
"\n",
" # symmetry breaking\n",
" # solver.Add(q[0] == 0)\n",
"\n",
" #\n",
" # search\n",
" #\n",
"\n",
" db = solver.Phase(q, solver.CHOOSE_MIN_SIZE_LOWEST_MAX,\n",
" solver.ASSIGN_CENTER_VALUE)\n",
"\n",
" solver.NewSearch(db)\n",
" num_solutions = 0\n",
" while solver.NextSolution():\n",
" if print_sol:\n",
" qval = [q[i].Value() for i in range(n)]\n",
" print(\"q:\", qval)\n",
" for i in range(n):\n",
" for j in range(n):\n",
" if qval[i] == j:\n",
" print(\"Q\", end=\" \")\n",
" else:\n",
" print(\"_\", end=\" \")\n",
" print()\n",
" print()\n",
" num_solutions += 1\n",
" if num_sol > 0 and num_solutions >= num_sol:\n",
" break\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",
"\n",
"n = 8\n",
"num_sol = 0\n",
"print_sol = 1\n",
"if len(sys.argv) > 1:\n",
" n = int(sys.argv[1])\n",
"if len(sys.argv) > 2:\n",
" num_sol = int(sys.argv[2])\n",
"if len(sys.argv) > 3:\n",
" print_sol = int(sys.argv[3])\n",
"\n",
"main(n, num_sol, print_sol)\n",
"\n",
"# print_sol = False\n",
"# show_all = False\n",
"# for n in range(1000,1001):\n",
"# print\n",
"# main(n, num_sol, print_sol)\n",
"\n"
]
}
],
"metadata": {},
"nbformat": 4,
"nbformat_minor": 5
}