| 1 | """The project's referee, on kaggle_environments' own Orbit Wars. check: the route's bot against each bot of the |
| 2 | 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 |
| 3 | the end), each opponent a subtask. practice: one map against each, the route in the first seat, a replay of each in replays/. |
| 4 | python3 -I referee/play.py check|practice <route folder> |
| 5 | """ |
| 6 | import json, os, random, sys |
| 7 | |
| 8 | ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) |
| 9 | sys.path.insert(0, os.path.join(ROOT, 'kit')) |
| 10 | sys.path.insert(0, os.path.join(ROOT, 'referee')) |
| 11 | import league |
| 12 | from kaggle_environments import make |
| 13 | from kaggle_seat import seat |
| 14 | |
| 15 | SEEDS = [11, 23, 37, 41, 59, 73] |
| 16 | |
| 17 | |
| 18 | def ships(obs, me): |
| 19 | 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) |
| 20 | |
| 21 | |
| 22 | def compact(replay): |
| 23 | """The player draws from the first seat's observation; the others repeat it, so they keep only what is theirs.""" |
| 24 | for step in replay['steps']: |
| 25 | for s in step[1:]: |
| 26 | s['observation'] = {'player': s['observation'].get('player'), 'remainingOverageTime': s['observation'].get('remainingOverageTime')} |
| 27 | return replay |
| 28 | |
| 29 | |
| 30 | def main(mode, route): |
| 31 | name = league.route_name() |
| 32 | reps = league.Replays() if mode == 'practice' else None |
| 33 | subtasks = {} |
| 34 | for opp in league.field(os.path.join(ROOT, 'field')): |
| 35 | points, n = 0.0, 0 |
| 36 | for seed in SEEDS if mode == 'check' else SEEDS[:1]: |
| 37 | for first in (0, 1) if mode == 'check' else (0,): |
| 38 | me, them = seat(league.bot_dir(route), idle=[]), seat(os.path.join(ROOT, 'field', opp), idle=[]) |
| 39 | random.seed(seed) |
| 40 | env = make('orbit_wars', configuration={'seed': seed}) |
| 41 | env.run([me, them] if first == 0 else [them, me]) |
| 42 | me.child.close(), them.child.close() |
| 43 | mine = first |
| 44 | last = env.steps[-1][0]['observation'] |
| 45 | a, b = ships(last, mine), ships(last, 1 - mine) |
| 46 | points += 1.0 if a > b else 0.5 if a == b else 0.0 |
| 47 | n += 1 |
| 48 | if reps: |
| 49 | who = [name, opp] if first == 0 else [opp, name] |
| 50 | end = f'{who[0]} {ships(last, 0)} ships, {who[1]} {ships(last, 1)}' |
| 51 | reps.add(f'{opp}-{seed}-seat{first}.json', compact(env.toJSON()), f'{who[0]} vs {who[1]}, map {seed}', who, end) |
| 52 | subtasks[opp] = points / n |
| 53 | league.write_metrics(sum(subtasks.values()) / len(subtasks), subtasks) |
| 54 | |
| 55 | |
| 56 | if __name__ == '__main__': |
| 57 | main(sys.argv[1], sys.argv[2]) |