run_tests.py 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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. img = obs if type(obs) is np.ndarray else obs['image']
  25. grid = Grid.decode(img)
  26. img2 = grid.encode()
  27. assert np.array_equal(img2, img)
  28. # Check that the reward is within the specified range
  29. assert reward >= env.reward_range[0], reward
  30. assert reward <= env.reward_range[1], reward
  31. if done:
  32. env.reset()
  33. # Check that the agent doesn't overlap with an object
  34. assert env.grid.get(*env.agentPos) is None
  35. env.render('rgb_array')
  36. env.close()