-
Notifications
You must be signed in to change notification settings - Fork 0
/
tournament.rb
executable file
·231 lines (195 loc) · 5.42 KB
/
tournament.rb
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
#!/usr/bin/ruby
$against = false;
$parallel = 1;
$rounds = 1;
$players = [];
$time_per_game = 100;
$time_per_move = 0;
$max_runs = 0;
$players = [];
$cmds = [];
$msg = nil;
$log = nil;
while(ARGV.length > 0)
arg = ARGV.shift
case arg
when "-a", "--against" then $against = true;
when "-p", "--parallel" then $parallel = ARGV.shift.to_i;
when "-r", "--rounds" then $rounds = ARGV.shift.to_i;
when "-g", "--timegame" then $time_per_game = ARGV.shift.to_f;
when "-t", "--timemove" then $time_per_move = ARGV.shift.to_f;
when "-m", "--maxruns" then $max_runs = ARGV.shift.to_i;
when "-c", "--cmd" then $cmds << ARGV.shift;
when "-z", "--msg" then $msg = ARGV.shift;
when "-l", "--log" then $log = ARGV.shift;
when "-h", "--help" then
puts "Run a round robin tournament between players that all understand GTP."
puts "Usage: #{$0} [<options>] players..."
puts " -a --against The first player is the control and all others play it but not each other"
puts " -p --parallel Number of games to run in parallel [#{$parallel}]"
puts " -r --rounds Number of rounds to play [#{$rounds}]"
puts " -g --timegame Time given per game [#{$time_per_game}]"
puts " -t --timemove Time given per move [#{$time_per_move}]"
puts " -m --maxruns Simulations per move [#{$max_runs}]"
puts " -c --cmd Send an arbitrary GTP command at the beginning of the game"
puts " -z --msg Output a message of what this is testing with the results"
puts " -l --log Log the games to this directory, disabled by default"
puts " -h --help Print this help"
exit;
else
$players << arg
end
end
$cmds << "time -g #{$time_per_game} -m #{$time_per_move} -i #{$max_runs} -f 0" if $time_per_game > 0 || $time_per_move > 0 || $max_runs > 0
$num = $players.length();
$num_games = 0;
$interrupt = false;
trap("SIGTERM") { $interrupt = true; }
trap("SIGINT") { $interrupt = true; }
trap("SIGHUP") { $interrupt = true; }
require './lib.rb'
def play_game(n, p1, p2)
#start the programs
gtp = [nil, nil, nil];
begin
gtp[1] = GTPClient.new($players[p1])
gtp[2] = GTPClient.new($players[p2])
log = nil;
if($log)
log = File.open("#{$log}/#{n}-#{p1}-#{p2}", "w");
log.write("# Game #{n} between #{$players[p1]} vs #{$players[p2]}\n")
end
#send the initial commands
$cmds.each{|cmd|
puts cmd;
log.write(cmd+"\n") if log
gtp[1].cmd(cmd);
gtp[2].cmd(cmd);
}
turnstrings = ["draw","white","black"];
turn = 1;
i = 1;
ret = nil;
totaltime = timer {
loop{
$0 = "Game #{n}/#{$num_games} move #{i}: #{$players[p1]} vs #{$players[p2]}"
i += 1;
#ask for a move
print "genmove #{turnstrings[turn]}: ";
time = timer {
ret = gtp[turn].cmd("genmove #{turnstrings[turn]}\n")[2..-3];
}
puts ret
break if(ret == "resign" || ret == "none")
turn = 3-turn;
#pass the move to the other player
gtp[turn].cmd("play #{turnstrings[3-turn]} #{ret}");
log.write("play #{turnstrings[3-turn]} #{ret}\n") if log
log.write("# took #{time} seconds\n\n") if log
}
}
ret = gtp[1].cmd("winner")[2..-3];
turn = turnstrings.index(ret);
log.write("# Winner: #{ret}, #{$players[turn]}\n") if log
log.write("# Total time: #{totaltime} seconds\n") if log
gtp[1].cmd("quit");
gtp[2].cmd("quit");
gtp[1].close
gtp[2].close
return turn;
rescue
gtp[1].close if gtp[1]
gtp[2].close if gtp[2]
return 0;
end
end
#queue up the games
$games = [];
n = 0;
if($against)
(0...$rounds).each{|r|
(1...$num).each{|i|
$games << [n+1, 0, i]
$games << [n+2, i, 0]
n += 2;
}
}
else
(0...$rounds).each{|r|
(0...$num).each{|i|
(0...$num).each{|j|
if(i != j)
n += 1;
$games << [n,i,j]
end
}
}
}
end
$num_games = n;
#play the games
puts "Starting a tournament of #{$rounds} rounds and #{$num_games} games\n";
if($time_per_game > 0 && $time_per_move == 0 && $max_runs == 0)
expected_time = $num_games*2*$time_per_game/$parallel
puts "Max expected time: #{expected_time} seconds, Ends before: " + (Time.now + expected_time).to_s;
end
puts "\n"
time = timer {
$outcomes = $games.map_fork($parallel){|n,i,j|
result = ($interrupt ? 0 : play_game(n, i, j));
[i,j,result]
}
}
#compute the results
$results = []
$num.times{ $results << [0]*$num }
$outcomes.each{|i,j,result|
if(result == 1)
$results[i][j] += 1;
elsif(result == 2)
$results[j][i] += 1;
end
}
#start output
out = "\n\n";
out << "#{$msg}\n" if $msg;
(0...$num).each{|i|
out << "Player #{i+1}: #{$players[i]}\n";
}
puts "\n";
#win vs loss matrix
out << "Win vs Loss Matrix:\n";
out << " ";
(1..$num).each{|i| out << i.to_s.rjust(3) }
out << "\n"
(0...$num).each{|i|
wins = 0;
losses = 0;
out << (i+1).to_s.rjust(2) + ":"
(0...$num).each{|j|
if(i == j)
out << " ";
else
out << $results[i][j].to_s.rjust(3);
wins += $results[i][j];
losses += $results[j][i];
end
}
ties = ($against && i > 0 ? 1 : $num-1)*2*$rounds - wins - losses;
out << " : #{wins} wins, #{losses} losses, #{ties} ties\n";
}
out << "Played #{$num_games} games, Total Time: #{time.to_i} s\n";
print out
if($log)
File.open("#{$log}/players", "w"){|fp|
$players.each{|p|
fp.write("#{p}\n")
}
}
File.open("#{$log}/results", "w"){|fp|
$outcomes.each{|i,j,r| fp.write("#{i},#{j},#{r}\n") }
}
File.open("#{$log}/summary", "w"){|fp|
fp.write(out);
}
end