예제 #1
0
def single_gbfs(inputFile):
    full_state = FullState(inputFile)
    maze = full_state.get_maze()
    dynamic_state_1 = DynamicState(full_state.get_mouse_loc(),
                                   full_state.get_cheese_loc())
    prize_loc = dynamic_state_1.get_cheese_loc()[0]  #single prize

    root_node = Node(dynamic_state_1)
    gbf_tree = InformedSearchTree()
    gbf_tree.add_to_frontier(root_node)

    while gbf_tree.get_frontier() != []:
        node_to_exp = gbf_tree.pop_frontier_min()  #node to expand

        if node_to_exp.get_state().goal_test():  #goal achieved
            goal_node = node_to_exp
            break

        if node_to_exp.get_state() in gbf_tree.get_expanded_states():
            continue

        gbf_tree.add_to_expanded_states(node_to_exp.get_state())
        greedy_expand(node_to_exp, gbf_tree, maze, prize_loc)

    try:
        print_sin_solution(maze, goal_node)
        print("The path cost is: ", goal_node.get_path_cost())
    except NameError:  #goal_node is not defined
        print('Goal not reached')

    print("The number of nodes expanded is: ",
          len(gbf_tree.get_expanded_states()))
예제 #2
0
def single_astar(inputFile):
    full_state = FullState(inputFile)
    maze = full_state.get_maze()
    dynamic_state_1 = DynamicState(full_state.get_mouse_loc(),
                                   full_state.get_cheese_loc())
    prize_loc = dynamic_state_1.get_cheese_loc()[0]  #single prize

    root_node = Node(dynamic_state_1)
    astar_tree = InformedSearchTree()
    astar_tree.add_to_frontier(root_node)

    while astar_tree.get_frontier() != []:
        node_to_exp = astar_tree.pop_frontier_min()  #node to expand

        if node_to_exp.get_state() in astar_tree.get_expanded_states():
            continue

        # because there are duplicates in the frontier,
        # we need to check if node_to_exp is already in expanded_states to avoid expanding the same node twice:
        if node_to_exp.get_state().goal_test():
            goal_node = node_to_exp
            break

        astar_tree.add_to_expanded_states(node_to_exp.get_state())
        astar_expand(node_to_exp, astar_tree, maze, prize_loc)

    try:
        print_sin_solution(maze, goal_node)
        print("The path cost is: ", goal_node.get_path_cost())
    except NameError:  #goal_node is not defined
        print('Goal not reached')

    print("The number of nodes expanded is: ",
          len(astar_tree.get_expanded_states()))
예제 #3
0
def single_bfs(inputFile):
	full_state = FullState(inputFile)
	maze = full_state.get_maze()
	dynamic_state_1 = DynamicState(full_state.get_mouse_loc(), full_state.get_cheese_loc())
	root_node = Node(dynamic_state_1)

	bf_tree = BfSearchTree()
	bf_tree.add_to_frontier(root_node)

	while bf_tree.get_frontier() != deque(): 
	 	node_to_exp = bf_tree.deque_frontier() #node to expand
        
	 	if node_to_exp.get_state().goal_test(): #goal achieved
	 		goal_node = node_to_exp
	 		break

	 	# because there are duplicates in the frontier, 
	 	# we need to check if node_to_exp is already in expanded_states to avoid expanding the same node twice:
	 	if node_to_exp.get_state() in bf_tree.get_expanded_states():
	 		continue 

	 	bf_tree.add_to_expanded_states(node_to_exp.get_state())
	 	bf_expand(node_to_exp, bf_tree, maze) 
 	
	try:
		print_sin_solution(maze, goal_node)
		print("The path cost is: ", goal_node.get_path_cost())
	except NameError: #goal_node is not defined
		print('Goal not reached')

	print("The number of nodes expanded is: ", len(bf_tree.get_expanded_states()))
예제 #4
0
def bf_expand(parent_node, search_tree, maze):
	for action in ['N', 'E', 'S', 'W']:
		child_state = transition_model(maze, parent_node.get_state(), action)
		child_node = Node(child_state, parent_node, action, parent_node.get_path_cost() + 1) 

		if child_state not in search_tree.get_expanded_states():
			search_tree.add_to_frontier(child_node)  
예제 #5
0
def single_dfs(inputFile):
	full_state = FullState(inputFile)
	maze = full_state.get_maze()
	dynamic_state_1 = DynamicState(full_state.get_mouse_loc(), full_state.get_cheese_loc())
	root_node = Node(dynamic_state_1)

	df_tree = DfSearchTree()
	df_tree.add_to_frontier(root_node)

	while df_tree.get_frontier() != []:
	 	node_to_exp = df_tree.pop_frontier() #node to expand
        
	 	if node_to_exp.get_state().goal_test(): #goal achieved
	 		goal_node = node_to_exp
	 		break 

	 	df_tree.add_to_expanded_states(node_to_exp.get_state())
	 	df_expand(node_to_exp, df_tree, maze)
 	
	try:
		print_sin_solution(maze, goal_node)
		print("The path cost is: ", goal_node.get_path_cost())
	except NameError: #goal_node is not defined
		print('Goal not reached')

	print("The number of nodes expanded is: ", len(df_tree.get_expanded_states()))
예제 #6
0
def greedy_expand(parent_node, search_tree, maze, prize_loc):
    for action in ['N', 'E', 'S', 'W']:
        child_state = transition_model(maze, parent_node.get_state(), action)

        if child_state in search_tree.get_expanded_states(
        ):  #if it's already expanded
            continue

        child_priority = single_heuristic(child_state, prize_loc)
        child_node = Node(child_state, parent_node, action,
                          parent_node.get_path_cost() + 1, child_priority)

        search_tree.add_to_frontier(child_node)
예제 #7
0
def multi_astar_expand(parent_node, search_tree, maze):
    for action in ['N', 'E', 'S', 'W']:
        child_state = transition_model(maze, parent_node.get_state(), action)

        if child_state in search_tree.get_expanded_states():
            continue

        child_path_cost = parent_node.get_path_cost() + 1
        child_priority = child_path_cost + multi_heuristic(child_state)
        child_node = Node(child_state, parent_node, action, child_path_cost,
                          child_priority)

        search_tree.add_to_frontier(child_node)