コード例 #1
0
    def __draw_base_img(self):
        self._base_img = draw_grid(self._grid_shape[0], self._grid_shape[1], cell_size=CELL_SIZE, fill='white')
        for row in range(self._grid_shape[0]):
            for col in range(self._grid_shape[1]):
                if self.__wall_exists((row, col)):
                    fill_cell(self._base_img, (row, col), cell_size=CELL_SIZE, fill=WALL_COLOR)

        for agent_i, pos in list(self.final_agent_pos.items())[:self.n_agents]:
            row, col = pos[0], pos[1]
            draw_cell_outline(self._base_img, (row, col), cell_size=CELL_SIZE, fill=AGENT_COLORS[agent_i])
コード例 #2
0
    def render(self, mode='human'):
        img = copy.copy(self._base_img)
        for agent_i in range(self.n_agents):
            for neighbour in self.__get_neighbour_coordinates(
                    self.agent_pos[agent_i]):
                fill_cell(img,
                          neighbour,
                          cell_size=CELL_SIZE,
                          fill=AGENT_NEIGHBORHOOD_COLOR,
                          margin=0.1)
            fill_cell(img,
                      self.agent_pos[agent_i],
                      cell_size=CELL_SIZE,
                      fill=AGENT_NEIGHBORHOOD_COLOR,
                      margin=0.1)

        for agent_i in range(self.n_agents):
            draw_circle(img,
                        self.agent_pos[agent_i],
                        cell_size=CELL_SIZE,
                        fill=AGENT_COLOR)
            write_cell_text(img,
                            text=str(agent_i + 1),
                            pos=self.agent_pos[agent_i],
                            cell_size=CELL_SIZE,
                            fill='white',
                            margin=0.4)

        for prey_i in range(self.n_preys):
            if self._prey_alive[prey_i]:
                draw_circle(img,
                            self.prey_pos[prey_i],
                            cell_size=CELL_SIZE,
                            fill=PREY_COLOR)
                write_cell_text(img,
                                text=str(prey_i + 1),
                                pos=self.prey_pos[prey_i],
                                cell_size=CELL_SIZE,
                                fill='white',
                                margin=0.4)

        img = np.asarray(img)
        if mode == 'rgb_array':
            return img
        elif mode == 'human':
            from gym.envs.classic_control import rendering
            if self.viewer is None:
                self.viewer = rendering.SimpleImageViewer()
            self.viewer.imshow(img)
            return self.viewer.isopen
コード例 #3
0
ファイル: pong_duel.py プロジェクト: BaiLiping/BLPgym
    def render(self, mode='human'):
        img = copy.copy(self._base_img)
        for agent_i in range(self.n_agents):
            for row in range(self.agent_pos[agent_i][0] - 2,
                             self.agent_pos[agent_i][0] + 3):
                fill_cell(img, (row, self.agent_pos[agent_i][1]),
                          cell_size=CELL_SIZE,
                          fill=AGENT_COLORS[agent_i])

        ball_cells = self.__ball_cells
        fill_cell(img,
                  ball_cells[0],
                  cell_size=CELL_SIZE,
                  fill=BALL_HEAD_COLOR)
        fill_cell(img,
                  ball_cells[1],
                  cell_size=CELL_SIZE,
                  fill=BALL_TAIL_COLOR)
        fill_cell(img,
                  ball_cells[2],
                  cell_size=CELL_SIZE,
                  fill=BALL_TAIL_COLOR)

        img = draw_border(img, border_width=2, fill='gray')

        img = np.asarray(img)
        if mode == 'rgb_array':
            return img
        elif mode == 'human':
            from gym.envs.classic_control import rendering
            if self.viewer is None:
                self.viewer = rendering.SimpleImageViewer()
            self.viewer.imshow(img)
            return self.viewer.isopen
