run_tests.py 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. #!/usr/bin/env python3
  2. import random
  3. import gym
  4. import numpy as np
  5. from gym_minigrid.register import envSet
  6. from gym_minigrid.minigrid import Grid
  7. # Make sure directly importing a specific environment works
  8. from gym_minigrid.envs import DoorKeyEnv
  9. print('%d environments registered' % len(envSet))
  10. for envName in sorted(envSet):
  11. print('testing "%s"' % envName)
  12. # Load the gym environment
  13. env = gym.make(envName)
  14. env.reset()
  15. env.render('rgb_array')
  16. env.seed()
  17. env.reset()
  18. # Run for a few episodes
  19. for i in range(5 * env.maxSteps):
  20. # Pick a random action
  21. action = random.randint(0, env.action_space.n - 1)
  22. obs, reward, done, info = env.step(action)
  23. # Test observation encode/decode roundtrip
  24. if type(obs) is np.ndarray:
  25. grid = Grid.decode(obs)
  26. obs2 = grid.encode()
  27. assert np.array_equal(obs2, obs)
  28. assert reward >= env.reward_range[0], reward
  29. assert reward <= env.reward_range[1], reward
  30. if done:
  31. env.reset()
  32. # Check that the agent doesn't overlap with an object
  33. assert env.grid.get(*env.agentPos) is None
  34. env.render('rgb_array')
  35. env.close()