-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathalphazero_new.py
More file actions
549 lines (418 loc) · 19.8 KB
/
alphazero_new.py
File metadata and controls
549 lines (418 loc) · 19.8 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
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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
import numpy as np
print(np.__version__)
import torch
print(torch.__version__)
import torch.nn as nn
import torch.nn.functional as F
torch.manual_seed(0)
import random
import math
class ConnectFour:
def __init__(self):
self.row_count = 6
self.column_count = 7
self.action_size = self.column_count
self.in_a_row = 4
def __repr__(self):
return "ConnectFour"
def get_initial_state(self):
return np.zeros((self.row_count, self.column_count))
def get_next_state(self, state, action, player):
row = np.max(np.where(state[:, action] == 0))
state[row, action] = player
return state
def get_valid_moves(self, state):
return (state[0] == 0).astype(np.uint8)
def check_win(self, state, action):
if action == None:
return False
row = np.min(np.where(state[:, action] != 0))
column = action
player = state[row][column]
def count(offset_row, offset_column):
for i in range(1, self.in_a_row):
r = row + offset_row * i
c = action + offset_column * i
if (
r < 0
or r >= self.row_count
or c < 0
or c >= self.column_count
or state[r][c] != player
):
return i - 1
return self.in_a_row - 1
return (
count(1, 0) >= self.in_a_row - 1 # vertical
or (count(0, 1) + count(0, -1)) >= self.in_a_row - 1 # horizontal
or (count(1, 1) + count(-1, -1)) >= self.in_a_row - 1 # top left diagonal
or (count(1, -1) + count(-1, 1)) >= self.in_a_row - 1 # top right diagonal
)
def get_value_and_terminated(self, state, action):
if self.check_win(state, action):
return 1, True
if np.sum(self.get_valid_moves(state)) == 0:
return 0, True
return 0, False
def get_opponent(self, player):
return -player
def get_opponent_value(self, value):
return -value
def change_perspective(self, state, player):
return state * player
def get_encoded_state(self, state):
encoded_state = np.stack(
(state == -1, state == 0, state == 1)
).astype(np.float32)
if len(state.shape) == 3:
encoded_state = np.swapaxes(encoded_state, 0, 1)
return encoded_state
class ResNet(nn.Module):
def __init__(self, game, num_resBlocks, num_hidden, device):
super().__init__()
self.device = device
self.startBlock = nn.Sequential(
nn.Conv2d(3, num_hidden, kernel_size=3, padding=1),
nn.BatchNorm2d(num_hidden),
nn.ReLU()
)
self.backBone = nn.ModuleList(
[ResBlock(num_hidden) for i in range(num_resBlocks)]
)
self.policyHead = nn.Sequential(
nn.Conv2d(num_hidden, 32, kernel_size=3, padding=1),
nn.BatchNorm2d(32),
nn.ReLU(),
nn.Flatten(),
nn.Linear(32 * game.row_count * game.column_count, game.action_size)
)
self.valueHead = nn.Sequential(
nn.Conv2d(num_hidden, 3, kernel_size=3, padding=1),
nn.BatchNorm2d(3),
nn.ReLU(),
nn.Flatten(),
nn.Linear(3 * game.row_count * game.column_count, 1),
nn.Tanh()
)
self.to(device)
def forward(self, x):
x = self.startBlock(x)
for resBlock in self.backBone:
x = resBlock(x)
policy = self.policyHead(x)
value = self.valueHead(x)
return policy, value
class ResBlock(nn.Module):
def __init__(self, num_hidden):
super().__init__()
self.conv1 = nn.Conv2d(num_hidden, num_hidden, kernel_size=3, padding=1)
self.bn1 = nn.BatchNorm2d(num_hidden)
self.conv2 = nn.Conv2d(num_hidden, num_hidden, kernel_size=3, padding=1)
self.bn2 = nn.BatchNorm2d(num_hidden)
def forward(self, x):
residual = x
x = F.relu(self.bn1(self.conv1(x)))
x = self.bn2(self.conv2(x))
x += residual
x = F.relu(x)
return x
import matplotlib.pyplot as plt
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
class Node:
def __init__(self, game, args, state, parent=None, action_taken=None, prior=0, visit_count=0):
self.game = game
self.args = args
self.state = state
self.parent = parent
self.action_taken = action_taken
self.prior = prior
self.children = []
self.visit_count = visit_count
self.value_sum = 0
def is_fully_expanded(self):
return len(self.children) > 0
def select(self):
best_child = None
best_ucb = -np.inf
for child in self.children:
ucb = self.get_ucb(child)
if ucb > best_ucb:
best_child = child
best_ucb = ucb
return best_child
def get_ucb(self, child):
if child.visit_count == 0:
q_value = 0
else:
q_value = 1 - ((child.value_sum / child.visit_count) + 1) / 2
return q_value + self.args['C'] * (math.sqrt(self.visit_count) / (child.visit_count + 1)) * child.prior
def expand(self, policy):
for action, prob in enumerate(policy):
if prob > 0:
child_state = self.state.copy()
child_state = self.game.get_next_state(child_state, action, 1)
child_state = self.game.change_perspective(child_state, player=-1)
child = Node(self.game, self.args, child_state, self, action, prob)
self.children.append(child)
return child
def backpropagate(self, value):
self.value_sum += value
self.visit_count += 1
value = self.game.get_opponent_value(value)
if self.parent is not None:
self.parent.backpropagate(value)
class MCTS:
def __init__(self, game, args, model):
self.game = game
self.args = args
self.model = model
@torch.no_grad()
def search(self, state):
root = Node(self.game, self.args, state, visit_count=1)
policy, _ = self.model(
torch.tensor(self.game.get_encoded_state(state), device=self.model.device).unsqueeze(0)
)
policy = torch.softmax(policy, axis=1).squeeze(0).cpu().numpy()
policy = (1 - self.args['dirichlet_epsilon']) * policy + self.args['dirichlet_epsilon'] \
* np.random.dirichlet([self.args['dirichlet_alpha']] * self.game.action_size)
valid_moves = self.game.get_valid_moves(state)
policy *= valid_moves
policy /= np.sum(policy)
root.expand(policy)
for search in range(self.args['num_searches']):
node = root
while node.is_fully_expanded():
node = node.select()
value, is_terminal = self.game.get_value_and_terminated(node.state, node.action_taken)
value = self.game.get_opponent_value(value)
if not is_terminal:
policy, value = self.model(
torch.tensor(self.game.get_encoded_state(node.state), device=self.model.device).unsqueeze(0)
)
policy = torch.softmax(policy, axis=1).squeeze(0).cpu().numpy()
valid_moves = self.game.get_valid_moves(node.state)
policy *= valid_moves
policy /= np.sum(policy)
value = value.item()
node.expand(policy)
node.backpropagate(value)
action_probs = np.zeros(self.game.action_size)
for child in root.children:
action_probs[child.action_taken] = child.visit_count
action_probs /= np.sum(action_probs)
return action_probs
class MCTSParallel:
def __init__(self, game, args, model):
self.game = game
self.args = args
self.model = model
@torch.no_grad()
def search(self, states, spGames):
# print(self.game.get_encoded_state(states).shape)
policy, _ = self.model(
torch.tensor(self.game.get_encoded_state(states), device=self.model.device)
)
policy = torch.softmax(policy, axis=1).cpu().numpy()
policy = (1 - self.args['dirichlet_epsilon']) * policy + self.args['dirichlet_epsilon'] \
* np.random.dirichlet([self.args['dirichlet_alpha']] * self.game.action_size, size=policy.shape[0])
for i, spg in enumerate(spGames):
spg_policy = policy[i]
valid_moves = self.game.get_valid_moves(states[i])
spg_policy *= valid_moves
spg_policy /= np.sum(spg_policy)
spg.root = Node(self.game, self.args, states[i], visit_count=1)
spg.root.expand(spg_policy)
for search in range(self.args['num_searches']):
for spg in spGames:
spg.node = None
node = spg.root
while node.is_fully_expanded():
node = node.select()
value, is_terminal = self.game.get_value_and_terminated(node.state, node.action_taken)
value = self.game.get_opponent_value(value)
if is_terminal:
node.backpropagate(value)
else:
spg.node = node
expandable_spGames = [mappingIdx for mappingIdx in range(len(spGames)) if spGames[mappingIdx].node is not None]
if len(expandable_spGames) > 0:
states = np.stack([spGames[mappingIdx].node.state for mappingIdx in expandable_spGames])
policy, value = self.model(
torch.tensor(self.game.get_encoded_state(states), device=self.model.device)
)
policy = torch.softmax(policy, axis=1).cpu().numpy()
value = value.cpu().numpy()
for i, mappingIdx in enumerate(expandable_spGames):
node = spGames[mappingIdx].node
spg_policy, spg_value = policy[i], value[i]
valid_moves = self.game.get_valid_moves(node.state)
spg_policy *= valid_moves
spg_policy /= np.sum(spg_policy)
node.expand(spg_policy)
node.backpropagate(spg_value)
class AlphaZero:
def __init__(self, model, optimizer, game, args):
self.model = model
self.optimizer = optimizer
self.game = game
self.args = args
self.mcts = MCTS(game, args, model)
def selfPlay(self):
memory = []
player = 1
state = self.game.get_initial_state()
while True:
neutral_state = self.game.change_perspective(state, player)
action_probs = self.mcts.search(neutral_state)
memory.append((neutral_state, action_probs, player))
temperature_action_probs = action_probs ** (1 / self.args['temperature'])
action = np.random.choice(self.game.action_size, p=action_probs) # change to temperature_action_probs
state = self.game.get_next_state(state, action, player)
value, is_terminal = self.game.get_value_and_terminated(state, action)
if is_terminal:
returnMemory = []
for hist_neutral_state, hist_action_probs, hist_player in memory:
hist_outcome = value if hist_player == player else self.game.get_opponent_value(value)
returnMemory.append((
self.game.get_encoded_state(hist_neutral_state),
hist_action_probs,
hist_outcome
))
return returnMemory
player = self.game.get_opponent(player)
def train(self, memory):
random.shuffle(memory)
for batchIdx in range(0, len(memory), self.args['batch_size']):
sample = memory[batchIdx:min(len(memory) - 1, batchIdx + self.args['batch_size'])] # Change to memory[batchIdx:batchIdx+self.args['batch_size']] in case of an error
state, policy_targets, value_targets = zip(*sample)
state, policy_targets, value_targets = np.array(state), np.array(policy_targets), np.array(value_targets).reshape(-1, 1)
state = torch.tensor(state, dtype=torch.float32, device=self.model.device)
policy_targets = torch.tensor(policy_targets, dtype=torch.float32, device=self.model.device)
value_targets = torch.tensor(value_targets, dtype=torch.float32, device=self.model.device)
out_policy, out_value = self.model(state)
policy_loss = F.cross_entropy(out_policy, policy_targets)
value_loss = F.mse_loss(out_value, value_targets)
loss = policy_loss + value_loss
self.optimizer.zero_grad() # change to self.optimizer
loss.backward()
self.optimizer.step() # change to self.optimizer
def learn(self):
for iteration in range(self.args['num_iterations']):
memory = []
self.model.eval()
for selfPlay_iteration in range(self.args['num_selfPlay_iterations']):
print("num_selfplay_iterations: {}/{}".format(\
selfPlay_iteration, self.args['num_selfPlay_iterations']
))
memory += self.selfPlay()
self.model.train()
for epoch in range(self.args['num_epochs']):
print("num_epochs: {}/{}".format(\
epoch, self.args['num_epochs']
))
self.train(memory)
torch.save(self.model.state_dict(), f"model_{iteration}_{self.game}.pt")
torch.save(self.optimizer.state_dict(), f"optimizer_{iteration}_{self.game}.pt")
class AlphaZeroParallel:
def __init__(self, model, optimizer, game, args):
self.model = model
self.optimizer = optimizer
self.game = game
self.args = args
self.mcts = MCTSParallel(game, args, model)
def selfPlay(self):
return_memory = []
player = 1
spGames = [SPG(self.game) for spg in range(self.args['num_parallel_games'])]
while len(spGames) > 0:
states = np.stack([spg.state for spg in spGames])
neutral_states = self.game.change_perspective(states, player)
self.mcts.search(neutral_states, spGames)
for i in range(len(spGames))[::-1]:
spg = spGames[i]
action_probs = np.zeros(self.game.action_size)
for child in spg.root.children:
action_probs[child.action_taken] = child.visit_count
action_probs /= np.sum(action_probs)
spg.memory.append((spg.root.state, action_probs, player))
temperature_action_probs = action_probs ** (1 / self.args['temperature'])
temperature_action_probs /= np.sum(temperature_action_probs) # Divide temperature_action_probs with its sum in case of an error
action = np.random.choice(self.game.action_size, p=temperature_action_probs) # Divide temperature_action_probs with its sum in case of an error
spg.state = self.game.get_next_state(spg.state, action, player)
value, is_terminal = self.game.get_value_and_terminated(spg.state, action)
if is_terminal:
for hist_neutral_state, hist_action_probs, hist_player in spg.memory:
hist_outcome = value if hist_player == player else self.game.get_opponent_value(value)
return_memory.append((
self.game.get_encoded_state(hist_neutral_state),
hist_action_probs,
hist_outcome
))
del spGames[i]
player = self.game.get_opponent(player)
return return_memory
def train(self, memory):
random.shuffle(memory)
for batchIdx in range(0, len(memory), self.args['batch_size']):
sample = memory[batchIdx:min(len(memory) - 1, batchIdx + self.args['batch_size'])] # Change to memory[batchIdx:batchIdx+self.args['batch_size']] in case of an error
state, policy_targets, value_targets = zip(*sample)
state, policy_targets, value_targets = np.array(state), np.array(policy_targets), np.array(value_targets).reshape(-1, 1)
state = torch.tensor(state, dtype=torch.float32, device=self.model.device)
policy_targets = torch.tensor(policy_targets, dtype=torch.float32, device=self.model.device)
value_targets = torch.tensor(value_targets, dtype=torch.float32, device=self.model.device)
out_policy, out_value = self.model(state)
policy_loss = F.cross_entropy(out_policy, policy_targets)
value_loss = F.mse_loss(out_value, value_targets)
loss = policy_loss + value_loss
self.optimizer.zero_grad()
loss.backward()
self.optimizer.step()
def learn(self):
for iteration in range(self.args['num_iterations']):
print("num_iterations: {}/{}".format(\
iteration+1, self.args['num_iterations']
))
memory = []
self.model.eval()
for selfPlay_iteration in range(self.args['num_selfPlay_iterations'] // self.args['num_parallel_games']):
print(" num_selfplay_iterations: {}/{}".format(\
selfPlay_iteration+1, self.args['num_selfPlay_iterations'] // self.args['num_parallel_games']
))
memory += self.selfPlay()
self.model.train()
for epoch in range(self.args['num_epochs']):
print(" num_epochs: {}/{}".format(\
epoch+1, self.args['num_epochs']
))
self.train(memory)
torch.save(self.model.state_dict(), f"model_{iteration}_{self.game}.pth")
torch.save(self.optimizer.state_dict(), f"optimizer_{iteration}_{self.game}.pth")
class SPG:
def __init__(self, game):
self.state = game.get_initial_state()
self.memory = []
self.root = None
self.node = None
def main():
game = ConnectFour()
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
model = ResNet(game, 3, 64, device)
optimizer = torch.optim.Adam(model.parameters(), lr=0.001, weight_decay=0.0001)
args = {
'C': 2,
'num_searches': 200,
'num_iterations': 8,
'num_selfPlay_iterations': 300,
'num_parallel_games': 60,
'num_epochs': 5,
'batch_size': 64,
'temperature': 1.25,
'dirichlet_epsilon': 0.25,
'dirichlet_alpha': 0.3
}
# alphaZero = AlphaZero(model, optimizer, game, args)
# alphaZero.learn()
alphaZero = AlphaZeroParallel(model, optimizer, game, args)
alphaZero.learn()
if __name__ == "__main__":
main()