-
Notifications
You must be signed in to change notification settings - Fork 0
/
CompareToRandom.py
76 lines (62 loc) · 2.09 KB
/
CompareToRandom.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
import numpy as np
from keras.models import load_model
from keras.backend import clear_session
import go
from MCTS import get_action_coord
def random_action(state):
legal_actions = state.all_legal_moves()
sum_legal_actions = np.sum(legal_actions)
possibilities = legal_actions / sum_legal_actions
num = np.random.choice(len(legal_actions), p=possibilities) # if 9x9 -> 0 ~ 81
if num == len(legal_actions) - 1:
return None
row = num // go.N
column = num % go.N
coord = (row, column)
return coord
class Compare:
def __init__(self, model):
self.model = model
def play(self, black_action_fn, white_action_fn):
state: go.Position = go.Position()
while not state.is_game_over():
next_action = black_action_fn if state.to_play == 1 else white_action_fn
action = next_action(state)
state = state.play_move(action)
print(state)
result = state.result()
if result == 1: return 1
if result == 0: return 0.5
return 0
def compare(self):
action_model_fn = get_action_coord(self.model)
wins, draws, loses = 0, 0, 0
for i in range(10):
if i % 2 == 0:
result = self.play(action_model_fn, random_action)
if result == 1:
print("Win")
wins += 1
elif result == 0.5:
print("Draw")
draws += 1
else:
print("Lose")
loses += 1
else:
result = 1 - self.play(random_action, action_model_fn)
if result == 1:
print("Win")
wins += 1
elif result == 0.5:
print("Draw")
draws += 1
else:
print("Lose")
loses += 1
print(f"{wins}-{draws}-{loses}")
if __name__ == '__main__':
model = load_model('./model/trained.h5')
Compare(model).compare()
clear_session()
del model