コード例 #1
0
def do_search(states, goals):
    for i in range(len(states)):
        print_result(
            ucs.graph_search(problem_state.State(states[i]),
                             utils.get_goal_state(goals[i]), 5))
        print_result(
            run.graph_search(problem_state.State(states[i]),
                             utils.get_goal_state(goals[i]), 5))
        print_result(
            a_star_incons.graph_search(problem_state.State(states[i]),
                                       utils.get_goal_state(goals[i]), 5))
        print('-------------------')
コード例 #2
0
def main():
    """Main method.
    """
    height_limit, state, goal_state = int(input()), problem_state.State(input()), utils.get_goal_state(input())
    result = graph_search(state, goal_state, height_limit)
    if result[0] != -1:
        print(result[0])        
        print('; '.join([str(x) for x in result[1]]))
    else:
        print('No solution found')
コード例 #3
0
def main():
    start = time.time()  # Start time.
    os.system('cls' if os.name == 'nt' else 'clear')  # Clears the terminal.

    # Handles the arguments.
    if len(sys.argv) == 3:
        # If the args are 3 no output file name wasn't specified.
        method = sys.argv[1]
        input_file = sys.argv[2]
    elif len(sys.argv) == 4:
        # If the args are 4 the output file name was specified.
        method = sys.argv[1]
        input_file = sys.argv[2]
        output_file = sys.argv[3]
    else:
        print(
            f'Usage: {sys.argv[0]} <search algorithm> <problem file name> <solution file name>')
        print('- search algorithms: depth (Depth First), breadth (Breadth First), best (Best First), astar (A*)')
        sys.exit()

    # Initializes the type of queue based on the search method.
    search_queue = utils.METHODS[method]

    # Parse the data and get the objects (blocks), initial state and the goal state.
    data = utils.load_problem(input_file)
    objects = utils.get_objects_from_file(data)
    initial_state = utils.get_initial_state(data)
    goal_state = utils.get_goal_state(data)

    print('OBJECTS:', objects)

    print('\n#################### INITIAL STATE ####################\n')
    print(initial_state)
    i_blocks = utils.initialize_blocks(objects, initial_state)

    print('\n#################### GOAL STATE ####################\n')
    print(goal_state)
    g_blocks = utils.initialize_blocks(objects, goal_state)

    solution_node = search(search_queue, method, i_blocks, g_blocks)

    if solution_node != None:
        # If a solution is found.
        print('\n#################### SOLUTION ####################\n')
        solution_node.print_state()
        print(f'Number of moves: {solution_node.g}')

        # Calculates the time it took to find the solution.
        print('Took: ', time.time() - start)

        solution_path = solution_node.get_moves_to_solution()

        if len(sys.argv) == 3:
            # If the output file name was not specified.
            try:
                # Handling the paths with forward-slashes and back-slashes.
                file_name = input_file.split('\\')[-1]
                output_file = './solutions/' + method + '-' + file_name
                utils.write_solution(output_file, solution_path)
            except FileNotFoundError:
                file_name = input_file.split('/')[-1]
                output_file = './solutions/' + method + '-' + file_name
                utils.write_solution(output_file, solution_path)
        else:
            # If the output file name is specified.
            utils.write_solution(output_file, solution_path)
    else:
        print('Took: ', time.time() - start)
        print('############ ONE MINUTE PASSED AND NO SOLUTION WAS FOUND ############')
        sys.exit()