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 *
|
424 |
ira |
9 |
from Menu import Menu
|
423 |
ira |
10 |
import sys
|
424 |
ira |
11 |
import copy
|
423 |
ira |
12 |
|
|
|
13 |
(WALK, PUSH, CLIMB, GRAB) = ('Walk', 'Push', 'Climb', 'Grab')
|
|
|
14 |
|
|
|
15 |
def findNextMove (State):
|
|
|
16 |
"""Prioritized Search for the best next move"""
|
|
|
17 |
|
|
|
18 |
monkeyPosition = Action(State).findMonkeyPosition ()
|
|
|
19 |
boxPosition = Action(State).findBoxPosition ()
|
|
|
20 |
|
|
|
21 |
# Try to Grab
|
|
|
22 |
if Grab(State).meetsPreconditions():
|
|
|
23 |
return (GRAB,)
|
|
|
24 |
|
|
|
25 |
# Try to push box to middle
|
|
|
26 |
if Push(State, boxPosition, Push.MIDDLE).meetsPreconditions():
|
|
|
27 |
if boxPosition != Push.MIDDLE:
|
|
|
28 |
return (PUSH, boxPosition, Push.MIDDLE)
|
|
|
29 |
|
|
|
30 |
# Try to walk to box
|
|
|
31 |
if Walk(State, monkeyPosition, boxPosition).meetsPreconditions():
|
|
|
32 |
if monkeyPosition != boxPosition:
|
|
|
33 |
return (WALK, monkeyPosition, boxPosition)
|
|
|
34 |
|
|
|
35 |
# Try to climb the box
|
|
|
36 |
if Climb(State, Climb.UP).meetsPreconditions():
|
|
|
37 |
if not Action(State).monkeyOnBox ():
|
|
|
38 |
return (CLIMB, Climb.UP)
|
|
|
39 |
|
|
|
40 |
# Try to climb down from the box
|
|
|
41 |
if Climb(State, Climb.DOWN).meetsPreconditions():
|
|
|
42 |
if Action(State).monkeyOnBox ():
|
|
|
43 |
return (CLIMB, Climb.DOWN)
|
|
|
44 |
|
|
|
45 |
def isValidInitialState (State):
|
|
|
46 |
monkeyPos = Action(State).findMonkeyPosition()
|
|
|
47 |
boxPos = Action(State).findBoxPosition()
|
|
|
48 |
onBox = Action(State).monkeyOnBox()
|
|
|
49 |
|
|
|
50 |
if onBox and (monkeyPos != boxPos):
|
|
|
51 |
return False
|
|
|
52 |
|
|
|
53 |
return True
|
|
|
54 |
|
424 |
ira |
55 |
def enter_initial_state ():
|
|
|
56 |
State = set([])
|
423 |
ira |
57 |
|
424 |
ira |
58 |
# Get Monkey Position
|
|
|
59 |
m = Menu(autorun=True, name='Enter Monkey Position')
|
|
|
60 |
m.add_entry ('1', 'Window', lambda: Walk.WINDOW)
|
|
|
61 |
m.add_entry ('2', 'Door', lambda: Walk.DOOR)
|
|
|
62 |
m.add_entry ('3', 'Middle', lambda: Walk.MIDDLE)
|
|
|
63 |
m.add_entry ('Q', 'Quit', sys.exit)
|
|
|
64 |
|
|
|
65 |
pos = m.run_menu()[1]
|
|
|
66 |
State.add ('At(Monkey,%s)' % pos)
|
|
|
67 |
|
|
|
68 |
# Get Box Position
|
|
|
69 |
m = Menu(autorun=True, name='Enter Box Position')
|
|
|
70 |
m.add_entry ('1', 'Window', lambda: Push.WINDOW)
|
|
|
71 |
m.add_entry ('2', 'Door', lambda: Push.DOOR)
|
|
|
72 |
m.add_entry ('3', 'Middle', lambda: Push.MIDDLE)
|
|
|
73 |
m.add_entry ('Q', 'Quit', sys.exit)
|
|
|
74 |
|
|
|
75 |
pos = m.run_menu()[1]
|
|
|
76 |
State.add ('At(Box,%s)' % pos)
|
|
|
77 |
|
|
|
78 |
# Get Monkey Status
|
|
|
79 |
m = Menu(autorun=True, name='Enter Monkey Height Status')
|
|
|
80 |
m.add_entry ('1', 'Monkey on Box', lambda: 'OnBox()')
|
|
|
81 |
m.add_entry ('2', 'Monkey on Floor', lambda: 'OnFloor()')
|
|
|
82 |
m.add_entry ('Q', 'Quit', sys.exit)
|
|
|
83 |
|
|
|
84 |
State.add (m.run_menu()[1])
|
|
|
85 |
|
|
|
86 |
# Get Banana Status
|
|
|
87 |
m = Menu(autorun=True, name='Enter Banana Status')
|
|
|
88 |
m.add_entry ('1', 'Monkey has Banana', lambda: 'HasBanana()')
|
|
|
89 |
m.add_entry ('2', 'Monkey does not have Banana', lambda: 'NoBanana()')
|
|
|
90 |
m.add_entry ('Q', 'Quit', sys.exit)
|
|
|
91 |
|
|
|
92 |
State.add (m.run_menu()[1])
|
|
|
93 |
|
|
|
94 |
return State
|
|
|
95 |
|
|
|
96 |
def find_plan (State):
|
423 |
ira |
97 |
if not isValidInitialState (State):
|
424 |
ira |
98 |
print 'Bad initial state\n'
|
|
|
99 |
return
|
423 |
ira |
100 |
|
424 |
ira |
101 |
if Action(State).isGoal():
|
|
|
102 |
print 'Initial State is already a goal state\n'
|
|
|
103 |
return
|
|
|
104 |
|
|
|
105 |
print 'Plan:'
|
423 |
ira |
106 |
while not Action(State).isGoal():
|
|
|
107 |
|
|
|
108 |
next = findNextMove (State)
|
|
|
109 |
action = next[0]
|
|
|
110 |
|
|
|
111 |
if action == WALK:
|
|
|
112 |
print '%s from %s to %s' % next
|
|
|
113 |
State = Walk(State, next[1], next[2]).takeAction()
|
|
|
114 |
elif action == PUSH:
|
424 |
ira |
115 |
print '%s Box from %s to %s' % next
|
423 |
ira |
116 |
State = Push(State, next[1], next[2]).takeAction()
|
|
|
117 |
elif action == CLIMB:
|
|
|
118 |
print '%s %s' % next
|
|
|
119 |
State = Climb(State, next[1]).takeAction()
|
|
|
120 |
elif action == GRAB:
|
|
|
121 |
print '%s Banana' % next
|
|
|
122 |
State = Grab(State).takeAction()
|
|
|
123 |
else:
|
|
|
124 |
print 'BAD STATE'
|
424 |
ira |
125 |
return
|
423 |
ira |
126 |
|
424 |
ira |
127 |
print
|
423 |
ira |
128 |
|
424 |
ira |
129 |
|
|
|
130 |
def main ():
|
|
|
131 |
|
|
|
132 |
# Create the main menu
|
|
|
133 |
m = Menu (name='Main Menu')
|
|
|
134 |
m.add_entry ('1', 'Enter Initial State', enter_initial_state)
|
|
|
135 |
m.add_entry ('2', 'Find Plan', find_plan)
|
|
|
136 |
m.add_entry ('Q', 'Quit', sys.exit)
|
|
|
137 |
m.hide_entry ('2')
|
|
|
138 |
|
|
|
139 |
# Run the menu
|
|
|
140 |
while True:
|
|
|
141 |
(num, func) = m.run_menu ()
|
|
|
142 |
|
|
|
143 |
if num == '1':
|
|
|
144 |
State = func()
|
|
|
145 |
m.unhide_entry ('2')
|
|
|
146 |
elif num == '2':
|
|
|
147 |
func(copy.deepcopy(State))
|
|
|
148 |
else:
|
|
|
149 |
func()
|
|
|
150 |
|
423 |
ira |
151 |
if __name__ == '__main__':
|
|
|
152 |
main ()
|
|
|
153 |
|
|
|
154 |
# vim: set ts=4 sts=4 sw=4:
|
|
|
155 |
|