| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 132 |
reasoner.related_to(get_i('spaniard'), has_pet, get_i('dog')) |
| 133 |
|
| 134 |
|
| 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 |
|
| 142 |
reasoner.related_to(get_i('ukrainian'), drinks, get_i('tea')) |
| 143 |
|
| 144 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 169 |
reasoner.related_to(get_i('norwegian'), lives_in, get_i('house1')) |
| 170 |
|
| 171 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 199 |
reasoner.related_to(get_i('japanese'), smokes, get_i('parliament')) |
| 200 |
|
| 201 |
|
| 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 |
|
| 217 |
is_same('x3', 'x7') |
| 218 |
is_same('house1', 'h10') |
| 219 |
|
| 220 |
|
| 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 |
|
| 231 |
is_related_to('water', 'is drunk by', reasoner.inverse(drinks)) |
| 232 |
|
| 233 |
is_related_to('zebra', 'is owned by', reasoner.inverse(has_pet)) |
| 234 |
|
| 235 |
print('\nsolved in {:.2f}s'.format(duration)) |
| 236 |
|
| 237 |
|