forked from Farama-Foundation/Minigrid
-
Notifications
You must be signed in to change notification settings - Fork 0
/
standalone.py
executable file
·79 lines (61 loc) · 1.71 KB
/
standalone.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#!/usr/bin/env python3
from __future__ import division, print_function
import sys
import numpy
import gym
import time
from optparse import OptionParser
import gym_minigrid
def main():
parser = OptionParser()
parser.add_option(
"-e",
"--env-name",
dest="env_name",
help="gym environment to load",
default='MiniGrid-MultiRoom-N6-v0'
)
(options, args) = parser.parse_args()
# Load the gym environment
env = gym.make(options.env_name)
def resetEnv():
env.reset()
if hasattr(env, 'mission'):
print('Mission: %s' % env.mission)
resetEnv()
# Create a window to render into
renderer = env.render('human')
def keyDownCb(keyName):
if keyName == 'BACKSPACE':
resetEnv()
return
if keyName == 'ESCAPE':
sys.exit(0)
action = 0
if keyName == 'LEFT':
action = env.actions.left
elif keyName == 'RIGHT':
action = env.actions.right
elif keyName == 'UP':
action = env.actions.forward
elif keyName == 'SPACE':
action = env.actions.toggle
elif keyName == 'CTRL':
action = env.actions.wait
else:
print("unknown key %s" % keyName)
return
obs, reward, done, info = env.step(action)
print('step=%s, reward=%s' % (env.stepCount, reward))
if done:
print('done!')
resetEnv()
renderer.window.setKeyDownCb(keyDownCb)
while True:
env.render('human')
time.sleep(0.01)
# If the window was closed
if renderer.window == None:
break
if __name__ == "__main__":
main()