-
Notifications
You must be signed in to change notification settings - Fork 0
/
testbed.py
170 lines (146 loc) · 5.43 KB
/
testbed.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
from env import *
from agents import *
from starts import *
from matplotlib import pyplot as plt
import os
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('--a', type=str, required=True) # agent type.
parser.add_argument('--t', type=str, required=True) # trainer type.
parser.add_argument('--o', type=str, required=True) # opponent type.
parser.add_argument('--s', type=str, required=True) # save directory location.
parser.add_argument('--ep', type=int, required=False) # save directory location.
parser.add_argument('--step', type=int, required=False) # save directory location.
# save directory argument
args = parser.parse_args()
"""
usage:
python testbed.py --a <agent> --t <trainer> --o <opponent> --s <path>
<agent>, choose {q,s}:
q --> q agent
s --> sarsa agent
<trainer>, choose {h,r}:
h --> heuristic trainer
r --> random trainer
<opponent>, choose {h,r}:
h --> heuristic trainer
r --> random trainer
<path>:
for loading model
path up until before models/logs
"""
ALPHA = 0.01
GAMMA = 1
EPS = 0.1
def play_game(black_agent, white_agent, board=None):
if board==None:
board=Board()
board=deepcopy(board)
#board.print_board()
while not board.game_ended():
black_current_state, black_legal_moves = board.get_current_state(), board.get_valid_moves(BLACK)
if len(black_legal_moves) != 0:
black_move = black_agent.get_move(black_current_state, black_legal_moves)
board.play(black_move, BLACK)
white_current_state, white_legal_moves = board.get_current_state(), board.get_valid_moves(WHITE)
if len(white_legal_moves) != 0:
white_move = white_agent.get_move(white_current_state, white_legal_moves)
board.play(white_move, WHITE)
white_count, black_count, _ = board.count_stones()
if black_count>white_count:
return BLACK_WIN
if black_count<white_count:
return WHITE_WIN
return DRAW
def play_testbed(agent, other, multiplier=1, both_colors=False):
"""multiplier is for random agents"""
starting_boards=generate_starting_boards()
agent_wins = 0
agent_loss = 0
agent_draws = 0
games_played = 0
for i in range(multiplier):
for b in starting_boards:
result1=play_game(agent, other, board=b)
if result1 == BLACK_WIN:
agent_wins += 1
elif result1 == WHITE_WIN:
agent_loss += 1
else:
agent_draws += 1
games_played+=1
#input()
if both_colors:#If we want agent to play both colors
result2=play_game(other, agent, board=b)
if result2 == WHITE_WIN:
agent_wins += 1
elif result2 == BLACK_WIN:
agent_loss += 1
else:
agent_draws += 1
games_played+=1
return agent_wins, agent_loss, agent_draws, games_played
def model_score(AgentClass, OpponentClass, modeldir, multiplier=1):
#ModelClass only supports Q_Agent for now
print(modeldir)
model = AgentClass(eps=0)
model.import_model(modeldir)
model.eval()
other = OpponentClass() #Assuming it is fully deterministic
w, _, _, g = play_testbed(model, other, multiplier)
score = w/g
return score
names = {
Q_Agent: "qagent",
Sarsa_Agent: "sarsaagent",
Rand_Agent: "rand",
Heu_Agent: "heu",
}
def run_test_over_models(AgentClass, TrainerClass, OpponentClass, maxepisode=2000000, episodestep=5000):
"""
agentname must be one of {'qagent', 'sarsaagent'} --> i.e. args.a must be "q" or "s"
othername must be one of {'heu', 'rand'} --> i.e. args.t, and args.o must be "h" or "r"
"""
indices = []
scores = []
#need to check backslash or frontslash
agentname = names[AgentClass]
trainername = names[TrainerClass]
opponentname = names[OpponentClass]
dir=f'{args.s}/models/'+agentname+'/'+trainername+'/'
fnames = os.listdir(dir)
for idx in range(0,maxepisode+1, episodestep):
"""Cycle over maxepisode. We have a model at every episodestep episodes, but we don't need to sample that frequently"""
fname = agentname+'_vs_'+trainername+'_'+str(idx)+'.pth'
modeldir = dir+fname
if fname not in fnames:
print('FATAL ERROR, model not found', fname)
exit()
print('scoring ',modeldir)
score = model_score(AgentClass, OpponentClass, modeldir)
indices.append(idx)
scores.append(score)
indices = np.array(indices)
scores = np.array(scores)
result=np.stack((indices, scores)).T
outputdir = f'{args.s}/results/'
outputfname = agentname+'_'+trainername+'_'+opponentname+'_scores.txt'
np.savetxt(outputdir+outputfname, result)
return indices, scores
reverse = {
"q": Q_Agent,
"s": Sarsa_Agent,
"r": Rand_Agent,
"h": Heu_Agent,
}
if args.ep is None and args.step is None:
print("1,2")
run_test_over_models(reverse[args.a], reverse[args.t], reverse[args.o], episodestep=args.step)
elif args.ep is None:
print("1")
run_test_over_models(reverse[args.a], reverse[args.t], reverse[args.o], episodestep=args.step)
elif args.step is None:
print("2")
run_test_over_models(reverse[args.a], reverse[args.t], reverse[args.o], maxepisode=args.ep)
else:
run_test_over_models(reverse[args.a], reverse[args.t], reverse[args.o], args.ep, args.step)