Files
ortools-clone/examples/notebook/constraint_solver/nqueens_cp.ipynb
2022-04-14 14:31:02 +02:00

148 lines
4.4 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": [
"# nqueens_cp"
]
},
{
"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/constraint_solver/nqueens_cp.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/ortools/constraint_solver/samples/nqueens_cp.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": "markdown",
"id": "description",
"metadata": {},
"source": [
"OR-Tools solution to the N-queens problem."
]
},
{
"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(board_size):\n",
" # Creates the solver.\n",
" solver = pywrapcp.Solver('n-queens')\n",
"\n",
" # Creates the variables.\n",
" # The array index is the column, and the value is the row.\n",
" queens = [\n",
" solver.IntVar(0, board_size - 1, f'x{i}') for i in range(board_size)\n",
" ]\n",
"\n",
" # Creates the constraints.\n",
" # All rows must be different.\n",
" solver.Add(solver.AllDifferent(queens))\n",
"\n",
" # No two queens can be on the same diagonal.\n",
" solver.Add(solver.AllDifferent([queens[i] + i for i in range(board_size)]))\n",
" solver.Add(solver.AllDifferent([queens[i] - i for i in range(board_size)]))\n",
"\n",
" db = solver.Phase(queens, solver.CHOOSE_FIRST_UNBOUND,\n",
" solver.ASSIGN_MIN_VALUE)\n",
"\n",
" # Iterates through the solutions, displaying each.\n",
" num_solutions = 0\n",
" solver.NewSearch(db)\n",
" while solver.NextSolution():\n",
" # Displays the solution just computed.\n",
" for i in range(board_size):\n",
" for j in range(board_size):\n",
" if queens[j].Value() == i:\n",
" # There is a queen in column j, row i.\n",
" print('Q', end=' ')\n",
" else:\n",
" print('_', end=' ')\n",
" print()\n",
" print()\n",
" num_solutions += 1\n",
" solver.EndSearch()\n",
"\n",
" # Statistics.\n",
" print('\\nStatistics')\n",
" print(f' failures: {solver.Failures()}')\n",
" print(f' branches: {solver.Branches()}')\n",
" print(f' wall time: {solver.WallTime()} ms')\n",
" print(f' Solutions found: {num_solutions}')\n",
"\n",
"\n",
"# By default, solve the 8x8 problem.\n",
"size = 8\n",
"if len(sys.argv) > 1:\n",
" size = int(sys.argv[1])\n",
"main(size)\n",
"\n"
]
}
],
"metadata": {},
"nbformat": 4,
"nbformat_minor": 5
}