Files
ortools-clone/examples/notebook/sat/cp_is_fun_sat.ipynb
2025-06-03 18:10:28 +02:00

177 lines
5.4 KiB
Plaintext

{
"cells": [
{
"cell_type": "markdown",
"id": "google",
"metadata": {},
"source": [
"##### Copyright 2025 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",
"\n",
"class VarArraySolutionPrinter(cp_model.CpSolverSolutionCallback):\n",
" \"\"\"Print intermediate solutions.\"\"\"\n",
"\n",
" def __init__(self, variables: list[cp_model.IntVar]):\n",
" cp_model.CpSolverSolutionCallback.__init__(self)\n",
" self.__variables = variables\n",
" self.__solution_count = 0\n",
"\n",
" def on_solution_callback(self) -> None:\n",
" self.__solution_count += 1\n",
" for v in self.__variables:\n",
" print(f\"{v}={self.value(v)}\", end=\" \")\n",
" print()\n",
"\n",
" @property\n",
" def solution_count(self) -> int:\n",
" return self.__solution_count\n",
"\n",
"\n",
"def main() -> None:\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.new_int_var(1, base - 1, \"C\")\n",
" p = model.new_int_var(0, base - 1, \"P\")\n",
" i = model.new_int_var(1, base - 1, \"I\")\n",
" s = model.new_int_var(0, base - 1, \"S\")\n",
" f = model.new_int_var(1, base - 1, \"F\")\n",
" u = model.new_int_var(0, base - 1, \"U\")\n",
" n = model.new_int_var(0, base - 1, \"N\")\n",
" t = model.new_int_var(1, base - 1, \"T\")\n",
" r = model.new_int_var(0, base - 1, \"R\")\n",
" e = model.new_int_var(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.add_all_different(letters)\n",
"\n",
" # CP + IS + FUN = TRUE\n",
" model.add(\n",
" 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",
"\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.status_name(status)}\")\n",
" print(f\" conflicts: {solver.num_conflicts}\")\n",
" print(f\" branches : {solver.num_branches}\")\n",
" print(f\" wall time: {solver.wall_time} s\")\n",
" print(f\" sol found: {solution_printer.solution_count}\")\n",
"\n",
"\n",
"main()\n",
"\n"
]
}
],
"metadata": {
"language_info": {
"name": "python"
}
},
"nbformat": 4,
"nbformat_minor": 5
}