-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
38 lines (35 loc) · 1.1 KB
/
utils.py
File metadata and controls
38 lines (35 loc) · 1.1 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
import numpy as np
import cv2
import gym
def preprocess_frame(frame):
frame = cv2.cvtColor(frame, cv2.COLOR_RGB2GRAY)
frame = cv2.resize(frame, (84, 84))
frame = frame.astype(np.float32) / 255.0
return frame
class SkipFrame(gym.Wrapper):
def __init__(self, env, skip=4):
"""跳幀 Wrapper,讓環境每隔 skip 幀進行一次動作選擇"""
super(SkipFrame, self).__init__(env)
self._skip = skip
def step(self, action):
"""執行動作並跳過多幀"""
total_reward = 0.0
done = False
for _ in range(self._skip):
obs, reward, done, info = self.env.step(action) # 執行動作
total_reward += reward # 累積獎勵
if done: # 如果遊戲結束,提前退出
break
return obs, total_reward, done, info
def reset(self, **kwargs):
"""重置環境"""
return self.env.reset(**kwargs)
CUSTOM_MOVEMENT = [
['NOOP'],
['right'],
['right', 'B'],
['right', 'A'],
['B', 'A'],
['right', 'B', 'A'],
['left']
]