Comparing changes
Choose two branches to see what changed, then open a pull request to bring the work across.
Able to merge. These branches can be merged automatically.
A pull request is already open: #3 Route for #1: nearest winnable planet, aimed ahead, clear of the sun (1.0 vs the field)
4 commits
+62 −0 routes/bo-claude/1-take-the-nearest-planet-that-can/README.md added View file
| @@ -0,0 +1,62 @@ | ||
| 1 | +--- | |
| 2 | +issue: 1 | |
| 3 | +status: done | |
| 4 | +tags: search | |
| 5 | +--- | |
| 6 | +# Nearest winnable planet, aimed ahead, without crossing the sun | |
| 7 | + | |
| 8 | +Issue #1's strategy, rebuilt from the starter: each planet sends just enough ships to take the nearest planet it can | |
| 9 | +win (nearest in turns to arrive), aimed at where an orbiting target will be. The first try, `swarm` (cc591d1), scored | |
| 10 | +0.444 and lost every game to the starter. This route scores **1.0** locally: it wins all 36 check games against | |
| 11 | +random, starter and swarm. | |
| 12 | + | |
| 13 | +## What `main.py` does | |
| 14 | + | |
| 15 | +- **Predicts where every planet will be** for the next 160 turns. Orbiting planets turn at `angular_velocity` around | |
| 16 | + (50, 50), comets follow their `paths`, and static planets stay put (`paths`). | |
| 17 | +- **Flies fleets the way the engine does** (`fly`). It uses the same speed curve and the same swept fleet/planet test. | |
| 18 | + It also checks for the sun and the edge, and returns which planet a fleet hits and on which turn. Every fleet in | |
| 19 | + flight, mine or the enemy's, goes through it, so the bot knows what arrives where and when. | |
| 20 | +- **Forecasts each planet's owner turn by turn** (`owners`), with production and the engine's combat: the largest | |
| 21 | + force minus the second, then against the garrison. | |
| 22 | +- **Sends only what its planets can spare.** A planet keeps the least it needs to hold against the fleets already | |
| 23 | + coming at it. | |
| 24 | +- **Picks the target.** For each target not already bound to be mine, the bot iterates *ships → speed → arrival turn → | |
| 25 | + ships needed* (binary search on the forecast). It aims at the target's centre on the arrival turn. It skips targets | |
| 26 | + it cannot afford, and neutrals whose production would not pay back their garrison before turn 500. Then it takes | |
| 27 | + the candidates nearest-first. The first one whose simulated flight hits that target, with no sun or other planet in | |
| 28 | + between, gets the fleet. | |
| 29 | +- **Reinforces its own planets.** A planet of mine that is forecast to fall is a target like any other, so the nearest | |
| 30 | + planet that can save it does. | |
| 31 | + | |
| 32 | +## Results | |
| 33 | + | |
| 34 | +| | random | starter | swarm | score | | |
| 35 | +|---|---|---|---|---| | |
| 36 | +| baseline: starter (`routes/main.py`) | 1 | 0.5 | 1 | 0.833 | | |
| 37 | +| `swarm` route (cc591d1, check job 25) | 0.833 | 0 | 0.5 | 0.444 | | |
| 38 | +| this route, check seeds (6 maps × 2 seats) | 1 | 1 | 1 | **1.0** | | |
| 39 | +| this route, seeds 1–10 × 2 seats (not in the check) | 20/20 | 20/20 | 20/20 | 1.0 | | |
| 40 | + | |
| 41 | +Most games end by elimination before turn 160. The slowest turn I measured is under 0.05 s, in field games and in | |
| 42 | +mirror matches that run the full 500 turns with many fleets in flight; the limit is 1 s. | |
| 43 | + | |
| 44 | +What `swarm` misses (I did not measure which of these costs it most): | |
| 45 | +1. It sends every turn without counting the fleets already on the way, so its ships trickle out. | |
| 46 | +2. Its paths cross the sun. | |
| 47 | +3. Its guess for an enemy's garrison (`ships + 10·production`) has nothing to do with travel time. | |
| 48 | +4. It never keeps ships back to hold a planet. | |
| 49 | + | |
| 50 | +## Reproduce | |
| 51 | + | |
| 52 | +``` | |
| 53 | +python3 routes/bo-claude/1-take-the-nearest-planet-that-can/main.py # self-check: combat, intercept, ok | |
| 54 | +PYTHONHASHSEED=0 python3 -s referee/play.py check routes/bo-claude/1-take-the-nearest-planet-that-can | |
| 55 | +``` | |
| 56 | + | |
| 57 | +## Next | |
| 58 | + | |
| 59 | +The field is beaten, so the next test is a harder opponent. Mirror matches vs this bot are close, often tied 1054 to | |
| 60 | +1054. To improve on it: when no single planet can win a target, gather ships from several planets (it now waits until | |
| 61 | +one planet has enough). Weigh targets by production per ship and turn, not by distance alone. Strike the enemy before | |
| 62 | +it attacks, when its fleets in flight leave a planet thin. | |
+200 −0 routes/bo-claude/1-take-the-nearest-planet-that-can/main.py added View file
| @@ -0,0 +1,200 @@ | ||
| 1 | +"""Issue #1: each planet sends just enough ships to take the nearest planet it can win, aiming where an orbiting | |
| 2 | +target will be. Unlike the field's swarm it counts the fleets already flying (its own and the enemy's), keeps what | |
| 3 | +its planets need to hold, never flies through the sun or another planet, and stops buying neutrals that cannot pay | |
| 4 | +back before turn 500.""" | |
| 5 | +import math | |
| 6 | + | |
| 7 | +C, SUN, END, H = 50.0, 10.0, 500, 160 # centre, sun radius, last turn, look-ahead in turns | |
| 8 | + | |
| 9 | + | |
| 10 | +def speed(n): | |
| 11 | + return min(6.0, 1.0 + 5.0 * (math.log(max(n, 1)) / math.log(1000)) ** 1.5) | |
| 12 | + | |
| 13 | + | |
| 14 | +def seg_dist(px, py, ax, ay, bx, by): | |
| 15 | + dx, dy = bx - ax, by - ay | |
| 16 | + l2 = dx * dx + dy * dy | |
| 17 | + t = 0.0 if l2 == 0 else max(0.0, min(1.0, ((px - ax) * dx + (py - ay) * dy) / l2)) | |
| 18 | + return math.hypot(px - ax - t * dx, py - ay - t * dy) | |
| 19 | + | |
| 20 | + | |
| 21 | +def swept_hit(a, b, p0, p1, r): | |
| 22 | + """The engine's test: fleet a->b and planet p0->p1 come within r during the tick.""" | |
| 23 | + d0x, d0y = a[0] - p0[0], a[1] - p0[1] | |
| 24 | + vx, vy = (b[0] - a[0]) - (p1[0] - p0[0]), (b[1] - a[1]) - (p1[1] - p0[1]) | |
| 25 | + qa, qb, qc = vx * vx + vy * vy, 2 * (d0x * vx + d0y * vy), d0x * d0x + d0y * d0y - r * r | |
| 26 | + if qa < 1e-12: | |
| 27 | + return qc <= 0 | |
| 28 | + disc = qb * qb - 4 * qa * qc | |
| 29 | + if disc < 0: | |
| 30 | + return False | |
| 31 | + s = math.sqrt(disc) | |
| 32 | + return (-qb + s) / (2 * qa) >= 0 and (-qb - s) / (2 * qa) <= 1 | |
| 33 | + | |
| 34 | + | |
| 35 | +def paths(obs): | |
| 36 | + """Each planet's centre at t = 0..H turns from now (None once a comet has left).""" | |
| 37 | + w = obs['angular_velocity'] | |
| 38 | + comet = {} | |
| 39 | + for g in obs.get('comets') or []: | |
| 40 | + for i, pid in enumerate(g['planet_ids']): | |
| 41 | + comet[pid] = (g['paths'][i], g['path_index']) | |
| 42 | + out = {} | |
| 43 | + for p in obs['planets']: | |
| 44 | + pid, x, y, r = p[0], p[2], p[3], p[4] | |
| 45 | + if pid in comet: | |
| 46 | + path, k = comet[pid] | |
| 47 | + out[pid] = [tuple(path[k + t]) if 0 <= k + t < len(path) else None for t in range(H + 1)] | |
| 48 | + elif math.hypot(x - C, y - C) + r < 50: | |
| 49 | + rad, a = math.hypot(x - C, y - C), math.atan2(y - C, x - C) | |
| 50 | + out[pid] = [(C + rad * math.cos(a + w * t), C + rad * math.sin(a + w * t)) for t in range(H + 1)] | |
| 51 | + else: | |
| 52 | + out[pid] = [(x, y)] * (H + 1) | |
| 53 | + return out | |
| 54 | + | |
| 55 | + | |
| 56 | +def fly(x, y, ang, n, planets, pos, until=H): | |
| 57 | + """Where a fleet at (x, y) heading ang lands: (planet id, turn), or (None, turn) if the sun or the edge takes it.""" | |
| 58 | + v = speed(n) | |
| 59 | + dx, dy = math.cos(ang) * v, math.sin(ang) * v | |
| 60 | + for t in range(1, until + 1): | |
| 61 | + nx, ny = x + dx, y + dy | |
| 62 | + for p in planets: | |
| 63 | + pp = pos[p[0]] | |
| 64 | + if pp[t] is None or pp[t - 1] is None: | |
| 65 | + continue | |
| 66 | + if abs(pp[t][0] - nx) > v + 9 or abs(pp[t][1] - ny) > v + 9: | |
| 67 | + continue | |
| 68 | + if swept_hit((x, y), (nx, ny), pp[t - 1], pp[t], p[4]): | |
| 69 | + return p[0], t | |
| 70 | + if not (0 <= nx <= 100 and 0 <= ny <= 100) or seg_dist(C, C, x, y, nx, ny) < SUN: | |
| 71 | + return None, t | |
| 72 | + x, y = nx, ny | |
| 73 | + return None, until | |
| 74 | + | |
| 75 | + | |
| 76 | +def owners(p, ships, arrivals, extra=None): | |
| 77 | + """Planet p's owner after each of the next turns, up to the last known fleet, given fleets arriving as | |
| 78 | + {turn: {owner: ships}} and `extra` = (turn, owner, ships) more. Combat as the engine does it.""" | |
| 79 | + arr = arrivals | |
| 80 | + if extra: | |
| 81 | + t, who, n = extra | |
| 82 | + arr = dict(arrivals) | |
| 83 | + arr[t] = dict(arr.get(t, {})) | |
| 84 | + arr[t][who] = arr[t].get(who, 0) + n | |
| 85 | + owner, out = p[1], [p[1]] | |
| 86 | + for t in range(1, max(list(arr) + [1]) + 1): | |
| 87 | + if owner != -1: | |
| 88 | + ships += p[6] | |
| 89 | + a = arr.get(t) | |
| 90 | + if a: | |
| 91 | + top = sorted(a.items(), key=lambda kv: -kv[1]) | |
| 92 | + who, n = top[0] | |
| 93 | + if len(top) > 1: | |
| 94 | + n -= top[1][1] | |
| 95 | + if n > 0: | |
| 96 | + if who == owner: | |
| 97 | + ships += n | |
| 98 | + else: | |
| 99 | + ships -= n | |
| 100 | + if ships < 0: | |
| 101 | + owner, ships = who, -ships | |
| 102 | + out.append(owner) | |
| 103 | + return out | |
| 104 | + | |
| 105 | + | |
| 106 | +def aim(src, tgt, n, pos): | |
| 107 | + """The angle and turn at which a fleet of n from src meets tgt's centre, or None.""" | |
| 108 | + sx, sy, v = src[2], src[3], speed(n) | |
| 109 | + tp = pos[tgt[0]] | |
| 110 | + f = lambda t: math.hypot(tp[t][0] - sx, tp[t][1] - sy) - src[4] - 0.1 - v * t | |
| 111 | + for t in range(1, H + 1): | |
| 112 | + if tp[t] is None: | |
| 113 | + return None | |
| 114 | + if f(t) <= 0: # the meeting is in (t-1, t]: aim at the planet's centre at the nearer whole turn | |
| 115 | + x, y = tp[t] if tp[t - 1] is None or -f(t) < f(t - 1) else tp[t - 1] | |
| 116 | + return math.atan2(y - sy, x - sx), t | |
| 117 | + return None | |
| 118 | + | |
| 119 | + | |
| 120 | +def agent(obs, config=None): | |
| 121 | + me, step = obs['player'], obs.get('step', 0) | |
| 122 | + planets = obs['planets'] | |
| 123 | + pos = paths(obs) | |
| 124 | + arrivals = {p[0]: {} for p in planets} # planet -> turn -> owner -> ships, from the fleets in flight | |
| 125 | + for f in obs['fleets']: | |
| 126 | + pid, t = fly(f[2], f[3], f[4], f[6], planets, pos) | |
| 127 | + if pid is not None: | |
| 128 | + d = arrivals[pid].setdefault(t, {}) | |
| 129 | + d[f[1]] = d.get(f[1], 0) + f[6] | |
| 130 | + ships = {p[0]: p[5] for p in planets} | |
| 131 | + safe = lambda p, n: all(o == me for o in owners(p, ships[p[0]] - n, arrivals[p[0]])) | |
| 132 | + takes = lambda p, t, n: all(o == me for o in owners(p, ships[p[0]], arrivals[p[0]], (t, me, n))[t:]) | |
| 133 | + | |
| 134 | + def least(lo, hi, ok): | |
| 135 | + """The least n in [lo, hi] with ok(n), ok being monotone; None if even hi fails.""" | |
| 136 | + if not ok(hi): | |
| 137 | + return None | |
| 138 | + while lo < hi: | |
| 139 | + mid = (lo + hi) // 2 | |
| 140 | + lo, hi = (lo, mid) if ok(mid) else (mid + 1, hi) | |
| 141 | + return lo | |
| 142 | + | |
| 143 | + moves = [] | |
| 144 | + for src in sorted((p for p in planets if p[1] == me), key=lambda p: -p[5]): | |
| 145 | + if not safe(src, 0): | |
| 146 | + continue | |
| 147 | + keep = least(0, ships[src[0]], lambda k: safe(src, ships[src[0]] - k)) # what it needs to hold | |
| 148 | + have = ships[src[0]] - keep | |
| 149 | + options = [] | |
| 150 | + for tgt in planets: | |
| 151 | + if tgt[0] == src[0] or have <= 0 or owners(tgt, ships[tgt[0]], arrivals[tgt[0]])[-1] == me: | |
| 152 | + continue | |
| 153 | + n, hit = 1, None | |
| 154 | + for _ in range(4): # n sets the speed, the speed the turn, the turn the garrison to beat | |
| 155 | + hit = aim(src, tgt, n, pos) | |
| 156 | + if hit is None: | |
| 157 | + break | |
| 158 | + m = least(n, have, lambda k: takes(tgt, hit[1], k)) | |
| 159 | + if m is None: | |
| 160 | + hit = None | |
| 161 | + break | |
| 162 | + if m == n: | |
| 163 | + break | |
| 164 | + n = m | |
| 165 | + if hit is None or n > have: | |
| 166 | + continue | |
| 167 | + ang, t = hit | |
| 168 | + if tgt[1] == -1 and tgt[6] * (END - step - t) <= tgt[5]: | |
| 169 | + continue # a neutral that would not earn back its garrison before the end | |
| 170 | + options.append((t, tgt[0], tgt, ang, n)) | |
| 171 | + for t, _, tgt, ang, n in sorted(options): # the nearest, in turns, whose path is clear | |
| 172 | + sx, sy = src[2] + math.cos(ang) * (src[4] + 0.1), src[3] + math.sin(ang) * (src[4] + 0.1) | |
| 173 | + pid, t2 = fly(sx, sy, ang, n, planets, pos, t + 3) | |
| 174 | + if pid != tgt[0] or not takes(tgt, t2, n): | |
| 175 | + continue # the sun or another planet is in the way, or it lands later than aimed and too late | |
| 176 | + d = arrivals[tgt[0]].setdefault(t2, {}) | |
| 177 | + d[me] = d.get(me, 0) + n | |
| 178 | + ships[src[0]] -= n | |
| 179 | + moves.append([src[0], ang, n]) | |
| 180 | + break | |
| 181 | + return moves | |
| 182 | + | |
| 183 | + | |
| 184 | +if __name__ == '__main__': # self-check: python3 main.py | |
| 185 | + p = [0, -1, 20, 20, 1, 10, 2] # neutral, 10 ships | |
| 186 | + assert owners(p, 10, {3: {0: 10}}) == [-1, -1, -1, -1] # 10 on 10 does not take it | |
| 187 | + assert owners(p, 10, {3: {0: 11}}) == [-1, -1, -1, 0] | |
| 188 | + assert owners(p, 10, {3: {0: 15, 1: 5}}) == [-1, -1, -1, -1] # 15 - 5 = 10 left over: still not enough | |
| 189 | + assert owners([0, 1, 20, 20, 1, 10, 2], 10, {3: {0: 16}})[-1] == 1 # an owned planet grows 2 a turn: 16 vs 16 | |
| 190 | + assert owners([0, 1, 20, 20, 1, 10, 2], 10, {3: {0: 17}})[-1] == 0 | |
| 191 | + obs = {'angular_velocity': 0.03, 'planets': [[0, 0, 10, 10, 1, 50, 1], [1, -1, 10, 90, 2, 5, 1]], 'fleets': [], 'comets': []} | |
| 192 | + pos = paths(obs) | |
| 193 | + ang, t = aim(obs['planets'][0], obs['planets'][1], 6, pos) | |
| 194 | + sx, sy = 10 + math.cos(ang) * 1.1, 10 + math.sin(ang) * 1.1 | |
| 195 | + assert fly(sx, sy, ang, 6, obs['planets'], pos)[0] == 1 | |
| 196 | + obs = {'angular_velocity': 0.05, 'planets': [[0, 0, 90, 5, 1, 50, 1], [1, -1, 80, 50, 1, 5, 1]], 'fleets': [], 'comets': []} | |
| 197 | + pos, src = paths(obs), obs['planets'][0] # planet 1 orbits the sun at radius 30 | |
| 198 | + for ang, hits in ((aim(src, obs['planets'][1], 200, pos)[0], True), (math.atan2(45, -10), False)): # ahead, at it | |
| 199 | + assert (fly(90 + math.cos(ang) * 1.1, 5 + math.sin(ang) * 1.1, ang, 200, obs['planets'], pos)[0] == 1) == hits | |
| 200 | + print('ok') | |
