run_tests.py 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. #!/usr/bin/env python3
  2. import random
  3. import numpy as np
  4. import gym
  5. from gym_minigrid.register import env_list
  6. from gym_minigrid.minigrid import Grid, OBJECT_TO_IDX
  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. ##############################################################################
  12. print('%d environments registered' % len(env_list))
  13. for envName in env_list:
  14. print('testing "%s"' % envName)
  15. # Load the gym environment
  16. env = gym.make(envName)
  17. env.max_steps = min(env.max_steps, 200)
  18. env.reset()
  19. env.render('rgb_array')
  20. # Verify that the same seed always produces the same environment
  21. for i in range(0, 5):
  22. seed = 1337 + i
  23. env.seed(seed)
  24. grid1 = env.grid
  25. env.seed(seed)
  26. grid2 = env.grid
  27. assert grid1 == grid2
  28. env.reset()
  29. # Run for a few episodes
  30. num_episodes = 0
  31. while num_episodes < 5:
  32. # Pick a random action
  33. action = random.randint(0, env.action_space.n - 1)
  34. obs, reward, done, info = env.step(action)
  35. # Validate the agent position
  36. assert env.agent_pos[0] < env.grid_size
  37. assert env.agent_pos[1] < env.grid_size
  38. # Test observation encode/decode roundtrip
  39. img = obs['image']
  40. vis_mask = img[:, :, 0] != OBJECT_TO_IDX['unseen'] # hackish
  41. img2 = Grid.decode(img).encode(vis_mask=vis_mask)
  42. assert np.array_equal(img, img2)
  43. # Check that the reward is within the specified range
  44. assert reward >= env.reward_range[0], reward
  45. assert reward <= env.reward_range[1], reward
  46. if done:
  47. num_episodes += 1
  48. env.reset()
  49. env.render('rgb_array')
  50. # Test the fully observable wrapper
  51. env = FullyObsWrapper(env)
  52. env.reset()
  53. obs, _, _, _ = env.step(0)
  54. assert obs.shape == env.observation_space.shape
  55. env.close()
  56. ##############################################################################
  57. print('testing agent_sees method')
  58. env = gym.make('MiniGrid-DoorKey-6x6-v0')
  59. goal_pos = (env.grid.width - 2, env.grid.height - 2)
  60. # Test the "in" operator on grid objects
  61. assert ('green', 'goal') in env.grid
  62. assert ('blue', 'key') not in env.grid
  63. # Test the env.agent_sees() function
  64. env.reset()
  65. for i in range(0, 500):
  66. action = random.randint(0, env.action_space.n - 1)
  67. obs, reward, done, info = env.step(action)
  68. goal_visible = ('green', 'goal') in Grid.decode(obs['image'])
  69. agent_sees_goal = env.agent_sees(*goal_pos)
  70. assert agent_sees_goal == goal_visible
  71. if done:
  72. env.reset()
  73. #############################################################################