def move_to_top(current_node, visited_map, queue): """function moves the empty state up one position""" #The function checks to see if the move is valid (if it is on the board), #if the move left is valide, it will create a temporary new_state and it will #call the function to swap the empty position with the left position. The new_state is #returned and if that state has not been previously visited, the counter is incremented #for nodes created and the node is put into the queue. This process is the same for each #possible position: right, down, and left. The only change is math that is done to look #at each position. if (current_node.empty_spot - 3) < 0: return new_empty_spot = current_node.empty_spot - 3 new_state = current_node.start_state[:] new_state = swap_empty_position(new_state, current_node.empty_spot, new_empty_spot) if not check_visited(visited_map, new_state): set_glob_counter() add_to_visited(visited_map, new_state, COUNTER) new_node = Node(new_state) new_node.empty_spot = new_node.find_empty_position() new_node.depth = new_node.set_depth(current_node) new_node.set_path = new_node.set_path(current_node, new_empty_spot) queue.append(new_node)
def move_to_left(current_node, visited_map, queue): """function moves the empty state left one position if possible.""" if (current_node.empty_spot % 3) == 0: return # from this point, the same steps are repeated as the move_to_up function, but now swapping left new_empty_spot = current_node.empty_spot - 1 new_state = current_node.start_state[:] new_state = swap_empty_position(new_state, current_node.empty_spot, new_empty_spot) if not check_visited(visited_map, new_state): set_glob_counter() add_to_visited(visited_map, new_state, COUNTER) new_node = Node(new_state) new_node.empty_spot = new_node.find_empty_position() new_node.set_path = new_node.set_path(current_node, new_empty_spot) new_node.depth = new_node.set_depth(current_node) new_node.heuristic = new_node.calculate_misplaced_tiles() Q.heappush(queue, (new_node.heuristic, COUNTER, new_node))
def move_to_bottom(current_node, visited_map, queue): """function moves the empty state down one position""" if (current_node.empty_spot + 3) > 8: return new_empty_spot = current_node.empty_spot + 3 new_state = current_node.start_state[:] new_state = swap_empty_position(new_state, current_node.empty_spot, new_empty_spot) # from this point, the same steps are repeated as the move_to_up function, but now swapping down if not check_visited(visited_map, new_state): set_glob_counter() add_to_visited(visited_map, new_state, COUNTER) new_node = Node(new_state) new_node.empty_spot = new_node.find_empty_position() new_node.set_path = new_node.set_path(current_node, new_empty_spot) new_node.depth = new_node.set_depth(current_node) new_node.heuristic = new_node.calculate_manhattan_distance() Q.heappush(queue, (new_node.heuristic, COUNTER, new_node))
def move_to_top(current_node, visited_map, queue): """function moves the empty state up one position""" if (current_node.empty_spot - 3) < 0: return new_empty_spot = current_node.empty_spot - 3 #copies the current state to a temporary variable called new_state new_state = current_node.start_state[:] #makes the swap for the empty position and tile above the empty spot new_state = swap_empty_position(new_state, current_node.empty_spot, new_empty_spot) #if the new_state is not in the visited map then proceed with making a new node and adding it to the queue if not check_visited(visited_map, new_state): set_glob_counter() add_to_visited(visited_map, new_state, COUNTER) new_node = Node(new_state) new_node.empty_spot = new_node.find_empty_position() new_node.set_path = new_node.set_path(current_node, new_empty_spot) new_node.depth = new_node.set_depth(current_node) new_node.heuristic = new_node.calculate_misplaced_tiles() Q.heappush(queue, (new_node.heuristic, COUNTER, new_node))
def move_to_left(current_node, visited_map, queue): """function moves the empty state left one position if possible.""" if (current_node.empty_spot % 3) == 0: return new_empty_spot = current_node.empty_spot - 1 new_state = current_node.start_state[:] new_state = swap_empty_position(new_state, current_node.empty_spot, new_empty_spot) if not check_visited(visited_map, new_state): set_glob_counter() add_to_visited(visited_map, new_state, COUNTER) new_node = Node(new_state) new_node.empty_spot = new_node.find_empty_position() new_node.depth = new_node.set_depth(current_node) new_node.set_path = new_node.set_path(current_node, new_empty_spot) queue.append(new_node)
def move_to_bottom(current_node, visited_map, queue): """function moves the empty state down one position""" if (current_node.empty_spot + 3) > 8: return new_empty_spot = current_node.empty_spot + 3 new_state = current_node.start_state[:] new_state = swap_empty_position(new_state, current_node.empty_spot, new_empty_spot) if not check_visited(visited_map, new_state): set_glob_counter() add_to_visited(visited_map, new_state, COUNTER) new_node = Node(new_state) new_node.empty_spot = new_node.find_empty_position() new_node.depth = new_node.set_depth(current_node) new_node.set_path = new_node.set_path(current_node, new_empty_spot) queue.append(new_node)
def move_to_right(current_node, visited_map, stack): """function moves the empty state right one position""" if ((current_node.empty_spot + 1) % 3) == 0: return new_empty_spot = current_node.empty_spot + 1 new_state = current_node.start_state[:] new_state = swap_empty_position(new_state, current_node.empty_spot, new_empty_spot) if not check_visited(visited_map, new_state): set_glob_counter() add_to_visited(visited_map, new_state, COUNTER) new_node = Node(new_state) new_node.empty_spot = new_node.find_empty_position() new_node.depth = new_node.set_depth(current_node) if new_node.depth > MAX_DEPTH: increment_max_depth(new_node.depth) stack.append(new_node)
def user_search_choice(environment): """Menu for the user to decide which type of search they would like to perform""" #Gives the user choices on which type of search they would like to perform. It checks to see if the user input #valid. If it is, the appropriate functions are called to perform the search. print( "\nYour AI agent can search for the solution to your puzzle by using one of " "the following search algorithms. ") print("\nPlease select which search you would like to perform.") user_choice = input("1. Select 1 for breadth first search.\n" "2. Select 2 for depth first search\n" "3. Select 3 for A* Search using misplaced tiles.\n" "4. Select 4 for A* Search using manhattan distance\n") while not user_choice in ('1', '2', '3', '4'): print("\nYou did not select a valid option.") user_choice = input( "1. Select 1 for breadth first search\n" "2. Select 2 for depth first search\n" "3. Select 3 for A* Search using misplaced tiles.\n" "4. Select 4 for A* Search using manhattan distance\n") if user_choice == '1': visited_map = {} start_time = time.clock() current_node = Node(environment) current_node.empty_spot = current_node.find_empty_position() current_node.create_state_string() visited_map.update({current_node.state_string: 1}) begin_breadth_first_search(current_node, visited_map, start_time) if user_choice == '2': visited_map = {} start_time = time.clock() current_node = Node(environment) current_node.empty_spot = current_node.find_empty_position() current_node.create_state_string() visited_map.update({current_node.state_string: 1}) begin_depth_first_search(current_node, visited_map, start_time) if user_choice == '3': visited_map = {} start_time = time.clock() current_node = Node(environment) current_node.empty_spot = current_node.find_empty_position() current_node.create_state_string() current_node.heuristic = current_node.calculate_misplaced_tiles() visited_map.update({current_node.state_string: 1}) begin_a_star_misplaced_tiles(current_node, visited_map, start_time) if user_choice == '4': visited_map = {} start_time = time.clock() current_node = Node(environment) current_node.empty_spot = current_node.find_empty_position() current_node.create_state_string() current_node.heuristic = current_node.calculate_manhattan_distance() visited_map.update({current_node.state_string: 1}) begin_a_star_manhattan_distance(current_node, visited_map, start_time)