169 lines
5.2 KiB
Plaintext
169 lines
5.2 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": [
|
|
"# cp_is_fun_sat"
|
|
]
|
|
},
|
|
{
|
|
"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/sat/cp_is_fun_sat.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/ortools/sat/samples/cp_is_fun_sat.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",
|
|
"Cryptarithmetic puzzle.\n",
|
|
"\n",
|
|
"First attempt to solve equation CP + IS + FUN = TRUE\n",
|
|
"where each letter represents a unique digit.\n",
|
|
"\n",
|
|
"This problem has 72 different solutions in base 10.\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "code",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"from ortools.sat.python import cp_model\n",
|
|
"\n",
|
|
"\n",
|
|
"class VarArraySolutionPrinter(cp_model.CpSolverSolutionCallback):\n",
|
|
" \"\"\"Print intermediate solutions.\"\"\"\n",
|
|
"\n",
|
|
" def __init__(self, variables):\n",
|
|
" cp_model.CpSolverSolutionCallback.__init__(self)\n",
|
|
" self.__variables = variables\n",
|
|
" self.__solution_count = 0\n",
|
|
"\n",
|
|
" def on_solution_callback(self):\n",
|
|
" self.__solution_count += 1\n",
|
|
" for v in self.__variables:\n",
|
|
" print('%s=%i' % (v, self.Value(v)), end=' ')\n",
|
|
" print()\n",
|
|
"\n",
|
|
" def solution_count(self):\n",
|
|
" return self.__solution_count\n",
|
|
"\n",
|
|
"\n",
|
|
"def main():\n",
|
|
" \"\"\"Solve the CP+IS+FUN==TRUE cryptarithm.\"\"\"\n",
|
|
" # Constraint programming engine\n",
|
|
" model = cp_model.CpModel()\n",
|
|
"\n",
|
|
" base = 10\n",
|
|
"\n",
|
|
" c = model.NewIntVar(1, base - 1, 'C')\n",
|
|
" p = model.NewIntVar(0, base - 1, 'P')\n",
|
|
" i = model.NewIntVar(1, base - 1, 'I')\n",
|
|
" s = model.NewIntVar(0, base - 1, 'S')\n",
|
|
" f = model.NewIntVar(1, base - 1, 'F')\n",
|
|
" u = model.NewIntVar(0, base - 1, 'U')\n",
|
|
" n = model.NewIntVar(0, base - 1, 'N')\n",
|
|
" t = model.NewIntVar(1, base - 1, 'T')\n",
|
|
" r = model.NewIntVar(0, base - 1, 'R')\n",
|
|
" e = model.NewIntVar(0, base - 1, 'E')\n",
|
|
"\n",
|
|
" # We need to group variables in a list to use the constraint AllDifferent.\n",
|
|
" letters = [c, p, i, s, f, u, n, t, r, e]\n",
|
|
"\n",
|
|
" # Verify that we have enough digits.\n",
|
|
" assert base >= len(letters)\n",
|
|
"\n",
|
|
" # Define constraints.\n",
|
|
" model.AddAllDifferent(letters)\n",
|
|
"\n",
|
|
" # CP + IS + FUN = TRUE\n",
|
|
" model.Add(c * base + p + i * base + s + f * base * base + u * base +\n",
|
|
" n == t * base * base * base + r * base * base + u * base + e)\n",
|
|
"\n",
|
|
" # Creates a solver and solves the model.\n",
|
|
" solver = cp_model.CpSolver()\n",
|
|
" solution_printer = VarArraySolutionPrinter(letters)\n",
|
|
" # Enumerate all solutions.\n",
|
|
" solver.parameters.enumerate_all_solutions = True\n",
|
|
" # Solve.\n",
|
|
" status = solver.Solve(model, solution_printer)\n",
|
|
"\n",
|
|
" # Statistics.\n",
|
|
" print('\\nStatistics')\n",
|
|
" print(f' status : {solver.StatusName(status)}')\n",
|
|
" print(f' conflicts: {solver.NumConflicts()}')\n",
|
|
" print(f' branches : {solver.NumBranches()}')\n",
|
|
" print(f' wall time: {solver.WallTime()} s')\n",
|
|
" print(f' sol found: {solution_printer.solution_count()}')\n",
|
|
"\n",
|
|
"\n",
|
|
"main()\n",
|
|
"\n"
|
|
]
|
|
}
|
|
],
|
|
"metadata": {},
|
|
"nbformat": 4,
|
|
"nbformat_minor": 5
|
|
}
|