-
Notifications
You must be signed in to change notification settings - Fork 13
/
env_wrapper.py
200 lines (152 loc) · 5.83 KB
/
env_wrapper.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
"""
The Atari environment(env) wrapper. Some envs needed some configurations which you can find below.
"""
import gym
import numpy as np
from scipy.misc import imresize
class AtariWrapper():
def __init__(self, env):
self.env = env
self.observation_space = self.env.observation_space
self.reward_range = self.env.reward_range
self.metadata = self.env.metadata
self.spec = self.env.spec
def step(self, *args, **kwargs):
state, reward, done, info = self.env.step(*args, **kwargs)
info['org_obs'] = state
state = self.process_atari_image(state)
return state, reward, done, info
@property
def action_space(self):
return self.env.action_space
def close(self, *args, **kwargs):
return self.env.close(*args, **kwargs)
def render(self, mode='human', inspect=False, img=None):
if not inspect:
return self.env.render(mode)
else:
if mode == 'rgb_array':
return img
elif mode == 'human':
from gym.envs.classic_control import rendering
if self.env.env.viewer is None:
self.env.env.viewer = rendering.SimpleImageViewer()
self.env.env.viewer.imshow(img)
return self.env.env.viewer.isopen
def reset(self, inspect=False):
state = self.env.reset()
if inspect:
return self.process_atari_image(state), state
else:
return self.process_atari_image(state)
def seed(self, *args, **kwargs):
return self.env.seed(*args, **kwargs)
@staticmethod
def process_atari_image(img):
return imresize(img[5:195].mean(2), (80, 80)).astype(np.float32).reshape(1, 80, 80) / 255.0
class Crop35And195(AtariWrapper):
def __init__(self, env):
AtariWrapper.__init__(self, env)
@staticmethod
def process_atari_image(img):
return imresize(img[35:195].mean(2), (80, 80)).astype(np.float32).reshape(1, 80, 80) / 255.0
class Crop15And195(AtariWrapper):
def __init__(self, env):
AtariWrapper.__init__(self, env)
@staticmethod
def process_atari_image(img):
return imresize(img[:195].mean(2), (80, 80)).astype(np.float32).reshape(1, 80, 80) / 255.0
class PongWrapper(Crop35And195):
def __init__(self, env):
Crop35And195.__init__(self, env)
def step(self, action):
if action > 2:
raise Exception('Unknown Action')
if action == 1:
action = 4
elif action == 2:
action = 5
state, reward, done, info = self.env.step(action)
info['org_obs'] = state
state = self.process_atari_image(state)
return state, reward, done, info
@property
def action_space(self):
return gym.spaces.discrete.Discrete(3)
class SpaceInvaderWrapper(Crop15And195):
def __init__(self, env):
Crop15And195.__init__(self, env)
@property
def action_space(self):
return gym.spaces.discrete.Discrete(4)
class EnduroWrapper(AtariWrapper):
def __init__(self, env):
AtariWrapper.__init__(self, env)
@staticmethod
def process_atari_image(img):
return imresize(img[0:155, 10:].mean(2), (80, 80)).astype(np.float32).reshape(1, 80, 80) / 255.0
class BeamRiderWrapper(AtariWrapper):
def __init__(self, env):
AtariWrapper.__init__(self, env)
@staticmethod
def process_atari_image(img):
return imresize(img[30:180, 10:].mean(2), (80, 80)).astype(np.float32).reshape(1, 80, 80) / 255.0
class FreewayWrapper(AtariWrapper):
def __init__(self, env):
AtariWrapper.__init__(self, env)
@staticmethod
def process_atari_image(img):
return imresize(img[25:195, 10:].mean(2), (80, 80)).astype(np.float32).reshape(1, 80, 80) / 255.0
class BoxingWrapper(AtariWrapper):
def __init__(self, env):
AtariWrapper.__init__(self, env)
@staticmethod
def process_atari_image(img):
return imresize(img[15:180, 30:130].mean(2), (80, 80)).astype(np.float32).reshape(1, 80, 80) / 255.0
class BreakoutWrapper(Crop35And195):
def __init__(self, env):
Crop35And195.__init__(self, env)
class QbertWrapper(AtariWrapper):
def __init__(self, env):
AtariWrapper.__init__(self, env)
@staticmethod
def process_atari_image(img):
return imresize(img[30:190, 10:150].mean(2), (80, 80)).astype(np.float32).reshape(1, 80, 80) / 255.0
class BowlingWrapper(AtariWrapper):
def __init__(self, env):
AtariWrapper.__init__(self, env)
@property
def action_space(self):
return gym.spaces.discrete.Discrete(4)
@staticmethod
def process_atari_image(img):
return imresize(img[105:172, :].mean(2), (80, 80)).astype(np.float32).reshape(1, 80, 80) / 255.0
class ElevatorActionWrapper(AtariWrapper):
def __init__(self, env):
AtariWrapper.__init__(self, env)
def atari_wrapper(env_name):
x = env_name.lower()
x = x.split('deterministic')[0] if 'deterministic' in x else x.split('-')[0]
if x == 'pong':
env = PongWrapper(gym.make(env_name))
elif x == 'spaceinvaders':
env = SpaceInvaderWrapper(gym.make(env_name))
elif x == 'enduro':
env = EnduroWrapper(gym.make(env_name))
elif x == 'beamrider':
env = BeamRiderWrapper(gym.make(env_name))
elif x == 'freeway':
env = FreewayWrapper(gym.make(env_name))
elif x == 'boxing':
env = BoxingWrapper(gym.make(env_name))
elif x == 'breakout':
env = BreakoutWrapper(gym.make(env_name))
elif x == 'qbert':
env = QbertWrapper(gym.make(env_name))
elif x in ['bowling']:
env = BowlingWrapper(gym.make(env_name))
elif x in ['elevatoraction']:
env = ElevatorActionWrapper(gym.make(env_name))
else:
env = AtariWrapper(gym.make(env_name))
return env