Subversion Repositories programming

Rev

Rev 387 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 387 Rev 388
Line 45... Line 45...
45
				return n
45
				return n
46
 
46
 
47
		# This should never happen
47
		# This should never happen
48
		raise ValueError
48
		raise ValueError
49
 
49
 
50
	def search (self):
50
	def search (self, add_function, MAX_ITERATIONS=100):
51
 
51
 
52
		# Create the result graph
52
		# Create the result graph
53
		result = Graph ()
53
		result = Graph ()
54
		firsttime = True
54
		firsttime = True
55
		counter = 0
55
		counter = 0
Line 81... Line 81...
81
 
81
 
82
			# Check if we've reached the goal
82
			# Check if we've reached the goal
83
			if N in self.__goal_nodes:
83
			if N in self.__goal_nodes:
84
				return result
84
				return result
85
 
85
 
86
			# FIXME
86
			# Add the children of N (aka M) to OPEN
87
			OPEN = add_bfs (M, OPEN, CLOSED)
87
			OPEN = add_function (M, OPEN, CLOSED)
88
 
88
 
89
			counter += 1
89
			counter += 1
90
 
90
 
-
 
91
			# Check to make sure we don't loop for too long
-
 
92
			if counter > MAX_ITERATIONS:
-
 
93
				return 'FAILURE'
-
 
94
 
91
		return 'FAILURE'
95
		return 'FAILURE'
92
 
96
 
93
def add_bfs (M, OPEN, CLOSED):
97
def add_bfs (M, OPEN, CLOSED):
94
	for node in M:
98
	for node in M:
95
		if (node not in OPEN) and (node not in CLOSED):
99
		if (node not in OPEN) and (node not in CLOSED):
Line 118... Line 122...
118
 
122
 
119
def main ():
123
def main ():
120
	initial = [1, 2, 3, 4, 'E', 5, 6, 7, 8]
124
	initial = [1, 2, 3, 4, 'E', 5, 6, 7, 8]
121
 
125
 
122
	start = PuzzlePiece (initial)
126
	start = PuzzlePiece (initial)
123
	goal = get_nth_child (start, 2)
127
	goal = get_nth_child (start, 10)
124
 
128
 
125
	s = PuzzleSearch (start, (goal, ))
129
	s = PuzzleSearch (start, (goal, ))
126
	result = s.search ()
130
	result = s.search (add_bfs, 1000)
127
 
131
 
128
	if result != 'FAILURE':
132
	if result != 'FAILURE':
129
		DrawGraph ('result', result).render_graph ('res.svg', yapgvb.engines.dot)
133
		DrawGraph ('result', result).render_graph ('res.svg', yapgvb.engines.dot)
-
 
134
	else:
-
 
135
		print result
130
 
136
 
131
 
137
 
132
if __name__ == '__main__':
138
if __name__ == '__main__':
133
	main ()
139
	main ()