-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
a_round_of_golf.py
166 lines (132 loc) · 5.42 KB
/
a_round_of_golf.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# Copyright 2010 Hakan Kjellerstrand [email protected]
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""
A Round of Golf puzzle (Dell Logic Puzzles) in Google CP Solver.
From http://brownbuffalo.sourceforge.net/RoundOfGolfClues.html
'''
Title: A Round of Golf
Author: Ellen K. Rodehorst
Publication: Dell Favorite Logic Problems
Issue: Summer, 2000
Puzzle #: 9
Stars: 1
When the Sunny Hills Country Club golf course isn't in use by club members,
of course, it's open to the club's employees. Recently, Jack and three other
workers at the golf course got together on their day off to play a round of
eighteen holes of golf.
Afterward, all four, including Mr. Green, went to the clubhouse to total
their scorecards. Each man works at a different job (one is a short-order
cook), and each shot a different score in the game. No one scored below
70 or above 85 strokes. From the clues below, can you discover each man's
full name, job and golf score?
1. Bill, who is not the maintenance man, plays golf often and had the lowest
score of the foursome.
2. Mr. Clubb, who isn't Paul, hit several balls into the woods and scored ten
strokes more than the pro-shop clerk.
3. In some order, Frank and the caddy scored four and seven more strokes than
Mr. Sands.
4. Mr. Carter thought his score of 78 was one of his better games, even
though Frank's score was lower.
5. None of the four scored exactly 81 strokes.
Determine: First Name - Last Name - Job - Score
'''
Compare with the F1 model:
http://www.f1compiler.com/samples/A 20Round 20of 20Golf.f1.html
Compare with the following models:
* MiniZinc: http://www.hakank.org/minizinc/a_round_of_golf.mzn
* Comet : http://www.hakank.org/comet/a_round_of_golf.co
* ECLiPSe : http://www.hakank.org/eclipse/a_round_of_golf.ecl
* Gecode : http://hakank.org/gecode/a_round_of_golf.cpp
* SICStus : http://hakank.org/sicstus/a_round_of_golf.pl
This model was created by Hakan Kjellerstrand ([email protected])
Also see my other Google CP Solver models:
http://www.hakank.org/google_or_tools/
"""
from ortools.constraint_solver import pywrapcp
def main():
# Create the solver.
solver = pywrapcp.Solver("All interval")
#
# data
#
n = 4
[Jack, Bill, Paul, Frank] = [i for i in range(n)]
#
# declare variables
#
last_name = [solver.IntVar(0, n - 1, "last_name[%i]" % i) for i in range(n)]
[Green, Clubb, Sands, Carter] = last_name
job = [solver.IntVar(0, n - 1, "job[%i]" % i) for i in range(n)]
[cook, maintenance_man, clerk, caddy] = job
score = [solver.IntVar(70, 85, "score[%i]" % i) for i in range(n)]
#
# constraints
#
solver.Add(solver.AllDifferent(last_name))
solver.Add(solver.AllDifferent(job))
solver.Add(solver.AllDifferent(score))
# 1. Bill, who is not the maintenance man, plays golf often and had
# the lowest score of the foursome.
solver.Add(Bill != maintenance_man)
solver.Add(score[Bill] < score[Jack])
solver.Add(score[Bill] < score[Paul])
solver.Add(score[Bill] < score[Frank])
# 2. Mr. Clubb, who isn't Paul, hit several balls into the woods and
# scored ten strokes more than the pro-shop clerk.
solver.Add(Clubb != Paul)
solver.Add(solver.Element(score, Clubb) == solver.Element(score, clerk) + 10)
# 3. In some order, Frank and the caddy scored four and seven more
# strokes than Mr. Sands.
solver.Add(Frank != caddy)
solver.Add(Frank != Sands)
solver.Add(caddy != Sands)
b3_a_1 = solver.IsEqualVar(solver.Element(score, Sands) + 4, score[Frank])
b3_a_2 = solver.IsEqualVar(
solver.Element(score, caddy),
solver.Element(score, Sands) + 7)
b3_b_1 = solver.IsEqualVar(solver.Element(score, Sands) + 7, score[Frank])
b3_b_2 = solver.IsEqualVar(
solver.Element(score, caddy),
solver.Element(score, Sands) + 4)
solver.Add((b3_a_1 * b3_a_2) + (b3_b_1 * b3_b_2) == 1)
# 4. Mr. Carter thought his score of 78 was one of his better games,
# even though Frank's score was lower.
solver.Add(Frank != Carter)
solver.Add(solver.Element(score, Carter) == 78)
solver.Add(score[Frank] < solver.Element(score, Carter))
# 5. None of the four scored exactly 81 strokes.
[solver.Add(score[i] != 81) for i in range(n)]
#
# solution and search
#
solution = solver.Assignment()
solution.Add(last_name)
solution.Add(job)
solution.Add(score)
db = solver.Phase(last_name + job + score, solver.CHOOSE_FIRST_UNBOUND,
solver.INT_VALUE_DEFAULT)
solver.NewSearch(db)
num_solutions = 0
while solver.NextSolution():
print("last_name:", [last_name[i].Value() for i in range(n)])
print("job :", [job[i].Value() for i in range(n)])
print("score :", [score[i].Value() for i in range(n)])
num_solutions += 1
print()
print("num_solutions:", num_solutions)
print("failures:", solver.Failures())
print("branches:", solver.Branches())
print("WallTime:", solver.WallTime())
if __name__ == "__main__":
main()