-
Notifications
You must be signed in to change notification settings - Fork 0
/
pgqvrand.py
239 lines (201 loc) · 7.42 KB
/
pgqvrand.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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
import warnings
import pandas as pd
import matplotlib.pyplot as plt
from collections import Counter
import numpy as np
import tensorflow as tf
from common.benchmark import GameRate
import pickle as p
from common.game import Game
from common.dnn import ActorNetwork, CriticNetwork, PlayerTrainer
warnings.filterwarnings("ignore")
'''
This file contains the gameplay, passing state to the neural network, and receiving actions.
See common/game.py for the game environment.
See common/dnn.py for the neural network and training objects
'''
# ==========================
# Training Parameters
# ==========================
# Number of test episodes
TEST_EPISODES = 500
# Max training steps
MAX_EPISODES = 20000
# Max episode length
MAX_EP_STEPS = 100
# Base learning rate for the Actor network
ACTOR_LEARNING_RATE = 0.000001
# Base learning rate for the Critic Network
CRITIC_LEARNING_RATE = 0.00001
# Discount factor
GAMMA = 0.4
# Soft target update param
TAU = 0.5
# ===========================
# Utility Parameters
# ===========================
RANDOM_SEED = 1234
# Size of replay buffer
BUFFER_SIZE = 1000
MINIBATCH_SIZE = 128
def train(sess, a1, c1, scaler):
game = Game(verbose=False)
player1 = PlayerTrainer(actor=a1, critic=c1, buffersize=BUFFER_SIZE, game=game, player=1, batch_size=MINIBATCH_SIZE,
gamma=GAMMA)
sess.run(tf.global_variables_initializer())
# Initialize target network weights
a1.update_target_network()
c1.update_target_network()
episode = 0
all_wins = []
all_logs = []
win_p1, comp1, bloc1 = 0, 0, 0
win_p2, comp2, bloc2 = 0, 0, 0
stat = []
for i in range(MAX_EPISODES):
episode += 1
game.setup()
ep_reward = 0
ep_reward2 = 0
reward2 = 0
terminal = False
for j in range(MAX_EP_STEPS):
if not terminal:
if episode < 7500:
move = game.random_space()
game.move(move, 1)
state, reward = game.step(player=1)
else:
state, reward = player1.noisyMaxQMove()
_, reward2 = game.step(player=2)
ep_reward += reward
ep_reward2 += reward2
terminal = game.game_over
if terminal:
all_wins.append(game.game_over)
log = game.setup()
s = game.space
all_logs.append(log)
print(scaler, win_p1, comp1, bloc1, win_p2, comp2, bloc2, " | Episode", i, ep_reward, ep_reward2)
if episode % 1000 == 0:
win_p1, comp1, bloc1, win_p2, comp2, bloc2 = test(sess, a1)
stat.append([episode, win_p1, comp1, bloc1, win_p2, comp2, bloc2])
df = pd.DataFrame(stat)
print(df)
plt.close('all')
xwinp = plt.plot(df[0], df[1], label="P1wins")
xcomp = plt.plot(df[0], df[2], label="P1Imm Compl")
xbloc = plt.plot(df[0], df[3], label="p1immbloc")
xwinp2 = plt.plot(df[0], df[4], label="P2wins")
xcomp2 = plt.plot(df[0], df[5], label="P2Imm Compl")
xbloc2 = plt.plot(df[0], df[6], label="p2immbloc")
plt.legend()
plt.ylim(0, 1)
plt.ylabel('percent')
plt.show(block=False)
break
else:
move = game.random_space()
game.move(move, 2)
_, reward = game.step(player=1)
terminal = game.game_over
ep_reward2 += reward2
ep_reward += reward
return stat
def test(sess, actor1):
game = Game(verbose=False)
logs = []
wins = []
for i in range(TEST_EPISODES):
game.setup()
s = game.space
terminal = False
for j in range(MAX_EP_STEPS):
if not terminal:
a = actor1.predict(np.reshape(game.space, (1, *s.shape)))
avail = game.avail()
# noinspection PyPep8Naming
availQ = {}
for x in avail:
availQ[x] = a[0][x]
action = max(availQ, key=availQ.get)
game.move(action, 1)
s2, r = game.step(1)
terminal = game.game_over
info = None
if terminal:
wins.append(game.game_over)
log = game.setup()
logs.append(log)
s = game.space
break
else:
action = game.random_space()
game.move(action, 2)
s2, r = game.step(1)
terminal = game.game_over
info = None
c = Counter(wins)
r = GameRate(verbose=False, list=logs, player=1, opponent=2)
r2 = GameRate(verbose=False, list=logs, player=2, opponent=1)
bloc1, bloc2 = 0, 0
r.check_games()
r2.check_games()
win_p1 = c[1] / (TEST_EPISODES - 1)
print("1win percentage", win_p1)
if r.completions + r.missed_completions > 0:
comp1 = r.completions / (r.completions + r.missed_completions)
else:
comp1 = 0
print("1immediate completions", comp1)
if r.blocks + r.missed_blocks > 0:
bloc1 = r.blocks / (r.blocks + r.missed_blocks)
win_p2 = c[2] / (TEST_EPISODES - 1)
print("2win percentage", win_p2)
if r2.completions + r2.missed_completions > 0:
comp2 = r2.completions / (r2.completions + r2.missed_completions)
else:
comp2 = 0
print("2immediate completions", comp2)
if r2.blocks + r2.missed_blocks > 0:
bloc2 = r2.blocks / (r2.blocks + r2.missed_blocks)
return win_p1, comp1, bloc1, win_p2, comp2, bloc2
def test_log(wins, log):
c = Counter(wins)
r = GameRate(verbose=False, list=log)
r.check_games()
win_p = c[1] / (TEST_EPISODES - 1)
print("win percentage", win_p)
comp = r.completions / (r.completions + r.missed_completions)
print("immediate completions", comp)
return win_p, comp
def main(_):
with tf.Session() as sess:
np.random.seed(RANDOM_SEED)
tf.set_random_seed(RANDOM_SEED)
state_dim = 27
action_dim = 27
action_bound = 27
# Ensure action bound is symmetric
results = []
tot_stat = []
for i in range(10):
p1 = dict()
p1["a"] = ActorNetwork(sess, state_dim, action_dim, action_bound,
ACTOR_LEARNING_RATE, TAU, vscope="p1a")
p1["c"] = CriticNetwork(sess, state_dim, action_dim,
CRITIC_LEARNING_RATE, TAU, p1["a"].get_num_trainable_vars(), vscope="p1c")
# p2 = {}
# p2["a"] = ActorNetwork(sess, state_dim, action_dim, action_bound,
# ACTOR_LEARNING_RATE, TAU, vscope="p2a")
# p2["c"] = CriticNetwork(sess, state_dim, action_dim,
# CRITIC_LEARNING_RATE, TAU, p2["a"].get_num_trainable_vars(), vscope="p2c")
stat = train(sess, p1["a"], p1["c"], i) # , p2["a"], p2["c"])
tot_stat.append(stat)
# results.append([win_p,comp,epi])
with open('data/pgvrdd7500', 'wb') as f:
p.dump(tot_stat, f)
# print('done',results)
# print('stats',tot_stat)
if __name__ == '__main__':
tf.app.run()