151 lines
4.6 KiB
Plaintext
151 lines
4.6 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_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/main/examples/notebook/constraint_solver/cp_is_fun_cp.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/constraint_solver/samples/cp_is_fun_cp.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.constraint_solver import pywrapcp\n",
|
|
"\n",
|
|
"\n",
|
|
"def main():\n",
|
|
" # Constraint programming engine\n",
|
|
" solver = pywrapcp.Solver('CP is fun!')\n",
|
|
"\n",
|
|
" base = 10\n",
|
|
"\n",
|
|
" # Decision variables.\n",
|
|
" digits = list(range(0, base))\n",
|
|
" digits_without_zero = list(range(1, base))\n",
|
|
" c = solver.IntVar(digits_without_zero, 'C')\n",
|
|
" p = solver.IntVar(digits, 'P')\n",
|
|
" i = solver.IntVar(digits_without_zero, 'I')\n",
|
|
" s = solver.IntVar(digits, 'S')\n",
|
|
" f = solver.IntVar(digits_without_zero, 'F')\n",
|
|
" u = solver.IntVar(digits, 'U')\n",
|
|
" n = solver.IntVar(digits, 'N')\n",
|
|
" t = solver.IntVar(digits_without_zero, 'T')\n",
|
|
" r = solver.IntVar(digits, 'R')\n",
|
|
" e = solver.IntVar(digits, '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",
|
|
" solver.Add(solver.AllDifferent(letters))\n",
|
|
"\n",
|
|
" # CP + IS + FUN = TRUE\n",
|
|
" solver.Add(p + s + n + base * (c + i + u) + base * base * f == e +\n",
|
|
" base * u + base * base * r + base * base * base * t)\n",
|
|
"\n",
|
|
" solution_count = 0\n",
|
|
" db = solver.Phase(letters, solver.INT_VAR_DEFAULT, solver.INT_VALUE_DEFAULT)\n",
|
|
" solver.NewSearch(db)\n",
|
|
" while solver.NextSolution():\n",
|
|
" print(letters)\n",
|
|
" # Is CP + IS + FUN = TRUE?\n",
|
|
" assert (base * c.Value() + p.Value() + base * i.Value() + s.Value() +\n",
|
|
" base * base * f.Value() + base * u.Value() +\n",
|
|
" n.Value() == base * base * base * t.Value() +\n",
|
|
" base * base * r.Value() + base * u.Value() + e.Value())\n",
|
|
" solution_count += 1\n",
|
|
" solver.EndSearch()\n",
|
|
" print(f'Number of solutions found: {solution_count}')\n",
|
|
"\n",
|
|
"\n",
|
|
"main()\n",
|
|
"\n"
|
|
]
|
|
}
|
|
],
|
|
"metadata": {},
|
|
"nbformat": 4,
|
|
"nbformat_minor": 5
|
|
}
|