-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathenvironment.py
More file actions
167 lines (151 loc) · 4.75 KB
/
environment.py
File metadata and controls
167 lines (151 loc) · 4.75 KB
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
import numpy as np
import cv2
import gymnasium as gym
from collections import deque
from utils import preprocess
class AtariEnv:
def __init__(
self,
env_id,
shape=(64, 64),
repeat=4,
clip_rewards=False,
no_ops=0,
fire_first=False,
):
base_env = gym.make(env_id, render_mode="rgb_array")
env = RepeatActionAndMaxFrame(
base_env, repeat, clip_rewards, no_ops, fire_first
)
env = PreprocessFrame(env, shape)
env = StackFrames(env, repeat)
self.env = env
def make(self):
return self.env
class RepeatActionAndMaxFrame(gym.Wrapper):
def __init__(self, env, repeat=4, clip_reward=True, no_ops=0, fire_first=False):
super().__init__(env)
self.repeat = repeat
self.clip_reward = clip_reward
self.no_ops = no_ops
self.fire_first = fire_first
self.frame_buffer = np.zeros(
(2, *env.observation_space.shape), dtype=np.float32
)
def step(self, action):
total_reward = 0
term, trunc = False, False
for i in range(self.repeat):
state, reward, term, trunc, info = self.env.step(action)
if self.clip_reward:
reward = np.clip(reward, -1, 1)
total_reward += reward
self.frame_buffer[i % 2] = state
if term or trunc:
break
max_frame = np.maximum(self.frame_buffer[0], self.frame_buffer[1])
return max_frame, total_reward, term, trunc, info
def reset(self, seed=None, options=None):
state, info = self.env.reset(seed=seed, options=options)
no_ops = np.random.randint(self.no_ops) + 1 if self.no_ops > 0 else 0
for _ in range(no_ops):
_, _, term, trunc, info = self.env.step(0)
if term or trunc:
state, info = self.env.reset()
if self.fire_first:
assert self.env.unwrapped.get_action_meanings()[1] == "FIRE"
state, _, term, trunc, info = self.env.step(1)
self.frame_buffer = np.zeros(
(2, *self.env.observation_space.shape), dtype=np.float32
)
self.frame_buffer[0] = state
return state, info
class PreprocessFrame(gym.ObservationWrapper):
def __init__(self, env, shape=(64, 64)):
super().__init__(env)
self.shape = shape
self.observation_space = gym.spaces.Box(0.0, 1.0, self.shape, dtype=np.float32)
def observation(self, state):
state = cv2.cvtColor(state, cv2.COLOR_RGB2GRAY)
state = cv2.resize(state, self.shape, interpolation=cv2.INTER_AREA)
return preprocess(state)
class StackFrames(gym.ObservationWrapper):
def __init__(self, env, size=4):
super().__init__(env)
self.size = int(size)
self.stack = deque([], maxlen=self.size)
shape = self.env.observation_space.shape
self.observation_space = gym.spaces.Box(
0.0, 1.0, (self.size, *shape), dtype=np.float32
)
def reset(self, seed=None, options=None):
state, info = self.env.reset(seed=seed, options=options)
self.stack = deque([state] * self.size, maxlen=self.size)
return np.array(self.stack), info
def observation(self, state):
self.stack.append(state)
return np.array(self.stack)
ENV_LIST = [
"ALE/Adventure-v5",
"ALE/AirRaid-v5",
"ALE/Alien-v5",
"ALE/Amidar-v5",
"ALE/Assault-v5",
"ALE/Asterix-v5",
"ALE/Asteroids-v5",
"ALE/Atlantis-v5",
"ALE/BankHeist-v5",
"ALE/BattleZone-v5",
"ALE/BeamRider-v5",
"ALE/Berzerk-v5",
"ALE/Bowling-v5",
"ALE/Boxing-v5",
"ALE/Breakout-v5",
"ALE/Carnival-v5",
"ALE/Centipede-v5",
"ALE/ChopperCommand-v5",
"ALE/CrazyClimber-v5",
"ALE/Defender-v5",
"ALE/DemonAttack-v5",
"ALE/DoubleDunk-v5",
"ALE/ElevatorAction-v5",
"ALE/Enduro-v5",
"ALE/FishingDerby-v5",
"ALE/Freeway-v5",
"ALE/Frostbite-v5",
"ALE/Gopher-v5",
"ALE/Gravitar-v5",
"ALE/Hero-v5",
"ALE/IceHockey-v5",
"ALE/Jamesbond-v5",
"ALE/JourneyEscape-v5",
"ALE/Kangaroo-v5",
"ALE/Krull-v5",
"ALE/KungFuMaster-v5",
"ALE/MontezumaRevenge-v5",
"ALE/MsPacman-v5",
"ALE/NameThisGame-v5",
"ALE/Phoenix-v5",
"ALE/Pitfall-v5",
"ALE/Pong-v5",
"ALE/Pooyan-v5",
"ALE/PrivateEye-v5",
"ALE/Qbert-v5",
"ALE/Riverraid-v5",
"ALE/RoadRunner-v5",
"ALE/Robotank-v5",
"ALE/Seaquest-v5",
"ALE/Skiing-v5",
"ALE/Solaris-v5",
"ALE/SpaceInvaders-v5",
"ALE/StarGunner-v5",
"ALE/Tennis-v5",
"ALE/TimePilot-v5",
"ALE/Tutankham-v5",
"ALE/UpNDown-v5",
"ALE/Venture-v5",
"ALE/VideoPinball-v5",
"ALE/WizardOfWor-v5",
"ALE/YarsRevenge-v5",
"ALE/Zaxxon-v5",
]