Files
ortools-clone/examples/notebook/contrib/safe_cracking.ipynb

184 lines
5.0 KiB
Plaintext
Raw Permalink Normal View History

{
"cells": [
2020-09-27 16:03:47 +02:00
{
"cell_type": "markdown",
2021-12-14 13:05:00 +01:00
"id": "google",
2020-09-27 16:03:47 +02:00
"metadata": {},
"source": [
2025-02-04 18:04:03 +01:00
"##### Copyright 2025 Google LLC."
2020-09-27 16:03:47 +02:00
]
},
{
"cell_type": "markdown",
2021-12-14 13:05:00 +01:00
"id": "apache",
2020-09-27 16:03:47 +02:00
"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",
2021-12-14 13:05:00 +01:00
"id": "basename",
2020-09-27 16:03:47 +02:00
"metadata": {},
"source": [
"# safe_cracking"
]
},
{
"cell_type": "markdown",
2021-12-14 13:05:00 +01:00
"id": "link",
2020-09-27 16:03:47 +02:00
"metadata": {},
"source": [
"<table align=\"left\">\n",
"<td>\n",
2022-06-27 15:42:26 +02:00
"<a href=\"https://colab.research.google.com/github/google/or-tools/blob/main/examples/notebook/contrib/safe_cracking.ipynb\"><img src=\"https://raw.githubusercontent.com/google/or-tools/main/tools/colab_32px.png\"/>Run in Google Colab</a>\n",
2020-09-27 16:03:47 +02:00
"</td>\n",
"<td>\n",
2022-06-27 15:42:26 +02:00
"<a href=\"https://github.com/google/or-tools/blob/main/examples/contrib/safe_cracking.py\"><img src=\"https://raw.githubusercontent.com/google/or-tools/main/tools/github_32px.png\"/>View source on GitHub</a>\n",
2020-09-27 16:03:47 +02:00
"</td>\n",
"</table>"
]
},
{
"cell_type": "markdown",
2021-12-14 13:05:00 +01:00
"id": "doc",
2020-09-27 16:03:47 +02:00
"metadata": {},
"source": [
"First, you must install [ortools](https://pypi.org/project/ortools/) package in this colab."
]
},
{
"cell_type": "code",
"execution_count": null,
2021-12-14 13:05:00 +01:00
"id": "install",
2020-09-27 16:03:47 +02:00
"metadata": {},
"outputs": [],
"source": [
"%pip install ortools"
2020-09-27 16:03:47 +02:00
]
},
{
2022-04-14 14:07:58 +02:00
"cell_type": "markdown",
"id": "description",
"metadata": {},
"source": [
2022-04-14 14:07:58 +02:00
"\n",
"\n",
" Safe cracking puzzle in Google CP Solver.\n",
"\n",
" From the Oz Primer:\n",
" http://www.comp.nus.edu.sg/~henz/projects/puzzles/digits/index.html\n",
" '''\n",
" The code of Professor Smart's safe is a sequence of 9 distinct\n",
" nonzero digits C1 .. C9 such that the following equations and\n",
" inequations are satisfied:\n",
"\n",
" C4 - C6 = C7\n",
" C1 * C2 * C3 = C8 + C9\n",
" C2 + C3 + C6 < C8\n",
" C9 < C8\n",
"\n",
" and\n",
"\n",
" C1 <> 1, C2 <> 2, ..., C9 <> 9\n",
"\n",
" can you find the correct combination?\n",
" '''\n",
"\n",
" Compare with the following models:\n",
" * MiniZinc: http://www.hakank.org/minizinc/safe_cracking.mzn\n",
" * ECLiPSe : http://www.hakank.org/eclipse/safe_cracking.ecl\n",
" * SICStus : http://www.hakank.org/sicstus/safe_cracking.pl\n",
" * Gecode: http://hakank.org/gecode/safe_cracking.cpp\n",
"\n",
" This model was created by Hakan Kjellerstrand (hakank@gmail.com)\n",
" Also see my other Google CP Solver models:\n",
2022-04-14 14:07:58 +02:00
" http://www.hakank.org/google_or_tools/\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "code",
"metadata": {},
"outputs": [],
"source": [
"from ortools.constraint_solver import pywrapcp\n",
"\n",
"\n",
2022-04-14 14:07:58 +02:00
"def main():\n",
"\n",
" # Create the solver.\n",
" solver = pywrapcp.Solver('Safe cracking puzzle')\n",
"\n",
" #\n",
" # data\n",
" #\n",
" n = 9\n",
" digits = list(range(1, n + 1))\n",
"\n",
2022-04-14 14:07:58 +02:00
" #\n",
" # variables\n",
" #\n",
"\n",
2022-04-14 14:07:58 +02:00
" LD = [solver.IntVar(digits, 'LD[%i]' % i) for i in range(n)]\n",
" C1, C2, C3, C4, C5, C6, C7, C8, C9 = LD\n",
"\n",
2022-04-14 14:07:58 +02:00
" #\n",
" # constraints\n",
" #\n",
" solver.Add(solver.AllDifferent(LD))\n",
"\n",
2022-04-14 14:07:58 +02:00
" solver.Add(C4 - C6 == C7)\n",
" solver.Add(C1 * C2 * C3 == C8 + C9)\n",
" solver.Add(C2 + C3 + C6 < C8)\n",
" solver.Add(C9 < C8)\n",
" for i in range(n):\n",
" solver.Add(LD[i] != i + 1)\n",
"\n",
2022-04-14 14:07:58 +02:00
" #\n",
" # search and result\n",
" #\n",
" db = solver.Phase(LD, solver.INT_VAR_DEFAULT, solver.INT_VALUE_DEFAULT)\n",
"\n",
2022-04-14 14:07:58 +02:00
" solver.NewSearch(db)\n",
"\n",
2022-04-14 14:07:58 +02:00
" num_solutions = 0\n",
"\n",
2022-04-14 14:07:58 +02:00
" while solver.NextSolution():\n",
" num_solutions += 1\n",
" print('LD:', [LD[i].Value() for i in range(n)])\n",
"\n",
2022-04-14 14:07:58 +02:00
" solver.EndSearch()\n",
"\n",
2022-04-14 14:07:58 +02:00
" print()\n",
" print('num_solutions:', num_solutions)\n",
" print('failures:', solver.Failures())\n",
" print('branches:', solver.Branches())\n",
" print('WallTime:', solver.WallTime(), 'ms')\n",
"\n",
"\n",
2022-04-14 14:07:58 +02:00
"main()\n",
"\n"
]
}
],
2025-02-04 18:04:03 +01:00
"metadata": {
"language_info": {
"name": "python"
}
},
"nbformat": 4,
2021-09-30 01:24:08 +02:00
"nbformat_minor": 5
}