-
Notifications
You must be signed in to change notification settings - Fork 1
/
manual_control.py
executable file
·209 lines (179 loc) · 4.79 KB
/
manual_control.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
#!/usr/bin/env python3
# file taken from gym-minigrid @ Farama-Foundation and slightly modified
import time
import argparse
import numpy as np
import gym
import gym_minigrid
from gym_minigrid.envs.risky import ABSORBING_REWARD_GOAL, ABSORBING_REWARD_LAVA, ABSORBING_STATES, GOAL_REWARD, LAVA_REWARD, SPIKY_TILE_REWARD, STEP_PENALTY, RiskyPathEnv
from gym_minigrid.wrappers import *
from gym_minigrid.window import Window
from special_wrappers import IntrinsicMotivationWrapper, RandomizeGoalWrapper
is_RiskyPathEnv = False
def redraw(img):
if not args.agent_view:
img = env.render('rgb_array', tile_size=args.tile_size)
window.show_img(img)
def reset():
if args.seed != -1:
env.seed(args.seed)
obs = env.reset()
if hasattr(env, 'mission'):
print('Mission: %s' % env.mission)
window.set_caption(env.mission)
redraw(obs)
def step(action):
obs, reward, done, info = env.step(action)
print('step=%s, reward=%.2f' % (env.step_count, reward))
if done:
print('done!')
print('Resetting environment.')
redraw(obs)
time.sleep(0.2)
reset()
else:
redraw(obs)
def key_handler(event):
print('pressed', event.key)
if event.key == 'escape':
window.close()
return
if event.key == 'backspace':
reset()
return
if event.key == 'left':
if is_RiskyPathEnv:
action = env.new_actions.west
step(action)
return
step(env.actions.left)
return
if event.key == 'right':
if is_RiskyPathEnv:
action = env.new_actions.east
step(action)
return
step(env.actions.right)
return
if event.key == 'up':
if is_RiskyPathEnv:
action = env.new_actions.north
step(action)
return
step(env.actions.forward)
return
if event.key == 'down':
if is_RiskyPathEnv:
step(env.new_actions.south)
return
# Spacebar
if event.key == ' ':
step(env.actions.toggle)
return
if event.key == 'pageup':
step(env.actions.pickup)
return
if event.key == 'pagedown':
step(env.actions.drop)
return
if event.key == 'enter':
step(env.actions.done)
return
parser = argparse.ArgumentParser()
parser.add_argument(
"--env",
help="gym environment to load",
default='MiniGrid-RiskyPath-v0'
)
parser.add_argument(
"--seed",
type=int,
help="random seed to generate the environment with",
default=-1
)
parser.add_argument(
"--tile_size",
type=int,
help="size at which to render tiles",
default=32
)
parser.add_argument(
'--agent_view',
default=False,
help="draw what the agent sees (partially observable view)",
action='store_true'
)
# modifications by Tilio Schulze
parser.add_argument(
"--spiky_active",
default=False,
help="if set, spiky tiles will be set",
action='store_true'
)
parser.add_argument(
"--wall_rebound",
default=False,
help="if set, the agent can rebound on walls",
action='store_true'
)
parser.add_argument(
"--slip_proba",
default=0.,
type=float,
help="sets the agent's probability of slipping"
)
parser.add_argument(
"--show_agent_dir",
default=False,
help="Whether or not the direction of the agent is to be shown",
action="store_true"
)
parser.add_argument(
"--wrap_IM",
help="Will wrap environment with the 'IntrinsicMotivationWrapper'",
action='store_true'
)
parser.add_argument(
"--wrap_randomize",
help="Will wrap environment with RandomizeGoalWrapper",
action='store_true'
)
parser.add_argument(
"--test_eval_randomizer",
help="wrap env on the hard-coded goal randomization",
action='store_true'
)
reward_model= {
STEP_PENALTY : 0,
GOAL_REWARD : 1,
ABSORBING_STATES : False,
ABSORBING_REWARD_GOAL : 0,
ABSORBING_REWARD_LAVA : 0,
SPIKY_TILE_REWARD : 0,
LAVA_REWARD : -1
}
args = parser.parse_args()
env = gym.make(
args.env,
spiky_active=args.spiky_active,
wall_rebound=args.wall_rebound,
slip_proba=args.slip_proba,
reward_spec=reward_model,
show_agent_dir=args.show_agent_dir
)
is_RiskyPathEnv = True if "RiskyPath" in args.env else False
if args.agent_view:
env = RGBImgPartialObsWrapper(env)
env = ImgObsWrapper(env)
if args.wrap_IM:
env = TensorObsWrapper(env)
env = IntrinsicMotivationWrapper(env, 100, stop_after_n_steps=30)
if args.wrap_randomize:
env = RandomizeGoalWrapper(env, randomization=0.5)
if args.test_eval_randomizer:
env = RandomizeGoalWrapper(env, eval_mode=True)
window = Window('gym_minigrid - ' + args.env)
window.reg_key_handler(key_handler)
reset()
# Blocking event loop
window.show(block=True)