Пример #1
0
def a_star( start ):
	


	frontier = Queue.PriorityQueue() # Create a Queue to perform BFS on the world
	frontier.put( (0, start) )


	previous = {}
	cost = {}

	previous[tuple(start)] = (None,'')
	cost[tuple(start)] = 0

	neighbors = {'L':start, 'S':start, 'R':start, 'U':start, 'D':start}
	while frontier.empty() == False:
		
		current = frontier.get() # Remove next state from the Queue
		current_state = current[1]


		if problem23_1.is_goal(current_state):
			break


		neighbors['L'] = go_left(current_state)
		neighbors['R'] = go_right(current_state)
		neighbors['D'] = go_down(current_state)
		neighbors['U'] = go_up(current_state)
		neighbors['S'] = suck(current_state)
		insertion_order = ('L', 'U', 'R', 'D', 'S')

		for action in insertion_order:
			new_cost = cost[current_state] + 1
			if neighbors[action] not in cost or new_cost < cost[neighbors[action]]:
				cost[neighbors[action]] = new_cost
				priority = new_cost + heuristic( neighbors[action] )
				frontier.put( (priority, neighbors[action]) )
				previous[neighbors[action]] = (current_state, action) 




	""" 
	Once a graph of states has been traversed by the BFS algorithm
	We can then print out the pathway from teh start state to
	the goal by tracing a path along the dictionary named previous
	"""
	path = []
	while current_state != start:
		path.append(previous[current_state][1])
		current_state = previous[current_state][0]
		
	path.reverse()
	return ''.join(tuple(path))
Пример #2
0
def breadth_first( start ):
	
	#vacuum_location = start[-1]
	#start_state = start[:-1]

	#print start
	#print type(start) is list

	"""
	We first create a frontier queue for the expansion of the search
	and will start by enqueing the start state, which is noted by the 
	tuple start = ( int room1, int room2, vacuum_location )
	"""
	frontier = Queue.Queue() # Create a Queue to perform BFS on the world
	frontier.put(start)

	""" 
	We then create a dictionary noted previous so that we may trace our 
	way back to the start from any of the expanded nodes.
	The first node placed in will be noted by the tuple start and will have
	None as its value of previous. All other subsequent entries will be 
	in the format.... previous[state_tuple] = (previous_tuple, action taken)
	"""
	previous = {}
	previous[tuple(start)] = (None,'')


	""" 
	We then begin the bulk of the BFS algorithm and will create a 
	tuple for every state that is adjacent to the currently visited
	node/state. These adjacent nodes will be called chlidren[L/R/S]
	in the code and will create a unique tuple to be enqueued
	"""
	adjacency_list = {'L':start, 'S':start, 'R':start, 'U':start, 'D':start}
	while( frontier.empty() == False ):
		
		""" 
		each element of the queue will be a tuple containing the 
		rows as lists that will make up the vacuum world grid
		"""
		current = frontier.get() # Remove next state from the Queue

		if problem23_1.is_goal(current):
			break


		adjacency_list['L'] = go_left(current)
		adjacency_list['R'] = go_right(current)
		adjacency_list['D'] = go_down(current)
		adjacency_list['U'] = go_up(current)
		adjacency_list['S'] = suck(current)
		insertion_order = ('L', 'U', 'R', 'D', 'S')

		for action in insertion_order:
			if adjacency_list[action] not in previous:
				frontier.put(adjacency_list[action])
				previous[adjacency_list[action]] = (current, action) 




	""" 
	Once a graph of states has been traversed by the BFS algorithm
	We can then print out the pathway from teh start state to
	the goal by tracing a path along the dictionary named previous
	"""
	path = []
	while current != start:
		path.append(previous[current][1])
		current = previous[current][0]
		
	path.reverse()
	return ''.join(tuple(path))
Пример #3
0
def depth_limited( start, max_depth ):
	

	"""
	We first create a frontier queue for the expansion of the search
	and will start by enqueing the start state, which is noted by the 
	tuple start = ( int room1, int room2, vacuum_location )
	"""
	frontier = [] # Create a Queue to perform BFS on the world
	frontier.append( (start, 0) )

	""" 
	We then create a dictionary noted previous so that we may trace our 
	way back to the start from any of the expanded nodes.
	The first node placed in will be noted by the tuple start and will have
	None as its value of previous. All other subsequent entries will be 
	in the format.... previous[state_tuple] = (previous_tuple, action taken)
	"""
	previous = {}
	previous[tuple(start)] = (None,'')

#	visited = {}
#	visited[tuple(start)] = True

	""" 
	We then begin the bulk of the BFS algorithm and will create a 
	tuple for every state that is adjacent to the currently visited
	node/state. These adjacent nodes will be called chlidren[L/R/S]
	in the code and will create a unique tuple to be enqueued
	"""
	adjacency_list = {'L':start, 'S':start, 'R':start, 'U':start, 'D':start}
	while( len(frontier) > 0 ):
		
		""" 
		each element of the queue will be a tuple containing the 
		rows as lists that will make up the vacuum world grid
		"""
		current = frontier.pop() # Remove next state from the Queue
		#visited[tuple(current)] = True

		"""
		if we reached the goal, we terminate the traversal early
		"""
		if problem23_1.is_goal(current[0]):
			break


		adjacency_list['L'] = go_left(current[0])
		adjacency_list['R'] = go_right(current[0])
		adjacency_list['D'] = go_down(current[0])
		adjacency_list['U'] = go_up(current[0])
		adjacency_list['S'] = suck(current[0])
		insertion_order = ('S', 'D', 'R', 'U', 'L')

		for action in insertion_order:
			if adjacency_list[action] not in previous and current[1] < max_depth:
				frontier.append( (adjacency_list[action], current[1] + 1) )
				previous[adjacency_list[action]] = (current[0], action) 
				



	""" 
	Once a graph of states has been traversed by the BFS algorithm
	We can then print out the pathway from teh start state to
	the goal by tracing a path along the dictionary named previous
	"""

	#print current
	output= []
	pathfinder = current[0] # Assigne goal state to pathfinder
	while pathfinder != start: # Trace path until the start
		output.append(previous[pathfinder][1]) # trace previous
		pathfinder = previous[pathfinder][0]
		
	output.reverse()
	if problem23_1.is_goal( current[0] ):
		return ''.join(tuple(output))
	else:
		return None