コード例 #4
0
    def __draw_base_img(self):
        # create grid and make everything black
        img = draw_grid(self._grid_shape[0],
                        self._grid_shape[1],
                        cell_size=CELL_SIZE,
                        fill=WALL_COLOR)

        # draw tracks
        for i, row in enumerate(self._full_obs):
            for j, col in enumerate(row):
                if col == PRE_IDS['empty']:
                    fill_cell(img, (i, j),
                              cell_size=CELL_SIZE,
                              fill='white',
                              margin=0.05)
        return img
コード例 #5
0
    def render(self, mode='human'):
        for agent_i in range(self.n_agents):
            fill_cell(self._base_img, self.agent_pos[agent_i], cell_size=CELL_SIZE, fill='white', margin=0.05)
            fill_cell(self._base_img, self.agent_prev_pos[agent_i], cell_size=CELL_SIZE, fill='white', margin=0.05)
            draw_circle(self._base_img, self.agent_pos[agent_i], cell_size=CELL_SIZE, fill=AGENT_COLORS[agent_i])
            write_cell_text(self._base_img, text=str(agent_i + 1), pos=self.agent_pos[agent_i], cell_size=CELL_SIZE,
                            fill='white', margin=0.4)

        # adds a score board on top of the image
        # img = draw_score_board(self._base_img, score=self._total_episode_reward)
        # img = np.asarray(img)

        img = np.asarray(self._base_img)
        if mode == 'rgb_array':
            return img
        elif mode == 'human':
            from gym.envs.classic_control import rendering
            if self.viewer is None:
                self.viewer = rendering.SimpleImageViewer()
            self.viewer.imshow(img)
            return self.viewer.isopen
コード例 #6
0
    def render(self, mode='human'):
        img = copy.copy(self._base_img)
        cell_size = (CELL_SIZE, CELL_SIZE / 2)

        mask = (
            slice(self._agent_view[0], self._agent_view[0] + self._grid_shape[0]),
            slice(self._agent_view[1], self._agent_view[1] + self._grid_shape[1]),
        )

        # Iterate over all grid positions
        for pos, agent_strength, tree_strength in self._view_generator(mask):
            if tree_strength and agent_strength:
                cell_size = (CELL_SIZE, CELL_SIZE / 2)
                tree_pos = (pos[0], 2 * pos[1])
                agent_pos = (pos[0], 2 * pos[1] + 1)
            else:
                cell_size = (CELL_SIZE, CELL_SIZE)
                tree_pos = agent_pos = (pos[0], pos[1])

            if tree_strength != 0:
                fill_cell(img, pos=tree_pos, cell_size=cell_size, fill=TREE_COLOR, margin=0.1)
                write_cell_text(img, text=str(tree_strength), pos=tree_pos,
                                cell_size=cell_size, fill='white', margin=0.4)

            if agent_strength != 0:
                draw_circle(img, pos=agent_pos, cell_size=cell_size, fill=AGENT_COLOR, radius=0.30)
                write_cell_text(img, text=str(agent_strength), pos=agent_pos,
                                cell_size=cell_size, fill='white', margin=0.4)

        img = np.asarray(img)
        if mode == 'rgb_array':
            return img
        elif mode == 'human':
            from gym.envs.classic_control import rendering
            if self._viewer is None:
                self._viewer = rendering.SimpleImageViewer()
            self._viewer.imshow(img)
            return self._viewer.isopen
コード例 #7
0
 def __draw_base_img(self):
     self._base_img = draw_grid(self._grid_shape[0], self._grid_shape[1], cell_size=CELL_SIZE, fill='white')
     for row in range(self._grid_shape[0]):
         for col in range(self._grid_shape[1]):
             if PRE_IDS['wall'] in self._full_obs[row][col]:
                 fill_cell(self._base_img, (row, col), cell_size=CELL_SIZE, fill=WALL_COLOR, margin=0.05)
             elif PRE_IDS['lemon'] in self._full_obs[row][col]:
                 fill_cell(self._base_img, (row, col), cell_size=CELL_SIZE, fill=LEMON_COLOR, margin=0.05)
             elif PRE_IDS['apple'] in self._full_obs[row][col]:
                 fill_cell(self._base_img, (row, col), cell_size=CELL_SIZE, fill=APPLE_COLOR, margin=0.05)