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

247 lines
8.5 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": [
"# a_round_of_golf"
]
},
{
"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/a_round_of_golf.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/a_round_of_golf.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",
" A Round of Golf puzzle (Dell Logic Puzzles) in Google CP Solver.\n",
"\n",
" From http://brownbuffalo.sourceforge.net/RoundOfGolfClues.html\n",
" '''\n",
" Title: A Round of Golf\n",
" Author: Ellen K. Rodehorst\n",
" Publication: Dell Favorite Logic Problems\n",
" Issue: Summer, 2000\n",
" Puzzle #: 9\n",
" Stars: 1\n",
"\n",
" When the Sunny Hills Country Club golf course isn't in use by club members,\n",
" of course, it's open to the club's employees. Recently, Jack and three other\n",
" workers at the golf course got together on their day off to play a round of\n",
" eighteen holes of golf.\n",
" Afterward, all four, including Mr. Green, went to the clubhouse to total\n",
" their scorecards. Each man works at a different job (one is a short-order\n",
" cook), and each shot a different score in the game. No one scored below\n",
" 70 or above 85 strokes. From the clues below, can you discover each man's\n",
" full name, job and golf score?\n",
"\n",
" 1. Bill, who is not the maintenance man, plays golf often and had the lowest\n",
" score of the foursome.\n",
" 2. Mr. Clubb, who isn't Paul, hit several balls into the woods and scored ten\n",
" strokes more than the pro-shop clerk.\n",
" 3. In some order, Frank and the caddy scored four and seven more strokes than\n",
" Mr. Sands.\n",
" 4. Mr. Carter thought his score of 78 was one of his better games, even\n",
" though Frank's score was lower.\n",
" 5. None of the four scored exactly 81 strokes.\n",
"\n",
" Determine: First Name - Last Name - Job - Score\n",
" '''\n",
"\n",
" Compare with the F1 model:\n",
" http://www.f1compiler.com/samples/A 20Round 20of 20Golf.f1.html\n",
"\n",
"\n",
" Compare with the following models:\n",
" * MiniZinc: http://www.hakank.org/minizinc/a_round_of_golf.mzn\n",
" * Comet : http://www.hakank.org/comet/a_round_of_golf.co\n",
" * ECLiPSe : http://www.hakank.org/eclipse/a_round_of_golf.ecl\n",
" * Gecode : http://hakank.org/gecode/a_round_of_golf.cpp\n",
" * SICStus : http://hakank.org/sicstus/a_round_of_golf.pl\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",
"\n",
"\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "code",
"metadata": {},
"outputs": [],
"source": [
"from ortools.constraint_solver import pywrapcp\n",
"\n",
"\n",
"def main():\n",
"\n",
" # Create the solver.\n",
" solver = pywrapcp.Solver(\"All interval\")\n",
"\n",
" #\n",
" # data\n",
" #\n",
" n = 4\n",
" [Jack, Bill, Paul, Frank] = [i for i in range(n)]\n",
"\n",
" #\n",
" # declare variables\n",
" #\n",
" last_name = [solver.IntVar(0, n - 1, \"last_name[%i]\" % i) for i in range(n)]\n",
" [Green, Clubb, Sands, Carter] = last_name\n",
"\n",
" job = [solver.IntVar(0, n - 1, \"job[%i]\" % i) for i in range(n)]\n",
" [cook, maintenance_man, clerk, caddy] = job\n",
"\n",
" score = [solver.IntVar(70, 85, \"score[%i]\" % i) for i in range(n)]\n",
"\n",
" #\n",
" # constraints\n",
" #\n",
" solver.Add(solver.AllDifferent(last_name))\n",
" solver.Add(solver.AllDifferent(job))\n",
" solver.Add(solver.AllDifferent(score))\n",
"\n",
" # 1. Bill, who is not the maintenance man, plays golf often and had\n",
" # the lowest score of the foursome.\n",
" solver.Add(Bill != maintenance_man)\n",
" solver.Add(score[Bill] < score[Jack])\n",
" solver.Add(score[Bill] < score[Paul])\n",
" solver.Add(score[Bill] < score[Frank])\n",
"\n",
" # 2. Mr. Clubb, who isn't Paul, hit several balls into the woods and\n",
" # scored ten strokes more than the pro-shop clerk.\n",
" solver.Add(Clubb != Paul)\n",
" solver.Add(solver.Element(score, Clubb) == solver.Element(score, clerk) + 10)\n",
"\n",
" # 3. In some order, Frank and the caddy scored four and seven more\n",
" # strokes than Mr. Sands.\n",
" solver.Add(Frank != caddy)\n",
" solver.Add(Frank != Sands)\n",
" solver.Add(caddy != Sands)\n",
"\n",
" b3_a_1 = solver.IsEqualVar(solver.Element(score, Sands) + 4, score[Frank])\n",
" b3_a_2 = solver.IsEqualVar(\n",
" solver.Element(score, caddy),\n",
" solver.Element(score, Sands) + 7)\n",
"\n",
" b3_b_1 = solver.IsEqualVar(solver.Element(score, Sands) + 7, score[Frank])\n",
" b3_b_2 = solver.IsEqualVar(\n",
" solver.Element(score, caddy),\n",
" solver.Element(score, Sands) + 4)\n",
"\n",
" solver.Add((b3_a_1 * b3_a_2) + (b3_b_1 * b3_b_2) == 1)\n",
"\n",
" # 4. Mr. Carter thought his score of 78 was one of his better games,\n",
" # even though Frank's score was lower.\n",
" solver.Add(Frank != Carter)\n",
" solver.Add(solver.Element(score, Carter) == 78)\n",
" solver.Add(score[Frank] < solver.Element(score, Carter))\n",
"\n",
" # 5. None of the four scored exactly 81 strokes.\n",
" [solver.Add(score[i] != 81) for i in range(n)]\n",
"\n",
" #\n",
" # solution and search\n",
" #\n",
" solution = solver.Assignment()\n",
" solution.Add(last_name)\n",
" solution.Add(job)\n",
" solution.Add(score)\n",
"\n",
" db = solver.Phase(last_name + job + score, solver.CHOOSE_FIRST_UNBOUND,\n",
" solver.INT_VALUE_DEFAULT)\n",
"\n",
" solver.NewSearch(db)\n",
" num_solutions = 0\n",
" while solver.NextSolution():\n",
" print(\"last_name:\", [last_name[i].Value() for i in range(n)])\n",
" print(\"job :\", [job[i].Value() for i in range(n)])\n",
" print(\"score :\", [score[i].Value() for i in range(n)])\n",
" num_solutions += 1\n",
" print()\n",
"\n",
" print(\"num_solutions:\", num_solutions)\n",
" print(\"failures:\", solver.Failures())\n",
" print(\"branches:\", solver.Branches())\n",
" print(\"WallTime:\", solver.WallTime())\n",
"\n",
"\n",
"main()\n",
"\n"
]
}
],
"metadata": {
"language_info": {
"name": "python"
}
},
"nbformat": 4,
"nbformat_minor": 5
}