423 |
ira |
1 |
#!/usr/bin/env python
|
|
|
2 |
|
|
|
3 |
__author__ = "Ira W. Snyder (devel@irasnyder.com)"
|
|
|
4 |
__copyright__ = "Copyright (c) 2006, Ira W. Snyder (devel@irasnyder.com)"
|
|
|
5 |
__license__ = "GNU GPL v2 (or, at your option, any later version)"
|
|
|
6 |
|
|
|
7 |
from PyCompat import *
|
|
|
8 |
from Actions import *
|
|
|
9 |
import sys
|
|
|
10 |
|
|
|
11 |
(WALK, PUSH, CLIMB, GRAB) = ('Walk', 'Push', 'Climb', 'Grab')
|
|
|
12 |
|
|
|
13 |
def findNextMove (State):
|
|
|
14 |
"""Prioritized Search for the best next move"""
|
|
|
15 |
|
|
|
16 |
monkeyPosition = Action(State).findMonkeyPosition ()
|
|
|
17 |
boxPosition = Action(State).findBoxPosition ()
|
|
|
18 |
|
|
|
19 |
# Try to Grab
|
|
|
20 |
if Grab(State).meetsPreconditions():
|
|
|
21 |
return (GRAB,)
|
|
|
22 |
|
|
|
23 |
# Try to push box to middle
|
|
|
24 |
if Push(State, boxPosition, Push.MIDDLE).meetsPreconditions():
|
|
|
25 |
if boxPosition != Push.MIDDLE:
|
|
|
26 |
return (PUSH, boxPosition, Push.MIDDLE)
|
|
|
27 |
|
|
|
28 |
# Try to walk to box
|
|
|
29 |
if Walk(State, monkeyPosition, boxPosition).meetsPreconditions():
|
|
|
30 |
if monkeyPosition != boxPosition:
|
|
|
31 |
return (WALK, monkeyPosition, boxPosition)
|
|
|
32 |
|
|
|
33 |
# Try to climb the box
|
|
|
34 |
if Climb(State, Climb.UP).meetsPreconditions():
|
|
|
35 |
if not Action(State).monkeyOnBox ():
|
|
|
36 |
return (CLIMB, Climb.UP)
|
|
|
37 |
|
|
|
38 |
# Try to climb down from the box
|
|
|
39 |
if Climb(State, Climb.DOWN).meetsPreconditions():
|
|
|
40 |
if Action(State).monkeyOnBox ():
|
|
|
41 |
return (CLIMB, Climb.DOWN)
|
|
|
42 |
|
|
|
43 |
def isValidInitialState (State):
|
|
|
44 |
monkeyPos = Action(State).findMonkeyPosition()
|
|
|
45 |
boxPos = Action(State).findBoxPosition()
|
|
|
46 |
onBox = Action(State).monkeyOnBox()
|
|
|
47 |
|
|
|
48 |
if onBox and (monkeyPos != boxPos):
|
|
|
49 |
return False
|
|
|
50 |
|
|
|
51 |
return True
|
|
|
52 |
|
|
|
53 |
def main ():
|
|
|
54 |
#State = set(['At(Monkey,Door)', 'At(Box,Window)', 'OnFloor()', 'NoBanana()'])
|
|
|
55 |
State = set(['At(Monkey,Middle)', 'At(Box,Window)', 'OnFloor()', 'NoBanana()'])
|
|
|
56 |
#State = set(['At(Monkey,Window)', 'At(Box,Window)', 'OnFloor()', 'NoBanana()'])
|
|
|
57 |
#State = set(['At(Monkey,Window)', 'At(Box,Window)', 'OnBox()', 'NoBanana()'])
|
|
|
58 |
#State = set(['At(Monkey,Window)', 'At(Box,Window)', 'OnBox()', 'HasBanana()'])
|
|
|
59 |
#State = set(['At(Monkey,Door)', 'At(Box,Window)', 'OnBox()', 'HasBanana()'])
|
|
|
60 |
#State = set(['At(Monkey,Window)', 'At(Box,Middle)', 'OnFloor()', 'NoBanana()'])
|
|
|
61 |
#State = set(['At(Monkey,Door)', 'At(Box,Middle)', 'OnFloor()', 'NoBanana()'])
|
|
|
62 |
#State = set(['At(Monkey,Middle)', 'At(Box,Middle)', 'OnFloor()', 'NoBanana()'])
|
|
|
63 |
#State = set(['At(Monkey,Middle)', 'At(Box,Middle)', 'OnBox()', 'NoBanana()'])
|
|
|
64 |
#State = set(['At(Monkey,Middle)', 'At(Box,Door)', 'OnFloor()', 'NoBanana()'])
|
|
|
65 |
|
|
|
66 |
if not isValidInitialState (State):
|
|
|
67 |
print 'Bad initial state'
|
|
|
68 |
sys.exit (1)
|
|
|
69 |
|
|
|
70 |
while not Action(State).isGoal():
|
|
|
71 |
|
|
|
72 |
next = findNextMove (State)
|
|
|
73 |
action = next[0]
|
|
|
74 |
|
|
|
75 |
if action == WALK:
|
|
|
76 |
print '%s from %s to %s' % next
|
|
|
77 |
State = Walk(State, next[1], next[2]).takeAction()
|
|
|
78 |
elif action == PUSH:
|
|
|
79 |
print '%s from %s to %s' % next
|
|
|
80 |
State = Push(State, next[1], next[2]).takeAction()
|
|
|
81 |
elif action == CLIMB:
|
|
|
82 |
print '%s %s' % next
|
|
|
83 |
State = Climb(State, next[1]).takeAction()
|
|
|
84 |
elif action == GRAB:
|
|
|
85 |
print '%s Banana' % next
|
|
|
86 |
State = Grab(State).takeAction()
|
|
|
87 |
else:
|
|
|
88 |
print 'BAD STATE'
|
|
|
89 |
sys.exit (2)
|
|
|
90 |
|
|
|
91 |
print 'Reached the Goal!'
|
|
|
92 |
|
|
|
93 |
if __name__ == '__main__':
|
|
|
94 |
main ()
|
|
|
95 |
|
|
|
96 |
# vim: set ts=4 sts=4 sw=4:
|
|
|
97 |
|