Debian Codegraph @

FaCT++.Python/examples/zebra-puzzle.py

raw symbols SCIP
1 #
2 # pyfactxx - Python interface to FaCT++ reasoner
3 #
4 # Copyright (C) 2016-2018 by Artur Wroblewski <[email protected]>
5 #
6 # This program is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation, either version 3 of the License, or
9 # (at your option) any later version.
10 #
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with this program. If not, see <http://www.gnu.org/licenses/>.
18 #
19
20 """
21 Zebra puzzle as described at
22
23 https://en.wikipedia.org/wiki/Zebra_Puzzle
24
25 The example is based on the ontology described at
26
27 http://folk.uio.no/martige/what/20120422/zebra.html
28
29 """
30
31 import time
32 import pyfactxx
33
34 CLASSES = ['Color', 'Drink', 'House', 'Person', 'Pet', 'Smoke']
35
36 COLORS = ['blue', 'yellow', 'ivory', 'green', 'red']
37 DRINKS = ['orange juice', 'water', 'tea', 'milk', 'coffee']
38 PETS = ['horse', 'zebra', 'fox', 'dog', 'snails']
39 SMOKES = ['kools', 'chesterfield', 'lucky strike', 'old gold', 'parliament']
40 HOUSES = ['house5', 'house4', 'house2', 'house3', 'house1']
41 PERSONS = ['norwegian', 'spaniard', 'ukrainian', 'japanese', 'englishman']
42
43 def create_cls(cls_name, individuals):
44 individuals = {i: reasoner.individual(i) for i in individuals}
45 reasoner.different_individuals(*individuals.values())
46
47 cls = reasoner.concept(cls_name)
48 an_individual = reasoner.one_of(*individuals.values())
49 reasoner.equal_concepts(cls, an_individual)
50
51 return cls
52
53 def create_property(name, domain, range):
54 r = reasoner.object_role(name)
55 reasoner.set_o_functional(r)
56 reasoner.set_inverse_functional(r)
57 reasoner.set_o_domain(r, domain)
58 reasoner.set_o_range(r, range)
59 return r
60
61 def is_same(s1, s2):
62 i1 = reasoner.individual(s1)
63 i2 = reasoner.individual(s2)
64 same = reasoner.is_same_individuals(i1, i2)
65 print('{} is {}: {}'.format(s1, s2, 'yes' if same else 'no'))
66
67 def is_related_to(name, property_name, property):
68 i = reasoner.individual(name)
69 values = reasoner.get_role_fillers(i, property)
70 print(name, property_name, [v.name for v in values])
71
72 reasoner = pyfactxx.Reasoner()
73
74 get_i = reasoner.individual
75
76 classes = [reasoner.concept(c) for c in CLASSES]
77 reasoner.disjoint_concepts(*classes)
78
79 color = create_cls('Color', COLORS)
80 drink = create_cls('Drink', DRINKS)
81 pet = create_cls('Pet', PETS)
82 smoke = create_cls('Smoke', SMOKES)
83 house = create_cls('House', HOUSES)
84 person = create_cls('Person', PERSONS)
85
86 drinks = create_property('drinks', person, drink)
87 has_color = create_property('has_color', house, color)
88 has_pet = create_property('has_pet', person, pet)
89 lives_in = create_property('lives_in', person, house)
90 smokes = create_property('smokes', person, smoke)
91
92 is_next_to = reasoner.object_role('is_next_to')
93 reasoner.set_symmetric(is_next_to)
94 reasoner.set_irreflexive(is_next_to)
95 reasoner.set_o_domain(is_next_to, house)
96 reasoner.set_o_range(is_next_to, house)
97
98 is_left_to = reasoner.object_role('is_left_to')
99 reasoner.implies_o_roles(is_left_to, is_next_to)
100
101 is_right_to = reasoner.inverse(is_left_to)
102 reasoner.implies_o_roles(is_right_to, is_next_to)
103 reasoner.set_o_functional(is_right_to)
104 reasoner.set_inverse_functional(is_right_to)
105
106 a_house_color = reasoner.o_exists(has_color, color)
107 reasoner.implies_concepts(house, a_house_color)
108 a_house_next = reasoner.max_o_cardinality(2, is_next_to, house)
109 reasoner.implies_concepts(house, a_house_next)
110
111 a_person = reasoner.intersection(
112 reasoner.o_exists(drinks, drink),
113 reasoner.o_exists(has_pet, pet),
114 reasoner.o_exists(lives_in, house),
115 reasoner.o_exists(smokes, smoke),
116 )
117 reasoner.implies_concepts(person, a_person)
118
119 # 1. There are five houses.
120 reasoner.related_to(get_i('house1'), is_left_to, get_i('house2'))
121 reasoner.related_to(get_i('house2'), is_left_to, get_i('house3'))
122 reasoner.related_to(get_i('house3'), is_left_to, get_i('house4'))
123 reasoner.related_to(get_i('house4'), is_left_to, get_i('house5'))
124 reasoner.related_to_not(get_i('house1'), is_next_to, get_i('house5'))
125
126 # 2. The Englishman lives in the red house.
127 h = get_i('h1')
128 reasoner.related_to(h, has_color, get_i('red'))
129 reasoner.related_to(get_i('englishman'), lives_in, h)
130
131 # 3. The Spaniard owns the dog.
132 reasoner.related_to(get_i('spaniard'), has_pet, get_i('dog'))
133
134 # 4. Coffee is drunk in the green house.
135 x = get_i('x1')
136 h = get_i('h2')
137 reasoner.related_to(x, drinks, get_i('coffee'))
138 reasoner.related_to(h, has_color, get_i('green'))
139 reasoner.related_to(x, lives_in, h)
140
141 # 5. The Ukrainian drinks tea.
142 reasoner.related_to(get_i('ukrainian'), drinks, get_i('tea'))
143
144 # 6. The green house is immediately to the right of the ivory house.
145 h1 = get_i('h3')
146 h2 = get_i('h4')
147 reasoner.related_to(h1, has_color, get_i('green'))
148 reasoner.related_to(h2, has_color, get_i('ivory'))
149 reasoner.related_to(h1, is_right_to, h2)
150
151 # 7. The Old Gold smoker owns snails.
152 x = get_i('x2')
153 reasoner.related_to(x, smokes, get_i('old gold'))
154 reasoner.related_to(x, has_pet, get_i('snails'))
155
156 # 8. Kools are smoked in the yellow house.
157 x = get_i('x3')
158 h = get_i('h5')
159 reasoner.related_to(x, smokes, get_i('kools'))
160 reasoner.related_to(h, has_color, get_i('yellow'))
161 reasoner.related_to(x, lives_in, h)
162
163 # 9. Milk is drunk in the middle house.
164 x = get_i('x4')
165 reasoner.related_to(x, drinks, get_i('milk'))
166 reasoner.related_to(x, lives_in, get_i('house3'))
167
168 # 10. The Norwegian lives in the first house.
169 reasoner.related_to(get_i('norwegian'), lives_in, get_i('house1'))
170
171 # 11. The man who smokes Chesterfields lives in the house next to the man with the fox.
172 x1 = get_i('x5')
173 x2 = get_i('x6')
174 h1 = get_i('h6')
175 h2 = get_i('h7')
176 reasoner.related_to(x1, smokes, get_i('chesterfield'))
177 reasoner.related_to(x1, lives_in, h1)
178 reasoner.related_to(x2, has_pet, get_i('fox'))
179 reasoner.related_to(x2, lives_in, h2)
180 reasoner.related_to(h1, is_next_to, h2)
181
182 # 12. Kools are smoked in a house next to the house where the horse is kept.
183 x1 = get_i('x7')
184 x2 = get_i('x8')
185 h1 = get_i('h8')
186 h2 = get_i('h9')
187 reasoner.related_to(x1, smokes, get_i('kools'))
188 reasoner.related_to(x1, lives_in, h1)
189 reasoner.related_to(x2, has_pet, get_i('horse'))
190 reasoner.related_to(x2, lives_in, h2)
191 reasoner.related_to(h1, is_next_to, h2)
192
193 # 13. The Lucky Strike smoker drinks orange juice.
194 x = get_i('x9')
195 reasoner.related_to(x, smokes, get_i('lucky strike'))
196 reasoner.related_to(x, drinks, get_i('orange juice'))
197
198 # 14. The Japanese smokes Parliaments.
199 reasoner.related_to(get_i('japanese'), smokes, get_i('parliament'))
200
201 # 15. The Norwegian lives next to the blue house.
202 h1 = get_i('h10')
203 h2 = get_i('h11')
204 reasoner.related_to(get_i('norwegian'), lives_in, h1)
205 reasoner.related_to(h2, has_color, get_i('blue'))
206 reasoner.related_to(h1, is_next_to, h2)
207
208 assert reasoner.is_consistent()
209
210 start = time.monotonic()
211 reasoner.realise()
212 duration = time.monotonic() - start
213
214 print('debug:')
215
216 # 8, 12 and 15: norwegian smokes kools and lives in house1
217 is_same('x3', 'x7')
218 is_same('house1', 'h10')
219
220 # ... h11 is next to house1, so h11 is house2
221 is_same('house2', 'h11')
222
223 is_related_to('yellow', 'color of', reasoner.inverse(has_color))
224 is_related_to('blue', 'color of', reasoner.inverse(has_color))
225 is_related_to('red', 'color of', reasoner.inverse(has_color))
226 is_related_to('ivory', 'color of', reasoner.inverse(has_color))
227 is_related_to('green', 'color of', reasoner.inverse(has_color))
228
229 print('\nsolution:')
230 # norwegian expected
231 is_related_to('water', 'is drunk by', reasoner.inverse(drinks))
232 # japanese expected
233 is_related_to('zebra', 'is owned by', reasoner.inverse(has_pet))
234
235 print('\nsolved in {:.2f}s'.format(duration))
236
237 # vim: sw=4:et:ai