Skip to content

Snake-misc

Snake

Snake(height: int = 10, width: int = 10)

Bases: Environment[EnvState, EnvParams]

Steer a growing snake to eat apples on a fixed-size grid.

The snake starts with length 1 at a uniformly random cell with a random direction, and the apple at a uniformly random other cell. Eating an apple increases the length by one and places a new apple on a uniformly random free cell. The current length is returned as info["length"].

  • Observation: Box of shape (height, width, 3) with binary channels for the body, the head and the apple.
  • Actions: Discrete(4). 0 moves up, 1 right, 2 down and 3 left. An action opposite to the current direction is ignored while the length exceeds 1.
  • Reward: params.reward_apple for eating an apple, plus params.reward_death for a wall or self collision, plus params.reward_step at every step.
  • Termination: wall or self collision, or the board is completely filled.
  • Truncation: after params.max_steps_in_episode steps (1000 by default).

Parameters:

  • height (int, default: 10 ) –

    Number of rows of the board. Must be at least 2.

  • width (int, default: 10 ) –

    Number of columns of the board. Must be at least 2.

Parameters and state

EnvParams

Bases: EnvParams

Parameters of the Snake environment.

Attributes:

  • reward_apple (float) –

    Reward for eating an apple.

  • reward_death (float) –

    Reward for a wall or self collision.

  • reward_step (float) –

    Reward added at every step.

  • max_steps_in_episode (int) –

    Number of steps after which the episode is truncated.

EnvState

Bases: EnvState

State of the Snake environment.

Attributes:

  • board (Array) –

    Integer array of shape (height, width). A positive entry is the number of steps until the snake vacates the cell, so the head cell equals length. Free cells are 0.

  • head (Array) –

    Position of the head as (row, column).

  • direction (Array) –

    Last executed action.

  • apple (Array) –

    Position of the apple as (row, column).

  • length (Array) –

    Number of cells occupied by the snake.

  • alive (Array) –

    Whether the snake is alive. False after a wall or self collision.

  • time (int) –

    Number of steps taken in the current episode.