Migrate babysnake to retro_gamer's observation_function
BabySnake's tabular Q-learning exercise needs a small discrete state, not the flattened board encoding retro_gamer normally produces. Define get_state(game) in babysnake_env.py, returning a plain (agent_x, agent_y, food_x, food_y) tuple, and point babysnake's observation_function at it instead of declaring a character_set. Disable the on-screen state overlay so it doesn't clutter the terminal watch view.
This commit is contained in:
@@ -101,6 +101,7 @@ def create_game():
|
|||||||
},
|
},
|
||||||
board_size=(bw, bh),
|
board_size=(bw, bh),
|
||||||
framerate=6,
|
framerate=6,
|
||||||
|
show_state=False,
|
||||||
)
|
)
|
||||||
forager.position = (randint(0, bw - 1), randint(0, bh - 1))
|
forager.position = (randint(0, bw - 1), randint(0, bh - 1))
|
||||||
food.relocate(game)
|
food.relocate(game)
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
[tool.retro-gamer]
|
[tool.retro-gamer]
|
||||||
actions = ["KEY_RIGHT", "KEY_UP", "KEY_LEFT", "KEY_DOWN"]
|
actions = ["KEY_RIGHT", "KEY_UP", "KEY_LEFT", "KEY_DOWN"]
|
||||||
reward = "reward"
|
reward = "reward"
|
||||||
character_set = ["@", "*"]
|
observation_function = "babysnake_env:get_state"
|
||||||
|
|||||||
13
babysnake_env.py
Normal file
13
babysnake_env.py
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
"""BabySnake's observation_function: maps a game to its tabular Q-learning state.
|
||||||
|
|
||||||
|
Referenced from babysnake/pyproject.toml's [tool.retro-gamer] section, and
|
||||||
|
used directly by train_babysnake.py via GameEnvironment.
|
||||||
|
"""
|
||||||
|
|
||||||
|
ACTIONS = ["KEY_RIGHT", "KEY_DOWN", "KEY_LEFT", "KEY_UP"]
|
||||||
|
|
||||||
|
|
||||||
|
def get_state(game):
|
||||||
|
"""Return BabySnake's state as a hashable (agent_x, agent_y, food_x, food_y) tuple."""
|
||||||
|
s = game.state
|
||||||
|
return (int(s['agent_x']), int(s['agent_y']), int(s['food_x']), int(s['food_y']))
|
||||||
Reference in New Issue
Block a user