simple_rpg.py 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. import gymnasium as gym
  2. from gymnasium.spaces import Discrete, Box, Dict
  3. from ray.rllib.utils.spaces.repeated import Repeated
  4. # Constraints on the Repeated space.
  5. MAX_PLAYERS = 4
  6. MAX_ITEMS = 7
  7. MAX_EFFECTS = 2
  8. class SimpleRPG(gym.Env):
  9. """Example of a custom env with a complex, structured observation.
  10. The observation is a list of players, each of which is a Dict of
  11. attributes, and may further hold a list of items (categorical space).
  12. Note that the env doesn't train, it's just a dummy example to show how to
  13. use spaces.Repeated in a custom model (see CustomRPGModel below).
  14. """
  15. def __init__(self, config):
  16. self.cur_pos = 0
  17. self.action_space = Discrete(4)
  18. # Represents an item.
  19. self.item_space = Discrete(5)
  20. # Represents an effect on the player.
  21. self.effect_space = Box(9000, 9999, shape=(4,))
  22. # Represents a player.
  23. self.player_space = Dict(
  24. {
  25. "location": Box(-100, 100, shape=(2,)),
  26. "status": Box(-1, 1, shape=(10,)),
  27. "items": Repeated(self.item_space, max_len=MAX_ITEMS),
  28. "effects": Repeated(self.effect_space, max_len=MAX_EFFECTS),
  29. }
  30. )
  31. # Observation is a list of players.
  32. self.observation_space = Repeated(self.player_space, max_len=MAX_PLAYERS)
  33. def reset(self, *, seed=None, options=None):
  34. return self.observation_space.sample(), {}
  35. def step(self, action):
  36. return self.observation_space.sample(), 1, True, False, {}