-
Notifications
You must be signed in to change notification settings - Fork 1
/
retro_dueling.py
115 lines (102 loc) · 3.14 KB
/
retro_dueling.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @File : retro_dueling.py
# @Author: zixiao
# @Date : 2019-04-04
# @Desc :
from env.mario import env, actions, sample
from dueling.dueling_brain import Brain
import numpy as np
import PIL.Image as Image
def RGB2gray(obs):
img = Image.fromarray(obs).crop((0, 24, 240, 224)).resize((200, 200))
img = img.convert('L')
# img.show()
return np.asarray(img)
if __name__ == '__main__':
state = env.reset() # 224 240 3
state = RGB2gray(state)
frame_len = 4
memory_size = 10000
brain = Brain(memory_size=memory_size,
input_args=frame_len,
num_actions=7,
shape=state.shape,
learning_rate=0.00025,
reward_decay=0.99,
e_greedy=0.9,
e_greedy_increment=0.001,
e_greedy_start=0,
batch_size=32,
replace_target_iter=10000)
last_info = {
'time': 400,
'lives': 2,
'xscrollLo': 0
}
brain.store_start_frame(state)
for i in range(int(memory_size / 10) + 5):
act_index = sample()
obs, re, done, info = env.step(actions[act_index])
a = info['time'] - last_info['time']
b = info['lives'] - last_info['lives']
c = info['xscrollLo'] - last_info['xscrollLo']
if a != 0 and a != -1:
a = 0
if b != 0 and b != -1:
b = -1
if c < -20:
c = 0
b *= 15
reward = (a + b + c) / 15.0
print(act_index, reward, i)
if reward < -0.6:
print(last_info)
print(info)
if done:
obs = env.reset()
info['time'] = 0
info['lives'] = (info['lives'] - 1) % 3
info['xscrollLo'] = 0
last_info = info
obs = RGB2gray(obs)
env.render()
brain.store_transition(action=act_index, reward=re, obs_=obs)
step = 1
while True:
last_frame = brain.get_last_memory()
# get_gif(last_frame)
act_index = brain.choose_action(last_frame)
obs_, re, done, info = env.step(actions[act_index])
a = info['time'] - last_info['time']
b = info['lives'] - last_info['lives']
c = info['xscrollLo'] - last_info['xscrollLo']
if a != 0 and a != -1:
a = 0
if b != 0 and b != -1:
b = -1
if c < -20:
c = 0
b *= 15
reward = (a + b + c) / 15.0
print(act_index, reward, i)
if reward < -0.6:
print(last_info)
print(info)
if done:
obs_ = env.reset()
info['time'] = 0
info['lives'] = (info['lives'] - 1) % 3
info['xscrollLo'] = 0
obs_ = RGB2gray(obs_)
env.render()
print(act_index, reward, brain.epsilon, step)
if reward < -0.6:
print(reward)
print(last_info)
print(info)
brain.store_transition(action=act_index, reward=reward, obs_=obs_)
last_info = info
if step % 30 == 0:
brain.double_learn()
step += 1