Subversion Repositories programming

Rev

Rev 423 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 423 Rev 424
Line 4... Line 4...
4
__copyright__ = "Copyright (c) 2006, 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)"
5
__license__   = "GNU GPL v2 (or, at your option, any later version)"
6
 
6
 
7
from PyCompat import *
7
from PyCompat import *
8
from Actions import *
8
from Actions import *
-
 
9
from Menu import Menu
9
import sys
10
import sys
-
 
11
import copy
10
 
12
 
11
(WALK, PUSH, CLIMB, GRAB) = ('Walk', 'Push', 'Climb', 'Grab')
13
(WALK, PUSH, CLIMB, GRAB) = ('Walk', 'Push', 'Climb', 'Grab')
12
 
14
 
13
def findNextMove (State):
15
def findNextMove (State):
14
	"""Prioritized Search for the best next move"""
16
	"""Prioritized Search for the best next move"""
Line 48... Line 50...
48
	if onBox and (monkeyPos != boxPos):
50
	if onBox and (monkeyPos != boxPos):
49
		return False
51
		return False
50
 
52
 
51
	return True
53
	return True
52
 
54
 
53
def main ():
55
def enter_initial_state ():
-
 
56
	State = set([])
-
 
57
 
-
 
58
	# Get Monkey Position
54
	#State = set(['At(Monkey,Door)', 'At(Box,Window)', 'OnFloor()', 'NoBanana()'])
59
	m = Menu(autorun=True, name='Enter Monkey Position')
55
	State = set(['At(Monkey,Middle)', 'At(Box,Window)', 'OnFloor()', 'NoBanana()'])
60
	m.add_entry ('1', 'Window', lambda: Walk.WINDOW)
-
 
61
	m.add_entry ('2', 'Door', lambda: Walk.DOOR)
56
	#State = set(['At(Monkey,Window)', 'At(Box,Window)', 'OnFloor()', 'NoBanana()'])
62
	m.add_entry ('3', 'Middle', lambda: Walk.MIDDLE)
-
 
63
	m.add_entry ('Q', 'Quit', sys.exit)
-
 
64
 
-
 
65
	pos = m.run_menu()[1]
57
	#State = set(['At(Monkey,Window)', 'At(Box,Window)', 'OnBox()', 'NoBanana()'])
66
	State.add ('At(Monkey,%s)' % pos)
-
 
67
 
-
 
68
	# Get Box Position
58
	#State = set(['At(Monkey,Window)', 'At(Box,Window)', 'OnBox()', 'HasBanana()'])
69
	m = Menu(autorun=True, name='Enter Box Position')
59
	#State = set(['At(Monkey,Door)', 'At(Box,Window)', 'OnBox()', 'HasBanana()'])
70
	m.add_entry ('1', 'Window', lambda: Push.WINDOW)
-
 
71
	m.add_entry ('2', 'Door', lambda: Push.DOOR)
60
	#State = set(['At(Monkey,Window)', 'At(Box,Middle)', 'OnFloor()', 'NoBanana()'])
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')
61
	#State = set(['At(Monkey,Door)', 'At(Box,Middle)', 'OnFloor()', 'NoBanana()'])
80
	m.add_entry ('1', 'Monkey on Box', lambda: 'OnBox()')
62
	#State = set(['At(Monkey,Middle)', 'At(Box,Middle)', 'OnFloor()', 'NoBanana()'])
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')
63
	#State = set(['At(Monkey,Middle)', 'At(Box,Middle)', 'OnBox()', 'NoBanana()'])
88
	m.add_entry ('1', 'Monkey has Banana', lambda: 'HasBanana()')
64
	#State = set(['At(Monkey,Middle)', 'At(Box,Door)', 'OnFloor()', 'NoBanana()'])
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])
65
 
93
 
-
 
94
	return State
-
 
95
 
-
 
96
def find_plan (State):
66
	if not isValidInitialState (State):
97
	if not isValidInitialState (State):
67
		print 'Bad initial state'
98
		print 'Bad initial state\n'
68
		sys.exit (1)
99
		return
-
 
100
 
-
 
101
	if Action(State).isGoal():
-
 
102
		print 'Initial State is already a goal state\n'
-
 
103
		return
69
 
104
 
-
 
105
	print 'Plan:'
70
	while not Action(State).isGoal():
106
	while not Action(State).isGoal():
71
 
107
 
72
		next = findNextMove (State)
108
		next = findNextMove (State)
73
		action = next[0]
109
		action = next[0]
74
 
110
 
75
		if action == WALK:
111
		if action == WALK:
76
			print '%s from %s to %s' % next
112
			print '%s from %s to %s' % next
77
			State = Walk(State, next[1], next[2]).takeAction()
113
			State = Walk(State, next[1], next[2]).takeAction()
78
		elif action == PUSH:
114
		elif action == PUSH:
79
			print '%s from %s to %s' % next
115
			print '%s Box from %s to %s' % next
80
			State = Push(State, next[1], next[2]).takeAction()
116
			State = Push(State, next[1], next[2]).takeAction()
81
		elif action == CLIMB:
117
		elif action == CLIMB:
82
			print '%s %s' % next
118
			print '%s %s' % next
83
			State = Climb(State, next[1]).takeAction()
119
			State = Climb(State, next[1]).takeAction()
84
		elif action == GRAB:
120
		elif action == GRAB:
85
			print '%s Banana' % next
121
			print '%s Banana' % next
86
			State = Grab(State).takeAction()
122
			State = Grab(State).takeAction()
87
		else:
123
		else:
88
			print 'BAD STATE'
124
			print 'BAD STATE'
89
			sys.exit (2)
125
			return
90
 
126
 
-
 
127
	print
-
 
128
 
-
 
129
 
-
 
130
def main ():
-
 
131
 
91
	print 'Reached the Goal!'
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()
92
 
150
 
93
if __name__ == '__main__':
151
if __name__ == '__main__':
94
	main ()
152
	main ()
95
 
153
 
96
# vim: set ts=4 sts=4 sw=4:
154
# vim: set ts=4 sts=4 sw=4: