This repository has been archived by the owner on Apr 18, 2020. It is now read-only.
forked from sdgMihai/Halite-III
-
Notifications
You must be signed in to change notification settings - Fork 0
/
run.py
379 lines (265 loc) · 11.5 KB
/
run.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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
import os
import sys
import json
import argparse
import math
import platform
from subprocess import call
from pprint import pprint as pp
def produce_game_environment():
sys.stdout.write("Compiling game engine..\n")
if not os.path.exists("environment"):
sys.stderr.write("Couldn't find the game engine source files!\n")
sys.stderr.write("Make sure you have the environment/ folder in the current path.\n")
exit(1)
cmd = "cd ./environment; cmake .; make -j 4; cd ../; cp ./environment/halite ./halite"
call([cmd], shell=True)
if not os.path.exists("halite"):
sys.stderr.write("Failed to produce executable environment\n")
sys.stderr.write("Corrupt archive?")
exit(1)
def prepare_env(args):
produce_game_environment()
makefile_set = {"makefile", "Makefile", "MAKEFILE"}
if os.path.exists("CMakeLists.txt"):
call(["cmake ."], shell=True)
for makefile in makefile_set:
if os.path.exists(makefile):
sys.stdout.write("Compiling player sources..\n")
if args.clean:
call(["make clean"], shell=True)
call(["make"], shell=True)
call(["rm -rf *.hlt; "
"rm -rf replays/*.hlt; "
"rm -rf replays-readable/*.hlt; "
"mkdir -p replays; "
"mkdir -p replays-readable"], shell=True)
class HaliteEnv(object):
def __init__(self,
player_bot_cmd,
height=30,
width=30,
seed=42,
max_turns=-1):
self.bots = [player_bot_cmd]
self.height = height
self.width = width
self.seed = seed
self.max_turns = max_turns
def __add_map(self, cmd):
# Specify the map configuration
cmd += "--height {} --width {} ".format(self.height, self.width)
cmd += "-s {0}".format(self.seed)
return cmd
def __add_bot(self, cmd, bot_cmd):
cmd += " \"{0}\" ".format(bot_cmd)
return cmd
def __add_bots(self, cmd):
# Specify the number of bots and how to run them
# cmd += " -n {} ".format(len(self.bots))
for bot in self.bots:
cmd = self.__add_bot(cmd, bot)
return cmd
def cleanup_old_replays(self, fname):
if os.path.isfile(fname):
os.unlink(fname)
if os.path.isfile(fname + ".json"):
os.unlink(fname + ".json")
def run(self):
sys.stdout.write("Map: Height {0}, Width {1}, Seed {2}\n".format(self.height, self.width, self.seed))
cmd = "./halite --results-as-json "
cmd = self.__add_map(cmd)
fname = "./replay-{}-{}-{}".format(self.seed, self.width, self.height)
self.cleanup_old_replays(fname)
cmd = self.__add_bots(cmd)
call([cmd], shell=True)
binary_res, text_res = None, None
if os.path.isfile(fname + ".hlt"):
binary_res = fname + ".hlt"
if os.path.isfile(fname + ".json"):
text_res = fname + ".json"
if not text_res:
sys.stderr.write("There was an error during the game, "
"no valid replay file was produced!\n")
return None, None
return binary_res, text_res
def default_map_limit(height, width):
return int(math.sqrt(height * width) * 10)
def compute_score(num_frames, soft_limit, hard_limit, game_weight):
if num_frames <= soft_limit:
return game_weight
if num_frames >= hard_limit:
return 0.0
return game_weight * (1 - (num_frames - soft_limit) / (hard_limit - soft_limit))
def round_one(cmd, map):
sys.stdout.write("Round 1 - single player challenge!\n")
env = HaliteEnv(cmd)
games = [
(32, 32, 42, 8000),
(32, 32, 1673031865, 12000),
(40, 40, 1773807367, 20000),
(48, 48, 1942373999, 13000),
(56, 56, 142342898, 12000)
]
max_score = 0.45 # Round score
game_weight = max_score / len(games) # Equal weight / game
player_score = 0.0
if map != -1:
games = games[map:map + 1]
game_scores = []
for idx, game in enumerate(games):
width, height, seed, expected_halite = game
env.height = height
env.width = width
env.seed = seed
points = 0.0
binary_log, text_log = env.run()
if text_log is None:
sys.stdout.write("Map {} score: {}\n".format(idx, points))
continue
else:
with open(text_log, "r") as f:
result = json.loads(f.read())
halite_mined = result["stats"]["0"]["score"]
sys.stdout.write("The objective was {} and your bot has collected {} halite.\n".format(expected_halite,
halite_mined))
if halite_mined >= expected_halite:
points = game_weight
else:
points = game_weight * (halite_mined / expected_halite * 100) / 100
sys.stdout.write("You failed to mine sufficient resources!\n")
sys.stdout.write("Map score: {}\n".format(points))
game_scores.append(points)
player_score += points
call(["mv {} ./replays/replay-map-{}.hlt".format(binary_log, idx)], shell=True)
call(["mv {} ./replays-readable/replay-map-{}.hlt".format(text_log, idx)], shell=True)
final_score = round(min(player_score, max_score), 2)
sys.stdout.write("Round 1 - done!\nFinal score: {}/{}\n".format(final_score,
max_score))
with open("result.json", "w") as f:
result = {"final_score": final_score}
json.dump(result, f)
def round_two(cmd, map):
sys.stdout.write("Round 2 - 1vs1 battles!\n")
env = HaliteEnv(cmd)
games = [
(32, 32, 20596, "./bots/Odysseus"),
(32, 32, 75273, "./bots/Dragon"),
(32, 32, 58900, "./bots/Joker"),
(40, 40, 93689, "./bots/Odysseus"),
(40, 40, 98091, "./bots/Dragon"),
(56, 56, 1234, "./bots/Odysseus"),
(56, 56, 42, "./bots/Odysseus"),
(56, 56, 1024, "./bots/Dragon"),
]
max_score = 0.45 # Round score
game_weight = max_score / 5 # For max score you need to win on 5/8 maps
player_score = 0.0
battles_won = 0
game_scores = []
if map != -1:
games = games[map:map + 1]
for idx, game in enumerate(games):
width, height, seed, bot = game
env.bots = [cmd, bot]
env.height = height
env.width = width
env.seed = seed
points = 0.0
binary_log, text_log = env.run()
if text_log is None:
sys.stdout.write("Map {} score: {}\n".format(idx, points))
continue
else:
with open(text_log, "r") as f:
result = json.loads(f.read())
if result["stats"]["0"]["rank"] == 1:
points = game_weight
sys.stdout.write("You've won!\n")
elif result["stats"]["0"]["score"] >= 0.9 * result["stats"]["1"]["score"]:
points = game_weight * 0.9
sys.stdout.write("You've lost.. but you were very close!\n")
else:
points = 0.0
sys.stdout.write("You've lost!\n")
sys.stdout.write("Map score: {}\n".format(points))
game_scores.append(points)
player_score += points
call(["mv {} ./replays/replay-map-{}.hlt".format(binary_log, idx)], shell=True)
call(["mv {} ./replays-readable/replay-map-{}.hlt".format(text_log, idx)], shell=True)
final_score = round(min(player_score, max_score), 2)
sys.stdout.write("Round 2 - done!\nFinal score: {}/{}\n".format(final_score,
max_score))
with open("result.json", "w") as f:
result = {"final_score": final_score}
json.dump(result, f)
return
def round_three(cmd, map):
sys.stdout.write("Round 3 - 1vs3 battles!\n")
env = HaliteEnv(cmd)
games = [
(56, 56, 1423, "./bots/Odysseus", "./bots/Odysseus", "./bots/Odysseus"),
(56, 56, 1312, "./bots/Rhaegal", "./bots/Odysseus", "./bots/Dragon"),
(32, 32, 5231, "./bots/Dragon", "./bots/Dragon", "./bots/Dragon"),
(56, 56, 3908, "./bots/Rhaegal", "./bots/Rhaegal", "./bots/Rhaegal"),
(56, 56, 1423, "./bots/Rhaegal", "./bots/Odysseus", "./bots/Rhaegal"),
(40, 56, 8762, "./bots/Rhaegal", "./bots/Dragon", "./bots/Dragon"),
(56, 40, 1931, "./bots/Dragon", "./bots/Dragon", "./bots/Dragon"),
(40, 40, 5421, "./bots/Dragon", "./bots/Rhaegal", "./bots/Rhaegal"),
]
max_score = 0.36 # Round score
ranks = 0.0
ranking = {1: '1st',
2: '2nd',
3: '3rd',
4: '4th'
}
if map != -1:
games = games[map:map + 1]
for idx, game in enumerate(games):
width, height, seed, bots = game[0], game[1], game[2], game[3:]
env.bots = [cmd, bots[0], bots[1], bots[2]]
env.height = height
env.width = width
env.seed = seed
points = 0.0
binary_log, text_log = env.run()
if text_log is None:
sys.stdout.write("Map {} score: {}\n".format(idx, points))
continue
else:
with open(text_log, "r") as f:
result = json.loads(f.read())
ranks += result["stats"]["0"]['rank']
sys.stdout.write("Got {} place!\n".format(ranking[result["stats"]["0"]['rank']]))
call(["mv {} ./replays/replay-map-{}.hlt".format(binary_log, idx)], shell=True)
call(["mv {} ./replays-readable/replay-map-{}.hlt".format(text_log, idx)], shell=True)
final_score = round(max_score - ((max((ranks / 8.0), 2.0) - 2.0) * max_score) / 2.0, 2)
sys.stdout.write("Round 3 - done!\nFinal score: {}/{}\n".format(final_score,
max_score))
with open("result.json", "w") as f:
result = {"final_score": final_score}
json.dump(result, f)
def cleanup():
call(["rm -f *.hlt; rm -rf replays/*.hlt; rm -rf replays-readable/*.hlt; rm -f *.log"], shell=True)
if os.path.exists("makefile") or os.path.exists("Makefile"):
call(["make clean"], shell=True)
def main():
parser = argparse.ArgumentParser(description='PA project evaluator')
parser.add_argument('--cmd', required=True, help="Command line instruction to execute the bot. eg: ./MyBot")
parser.add_argument('--round', type=int, default=3, help="Round index (1, 2, or 3), default 2")
parser.add_argument('--map', type=int, default=-1, help="Specify a specific map to play for the current round")
parser.add_argument('--clean_logs', action="store_true", help="Remove logs/game results, call make clean")
parser.add_argument('--clean', action="store_true", help="Call make clean before make when building player sources")
args = parser.parse_args()
prepare_env(args)
rounds = [round_one, round_two, round_three]
if args.round < 1 or args.round > len(rounds):
sys.stderr.write("Invalid round parameter (should be an integer in [1, 3])\n")
exit(1)
# Let the games begin!
rounds[args.round - 1](args.cmd, args.map)
if args.clean_logs:
cleanup()
if __name__ == "__main__":
main()