예제 #1
0
def depth_limited(start, max_depth):

    """
	We first create a frontier stack for the expansion of the search
	and will start by pushing the start state, which is noted by the 
	tuple start = ( int room1, int room2, int vacuum_location )
	"""
    frontier = []
    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[start] = (None, "")

    """ 
	We then begin the bulk of the DFS 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 pushed onto stack
	"""
    adjacency_list = {"L": start, "S": start, "R": start}
    while len(frontier) > 0:
        current = frontier.pop()  # Remove next state from the stack

        if problem21_1.is_goal(current[0]):
            break

        adjacency_list["L"] = go_left(current[0])
        adjacency_list["R"] = go_right(current[0])
        adjacency_list["S"] = suck(current[0])
        insertion_order = ("S", "R", "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 visited
	"""
    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 problem21_1.is_goal(current[0]):
        return "".join(tuple(output))
    else:
        return None
예제 #2
0
def breadth_first( start ):
	

	"""
	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, int 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[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}
	while( frontier.empty() == False ):
		current = frontier.get() # Remove next state from the Queue

		if problem21_1.is_goal(current):
			break

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

		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 a_star( start ):
	frontier = Queue.PriorityQueue()
	frontier.put( (0, start) )

	previous = {}
	cost = {}


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


	neighbors = {'L': start, 'S': start, 'R': start}
	while not frontier.empty():
		current = frontier.get()
		current_state = current[1]
		if problem21_1.is_goal( current_state ):
			break

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

		for action in insertion_order:
			new_cost = cost[current_state] + 1 #Add uniform cost of 1 to arc
			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 A* algorithm
	We can then print out the pathway from the start state to
	the goal by tracing a path along the dictionary named previous
	"""
	output= []
	while current_state != start: # Trace path until the start
		output.append(previous[current_state][1]) # trace previous
		current_state = previous[current_state][0]

	output.reverse()
	return ''.join(tuple(output))