forked from yl573/BetaGo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_selfplay.py
114 lines (86 loc) · 2.51 KB
/
test_selfplay.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
from MCTS import MCTS
import numpy as np
from Shared.Consts import BLACK, WHITE
from Shared.Functions import xy_to_index
from GoGame.GoSimulator import GoSimulator
import random
import time
def addBoardtoBoards(board,boards,turn_counter):
m = boards.shape[1]
board_temp = np.reshape(board,-1)
boards_temp = np.reshape(boards,-1)
new_boards_temp = np.append(boards,board)
new_boards = np.reshape(new_boards_temp, [turn_counter,m,m])
return new_boards
def switchTurn(player):
if player == WHITE:
return BLACK
elif player == BLACK:
return WHITE
class Model:
def eval(self, board):
P = np.ones(26)/26
V = 0.1
return P, V
model = Model()
game = GoSimulator(5)
boards = np.array([[
[0,0,0,0,0],
[0,0,0,0,0],
[0,0,0,0,0],
[0,0,0,0,0],
[0,0,0,0,0]
],[
[0,0,0,0,0],
[0,0,0,0,0],
[0,0,0,0,0],
[0,0,0,0,0],
[0,0,0,0,0]
]])
player = BLACK
game.set_board_from_prev_boards(boards, player)
# game.print_board()
# or if there is no KO, you can use
# game.set_board(boards[-1], BLACK)
print('+--------- START ---------+:\n')
turn_counter = 2
check_pass = 0
### --- Start game --- ###
mcts = MCTS(model, player, start_boards=boards)
while True:
pi = mcts.search_for_pi(iterations=1)
# Find position of next play that maximises pi
arg_pi_max = (np.argwhere(pi==np.max(pi)))
arg_pi_max = arg_pi_max.flatten()
arg_max = random.choice(arg_pi_max) # to deal with multiple maximums
# print('arg_max ',arg_max)
mcts.set_move(arg_max)
if arg_max == 25: # PASS
game.pass_move()
check_pass = check_pass + 1
print('Passed')
else:
move_y, move_x = divmod(arg_max, 5) # finds position of move on board and makes play
# print('Best move x:', move_x)
# print('Best move y:', move_y,'\n')
# print('pi: ', pi)
print('move: ',move_x, move_y)
# # Plays the best move
print(game.board)
board, next_player = game.play(move_x,move_y)
check_pass = 0
turn_counter = turn_counter + 1
# Add board to boards
# boards = addBoardtoBoards(board,boards,turn_counter)
# player = next_player
# game.set_board_from_prev_boards(boards, player)
# game.print_board()
# black_lead = game.black_score_lead()
# Check win condition
if check_pass >=2:
break
if turn_counter == 5 * 5 * 2:
break
# time.sleep(0.001)
black_lead = game.black_score_lead()
print('black lead: ', black_lead)