test_scripts.py 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. import gym
  2. import numpy as np
  3. from gym_minigrid.benchmark import benchmark
  4. from gym_minigrid.manual_control import key_handler, reset
  5. from gym_minigrid.window import Window
  6. def test_benchmark():
  7. "Test that the benchmark function works for a specific environment"
  8. env_id = "MiniGrid-Empty-16x16-v0"
  9. benchmark(env_id, num_resets=10, num_frames=100)
  10. def test_window():
  11. "Testing the class functions of window.Window. This should locally open a window !"
  12. title = "testing window"
  13. window = Window(title)
  14. img = np.random.rand(100, 100, 3)
  15. window.show_img(img)
  16. caption = "testing caption"
  17. window.set_caption(caption)
  18. window.show(block=False)
  19. window.close()
  20. def test_manual_control():
  21. class FakeRandomKeyboardEvent:
  22. active_actions = ["left", "right", "up", " ", "pageup", "pagedown"]
  23. reset_action = "backspace"
  24. close_action = "escape"
  25. def __init__(self, active_actions=True, reset_action=False) -> None:
  26. if active_actions:
  27. self.key = np.random.choice(self.active_actions)
  28. elif reset_action:
  29. self.key = self.reset_action
  30. else:
  31. self.key = self.close_action
  32. env_id = "MiniGrid-Empty-16x16-v0"
  33. env = gym.make(env_id)
  34. window = Window(env_id)
  35. reset(env, window)
  36. for i in range(3): # 3 resets
  37. for j in range(20): # Do 20 steps
  38. key_handler(env, window, FakeRandomKeyboardEvent())
  39. key_handler(
  40. env,
  41. window,
  42. FakeRandomKeyboardEvent(active_actions=False, reset_action=True),
  43. )
  44. # Close the environment
  45. key_handler(
  46. env, window, FakeRandomKeyboardEvent(active_actions=False, reset_action=False)
  47. )