-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
zebra_sat.py
executable file
·122 lines (100 loc) · 4.51 KB
/
zebra_sat.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
#!/usr/bin/env python3
# Copyright 2010-2024 Google LLC
# 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.
"""This is the zebra problem as invented by Lewis Caroll.
There are five houses.
The Englishman lives in the red house.
The Spaniard owns the dog.
Coffee is drunk in the green house.
The Ukrainian drinks tea.
The green house is immediately to the right of the ivory house.
The Old Gold smoker owns snails.
Kools are smoked in the yellow house.
Milk is drunk in the middle house.
The Norwegian lives in the first house.
The man who smokes Chesterfields lives in the house next to the man
with the fox.
Kools are smoked in the house next to the house where the horse is kept.
The Lucky Strike smoker drinks orange juice.
The Japanese smokes Parliaments.
The Norwegian lives next to the blue house.
Who owns a zebra and who drinks water?
"""
from ortools.sat.python import cp_model
# pylint: disable=too-many-statements
def solve_zebra():
"""Solves the zebra problem."""
# Create the model.
model = cp_model.CpModel()
red = model.new_int_var(1, 5, "red")
green = model.new_int_var(1, 5, "green")
yellow = model.new_int_var(1, 5, "yellow")
blue = model.new_int_var(1, 5, "blue")
ivory = model.new_int_var(1, 5, "ivory")
englishman = model.new_int_var(1, 5, "englishman")
spaniard = model.new_int_var(1, 5, "spaniard")
japanese = model.new_int_var(1, 5, "japanese")
ukrainian = model.new_int_var(1, 5, "ukrainian")
norwegian = model.new_int_var(1, 5, "norwegian")
dog = model.new_int_var(1, 5, "dog")
snails = model.new_int_var(1, 5, "snails")
fox = model.new_int_var(1, 5, "fox")
zebra = model.new_int_var(1, 5, "zebra")
horse = model.new_int_var(1, 5, "horse")
tea = model.new_int_var(1, 5, "tea")
coffee = model.new_int_var(1, 5, "coffee")
water = model.new_int_var(1, 5, "water")
milk = model.new_int_var(1, 5, "milk")
fruit_juice = model.new_int_var(1, 5, "fruit juice")
old_gold = model.new_int_var(1, 5, "old gold")
kools = model.new_int_var(1, 5, "kools")
chesterfields = model.new_int_var(1, 5, "chesterfields")
lucky_strike = model.new_int_var(1, 5, "lucky strike")
parliaments = model.new_int_var(1, 5, "parliaments")
model.add_all_different(red, green, yellow, blue, ivory)
model.add_all_different(englishman, spaniard, japanese, ukrainian, norwegian)
model.add_all_different(dog, snails, fox, zebra, horse)
model.add_all_different(tea, coffee, water, milk, fruit_juice)
model.add_all_different(parliaments, kools, chesterfields, lucky_strike, old_gold)
model.add(englishman == red)
model.add(spaniard == dog)
model.add(coffee == green)
model.add(ukrainian == tea)
model.add(green == ivory + 1)
model.add(old_gold == snails)
model.add(kools == yellow)
model.add(milk == 3)
model.add(norwegian == 1)
diff_fox_chesterfields = model.new_int_var(-4, 4, "diff_fox_chesterfields")
model.add(diff_fox_chesterfields == fox - chesterfields)
model.add_abs_equality(1, diff_fox_chesterfields)
diff_horse_kools = model.new_int_var(-4, 4, "diff_horse_kools")
model.add(diff_horse_kools == horse - kools)
model.add_abs_equality(1, diff_horse_kools)
model.add(lucky_strike == fruit_juice)
model.add(japanese == parliaments)
diff_norwegian_blue = model.new_int_var(-4, 4, "diff_norwegian_blue")
model.add(diff_norwegian_blue == norwegian - blue)
model.add_abs_equality(1, diff_norwegian_blue)
# Solve and print out the solution.
solver = cp_model.CpSolver()
status = solver.solve(model)
if status == cp_model.OPTIMAL:
people = [englishman, spaniard, japanese, ukrainian, norwegian]
water_drinker = [p for p in people if solver.value(p) == solver.value(water)][0]
zebra_owner = [p for p in people if solver.value(p) == solver.value(zebra)][0]
print("The", water_drinker.name, "drinks water.")
print("The", zebra_owner.name, "owns the zebra.")
else:
print("No solutions to the zebra problem, this is unusual!")
solve_zebra()