Ejemplo n.º 1
0
    def _gen_grid(self, width, height):
        super()._gen_grid(width=width, height=height)

        if self.n_zone > 1:
            self.current_zone_num = self.n_reset % self.n_zone
            color = IDX_TO_COLOR[self.current_zone_num + 1]
            self.change_floor_color(color=color)

        if self.use_lava:
            directions = self.get_surrounding_agent()

            # Turn water into lava, to force the agent to kill himself
            for pos in directions:
                cell = self.grid.get(*pos)
                if cell and cell.type == 'water':
                    self.grid.set(*pos, Lava())
                    return

            random.shuffle(directions)
            # If it doesn't have water, creates it close to the agent
            for pos in directions:
                cell = self.grid.get(*pos)

                assert pos != self.start_pos

                if cell is None or cell.type == 'floor':
                    self.grid.set(*pos, Lava())
                    return
Ejemplo n.º 2
0
    def step(self, action):
        self.step_count += 1

        done = False

        if action == self.actions.left:
            self.agent_dir = (self.agent_dir - 1) % 4

        elif action == self.actions.right:
            self.agent_dir = (self.agent_dir + 1) % 4

        elif action == self.actions.forward:
            pass
        else:
            assert False, "unknown action: %d" % action

        fwd_pos = self.agent_pos + self.dir_vec
        fwd_cell = self.grid.get(*fwd_pos)

        if fwd_cell is None:
            self.grid.set(*self.agent_pos, Lava())
            self.snake.grow_head(*fwd_pos)
            self.grid.set(*self.snake.rm_tail(), None)
            self.agent_pos = fwd_pos

            reward = -0.001

        elif fwd_cell.type == 'goal':
            self.grid.set(*self.agent_pos, Lava())
            self.snake.grow_head(*fwd_pos)
            self.agent_pos = fwd_pos

            self.spawn_new_food()
            reward = 1.0

        elif (fwd_cell.type == 'lava' or fwd_cell.type == 'wall'):
            reward = -1.0
            done = True

        else:
            assert False

        if self.step_count == 1 and done:
            assert False

        obs = self.gen_obs()
        assert any([
            isinstance(self.grid.get(i, j), Goal)
            for i in range(self.grid.height) for j in range(self.grid.width)
        ])
        return obs, reward, done, {}
Ejemplo n.º 3
0
    def add_lava(self, i=None, j=None, num_lava=1):
        lavas = []
        for _ in range(num_lava):
            # Add the object to a random room if no room specified
            room_i = i
            room_j = j
            if room_i == None:
                room_i = self._rand_int(0, self.num_cols)
            if room_j == None:
                room_j = self._rand_int(0, self.num_rows)

            obj, pos = self.place_in_room(room_i, room_j, Lava())
            lavas.append(obj)
        return lavas
    def decode(array):
        """
        Decode an array grid encoding back into a grid
        """

        width, height, channels = array.shape
        assert channels == 3

        grid = Grid(width, height)
        for i in range(width):
            for j in range(height):
                typeIdx, colorIdx, shadeIdx, sizeIdx, state = array[i, j]

                if typeIdx == OBJECT_TO_IDX['unseen'] or \
                        typeIdx == OBJECT_TO_IDX['empty']:
                    continue

                objType = IDX_TO_OBJECT[typeIdx]
                color = IDX_TO_COLOR[colorIdx]
                shade = IDX_TO_SHADE[shadeIdx]
                size = IDX_TO_SIZE[size]

                # State, 0: open, 1: closed, 2: locked
                is_open = state == 0
                is_locked = state == 2

                if objType == 'wall':
                    v = Wall(color)
                elif objType == 'floor':
                    v = Floor(color)
                elif objType == 'ball':
                    v = Ball(color, shade=shade, size=size)
                elif objType == 'key':
                    v = Key(color, shade=shade, size=size)
                elif objType == 'box':
                    v = Box(color)
                elif objType == 'door':
                    v = Door(color, is_open, is_locked)
                elif objType == 'goal':
                    v = Goal()
                elif objType == 'lava':
                    v = Lava()
                else:
                    assert False, "unknown obj type in decode '%s'" % objType
                grid.set(i, j, v)

        return grid
Ejemplo n.º 5
0
    def _gen_grid(self, width, height):
        self.grid = Grid(width, height)

        self.grid.wall_rect(0, 0, width, height)

        # self.start_pos = (2, 2)
        yl, xl, _ = self.observation_space.spaces["image"].shape
        self.start_pos = (random.randint(2, yl - 2), random.randint(2, xl - 2))
        self.agent_pos = self.start_pos  # TODO: the env holding agent traits is shit!
        self.start_dir = random.randint(0, 3)
        self.agent_dir = self.start_dir
        self.snake = Snake(
            [self.start_pos,
             tuple(self.start_pos - self.dir_vec)])
        [self.grid.set(*pos, Lava()) for pos in self.snake.body]

        self.spawn_new_food()

        self.mission = None
Ejemplo n.º 6
0
    def _gen_grid(self, width, height):
        # Create an empty grid
        self.grid = Grid(width, height)

        # Generate the surrounding walls
        self.grid.wall_rect(0, 0, width, height)

        # Place the goals
        for _ in range(self.n_goals):
            self.place_obj(Goal())

        # Place the traps
        for _ in range(self.n_traps):
            self.place_obj(Lava())

        # Place the agent
        if self.agent_start_pos is not None:
            self.agent_pos = self.agent_start_pos
            self.agent_dir = self.agent_start_dir
        else:
            self.place_agent()

        self.mission = "get to the green goal square, avoid the lava"