The source code and dockerfile for the GSW2024 AI Lab.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

49 lines
1.6 KiB

4 weeks ago
  1. from __future__ import annotations
  2. import gymnasium as gym
  3. import numpy as np
  4. from pytest_mock import MockerFixture
  5. from minigrid.benchmark import benchmark
  6. from minigrid.manual_control import ManualControl
  7. from minigrid.minigrid_env import MiniGridEnv
  8. def test_benchmark():
  9. "Test that the benchmark function works for a specific environment"
  10. env_id = "MiniGrid-Empty-16x16-v0"
  11. benchmark(env_id, num_resets=10, num_frames=100)
  12. def test_manual_control(mocker: MockerFixture):
  13. class FakeRandomKeyboardEvent:
  14. active_actions = ["left", "right", "up", " ", "pageup", "pagedown"]
  15. reset_action = "backspace"
  16. close_action = "escape"
  17. def __init__(self, reset: bool = False, close: bool = False) -> None:
  18. if reset:
  19. self.key = self.reset_action
  20. return
  21. if close:
  22. self.key = self.close_action
  23. return
  24. self.key = np.random.choice(self.active_actions)
  25. env_id = "MiniGrid-Empty-16x16-v0"
  26. env: MiniGridEnv = gym.make(env_id, render_mode="human")
  27. manual_control = ManualControl(env)
  28. for i in range(3): # 3 resets
  29. manual_control.reset()
  30. for j in range(20): # Do 20 steps
  31. manual_control.key_handler(FakeRandomKeyboardEvent())
  32. fake_event = FakeRandomKeyboardEvent(reset=True)
  33. manual_control.key_handler(fake_event)
  34. # Close the environment
  35. mocked_quit = mocker.patch("pygame.quit")
  36. fake_event = FakeRandomKeyboardEvent(close=True)
  37. manual_control.key_handler(fake_event)
  38. mocked_quit.assert_called_once()