コード例 #1
0
    def __init__(self, grid_size=[30,30], unit_size=10, unit_gap=1, snake_size=3, n_snakes=1, n_foods=1, random_init=True):

        assert n_snakes < grid_size[0]//3
        assert n_snakes < 25
        assert snake_size < grid_size[1]//2
        assert unit_gap >= 0 and unit_gap < unit_size

        self.snakes_remaining = n_snakes
        self.grid = Grid(grid_size, unit_size, unit_gap)
        self.score = 0

        self.snakes = []
        self.dead_snakes = []
        for i in range(1,n_snakes+1):
            #start_coord = [i*grid_size[0]//(n_snakes+1), snake_size+1]
            start_coord = [random.randint(0, grid_size[0]-1), random.randint(0, grid_size[1]-1)]
            self.snakes.append(Snake(start_coord, snake_size))
            color = [self.grid.HEAD_COLOR[0], i*10, 0]
            self.snakes[-1].head_color = color
            self.grid.draw_snake(self.snakes[-1], color)
            self.dead_snakes.append(None)

        if not random_init:
            for i in range(2,n_foods+2):
                start_coord = [i*grid_size[0]//(n_foods+3), grid_size[1]-5]
                self.grid.place_food(start_coord)
        else:
            for i in range(n_foods):
                self.grid.new_food()
コード例 #2
0
    def reset(self):
        if self.rewards:
            self.snake = Snake(
                self.blocks,
                self.width // self.blocks,
                self.np_random,
                rew_step=self.rewards[0],
                rew_apple=self.rewards[1],
                rew_death=self.rewards[2],
                rew_death2=self.rewards[3],
                rew_apple_func=self.rewards[4],
            )
        else:
            self.snake = Snake(self.blocks, self.width // self.blocks,
                               self.np_random)
        raw_state = self.snake.get_raw_state()

        state = np.array(raw_state[0], dtype=np.float32)
        state /= self.blocks

        return state
コード例 #3
0
    def __init__(self,
                 grid_size=[30, 30],
                 unit_size=10,
                 unit_gap=1,
                 snake_size=3,
                 n_snakes=1,
                 n_foods=1,
                 random_init=True,
                 start_coord=None,
                 food_pos=None):

        assert n_snakes < grid_size[0] // 3
        assert n_snakes < 25
        assert snake_size < grid_size[1] // 2
        assert unit_gap >= 0 and unit_gap < unit_size

        if food_pos is not None:
            assert food_pos[0] < grid_size[
                0]  # added 20190519 in mod19 version
            assert food_pos[1] < grid_size[
                1]  # added 20190519 in mod19 version

        self.snakes_remaining = n_snakes
        self.grid = Grid(grid_size, unit_size, unit_gap)

        self.snakes = []
        self.dead_snakes = []
        for i in range(1, n_snakes + 1):

            if start_coord is None:
                start_coord = [
                    i * grid_size[0] // (n_snakes + 1), snake_size + 1
                ]

            self.snakes.append(Snake(start_coord, snake_size))
            color = [self.grid.HEAD_COLOR[0], i * 10, 0]
            self.snakes[-1].head_color = color
            self.grid.draw_snake(self.snakes[-1], color)
            self.dead_snakes.append(None)

        if food_pos is not None:
            self.grid.place_food(food_pos)  # added 20190519 in mod19 version
        else:
            if not random_init:
                for i in range(2, n_foods + 2):
                    start_coord = [
                        i * grid_size[0] // (n_foods + 3), grid_size[1] - 5
                    ]
                    self.grid.place_food(start_coord)
            else:
                for i in range(n_foods):
                    self.grid.new_food()
コード例 #4
0
ファイル: controller.py プロジェクト: Sharanpai/Frontier
    def __init__(self,
                 grid_size=[30, 30],
                 unit_size=10,
                 unit_gap=1,
                 snake_size=3,
                 n_snakes=1,
                 n_foods=1,
                 random_init=True,
                 wall=False):

        assert n_snakes < grid_size[0] // 3
        assert n_snakes < 25
        assert snake_size < grid_size[1] // 2
        assert unit_gap >= 0 and unit_gap < unit_size

        self.snakes_remaining = n_snakes
        self.grid = Grid(grid_size, unit_size, unit_gap)

        self.empty = []
        self.douse = []
        self.r = [0., 0., 0., 0.]

        self.snakes = []
        self.dead_snakes = []
        for i in range(1, n_snakes + 1):
            start_coord = [i * grid_size[0] // (n_snakes + 1), snake_size + 1]
            self.snakes.append(Snake(start_coord, snake_size))
            color = [self.grid.HEAD_COLOR[0], i * 10, 0]
            color = [0, 0, 255]
            self.snakes[-1].head_color = color
            self.grid.draw_snake(self.snakes[-1], color)
            self.dead_snakes.append(None)

        # if wall:
        #     self.grid.create_wall()
        #     self.grid.create_wall()
        #     self.grid.create_wall()

        # if not random_init:
        #     for i in range(2,n_foods+2):
        #         start_coord = [i*grid_size[0]//(n_foods+3), grid_size[1]-5]
        #         self.grid.place_food(start_coord)
        # else:
        # for i in range(n_foods):
        #     self.grid.new_food(i)

        self.grid.init_fire()
コード例 #5
0
ファイル: controller.py プロジェクト: CarlBeek/Gym-Snake
    def __init__(self,
                 grid_size=[30, 30],
                 unit_size=10,
                 unit_gap=1,
                 snake_size=3,
                 random_init=True):

        assert snake_size < grid_size[1] // 2
        assert 0 <= unit_gap < unit_size

        self.grid = Grid(grid_size, unit_size, unit_gap)

        start_coord = [grid_size[0] // 2, snake_size + 1]
        self.snake = Snake(start_coord, snake_size)
        self.dead_snake = None
        color = [self.grid.HEAD_COLOR[0], 0, 0]
        self.snake.head_color = color
        self.grid.draw_snake(self.snake, color)

        if not random_init:
            start_coord = [grid_size[0] // 2, grid_size[1] - 5]
            self.grid.place_food(start_coord)
        else:
            self.grid.new_food()
コード例 #6
0
ファイル: snake_unittests.py プロジェクト: Riyer01/SnakeRL
 def test_action_UP_backwards(self):
     kaa = Snake(self.head_xy, self.bod_len)
     kaa.direction = kaa.UP
     head = kaa.action(kaa.DOWN)
     self.assertTrue(np.array_equal(head, [0, -1]))
コード例 #7
0
ファイル: snake_unittests.py プロジェクト: Riyer01/SnakeRL
 def test_action_LEFT_outofrange(self):
     kaa = Snake(self.head_xy, self.bod_len)
     expected_coord = [-1, 0]
     actual_coord = kaa.action(kaa.LEFT + 4)
     self.assertTrue(np.array_equal(expected_coord, actual_coord))
コード例 #8
0
ファイル: snake_unittests.py プロジェクト: Riyer01/SnakeRL
 def test_action_DOWN_outofrange(self):
     kaa = Snake(self.head_xy, self.bod_len)
     kaa.direction = kaa.DOWN
     expected_coord = [0, 1]
     actual_coord = kaa.action(kaa.DOWN + 4)
     self.assertTrue(np.array_equal(expected_coord, actual_coord))
コード例 #9
0
ファイル: snake_unittests.py プロジェクト: Riyer01/SnakeRL
 def test_action_RIGHT(self):
     kaa = Snake(self.head_xy, self.bod_len)
     expected_coord = [1, 0]
     actual_coord = kaa.action(kaa.RIGHT)
     self.assertTrue(np.array_equal(expected_coord, actual_coord))
コード例 #10
0
ファイル: snake_unittests.py プロジェクト: Riyer01/SnakeRL
 def test_action_UP(self):
     kaa = Snake(self.head_xy, self.bod_len)
     kaa.direction = kaa.UP
     expected_coord = [0, -1]
     actual_coord = kaa.action(kaa.UP)
     self.assertTrue(np.array_equal(expected_coord, actual_coord))
コード例 #11
0
ファイル: snake_unittests.py プロジェクト: Riyer01/SnakeRL
 def test_step_LEFT(self):
     kaa = Snake(self.head_xy, self.bod_len)
     expected_coord = [-1, 0]
     actual_coord = kaa.step(kaa.head, kaa.LEFT)
     self.assertTrue(np.array_equal(expected_coord, actual_coord))
コード例 #12
0
ファイル: snake_unittests.py プロジェクト: Riyer01/SnakeRL
 def test_step_DOWN(self):
     kaa = Snake(self.head_xy, self.bod_len)
     expected_coord = [0, 1]
     actual_coord = kaa.step(kaa.head, kaa.DOWN)
     self.assertTrue(np.array_equal(expected_coord, actual_coord))
コード例 #13
0
ファイル: snake_unittests.py プロジェクト: Riyer01/SnakeRL
 def test_body_Initialization(self):
     kaa = Snake(self.head_xy, self.bod_len)
     expected_body_coords = [[0, -2], [0, -1]]
     for i in range(len(kaa.body)):
         self.assertTrue(
             np.array_equal(kaa.body.popleft(), expected_body_coords[i]))
コード例 #14
0
ファイル: snake_unittests.py プロジェクト: Riyer01/SnakeRL
 def test_action_LEFT_backwards(self):
     kaa = Snake(self.head_xy, self.bod_len)
     kaa.direction = kaa.LEFT
     head = kaa.action(kaa.RIGHT)
     self.assertTrue(np.array_equal(head, [-1, 0]))
コード例 #15
0
ファイル: snake_unittests.py プロジェクト: Riyer01/SnakeRL
 def test_head_Initialization(self):
     kaa = Snake(self.head_xy, self.bod_len)
     self.assertTrue(np.array_equal(self.head_xy, kaa.head))
コード例 #16
0
class SnakeEnv(gym.Env):
    metadata = {'render.modes': ['human']}

    def __init__(self, blocks=10, block_size=50):
        self.blocks = blocks
        self.width = block_size * blocks
        self.snake = None

        self.action_space = spaces.Discrete(3)
        self.observation_space = spaces.Box(
            dtype=np.float32,
            low=np.array([0, 0, 0, -1, -1]),
            high=np.array([1, 1, 1, 1, 1]),
        )

        self.seed()
        self.viewer = None
        self.rewards = None

    def set_rewards(self, rew_step, rew_apple, rew_death, rew_death2,
                    rew_apple_func):
        self.rewards = [
            rew_step, rew_apple, rew_death, rew_death2, rew_apple_func
        ]

    def seed(self, seed=None):
        self.np_random, seed = seeding.np_random(seed)
        return [seed]

    def step(self, action):
        if action != 0:
            self.snake.direction = self.snake.DIRECTIONS[
                self.snake.direction[action]]

        info = {}

        self.snake.update()
        info['apple_ate'] = self.snake.apple_ate

        raw_state, reward, done = self.snake.get_raw_state()
        info['apples'] = self.snake.cnt_apples

        state = np.array(raw_state, dtype=np.float32)
        state /= self.blocks

        return state, reward, done, info

    def reset(self):
        if self.rewards:
            self.snake = Snake(
                self.blocks,
                self.width // self.blocks,
                self.np_random,
                rew_step=self.rewards[0],
                rew_apple=self.rewards[1],
                rew_death=self.rewards[2],
                rew_death2=self.rewards[3],
                rew_apple_func=self.rewards[4],
            )
        else:
            self.snake = Snake(self.blocks, self.width // self.blocks,
                               self.np_random)
        raw_state = self.snake.get_raw_state()

        state = np.array(raw_state[0], dtype=np.float32)
        state /= self.blocks

        return state

    def render(self, mode='human'):
        from gym.envs.classic_control import rendering
        w = self.snake.blockw

        if self.viewer is None:
            self.viewer = rendering.Viewer(self.width, self.width)
            apple = self._create_block(w)
            self.apple_trans = rendering.Transform()
            apple.add_attr(self.apple_trans)
            apple.set_color(*self.snake.apple.color)
            self.viewer.add_geom(apple)

            head = self._create_block(w)
            self.head_trans = rendering.Transform()
            head.add_attr(self.head_trans)
            head.set_color(*self.snake.head.color)
            self.viewer.add_geom(head)

            self.body = []
            for i in range(len(self.snake.body)):
                body = self._create_block(w)
                body_trans = rendering.Transform()
                body.add_attr(body_trans)
                body.set_color(*self.snake.body[0].color)

                self.body.append(body_trans)
                self.viewer.add_geom(body)

        self.apple_trans.set_translation(self.snake.apple.x,
                                         self.snake.apple.y)
        self.head_trans.set_translation(self.snake.head.x, self.snake.head.y)

        if len(self.snake.body) > len(self.body):
            body = self._create_block(w)
            body_trans = rendering.Transform()
            body.add_attr(body_trans)
            body.set_color(*self.snake.body[0].color)

            self.body.append(body_trans)
            self.viewer.add_geom(body)
        elif len(self.snake.body) < len(self.body):
            self.body, trash = self.body[len(self.body) - len(self.snake.body):], \
                               self.body[:len(self.body) - len(self.snake.body)]
            for i in range(len(trash)):
                trash[i].set_translation(-w, -w)

        for i in range(len(self.body)):
            self.body[i].set_translation(self.snake.body[i].x,
                                         self.snake.body[i].y)

        self.viewer.render()

    def _create_block(self, w):
        from gym.envs.classic_control import rendering
        return rendering.FilledPolygon([(0, 0), (0, w), (w, w), (w, 0)])

    def close(self):
        if self.viewer:
            self.viewer.close()
            self.viewer = None