-
Notifications
You must be signed in to change notification settings - Fork 4
/
wumpus_kb.py
755 lines (557 loc) · 26.5 KB
/
wumpus_kb.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
# wumpus_kb.py
# ------------
# Licensing Information:
# Please DO NOT DISTRIBUTE OR PUBLISH solutions to this project.
# You are free to use and extend these projects for EDUCATIONAL PURPOSES ONLY.
# The Hunt The Wumpus AI project was developed at University of Arizona
# by Clay Morrison ([email protected]), spring 2013.
# This project extends the python code provided by Peter Norvig as part of
# the Artificial Intelligence: A Modern Approach (AIMA) book example code;
# see http://aima.cs.berkeley.edu/code.html
# In particular, the following files come directly from the AIMA python
# code: ['agents.py', 'logic.py', 'search.py', 'utils.py']
# ('logic.py' has been modified by Clay Morrison in locations with the
# comment 'CTM')
# The file ['minisat.py'] implements a slim system call wrapper to the minisat
# (see http://minisat.se) SAT solver, and is directly based on the satispy
# python project, see https://github.com/netom/satispy .
import utils
# -------------------------------------------------------------------------------
# Wumpus Propositions
# -------------------------------------------------------------------------------
### atemporal variables
proposition_bases_atemporal_location = ['P', 'W', 'S', 'B']
def pit_str(x, y):
"There is a Pit at <x>,<y>"
return 'P{0}_{1}'.format(x, y)
def wumpus_str(x, y):
"There is a Wumpus at <x>,<y>"
return 'W{0}_{1}'.format(x, y)
def stench_str(x, y):
"There is a Stench at <x>,<y>"
return 'S{0}_{1}'.format(x, y)
def breeze_str(x, y):
"There is a Breeze at <x>,<y>"
return 'B{0}_{1}'.format(x, y)
### fluents (every proposition who's truth depends on time)
proposition_bases_perceptual_fluents = ['Stench', 'Breeze', 'Glitter', 'Bump', 'Scream']
def percept_stench_str(t):
"A Stench is perceived at time <t>"
return 'Stench{0}'.format(t)
def percept_breeze_str(t):
"A Breeze is perceived at time <t>"
return 'Breeze{0}'.format(t)
def percept_glitter_str(t):
"A Glitter is perceived at time <t>"
return 'Glitter{0}'.format(t)
def percept_bump_str(t):
"A Bump is perceived at time <t>"
return 'Bump{0}'.format(t)
def percept_scream_str(t):
"A Scream is perceived at time <t>"
return 'Scream{0}'.format(t)
proposition_bases_location_fluents = ['OK', 'L']
def state_OK_str(x, y, t):
"Location <x>,<y> is OK at time <t>"
return 'OK{0}_{1}_{2}'.format(x, y, t)
def state_loc_str(x, y, t):
"At Location <x>,<y> at time <t>"
return 'L{0}_{1}_{2}'.format(x, y, t)
def loc_proposition_to_tuple(loc_prop):
"""
Utility to convert location propositions to location (x,y) tuples
Used by HybridWumpusAgent for internal bookkeeping.
"""
parts = loc_prop.split('_')
return (int(parts[0][1:]), int(parts[1]))
proposition_bases_state_fluents = ['HeadingNorth', 'HeadingEast',
'HeadingSouth', 'HeadingWest',
'HaveArrow', 'WumpusAlive']
def state_heading_north_str(t):
"Heading North at time <t>"
return 'HeadingNorth{0}'.format(t)
def state_heading_east_str(t):
"Heading East at time <t>"
return 'HeadingEast{0}'.format(t)
def state_heading_south_str(t):
"Heading South at time <t>"
return 'HeadingSouth{0}'.format(t)
def state_heading_west_str(t):
"Heading West at time <t>"
return 'HeadingWest{0}'.format(t)
def state_have_arrow_str(t):
"Have Arrow at time <t>"
return 'HaveArrow{0}'.format(t)
def state_wumpus_alive_str(t):
"Wumpus is Alive at time <t>"
return 'WumpusAlive{0}'.format(t)
proposition_bases_actions = ['Forward', 'Grab', 'Shoot', 'Climb',
'TurnLeft', 'TurnRight', 'Wait']
def action_forward_str(t=None):
"Action Forward executed at time <t>"
return ('Forward{0}'.format(t) if t != None else 'Forward')
def action_grab_str(t=None):
"Action Grab executed at time <t>"
return ('Grab{0}'.format(t) if t != None else 'Grab')
def action_shoot_str(t=None):
"Action Shoot executed at time <t>"
return ('Shoot{0}'.format(t) if t != None else 'Shoot')
def action_climb_str(t=None):
"Action Climb executed at time <t>"
return ('Climb{0}'.format(t) if t != None else 'Climb')
def action_turn_left_str(t=None):
"Action Turn Left executed at time <t>"
return ('TurnLeft{0}'.format(t) if t != None else 'TurnLeft')
def action_turn_right_str(t=None):
"Action Turn Right executed at time <t>"
return ('TurnRight{0}'.format(t) if t != None else 'TurnRight')
def action_wait_str(t=None):
"Action Wait executed at time <t>"
return ('Wait{0}'.format(t) if t != None else 'Wait')
def add_time_stamp(prop, t): return '{0}{1}'.format(prop, t)
proposition_bases_all = [proposition_bases_atemporal_location,
proposition_bases_perceptual_fluents,
proposition_bases_location_fluents,
proposition_bases_state_fluents,
proposition_bases_actions]
# -------------------------------------------------------------------------------
# Axiom Generator: Current Percept Sentence
# -------------------------------------------------------------------------------
# def make_percept_sentence(t, tvec):
def axiom_generator_percept_sentence(t, tvec):
"""
Asserts that each percept proposition is True or False at time t.
t := time
tvec := a boolean (True/False) vector with entries corresponding to
percept propositions, in this order:
(<stench>,<breeze>,<glitter>,<bump>,<scream>)
Example:
Input: [False, True, False, False, True]
Output: '~Stench0 & Breeze0 & ~Glitter0 & ~Bump0 & Scream0'
"""
props = []
percepts = ['Stench','Breeze','Glitter','Bump','Scream']
for index in range(len(tvec)):
if tvec[index]:
props.append(percepts[index] + str(t))
else:
props.append('~' + percepts[index] + str(t))
axiom_str = ' & '.join(props)
return axiom_str
# -------------------------------------------------------------------------------
# Axiom Generators: Initial Axioms
# -------------------------------------------------------------------------------
def axiom_generator_initial_location_assertions(x, y):
"""
Assert that there is no Pit and no Wumpus in the location
x,y := the location
"""
axiom_str = '(~{0}) & (~{1})'.format(pit_str(x,y),wumpus_str(x,y))
return axiom_str
def axiom_generator_pits_and_breezes(x, y, xmin, xmax, ymin, ymax):
"""
Assert that Breezes (atemporal) are only found in locations where
there are one or more Pits in a neighboring location (or the same location!)
x,y := the location
xmin, xmax, ymin, ymax := the bounds of the environment; you use these
variables to 'prune' any neighboring locations that are outside
of the environment (and therefore are walls, so can't have Pits).
"""
axiom_str = ''
pits = []
for (xVal,yVal) in [((x-1),y),(x,(y-1)),((x+1),y),(x,(y+1))]:
if xVal >= xmin and xVal <= xmax and yVal >= ymin and yVal <= ymax:
pits.append(pit_str(xVal,yVal))
pits.append('P'+str(x)+'_'+str(y))
axiom_str += '{0} <=> ({1})'.format(breeze_str(x,y),(' | ').join(pits))
return axiom_str
# axioms =[]
# rooms = [(xVal,yVal) for xVal in range(xmin,xmax+1) for yVal in range(ymin,ymax+1)]
# for room in rooms:
# pits =[]
# for (xCoor,yCoor) in [((room[0]-1),room[1]),(room[0],(room[1]-1)),((room[0]+1),room[1]),(room[0],(room[1]+1))]:
# if xCoor >= xmin and xCoor <= xmax and yCoor >= ymin and yCoor <= ymax:
# pits.append(pit_str(xCoor,yCoor))
# axioms.append('({0} <=> ({1}))'.format(breeze_str(room[0],room[1]),(' | ').join(pits)))
# axiom_str = ' & '.join(axioms)
# return axiom_str
def generate_pit_and_breeze_axioms(xmin, xmax, ymin, ymax):
axioms = []
for x in range(xmin, xmax + 1):
for y in range(ymin, ymax + 1):
axioms.append(axiom_generator_pits_and_breezes(x, y, xmin, xmax, ymin, ymax))
if utils.all_empty_strings(axioms):
utils.print_not_implemented('axiom_generator_pits_and_breezes')
return axioms
def axiom_generator_wumpus_and_stench(x, y, xmin, xmax, ymin, ymax):
"""
Assert that Stenches (atemporal) are only found in locations where
there are one or more Wumpi in a neighboring location (or the same location!)
(Don't try to assert here that there is only one Wumpus;
we'll handle that separately)
x,y := the location
xmin, xmax, ymin, ymax := the bounds of the environment; you use these
variables to 'prune' any neighboring locations that are outside
of the environment (and therefore are walls, so can't have Wumpi).
"""
wumpi = []
for (xVal, yVal) in [((x - 1), y), (x, (y - 1)), ((x + 1), y), (x, (y + 1))]:
if xVal >= xmin and xVal <= xmax and yVal >= ymin and yVal <= ymax:
wumpi.append(wumpus_str(xVal, yVal))
wumpi.append('W'+str(x)+'_'+str(y))
axiom_str = '{0} <=> ({1})'.format(stench_str(x, y), (' | ').join(wumpi))
return axiom_str
def generate_wumpus_and_stench_axioms(xmin, xmax, ymin, ymax):
axioms = []
for x in range(xmin, xmax + 1):
for y in range(ymin, ymax + 1):
axioms.append(axiom_generator_wumpus_and_stench(x, y, xmin, xmax, ymin, ymax))
if utils.all_empty_strings(axioms):
utils.print_not_implemented('axiom_generator_wumpus_and_stench')
return axioms
def axiom_generator_at_least_one_wumpus(xmin, xmax, ymin, ymax):
"""
Assert that there is at least one Wumpus.
xmin, xmax, ymin, ymax := the bounds of the environment.
"""
rooms = [wumpus_str(xVal,yVal) for xVal in range(xmin,xmax + 1) for yVal in range(ymin,ymax + 1)]
axiom_str = ' | '.join(rooms)
return axiom_str
def axiom_generator_at_most_one_wumpus(xmin, xmax, ymin, ymax):
"""
Assert that there is at at most one Wumpus.
xmin, xmax, ymin, ymax := the bounds of the environment.
"""
# rooms = ['~' + wumpus_str(xVal,yVal) for xVal in range(xmin,xmax + 1) for yVal in range(ymin,ymax + 1)]
# axiom_str = ' | '.join(rooms)
# return axiom_str
rooms = [(xVal, yVal) for xVal in range(xmin, xmax + 1) for yVal in range(ymin, ymax + 1)]
axioms = []
for room in rooms:
notRooms = [thisRoom for thisRoom in rooms if thisRoom != room]
noWumpus = ['~'+wumpus_str(w[0],w[1]) for w in notRooms]
axioms.append('({0} >> ({1}))'.format(wumpus_str(room[0],room[1]),' & '.join(noWumpus)))
axiom_str = ' & '.join(axioms)
return axiom_str
def axiom_generator_only_in_one_location(xi, yi, xmin, xmax, ymin, ymax, t=0):
"""
Assert that the Agent can only be in one (the current xi,yi) location at time t.
xi,yi := the current location.
xmin, xmax, ymin, ymax := the bounds of the environment.
t := time; default=0
"""
currentRoom = ('~L'+str(xi)+'_'+str(yi)+'_'+str(t))
notRooms = ['~' + state_loc_str(xVal,yVal,t) for xVal in range(xmin,xmax+1) for yVal in range(ymin,ymax+1)]
notRooms.remove(currentRoom)
pre_axiom_str = 'L{0}_{1}_{2}'.format(xi,yi,t)
post_axiom_str = '{0}'.format(' & '.join(notRooms))
axiom_str = pre_axiom_str + " & " + post_axiom_str
return axiom_str
def axiom_generator_only_one_heading(heading='north', t=0):
"""
Assert that Agent can only head in one direction at a time.
heading := string indicating heading; default='north';
will be one of: 'north', 'east', 'south', 'west'
t := time; default=0
"""
axiom_str = ''
headings = ['North','South','East','West']
notHeadings = []
for h in headings:
if h.upper() == heading.upper():
axiom_str += 'Heading{0}{1} & ('.format(h,str(t))
else:
notHeadings.append('~Heading' + h + str(t))
axiom_str += '{0})'.format('&'.join(notHeadings))
return axiom_str
def axiom_generator_have_arrow_and_wumpus_alive(t=0):
"""
Assert that Agent has the arrow and the Wumpus is alive at time t.
t := time; default=0
"""
axiom_str = '{0} & {1}'.format(state_have_arrow_str(t),state_wumpus_alive_str(t))
return axiom_str
def initial_wumpus_axioms(xi, yi, width, height, heading='east'):
"""
Generate all of the initial wumpus axioms
xi,yi = initial location
width,height = dimensions of world
heading = str representation of the initial agent heading
"""
axioms = [axiom_generator_initial_location_assertions(xi, yi)]
axioms.extend(generate_pit_and_breeze_axioms(1, width, 1, height))
axioms.extend(generate_wumpus_and_stench_axioms(1, width, 1, height))
axioms.append(axiom_generator_at_least_one_wumpus(1, width, 1, height))
axioms.append(axiom_generator_at_most_one_wumpus(1, width, 1, height))
axioms.append(axiom_generator_only_in_one_location(xi, yi, 1, width, 1, height))
axioms.append(axiom_generator_only_one_heading(heading))
axioms.append(axiom_generator_have_arrow_and_wumpus_alive())
return axioms
# -------------------------------------------------------------------------------
# Axiom Generators: Temporal Axioms (added at each time step)
# -------------------------------------------------------------------------------
def axiom_generator_location_OK(x, y, t):
"""
Assert the conditions under which a location is safe for the Agent.
(Hint: Are Wumpi always dangerous?)
x,y := location
t := time
"""
axiom_str = '{0} <=> (~{1} & ({3} >> ~{2}))'.format(state_OK_str(x,y,t),pit_str(x,y),state_wumpus_alive_str(t),
wumpus_str(x,y))
return axiom_str
def generate_square_OK_axioms(t, xmin, xmax, ymin, ymax):
axioms = []
for x in range(xmin, xmax + 1):
for y in range(ymin, ymax + 1):
axioms.append(axiom_generator_location_OK(x, y, t))
if utils.all_empty_strings(axioms):
utils.print_not_implemented('axiom_generator_location_OK')
return filter(lambda s: s != '', axioms)
# -------------------------------------------------------------------------------
# Connection between breeze / stench percepts and atemporal location properties
def axiom_generator_breeze_percept_and_location_property(x, y, t):
"""
Assert that when in a location at time t, then perceiving a breeze
at that time (a percept) means that the location is breezy (atemporal)
x,y := location
t := time
"""
axiom_str = '{0} >> ({1} % {2})'.format(state_loc_str(x,y,t),percept_breeze_str(t),breeze_str(x,y))
return axiom_str
def generate_breeze_percept_and_location_axioms(t, xmin, xmax, ymin, ymax):
axioms = []
for x in range(xmin, xmax + 1):
for y in range(ymin, ymax + 1):
axioms.append(axiom_generator_breeze_percept_and_location_property(x, y, t))
if utils.all_empty_strings(axioms):
utils.print_not_implemented('axiom_generator_breeze_percept_and_location_property')
return filter(lambda s: s != '', axioms)
def axiom_generator_stench_percept_and_location_property(x, y, t):
"""
Assert that when in a location at time t, then perceiving a stench
at that time (a percept) means that the location has a stench (atemporal)
x,y := location
t := time
"""
axiom_str = '{0} >> ({1} % {2})'.format(state_loc_str(x,y,t),percept_stench_str(t),stench_str(x,y))
return axiom_str
def generate_stench_percept_and_location_axioms(t, xmin, xmax, ymin, ymax):
axioms = []
for x in range(xmin, xmax + 1):
for y in range(ymin, ymax + 1):
axioms.append(axiom_generator_stench_percept_and_location_property(x, y, t))
if utils.all_empty_strings(axioms):
utils.print_not_implemented('axiom_generator_stench_percept_and_location_property')
return filter(lambda s: s != '', axioms)
# -------------------------------------------------------------------------------
# Transition model: Successor-State Axioms (SSA's)
# Avoid the frame problem(s): don't write axioms about actions, write axioms about
# fluents! That is, write successor-state axioms as opposed to effect and frame
# axioms
#
# The general successor-state axioms pattern (where F is a fluent):
# F^{t+1} <=> (Action(s)ThatCause_F^t) | (F^t & ~Action(s)ThatCauseNot_F^t)
# NOTE: this is very expensive in terms of generating many (~170 per axiom) CNF clauses!
def axiom_generator_at_location_ssa(t, x, y, xmin, xmax, ymin, ymax):
"""
Assert the condidtions at time t under which the agent is in
a particular location (state_loc_str: L) at time t+1, following
the successor-state axiom pattern.
See Section 7. of AIMA. However...
NOTE: the book's version of this class of axioms is not complete
for the version in Project 3.
x,y := location
t := time
xmin, xmax, ymin, ymax := the bounds of the environment.
"""
moves = ['({0} & (~{1} | {2} | {3} | {4} | {5} | {6}))'.format(state_loc_str(x,y,t),action_forward_str(t),percept_bump_str(t+1),
action_grab_str(t),action_shoot_str(t),action_turn_left_str(t),action_turn_right_str(t))]
for ((xVal,yVal),move) in [(((x-1),y),'E'),((x,(y-1)),'N'),(((x+1),y),'W'),((x,(y+1)),'S')]:
if xVal >= xmin and xVal <= xmax and yVal >= ymin and yVal <= ymax:
if move == 'N':
moves.append('({0} & ({1} & {2}))'.format(state_loc_str(x,y-1,t),state_heading_north_str(t),action_forward_str(t)))
if move == 'E':
moves.append('({0} & ({1} & {2}))'.format(state_loc_str(x - 1, y, t), state_heading_east_str(t), action_forward_str(t)))
if move == 'W':
moves.append('({0} & ({1} & {2}))'.format(state_loc_str(x + 1, y, t), state_heading_west_str(t), action_forward_str(t)))
if move == 'S':
moves.append('({0} & ({1} & {2}))'.format(state_loc_str(x,y+1,t),state_heading_south_str(t),action_forward_str(t)))
sucLoc = state_loc_str(x,y,t+1)
axiom_str = '{0} <=> ({1})'.format(sucLoc,' | '.join(moves))
return axiom_str
def generate_at_location_ssa(t, x, y, xmin, xmax, ymin, ymax, heading):
"""
The full at_location SSA converts to a fairly large CNF, which in
turn causes the KB to grow very fast, slowing overall inference.
We therefore need to restric generating these axioms as much as possible.
This fn generates the at_location SSA only for the current location and
the location the agent is currently facing (in case the agent moves
forward on the next turn).
This is sufficient for tracking the current location, which will be the
single L location that evaluates to True; however, the other locations
may be False or Unknown.
"""
axioms = [axiom_generator_at_location_ssa(t, x, y, xmin, xmax, ymin, ymax)]
if heading == 'west' and x - 1 >= xmin:
axioms.append(axiom_generator_at_location_ssa(t, x - 1, y, xmin, xmax, ymin, ymax))
if heading == 'east' and x + 1 <= xmax:
axioms.append(axiom_generator_at_location_ssa(t, x + 1, y, xmin, xmax, ymin, ymax))
if heading == 'south' and y - 1 >= ymin:
axioms.append(axiom_generator_at_location_ssa(t, x, y - 1, xmin, xmax, ymin, ymax))
if heading == 'north' and y + 1 <= ymax:
axioms.append(axiom_generator_at_location_ssa(t, x, y + 1, xmin, xmax, ymin, ymax))
if utils.all_empty_strings(axioms):
utils.print_not_implemented('axiom_generator_at_location_ssa')
return filter(lambda s: s != '', axioms)
# ----------------------------------
def axiom_generator_have_arrow_ssa(t):
"""
Assert the conditions at time t under which the Agent
has the arrow at time t+1
t := time
"""
axiom_str = '{0} <=> ({1} & ~{2})'.format(state_have_arrow_str(t+1),state_have_arrow_str(t),action_shoot_str(t))
return axiom_str
def axiom_generator_wumpus_alive_ssa(t):
"""
Assert the conditions at time t under which the Wumpus
is known to be alive at time t+1
(NOTE: If this axiom is implemented in the standard way, it is expected
that it will take one time step after the Wumpus dies before the Agent
can infer that the Wumpus is actually dead.)
t := time
"""
axiom_str = '{0} <=> ({1} & ~{2})'.format(state_wumpus_alive_str(t+1),state_wumpus_alive_str(t),percept_scream_str(t+1))
return axiom_str
# ----------------------------------
def axiom_generator_heading_north_ssa(t):
"""
Assert the conditions at time t under which the
Agent heading will be North at time t+1
t := time
"""
noMove = '({0} & ({1} | {2} | {3} | {4} | {5}))'.format(state_heading_north_str(t),action_wait_str(t),action_grab_str(t),
action_shoot_str(t),percept_bump_str(t+1),action_forward_str(t))
left = '({0} & {1})'.format(state_heading_east_str(t),action_turn_left_str(t))
right = '({0} & {1})'.format(state_heading_west_str(t),action_turn_right_str(t))
axiom_str = '{0} <=> ({1} | {2} | {3})'.format(state_heading_north_str(t+1),noMove,left,right)
return axiom_str
def axiom_generator_heading_east_ssa(t):
"""
Assert the conditions at time t under which the
Agent heading will be East at time t+1
t := time
"""
noMove = '({0} & ({1} | {2} | {3} | {4} | {5}))'.format(state_heading_east_str(t),action_wait_str(t),action_grab_str(t),
action_shoot_str(t),percept_bump_str(t+1),action_forward_str(t))
left = '({0} & {1})'.format(state_heading_south_str(t),action_turn_left_str(t))
right = '({0} & {1})'.format(state_heading_north_str(t),action_turn_right_str(t))
axiom_str = '{0} <=> ({1} | {2} | {3})'.format(state_heading_east_str(t+1),noMove,left,right)
return axiom_str
def axiom_generator_heading_south_ssa(t):
"""
Assert the conditions at time t under which the
Agent heading will be South at time t+1
t := time
"""
noMove = '({0} & ({1} | {2} | {3} | {4} | {5}))'.format(state_heading_south_str(t),action_wait_str(t),action_grab_str(t),
action_shoot_str(t),percept_bump_str(t+1),action_forward_str(t))
left = '({0} & {1})'.format(state_heading_west_str(t),action_turn_left_str(t))
right = '({0} & {1})'.format(state_heading_east_str(t),action_turn_right_str(t))
axiom_str = '{0} <=> ({1} | {2} | {3})'.format(state_heading_south_str(t+1),noMove,left,right)
return axiom_str
def axiom_generator_heading_west_ssa(t):
"""
Assert the conditions at time t under which the
Agent heading will be West at time t+1
t := time
"""
noMove = '({0} & ({1} | {2} | {3} | {4} | {5}))'.format(state_heading_west_str(t),action_wait_str(t),action_grab_str(t),
action_shoot_str(t),percept_bump_str(t+1),action_forward_str(t))
left = '({0} & {1})'.format(state_heading_north_str(t),action_turn_left_str(t))
right = '({0} & {1})'.format(state_heading_south_str(t),action_turn_right_str(t))
axiom_str = '{0} <=> ({1} | {2} | {3})'.format(state_heading_west_str(t+1),noMove,left,right)
return axiom_str
def generate_heading_ssa(t):
"""
Generates all of the heading SSAs.
"""
return [axiom_generator_heading_north_ssa(t),
axiom_generator_heading_east_ssa(t),
axiom_generator_heading_south_ssa(t),
axiom_generator_heading_west_ssa(t)]
def generate_non_location_ssa(t):
"""
Generate all non-location-based SSAs
"""
axioms = [] # all_state_loc_ssa(t, xmin, xmax, ymin, ymax)
axioms.append(axiom_generator_have_arrow_ssa(t))
axioms.append(axiom_generator_wumpus_alive_ssa(t))
axioms.extend(generate_heading_ssa(t))
return filter(lambda s: s != '', axioms)
# ----------------------------------
def axiom_generator_heading_only_north(t):
"""
Assert that when heading is North, the agent is
not heading any other direction.
t := time
"""
axiom_str = '{0} <=> (~{1} & ~{2} & ~{3})'.format(state_heading_north_str(t), state_heading_south_str(t),
state_heading_east_str(t), state_heading_west_str(t))
return axiom_str
def axiom_generator_heading_only_east(t):
"""
Assert that when heading is East, the agent is
not heading any other direction.
t := time
"""
axiom_str = '{0} <=> (~{1} & ~{2} & ~{3})'.format(state_heading_east_str(t),state_heading_south_str(t),
state_heading_north_str(t),state_heading_west_str(t))
return axiom_str
def axiom_generator_heading_only_south(t):
"""
Assert that when heading is South, the agent is
not heading any other direction.
t := time
"""
axiom_str = '{0} <=> (~{1} & ~{2} & ~{3})'.format(state_heading_south_str(t),state_heading_north_str(t),
state_heading_east_str(t),state_heading_west_str(t))
return axiom_str
def axiom_generator_heading_only_west(t):
"""
Assert that when heading is West, the agent is
not heading any other direction.
t := time
"""
axiom_str = '{0} <=> (~{1} & ~{2} & ~{3})'.format(state_heading_west_str(t),state_heading_south_str(t),
state_heading_east_str(t),state_heading_north_str(t))
return axiom_str
def generate_heading_only_one_direction_axioms(t):
return [axiom_generator_heading_only_north(t),
axiom_generator_heading_only_east(t),
axiom_generator_heading_only_south(t),
axiom_generator_heading_only_west(t)]
def axiom_generator_only_one_action_axioms(t):
"""
Assert that only one axion can be executed at a time.
t := time
"""
actions = ['Forward','Grab','Shoot','Climb','TurnLeft','TurnRight','Wait']
axioms = []
for index in range(len(actions)):
otherActions = [('~' + x + str(t))for i,x in enumerate(actions) if i!=index]
axioms.append('({0}{1} <=> ({2}))'.format(actions[index],str(t),' & '.join(otherActions)))
axiom_str = ' & '. join(axioms)
return axiom_str
def generate_mutually_exclusive_axioms(t):
"""
Generate all time-based mutually exclusive axioms.
"""
axioms = []
# must be t+1 to constrain which direction could be heading _next_
axioms.extend(generate_heading_only_one_direction_axioms(t + 1))
# actions occur in current time, after percept
axioms.append(axiom_generator_only_one_action_axioms(t))
return filter(lambda s: s != '', axioms)
# -------------------------------------------------------------------------------