Subversion Repositories programming

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
394 ira 1
#!/usr/bin/env python
2
 
400 ira 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
 
397 ira 7
from PyCompat import * # fixes for school computers
395 ira 8
 
394 ira 9
from Menu import Menu
10
from PuzzlePiece import PuzzlePiece
11
import PuzzleSearch
12
import sys
13
 
14
################################################################################
15
### Search Algorithms
16
################################################################################
17
 
18
DEFAULT_MAX_ITERATIONS=1000
19
 
20
def m_search_generic (name, start, goals, add_algorithm, MAX_ITERATIONS=DEFAULT_MAX_ITERATIONS):
21
	s = PuzzleSearch.PuzzleSearch (start, goals)
22
 
23
	# Run the search
24
	result = s.search (add_algorithm, MAX_ITERATIONS)
25
	result.set_search_name (name)
26
	print result
27
	print
28
 
29
	return result
30
 
31
def m_bfs (start_node, goal_node):
32
	return m_search_generic ('Breadth First Search', start_node,
33
			(goal_node, ), PuzzleSearch.add_bfs)
34
 
35
def m_dfs (start_node, goal_node):
36
	return m_search_generic ('Depth First Search', start_node,
37
			(goal_node, ), PuzzleSearch.add_dfs)
38
 
39
def m_bfs_h1 (start_node, goal_node):
40
	return m_search_generic ('Best First Search: Heuristic 1', start_node,
41
			(goal_node, ), PuzzleSearch.bfs_oop)
42
 
43
def m_bfs_h2 (start_node, goal_node):
44
	return m_search_generic ('Best First Search: Heuristic 2', start_node,
45
			(goal_node, ), PuzzleSearch.bfs_dfc)
46
 
47
def m_asearch_h1 (start_node, goal_node):
48
	return m_search_generic ('A*-Search: Heuristic 1', start_node,
49
			(goal_node, ), PuzzleSearch.astar_oop)
50
 
51
def m_asearch_h2 (start_node, goal_node):
52
	return m_search_generic ('A*-Search: Heuristic 2', start_node,
53
			(goal_node, ), PuzzleSearch.astar_dfc)
54
 
55
################################################################################
56
 
57
def m_render_graph (result):
397 ira 58
	if have_yapgvb():
394 ira 59
		import yapgvb
60
 
398 ira 61
	if result.result_graph == None:
62
		print 'The search failed, therefore I cannot render the graph!'
63
		return
64
 
395 ira 65
	from DrawGraph import DrawGraph
394 ira 66
 
398 ira 67
	# Always render stupid mode
68
	dg = DrawGraph (result.search_name, result.result_graph)
69
	dg.render_stupid ('generated_by')
70
	print
395 ira 71
 
394 ira 72
	# Try to render the graph, nicely
398 ira 73
	if have_yapgvb():
394 ira 74
		fname = raw_input('Enter filename (press ENTER for \'res.svg\'): ')
75
		print
76
 
77
		if fname == '':
78
			fname = 'res.svg'
79
 
80
		# Actually draw the graph
81
		dg = DrawGraph (result.search_name, result.result_graph)
395 ira 82
		dg.render_graphviz (fname, yapgvb.engines.dot)
394 ira 83
 
84
def gen_start_state (goal_node):
85
	DEPTH=12
86
	result = PuzzleSearch.get_nth_child (goal_node, DEPTH)
87
 
88
	# "Fix" the node given back to us
89
	result.depth = 0
90
	result.goal = goal_node
397 ira 91
	result.generated_by = 'root'
394 ira 92
	return result
93
 
94
def getstr (prompt):
95
	return raw_input (prompt + ': ')
96
 
97
def input_start_state ():
98
	state = []
99
	valid_inputs = ('1', '2', '3', '4', '5', '6', '7', '8', 'E')
100
 
101
	print 'Input start state, one row at a time, space seperated:'
102
	state += getstr ('Row 1').split()
103
	state += getstr ('Row 2').split()
104
	state += getstr ('Row 3').split()
105
 
106
	# Check and make sure its ok
107
	if len(state) != 9:
108
		print 'Too many states, try again!\n'
109
		return input_start_state ()
110
 
111
	for el in state:
112
		if el not in valid_inputs:
113
			print '"%s" is not valid, try again!' % el
114
			return input_start_state ()
115
 
116
	return state
117
 
118
 
119
def main ():
120
 
121
	# Very important start state information
122
	goal_config = ['1', '2', '3', '8', 'E', '4', '7', '6', '5']
123
	goal_node = PuzzlePiece (goal_config)
124
 
125
	m = Menu ()
126
	m.add_entry ('1', 'Enter start state', input_start_state)
127
	m.add_entry ('2', 'Generate Reachable Start State', gen_start_state)
398 ira 128
	m.add_entry ('q', 'Quit', sys.exit)
394 ira 129
	(entry, callback) = m.run_menu ()
130
 
131
	# Must be from #1
132
	if entry == '1':
397 ira 133
		start_node = PuzzlePiece (callback(), 0, goal_node, 'root')
394 ira 134
	elif entry == '2':
135
		start_node = callback(goal_node)
398 ira 136
	else:
137
		callback ()
394 ira 138
 
139
	# Get set up for the next menu
140
	search_result = None
141
 
142
	# Set up the final menu
143
	m = Menu ()
144
	m.add_entry ('1', 'Breadth First Search', m_bfs)
145
	m.add_entry ('2', 'Depth First Search', m_dfs)
146
	m.add_entry ('3', 'Best First Search: Heuristic 1', m_bfs_h1)
147
	m.add_entry ('4', 'Best First Search: Heuristic 2', m_bfs_h2)
148
	m.add_entry ('5', 'A*-Search: Heuristic 1', m_asearch_h1)
149
	m.add_entry ('6', 'A*-Search: Heuristic 2', m_asearch_h2)
150
	m.add_entry ('7', 'Change Start State', input_start_state)
151
	m.add_entry ('8', 'Generate Reachable Start State', gen_start_state)
152
	m.add_entry ('9', 'Render Graph of last result', m_render_graph)
153
	m.add_entry ('q', 'Quit', sys.exit)
154
	m.hide_entry ('9')
155
 
156
	while True:
157
		# Add support for graph rendering
158
		m.hide_entry ('9')
159
		if search_result:
160
			if search_result.completed:
161
				m.unhide_entry ('9')
162
 
163
		(entry, callback) = m.run_menu ()
164
 
165
		# Check if we need to change the start node
166
		if entry == '7':
167
			start_node = PuzzlePiece (callback(), 0, goal_node)
168
		elif entry == '8':
169
			search_result = False
170
			start_node = callback (goal_node)
171
		elif entry == '9':
172
			callback (search_result)
173
		# Check if we need to quit
174
		elif entry == 'q':
175
			callback ()
176
		else:
177
			# Must be one of the search functions, so call them!
178
			search_result = callback (start_node, goal_node)
179
 
180
if __name__ == '__main__':
181
	main ()
182