Beispiel #1
0
    def test_successors(self):
        world = pacmaze.PacMaze('worlds/7x7_paper.txt')

        # without diagonals:
        world.set_diagonal_moves(False)

        # expected successors of 6, 6
        expected_successors = [
            ('imovel', (6, 6)),
            ('acima', (5, 6)),
            ('abaixo', (7, 6)),
            ('esquerda', (6, 5)),
            ('direita', (6, 7)),
        ]

        # converts to set 'coz order does not matter
        self.assertEquals(set(expected_successors), set(world.successors(6,
                                                                         6)))

        # allow diagonals:
        world.set_diagonal_moves(True)

        # add the diagonals
        expected_successors += [
            ('nordeste', (5, 7)),
            ('noroeste', (5, 5)),
            ('sudeste', (7, 7)),
            ('sudoeste', (7, 5)),
        ]

        # converts to set 'coz order does not matter
        self.assertEquals(set(expected_successors), set(world.successors(6,
                                                                         6)))
Beispiel #2
0
    def test_query_7x7paper_world(self):
        world = pacmaze.PacMaze('worlds/7x7_paper.txt')

        self.assertEquals('E', world.query(5, 5))

        # coordinate mod 7 should give the same note
        self.assertEquals('E', world.query(12, 12))
        self.assertEquals('E', world.query(19, 19))
        self.assertEquals('E', world.query(-2, -2))
Beispiel #3
0
    def test_move_7x7paper_world(self):
        world = pacmaze.PacMaze('worlds/7x7_paper.txt')

        path = world.walk((5, 5), ['direita', 'direita', 'direita'])

        # initial position (5, 5) won't appear in path
        self.assertEquals([
            (5, 6, 'B'),
            (5, 7, 'F'),
            (5, 8, 'C'),
        ], path)

        self.assertEquals((5, 8), world.pacman_position())
Beispiel #4
0
def is_optimal_solution(input_file, initial_state, actions):
    """
    Retorna se o conjunto de acoes a partir de estado_inicial leva ao estado final
     e e' o menor possivel
    :param initial_state:
    :param actions:
    :return: bool
    """

    maze = pacmaze.PacMaze(input_file)
    if maze.is_solution(initial_state, actions):
        # bfs is guaranteed to be optimal since costs are unitary:
        return len(bfs(input_file, initial_state)) == len(actions)

    return False
Beispiel #5
0
def dfs(input_file, initial_state):
    sol = graph_search(pacmaze.PacMaze(input_file), initial_state,
                       fringe.DFSFringe())
    #print ' '.join(sol)
    return sol
Beispiel #6
0
def run(params):
    """
    Runs the program with parameters specified from command line
    :param params:
    :return:
    """
    # sets random seed
    if 'random_seed' in params:
        random.seed(params['random_seed'])

    # creates output directory
    outdir = params['output_dir']
    if not os.path.exists(outdir):
        os.mkdir(outdir)

    worldfile = None
    if 'world' in params:
        worldfile = params['world']

    # sets target radius
    target_radius = params['target_radius']

    # sets max targets
    max_targets = params['max_targets']

    maze = pacmaze.PacMaze(worldfile)

    # sets diagonal moves as specified by user
    maze.set_diagonal_moves(args['diagonals'])

    for num in range(args['number_trials']):  #, goal in enumerate(goals):

        cont_steps = 0

        for i in range(args['max_targets']):
            col, row = create_target(maze, target_radius)
            goal = (row, col)
            maze.add_goal(goal[0], goal[1])

        # print(maze._goals) #print list goals

        outfile = open(os.path.join(outdir, 'log%d.log' % num), 'w')
        outfile.write('%d, %d, %s' %
                      (maze.pacman_position()[0], maze.pacman_position()[1],
                       maze.query(maze.pacman_position()[0],
                                  maze.pacman_position()[1])))
        outfile.write('\n')
        while cont_steps < args['max_steps']:

            # print(maze._goals) #print list remaining
            # print maze.__str__() # print world

            if len(maze._goals) == 0:
                break

            possible_directions = []
            # determines which method will be used for walking
            if params['method'] == 'astar':
                possible_directions = search.astar(maze,
                                                   maze.pacman_position())

            elif params['method'] == 'random':
                possible_directions = random_walk.random_walk(
                    maze, maze.pacman_position())

            real_directions = []
            for i in possible_directions:
                print cont_steps
                if cont_steps < args['max_steps']:
                    real_directions.append(i)
                    cont_steps = cont_steps + 1
                else:
                    break
            path = maze.walk(maze.pacman_position(), real_directions)
            # agent might have reached some goals, let's re-add them
            while len(maze._goals) < args['max_targets']:
                col, row = create_target(maze, target_radius)
                goal = (row, col)
                maze.add_goal(goal[0], goal[1])

            # print 'PM at', maze.pacman_position()
            outfile.write('\n'.join(
                ['%d, %d, %s' % (x[0], x[1], x[2]) for x in path]))
            outfile.write('\n')
            # print '\n'.join(['%d, %d, %s' % (x[0], x[1], pacmaze.NOTE_TO_INT[x[2]]) for x in path])
        print 'File ' + str(num) + ' written.'
        outfile.close()