<commit_before>{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##### Copyright 2020 The OR-Tools Authors."
   ]
  },
  {
   "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": [
    "# nqueens_sat"
   ]
  },
  {
   "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/examples/nqueens_sat.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/python/nqueens_sat.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-2018 Google LLC\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",
    "\"\"\"OR-tools solution to the N-queens problem.\"\"\"\n",
    "from __future__ import print_function\n",
    "import time\n",
    "import sys\n",
    "from ortools.sat.python import cp_model\n",
    "\n",
    "\n",
    "class NQueenSolutionPrinter(cp_model.CpSolverSolutionCallback):\n",
    "    \"\"\"Print intermediate solutions.\"\"\"\n",
    "\n",
    "    def __init__(self, queens):\n",
    "        cp_model.CpSolverSolutionCallback.__init__(self)\n",
    "        self.__queens = queens\n",
    "        self.__solution_count = 0\n",
    "        self.__start_time = time.time()\n",
    "\n",
    "    def solution_count(self):\n",
    "        return self.__solution_count\n",
    "\n",
    "    def on_solution_callback(self):\n",
    "        current_time = time.time()\n",
    "        print('Solution %i, time = %f s' % (self.__solution_count,\n",
    "                                            current_time - self.__start_time))\n",
    "        self.__solution_count += 1\n",
    "\n",
    "        all_queens = range(len(self.__queens))\n",
    "        for i in all_queens:\n",
    "            for j in all_queens:\n",
    "                if self.Value(self.__queens[j]) == 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",
    "\n",
    "\n",
    "# Creates the solver.\n",
    "model = cp_model.CpModel()\n",
    "# Creates the variables.\n",
    "# The array index is the column, and the value is the row.\n",
    "queens = [\n",
    "    model.NewIntVar(0, board_size - 1, 'x%i' % i) for i in range(board_size)\n",
    "]\n",
    "# Creates the constraints.\n",
    "\n",
    "# All rows must be different.\n",
    "model.AddAllDifferent(queens)\n",
    "\n",
    "# All columns must be different because the indices of queens are all\n",
    "# different.\n",
    "\n",
    "# No two queens can be on the same diagonal.\n",
    "diag1 = []\n",
    "diag2 = []\n",
    "for i in range(board_size):\n",
    "    q1 = model.NewIntVar(0, 2 * board_size, 'diag1_%i' % i)\n",
    "    q2 = model.NewIntVar(-board_size, board_size, 'diag2_%i' % i)\n",
    "    diag1.append(q1)\n",
    "    diag2.append(q2)\n",
    "    model.Add(q1 == queens[i] + i)\n",
    "    model.Add(q2 == queens[i] - i)\n",
    "model.AddAllDifferent(diag1)\n",
    "model.AddAllDifferent(diag2)\n",
    "\n",
    "### Solve model.\n",
    "solver = cp_model.CpSolver()\n",
    "solution_printer = NQueenSolutionPrinter(queens)\n",
    "status = solver.SearchForAllSolutions(model, solution_printer)\n",
    "\n",
    "print()\n",
    "print('Statistics')\n",
    "print('  - conflicts       : %i' % solver.NumConflicts())\n",
    "print('  - branches        : %i' % solver.NumBranches())\n",
    "print('  - wall time       : %f s' % solver.WallTime())\n",
    "print('  - solutions found : %i' % solution_printer.solution_count())\n",
    "\n",
    "\n",
    "# By default, solve the 8x8 problem.board_size = 8\n",
    "\n"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 4
}
<commit_msg>Update nqueens_sat.ipynb<commit_after>{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##### Copyright 2020 The OR-Tools Authors."
   ]
  },
  {
   "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": [
    "# nqueens_sat"
   ]
  },
  {
   "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/examples/nqueens_sat.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/python/nqueens_sat.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-2018 Google LLC\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",
    "\"\"\"OR-tools solution to the N-queens problem.\"\"\"\n",
    "from __future__ import print_function\n",
    "import time\n",
    "import sys\n",
    "from ortools.sat.python import cp_model\n",
    "\n",
    "\n",
    "class NQueenSolutionPrinter(cp_model.CpSolverSolutionCallback):\n",
    "    \"\"\"Print intermediate solutions.\"\"\"\n",
    "\n",
    "    def __init__(self, queens):\n",
    "        cp_model.CpSolverSolutionCallback.__init__(self)\n",
    "        self.__queens = queens\n",
    "        self.__solution_count = 0\n",
    "        self.__start_time = time.time()\n",
    "\n",
    "    def solution_count(self):\n",
    "        return self.__solution_count\n",
    "\n",
    "    def on_solution_callback(self):\n",
    "        current_time = time.time()\n",
    "        print('Solution %i, time = %f s' % (self.__solution_count,\n",
    "                                            current_time - self.__start_time))\n",
    "        self.__solution_count += 1\n",
    "\n",
    "        all_queens = range(len(self.__queens))\n",
    "        for i in all_queens:\n",
    "            for j in all_queens:\n",
    "                if self.Value(self.__queens[j]) == 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",
    "\n",
    "\n",
    "# Sets the size of the board.\n",
    "board_size = 8\n",
    "# Creates the solver.\n",
    "model = cp_model.CpModel()\n",
    "# Creates the variables.\n",
    "# The array index is the column, and the value is the row.\n",
    "queens = [\n",
    "    model.NewIntVar(0, board_size - 1, 'x%i' % i) for i in range(board_size)\n",
    "]\n",
    "# Creates the constraints.\n",
    "\n",
    "# All rows must be different.\n",
    "model.AddAllDifferent(queens)\n",
    "\n",
    "# All columns must be different because the indices of queens are all\n",
    "# different.\n",
    "\n",
    "# No two queens can be on the same diagonal.\n",
    "diag1 = []\n",
    "diag2 = []\n",
    "for i in range(board_size):\n",
    "    q1 = model.NewIntVar(0, 2 * board_size, 'diag1_%i' % i)\n",
    "    q2 = model.NewIntVar(-board_size, board_size, 'diag2_%i' % i)\n",
    "    diag1.append(q1)\n",
    "    diag2.append(q2)\n",
    "    model.Add(q1 == queens[i] + i)\n",
    "    model.Add(q2 == queens[i] - i)\n",
    "model.AddAllDifferent(diag1)\n",
    "model.AddAllDifferent(diag2)\n",
    "\n",
    "### Solve model.\n",
    "solver = cp_model.CpSolver()\n",
    "solution_printer = NQueenSolutionPrinter(queens)\n",
    "status = solver.SearchForAllSolutions(model, solution_printer)\n",
    "\n",
    "print()\n",
    "print('Statistics')\n",
    "print('  - conflicts       : %i' % solver.NumConflicts())\n",
    "print('  - branches        : %i' % solver.NumBranches())\n",
    "print('  - wall time       : %f s' % solver.WallTime())\n",
    "print('  - solutions found : %i' % solution_printer.solution_count())\n",
    "\n",
    "\n",
    "# By default, solve the 8x8 problem.board_size = 8\n",
    "\n"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 4
}

{'fraction_non_alphanumeric': 0.1885465505693235, 'fraction_numerical': 0.008288680509042197, 'mean_word_length': 2.34593837535014, 'pattern_counts': {'":': 58, '<': 27, '<?xml version=': 0, '>': 27, 'https://': 10, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4902660', 'n_tokens_mistral': 4226, 'n_tokens_neox': 4117, 'n_words': 947}