Files
ortools-clone/examples/notebook/contrib/mr_smith.ipynb
2025-02-04 18:04:03 +01:00

200 lines
5.8 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": [
"# mr_smith"
]
},
{
"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/contrib/mr_smith.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/examples/contrib/mr_smith.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",
"\n",
" Mr Smith in Google CP Solver.\n",
"\n",
" From an IF Prolog example (http://www.ifcomputer.de/)\n",
" '''\n",
" The Smith family and their three children want to pay a visit but they\n",
" do not all have the time to do so. Following are few hints who will go\n",
" and who will not:\n",
" o If Mr Smith comes, his wife will come too.\n",
" o At least one of their two sons Matt and John will come.\n",
" o Either Mrs Smith or Tim will come, but not both.\n",
" o Either Tim and John will come, or neither will come.\n",
" o If Matt comes, then John and his father will\n",
" also come.\n",
" '''\n",
"\n",
" The answer should be:\n",
" Mr_Smith_comes = 0\n",
" Mrs_Smith_comes = 0\n",
" Matt_comes = 0\n",
" John_comes = 1\n",
" Tim_comes = 1\n",
"\n",
" Compare with the following models:\n",
" * ECLiPSe: http://www.hakank.org/eclipse/mr_smith.ecl\n",
" * SICStus Prolog: http://www.hakank.org/sicstus/mr_smith.pl\n",
" * Gecode: http://www.hakank.org/gecode/mr_smith.cpp\n",
" * MiniZinc: http://www.hakank.org/minizinc/mr_smith.mzn\n",
"\n",
"\n",
" This model was created by Hakan Kjellerstrand (hakank@gmail.com)\n",
" Also see my other Google CP Solver models:\n",
" http://www.hakank.org/google_or_tools/\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "code",
"metadata": {},
"outputs": [],
"source": [
"import sys\n",
"from ortools.constraint_solver import pywrapcp\n",
"\n",
"\n",
"def main():\n",
"\n",
" # Create the solver.\n",
" solver = pywrapcp.Solver('Mr Smith problem')\n",
"\n",
" #\n",
" # data\n",
" #\n",
" n = 5\n",
"\n",
" #\n",
" # declare variables\n",
" #\n",
" x = [solver.IntVar(0, 1, 'x[%i]' % i) for i in range(n)]\n",
" Mr_Smith, Mrs_Smith, Matt, John, Tim = x\n",
"\n",
" #\n",
" # constraints\n",
" #\n",
"\n",
" #\n",
" # I've kept the MiniZinc constraints for clarity\n",
" # and debugging.\n",
" #\n",
"\n",
" # If Mr Smith comes then his wife will come too.\n",
" # (Mr_Smith -> Mrs_Smith)\n",
" solver.Add(Mr_Smith - Mrs_Smith <= 0)\n",
"\n",
" # At least one of their two sons Matt and John will come.\n",
" # (Matt \\/ John)\n",
" solver.Add(Matt + John >= 1)\n",
"\n",
" # Either Mrs Smith or Tim will come but not both.\n",
" # bool2int(Mrs_Smith) + bool2int(Tim) = 1 /\\\n",
" # (Mrs_Smith xor Tim)\n",
" solver.Add(Mrs_Smith + Tim == 1)\n",
"\n",
" # Either Tim and John will come or neither will come.\n",
" # (Tim = John)\n",
" solver.Add(Tim == John)\n",
"\n",
" # If Matt comes /\\ then John and his father will also come.\n",
" # (Matt -> (John /\\ Mr_Smith))\n",
" solver.Add(Matt - (John * Mr_Smith) <= 0)\n",
"\n",
" #\n",
" # solution and search\n",
" #\n",
" db = solver.Phase(x, solver.INT_VAR_DEFAULT, solver.INT_VALUE_DEFAULT)\n",
"\n",
" solver.NewSearch(db)\n",
"\n",
" num_solutions = 0\n",
" while solver.NextSolution():\n",
" num_solutions += 1\n",
" print('x:', [x[i].Value() for i in range(n)])\n",
"\n",
" 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",
"main()\n",
"\n"
]
}
],
"metadata": {
"language_info": {
"name": "python"
}
},
"nbformat": 4,
"nbformat_minor": 5
}