run_tests.py 1.3 KB

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