"""The project's referee, on kaggle_environments' own Orbit Wars. check: the route's bot against each bot of the season's field on six maps (seeds), in both seats; a win is 1, a tie ½ (Kaggle's reward: whoever has the most ships at the end), each opponent a subtask. practice: one map against each, the route in the first seat, a replay of each in replays/. python3 -I referee/play.py check|practice """ import json, os, random, sys ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) sys.path.insert(0, os.path.join(ROOT, 'kit')) sys.path.insert(0, os.path.join(ROOT, 'referee')) import league # noqa: E402 from kaggle_environments import make # noqa: E402 from kaggle_seat import seat # noqa: E402 SEEDS = [11, 23, 37, 41, 59, 73] def ships(obs, me): return sum(p[5] for p in obs['planets'] if p[1] == me) + sum(f[6] for f in obs['fleets'] if f[1] == me) def compact(replay): """The player draws from the first seat's observation; the others repeat it, so they keep only what is theirs.""" for step in replay['steps']: for s in step[1:]: s['observation'] = {'player': s['observation'].get('player'), 'remainingOverageTime': s['observation'].get('remainingOverageTime')} return replay def main(mode, route): name = league.route_name() reps = league.Replays() if mode == 'practice' else None subtasks = {} for opp in league.field(os.path.join(ROOT, 'field')): points, n = 0.0, 0 for seed in SEEDS if mode == 'check' else SEEDS[:1]: for first in (0, 1) if mode == 'check' else (0,): # practice keeps one seat: a replay is 2-4 MB me, them = seat(league.bot_dir(route), idle=[]), seat(os.path.join(ROOT, 'field', opp), idle=[]) random.seed(seed) env = make('orbit_wars', configuration={'seed': seed}) env.run([me, them] if first == 0 else [them, me]) me.child.close(), them.child.close() mine = first last = env.steps[-1][0]['observation'] a, b = ships(last, mine), ships(last, 1 - mine) points += 1.0 if a > b else 0.5 if a == b else 0.0 n += 1 if reps: who = [name, opp] if first == 0 else [opp, name] end = f'{who[0]} {ships(last, 0)} ships, {who[1]} {ships(last, 1)}' reps.add(f'{opp}-{seed}-seat{first}.json', compact(env.toJSON()), f'{who[0]} vs {who[1]}, map {seed}', who, end) subtasks[opp] = points / n league.write_metrics(sum(subtasks.values()) / len(subtasks), subtasks) if __name__ == '__main__': main(sys.argv[1], sys.argv[2])