standalone.py 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. #!/usr/bin/env python3
  2. from __future__ import division, print_function
  3. import sys
  4. import numpy
  5. import gym
  6. import time
  7. from optparse import OptionParser
  8. import gym_minigrid
  9. def main():
  10. parser = OptionParser()
  11. parser.add_option(
  12. "-e",
  13. "--env-name",
  14. dest="env_name",
  15. help="gym environment to load",
  16. default='MiniGrid-MultiRoom-N6-v0'
  17. )
  18. (options, args) = parser.parse_args()
  19. # Load the gym environment
  20. env = gym.make(options.env_name)
  21. def resetEnv():
  22. env.reset()
  23. if hasattr(env, 'mission'):
  24. print('Mission: %s' % env.mission)
  25. resetEnv()
  26. # Create a window to render into
  27. renderer = env.render('human')
  28. def keyDownCb(keyName):
  29. action = 0
  30. if keyName == 'LEFT':
  31. action = env.actions.left
  32. elif keyName == 'RIGHT':
  33. action = env.actions.right
  34. elif keyName == 'UP':
  35. action = env.actions.forward
  36. elif keyName == 'SPACE':
  37. action = env.actions.toggle
  38. elif keyName == 'CTRL':
  39. action = env.actions.wait
  40. elif keyName == 'RETURN':
  41. resetEnv()
  42. elif keyName == 'ESCAPE':
  43. sys.exit(0)
  44. else:
  45. print("unknown key %s" % keyName)
  46. return
  47. obs, reward, done, info = env.step(action)
  48. print('step=%s, reward=%s' % (env.stepCount, reward))
  49. if done:
  50. print('done!')
  51. resetEnv()
  52. renderer.window.setKeyDownCb(keyDownCb)
  53. while True:
  54. env.render('human')
  55. time.sleep(0.01)
  56. # If the window was closed
  57. if renderer.window == None:
  58. break
  59. if __name__ == "__main__":
  60. main()