run_tests.py 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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. print('%d environments registered' % len(envSet))
  8. for envName in sorted(envSet):
  9. print('testing "%s"' % envName)
  10. # Load the gym environment
  11. env = gym.make(envName)
  12. env.reset()
  13. env.render('rgb_array')
  14. env.seed()
  15. env.reset()
  16. # Run for a few episodes
  17. for i in range(5 * env.maxSteps):
  18. # Pick a random action
  19. action = random.randint(0, env.action_space.n - 1)
  20. obs, reward, done, info = env.step(action)
  21. # Test observation encode/decode roundtrip
  22. if type(obs) is np.ndarray:
  23. grid = Grid.decode(obs)
  24. obs2 = grid.encode()
  25. assert np.array_equal(obs2, obs)
  26. assert reward >= env.reward_range[0], reward
  27. assert reward <= env.reward_range[1], reward
  28. if done:
  29. env.reset()
  30. # Check that the agent doesn't overlap with an object
  31. assert env.grid.get(*env.agentPos) is None
  32. env.render('rgb_array')
  33. env.close()