-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfind_play_param_alphazero.py
More file actions
134 lines (105 loc) · 3.2 KB
/
find_play_param_alphazero.py
File metadata and controls
134 lines (105 loc) · 3.2 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
from env import ConnectFour, MCTS
from models import AlphaZeroResNet
import numpy as np
import torch
import time
import os
import copy
import random
def battle(k1, k2, mctss, result):
# player1: mcts1, player -1: mcts2
mcts1 = mctss[k1]
mcts2 = mctss[k2]
outcome = []
for start_player in [1,-1]:
player = start_player
state = CF.get_initial_state()
while True:
if player == -1:
neutral_state = CF.change_perspective(state, player)
mcts_probs = mcts2.search(neutral_state)
print("player -1:",mcts_probs)
# time.sleep(5)
action = np.argmax(mcts_probs)
elif player == 1:
neutral_state = CF.change_perspective(state, player)
mcts_probs = mcts1.search(neutral_state)
print("player 1:",mcts_probs)
# time.sleep(5)
action = np.argmax(mcts_probs)
state = CF.get_next_state(state, action, player)
value, is_terminal = CF.get_value_and_terminated(state, action)
if is_terminal:
print(state)
if value == 1:
print(player, "won")
if player == -1:
outcome.append(-1)
else: outcome.append(1)
else:
print("draw")
outcome.append(0)
break
player = CF.get_opponent(player)
if sum(outcome) == 2:
result[k1] += 3
result[k2] -= 3
log = "2:0"
elif sum(outcome) == -2:
result[k1] -= 3
result[k2] += 3
log = "0:2"
elif sum(outcome) == 1:
result[k1] += 2
result[k2] -= 2
log = "1:0"
elif sum(outcome) == -1:
result[k1] -= 2
result[k2] += 2
log = "0:1"
elif outcome == [0,0]:
result[k1] += 1
result[k2] += 1
log = "0:0"
else:
log = "1:1"
print("{} vs {}: ".format(k1,k2)+log)
print(result)
return result
# 모델을 로드한다
CF = ConnectFour()
folder_path = "model/alphazero/"
print("what the...")
num_battles = 55
nb, hl, model_name = 5, 128,'model_16/model_16_iter_3.pth'
args = {
'C': 2,
'num_searches': 100,
'dirichlet_epsilon': 0.,
'dirichlet_alpha': 0.3
}
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
model = AlphaZeroResNet(nb, hl).to(device)
model.load_state_dict(torch.load(folder_path+model_name, map_location=device))
model.eval()
# 경기결과
result = {}
# 풀을 만든다
mctss = {}
Cs = [i/10 for i in range(5,30)]
for c in Cs:
new_args = copy.deepcopy(args)
new_args['C'] = c
mctss[c] = MCTS(CF, new_args, model)
result[c] = 0
# 랜덤으로 뽑아서 대결을 한다
for i in range(num_battles):
keys = list(mctss.keys())
k1, k2 = random.sample(keys,2)
print(i, k1, k2)
# k1, k2가 배틀을 함
result = battle(k1,k2,mctss,result)
# 점수 순으로 나열한다.
print(result)
sorted_result = sorted(result.items(), key=lambda x: -x[1])
print(sorted_result)