-
Notifications
You must be signed in to change notification settings - Fork 1
/
controller.py
237 lines (223 loc) · 8.77 KB
/
controller.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
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
231
232
233
234
235
236
237
import argparse as arg
import sys, os
import subprocess as sub
import random as r
import time as t
def pos_to_str(pos):
if pos is None:
return "N"
else:
return "%d,%d"%pos
class Bot:
"A bot."
def __init__(self, name, directory, command, initial=None):
self.name = name
self.directory = directory
self.command = command
self.initial = initial
self.score = 0
self.delta_score = 0
self.handle = None
self.enemies = None
self.last_choice = None
self.report = None
def push_msg(self, message):
self.handle.stdin.write(message + '\n')
self.handle.stdin.flush()
def pull_msg(self):
return self.handle.stdout.readline().rstrip()
class Vertex:
"A vertex."
INACTIVE, ACTIVE, BROKEN = 0, 1, 2
def __init__(self, children):
self.children = children
self.is_sink = not children
self.status = self.INACTIVE
self.owners = set()
def open_bots(bot_path, verbose):
alp = list("abcdefghijklmnopqrstuvwxyz")
bots = []
bot_file = open(bot_path)
while True:
line = bot_file.readline().rstrip()
if not line:
break
if line[0] == '#':
continue
bot_name = line
directory = bot_file.readline().rstrip()
command = bot_file.readline().rstrip().split()
if verbose:
bots.append(Bot(bot_name, directory, command, initial=alp.pop(0)))
else:
bots.append(Bot(bot_name, directory, command))
bot_file.close()
for i in range(len(bots)):
order = list(range(len(bots)))
order.remove(i)
r.shuffle(order)
bots[i].enemies = [bots[j] for j in order]
return bots
def run_round(bots, verbose, suppress_errors):
slows = set()
# Initial message: number of bots, number of turns, sidelength
turns = len(bots)**2
side = int(4/3*turns)
for bot in bots:
directory = os.path.join("bots", bot.directory)
try:
err_pipe = sub.PIPE if suppress_errors else None
bot.handle = sub.Popen(bot.command, bufsize=1, universal_newlines=True, cwd=directory, stdin=sub.PIPE, stdout=sub.PIPE, stderr=err_pipe)
bot.handle.poll()
except IOError as err:
print("Error when executing %s with command "%bot.name + str(bot.command))
raise err
bot.report = "BEGIN %d %d %d"%(len(bots), turns, side)
grid = [[0]*side for y in range(side)]
for y in reversed(range(side)):
for x in range(side):
grid[y][x] = Vertex([] if y == side-1 else [grid[y+1][(x-1)%side], grid[y+1][x], grid[y+1][(x+1)%side]])
for turn in range(turns):
# Destruction phase
for bot in bots:
bot.last_choice = None
time = t.perf_counter()
bot.push_msg(bot.report)
bot.push_msg("DESTROY %d"%(turn,))
response = bot.pull_msg()
resp_split = response.split()
delta = t.perf_counter() - time
if delta > 1:
print(" Bot %s was too slow to destroy (%f seconds)."%(bot.name, delta))
slows.add(bot)
if resp_split and resp_split[0] == "VERTEX":
pos = (x,y) = tuple(map(int, resp_split[1].split(",")))
if 0 <= x < side and 0 <= y < side:
if grid[y][x].status == Vertex.INACTIVE:
grid[y][x].status = Vertex.BROKEN
bot.last_choice = pos
else:
print("Bot %s gave out-of-range vertex (%s)."%(bot.name, response))
sys.exit(0)
elif resp_split and resp_split[0] == "NONE":
pass
else:
print("Bot %s gave malformed response (%s)."%(bot.name, response))
sys.exit(0)
# Make destruction reports
for bot in bots:
bot.report = "BROKEN %d %s %s"%(turn, pos_to_str(bot.last_choice), " ".join(pos_to_str(enemy.last_choice) for enemy in bot.enemies))
# Activation phase
activated = set()
for bot in bots:
bot.last_choice = None
time = t.perf_counter()
bot.push_msg(bot.report)
bot.push_msg("ACTIVATE %d"%(turn,))
response = bot.pull_msg()
resp_split = response.split()
delta = t.perf_counter() - time
if delta > 1:
print(" Bot %s was too slow to claim (%f seconds)."%(bot.name, delta))
slows.add(bot)
if resp_split and resp_split[0] == "VERTEX":
pos = (x,y) = tuple(map(int, resp_split[1].split(",")))
if 0 <= x < side and 0 <= y < side:
if grid[y][x].status == Vertex.INACTIVE:
grid[y][x].owners.add(bot)
activated.add(pos)
bot.last_choice = pos
else:
print("Bot %s gave out-of-range vertex (%s)."%(bot.name, response))
sys.exit(0)
elif resp_split and resp_split[0] == "NONE":
pass
else:
print("Bot %s gave malformed response (%s)."%(bot.name, response))
sys.exit(0)
for (x,y) in activated:
grid[y][x].status = Vertex.ACTIVE
# Make activation reports
for bot in bots:
bot.report = "OWNED %d %s %s"%(turn, pos_to_str(bot.last_choice), " ".join(pos_to_str(enemy.last_choice) for enemy in bot.enemies))
# Compute scores by DFS
print(" Finished, computing score.")
if verbose:
for row in reversed(grid):
print(" " + "".join([r.sample(list(v.owners), 1)[0].initial if v.status == Vertex.ACTIVE else ("." if v.status == Vertex.INACTIVE else " ") for v in row]))
for bot in bots:
bot.delta_score = 0
for x in range(side):
if grid[0][x].status != Vertex.ACTIVE:
continue
for i in range(len(bots)):
visited = set()
cell = grid[0][x]
succs = [child for child in cell.children if child.status == Vertex.ACTIVE]
r.shuffle(succs)
path = [(cell, succs)]
while path:
cell, children = path[0]
if cell.is_sink:
break
if children:
child = children.pop(0)
if child not in visited:
succs = [gchild for gchild in child.children if gchild.status == Vertex.ACTIVE]
r.shuffle(succs)
path = [(child, succs)] + path
visited.add(child)
else:
path = path[1:]
if path:
for (cell, c) in path:
for bot in cell.owners:
bot.score += 1
bot.delta_score += 1
# Report back scores
for bot in bots:
message = "SCORE %d %s"%(bot.delta_score, " ".join(str(enemy.delta_score) for enemy in bot.enemies))
time = t.perf_counter()
bot.push_msg(bot.report)
bot.push_msg(message)
bot.handle.stdin.close()
bot.handle.wait()
delta = t.perf_counter() - time
if delta > 1:
print(" Bot %s was too slow to halt (%f seconds)."%(bot.name, delta))
slows.add(bot)
return slows
def main():
parser = arg.ArgumentParser(prog="controller.py")
parser.add_argument("-v", help="produce verbose ASCII art output", action="store_const", const=True, default=False)
parser.add_argument("-e", help="suppress STDERR for bot processes", action="store_const", const=True, default=False)
parser.add_argument("rounds", nargs="?", default=1)
args = parser.parse_args(sys.argv[1:])
verbose = args.v
suppress_errors = args.e
rounds = int(args.rounds)
print("Initializing bots.")
for bot_dir in os.listdir("bots"):
open(os.path.join("bots", bot_dir, "data.txt"), "w").close()
bots = open_bots("bots.txt", verbose)
print("Bots:")
for bot in bots:
if verbose:
print(" %s (%s)"%(bot.name, bot.initial))
else:
print(" " + bot.name)
print("Running %d rounds."%(rounds,))
slow_bots = set()
for i in range(rounds):
print(" Round %d"%(i,))
slow_bots.update(run_round(bots, verbose, suppress_errors))
print(" Results: " + " ".join(str(bot.delta_score) for bot in bots))
print("Final results:")
for bot in sorted(bots, key=lambda b: -b.score):
print(" %s: %d"%(bot.name, bot.score))
if slow_bots:
print("The following bots were too slow:")
for bot in slow_bots:
print(" " + bot.name)
if __name__ == "__main__":
main()