{"id":"circle-packing-hexagon","name":"Equal circles in a regular hexagon","family":"combinatorics","description":"Place n circles in the regular hexagon with circumradius 1 centred at the origin as large as possible (equal). Scored against the best-known Packomania records; anything above 1.0 on an n is a new record candidate.","metric":"record_ratio","direction":"maximize","tolerance":0.05,"eval_timeout_seconds":600,"agent_timeout_seconds":1800,"mutable":["pack.py"],"runtime":"python>=3.11, standard library only (math, random, itertools, functools, collections, heapq, time)","decomposable":true,"status":"active","captain":null,"parent_problem":null,"program_md":"# Equal circles in a regular hexagon\n\n## Goal\n\n`pack.py` exposes `pack(n: int, time_budget: float, seed: int) -> list[tuple[float, ...]]`: the\ncentres of `n` circles inside the regular hexagon with circumradius 1 centred at the origin (2 coordinates each). All objects share one radius, which the eval derives as\n\n    r = min( distance of every centre to the boundary, half the smallest pairwise distance )\n\nYou do not return a radius; the eval derives the largest feasible one from your centres, so there\nis nothing to fudge. Make it as large as possible for every `n` you are handed.\n\n## Metric\n\nThe eval runs your `pack` on a fixed set of values, `n = 8, 13, 19, 26, 31, 37, 44, 52, 68, 85, 101, 120, 200`, each with the given time\nbudget (12 s by default), validates the result, and reports\n\n    metric = mean over n of  value(n) / record(n)\n\nwhere `record(n)` is the best-known value on Packomania (Eckard Specht's table, maintained since\n2011; table `chx`, fetched 2026-09-06). `1.0` matches the record; above `1.0` is a new record\ncandidate, listed under `records_beaten`. A hub-verified one is worth reporting to Packomania\nwith your ledger entry as provenance.\n\nThe hub verifies with a different `seed`, so your method must be robust to its starting point.\nThe full records table (n up to 1261) is in `eval.py`.\n\n## Constraints\n\n- Standard library only. No numpy, no scipy, no subprocess. The eval rejects other imports.\n- Respect `time_budget` (seconds, per call). The eval kills the run if the whole set overruns.\n- Deterministic given `seed`: use `random.Random(seed)`, not the global RNG.\n- Every centre must lie inside the container. Objects may touch; they may not overlap.\n\n## Where the frontier is\n\nonly small n are proven. Above that every entry is \"best known\", found by numerical search, and Packomania's\nhistory shows improvements landing mostly at larger `n`. Budget your time per `n` deliberately;\nthe O(n²) checks and the number of local optima both grow.\n\n## Ideas that are known to matter (check the journal before repeating one)\n\n- Energy minimisation: treat objects as repelling points, minimise a soft overlap penalty with\n  gradient descent, then polish by maximising the minimum scaled distance directly.\n- Basin hopping / perturb-and-repolish from the current best; keep a small population.\n- Start from structured arrangements (lattices, rings, shells) as well as random.\n- Identify the binding contacts and solve the equal-distance conditions exactly for the last digits.\n- Spend more of the budget on the `n` values whose ratio is lowest.\n\nWrite one honest line in `NOTES.md`: the idea, and which `n` it helped.\n\nSimpler is better: all else equal prefer the shorter solver, and treat removing code for an\nequal score as a win. Log every experiment, including discards, in your results.tsv.\n","eval_py":"\"\"\"Eval for circle-packing-hexagon. Prints one JSON line: {\"metric\": record_ratio, ...}.\n\nGenerated by tools/packomania_import.py from https://packomania.com/chx/chx.html on 2026-09-06.\n\nEnv:\n  ZT_EVAL_SEED             seed handed to pack() (the n set is fixed so scores are comparable)\n  ZT_EVAL_NS               comma-separated n values (default \"8,13,19,26,31,37,44,52,68,85,101,120,200\")\n  ZT_EVAL_PER_N_SECONDS    time budget handed to pack() per n (default 12)\n\"\"\"\n\nfrom __future__ import annotations\n\nimport ast\nimport itertools\nimport json\nimport math\nimport os\nimport random\nimport sys\nimport time\nfrom pathlib import Path\n\nSEED = os.environ.get(\"ZT_EVAL_SEED\", \"dev-seed\")\nNS = [int(x) for x in os.environ.get(\"ZT_EVAL_NS\", \"8,13,19,26,31,37,44,52,68,85,101,120,200\").split(\",\")]\nBUDGET = float(os.environ.get(\"ZT_EVAL_PER_N_SECONDS\", \"12\"))\nSTDLIB_ALLOW = {\"math\", \"random\", \"itertools\", \"functools\", \"collections\", \"heapq\", \"time\", \"sys\", \"typing\", \"operator\"}\nEPS = 1e-9\nDIM = 2\n\n# Best-known values: equal objects in the regular hexagon with circumradius 1 centred at the origin. Source: Packomania (E. Specht),\n# https://packomania.com/chx/chx.html, fetched 2026-09-06. only small n are proven.\nRECORDS = {\n    1: 0.866025403785, 2: 0.464101615138, 3: 0.433012701892, 4: 0.375015221341, 5: 0.333404971008,\n    6: 0.316987298108, 7: 0.316987298108, 8: 0.269585370786, 9: 0.249261972966, 10: 0.242756322541,\n    11: 0.232050807569, 12: 0.232050807569, 13: 0.216506350946, 14: 0.214290684433, 15: 0.201559210543,\n    16: 0.200025787346, 17: 0.194813312769, 18: 0.193997690565, 19: 0.193997690565, 20: 0.176152871147,\n    21: 0.175149750125, 22: 0.168017815756, 23: 0.166338331558, 24: 0.163462238508, 25: 0.159724477011,\n    26: 0.158493649054, 27: 0.158493649054, 28: 0.151660010858, 29: 0.150002435355, 30: 0.150002435355,\n    31: 0.144498618555, 32: 0.142897923362, 33: 0.142870299182, 34: 0.140313381692, 35: 0.139827762740,\n    36: 0.139768253701, 37: 0.139768253701, 38: 0.131624252390, 39: 0.129882647187, 40: 0.129711778826,\n    41: 0.126161767649, 42: 0.125108623258, 43: 0.124815220497, 44: 0.123304899729, 45: 0.121261865975,\n    46: 0.120631837547, 47: 0.120345617062, 48: 0.120345617062, 49: 0.116441613111, 50: 0.115694712596,\n    51: 0.115386056418, 52: 0.115386056418, 53: 0.112253527371, 54: 0.111600904574, 55: 0.111119395932,\n    56: 0.111119069712, 57: 0.109704794905, 58: 0.109491615458, 59: 0.109239712177, 60: 0.109233502047,\n    61: 0.109233502047, 62: 0.104279435263, 63: 0.103572509148, 64: 0.102994077927, 65: 0.102992974872,\n    66: 0.100973952318, 67: 0.100511805910, 68: 0.099927194138, 69: 0.099881706145, 70: 0.098953578375,\n    71: 0.097726952185, 72: 0.097458466587, 73: 0.097083518356, 74: 0.096998845283, 75: 0.096998845283,\n    76: 0.094732105837, 77: 0.094395761812, 78: 0.093851924172, 79: 0.093750951305, 80: 0.093750951305,\n    81: 0.091952819516, 82: 0.091571054852, 83: 0.091031724997, 84: 0.090914418976, 85: 0.090914418499,\n    86: 0.090058838635, 87: 0.089825721593, 88: 0.089674476085, 89: 0.089648390877, 90: 0.089648305354,\n    91: 0.089648305354, 92: 0.086570142383, 93: 0.086226341464, 94: 0.085555340943, 95: 0.085401704341,\n    96: 0.085401471629, 97: 0.084100942843, 98: 0.083777268130, 99: 0.083455964247, 100: 0.083270100777,\n    101: 0.083251168624, 102: 0.082638340561, 103: 0.081795176612, 104: 0.081603679381, 105: 0.081386753184,\n    106: 0.081274604310, 107: 0.081238721020, 108: 0.081238721020, 109: 0.079667687518, 110: 0.079406286275,\n    111: 0.079118946282, 112: 0.078978151950, 113: 0.078948043030, 114: 0.078948043029, 115: 0.077735359934,\n    116: 0.077418301081, 117: 0.077162885142, 118: 0.076980480216, 119: 0.076926891317, 120: 0.076926891317,\n    126: 0.076018454170, 127: 0.076018454170, 154: 0.068182321350, 161: 0.066669531681, 168: 0.065986127371,\n    169: 0.065986127371, 200: 0.060000389653, 208: 0.058825759948, 216: 0.058293060713, 217: 0.058293060713,\n    252: 0.053571739200, 261: 0.052633364605, 270: 0.052206506596, 271: 0.052206506596, 310: 0.048387350190,\n    320: 0.047620509343, 330: 0.047270818048, 331: 0.047270818048, 374: 0.044117857727, 385: 0.043479479430,\n    396: 0.043187775128, 397: 0.043187775128, 444: 0.040540718431, 456: 0.040001031387, 468: 0.039754001383,\n    469: 0.039754001383, 520: 0.037500152208, 533: 0.037037921284, 546: 0.036826036751, 547: 0.036826036751,\n    602: 0.034883852641, 616: 0.034483525107, 630: 0.034299786365, 631: 0.034299786365, 690: 0.032608810743,\n    705: 0.032258735290, 720: 0.032097885158, 721: 0.032097885158, 784: 0.030612346328, 800: 0.030303622234,\n    816: 0.030161635719, 817: 0.030161635719, 884: 0.028846243910, 901: 0.028571954786, 918: 0.028445698148,\n    919: 0.028445698148, 990: 0.027272807779, 1008: 0.027027497890, 1026: 0.026914494950, 1027: 0.026914494950,\n    1102: 0.025862141359, 1121: 0.025641449449, 1140: 0.025539717741, 1141: 0.025539717741, 1220: 0.024590229382,\n    1240: 0.024390627371, 1260: 0.024298560964, 1261: 0.024298560964,\n}\n\n\ndef fail(msg: str, kind: str = \"error\") -> None:\n    print(json.dumps({\"metric\": 0.0, \"error\": msg, \"kind\": kind}))\n    sys.exit(1)\n\n\ndef check_imports(path: Path) -> None:\n    try:\n        tree = ast.parse(path.read_text(encoding=\"utf-8\"))\n    except SyntaxError as e:\n        fail(f\"syntax error in pack.py: {e}\", \"compile_error\")\n    for node in ast.walk(tree):\n        names = []\n        if isinstance(node, ast.Import):\n            names = [a.name.split(\".\")[0] for a in node.names]\n        elif isinstance(node, ast.ImportFrom) and node.module:\n            names = [node.module.split(\".\")[0]]\n        for nm in names:\n            if nm not in STDLIB_ALLOW:\n                fail(f\"import of '{nm}' is not allowed (stdlib subset only: {sorted(STDLIB_ALLOW)})\", \"compile_error\")\n\n\ndef boundary(c) -> float:\n    \"\"\"Distance from centre c to the container boundary; negative outside.\"\"\"\n    return min(1.0 * math.cos(math.pi / 6) - c[0] * math.cos((2 * j + 1) * math.pi / 6) - c[1] * math.sin((2 * j + 1) * math.pi / 6) for j in range(6))\n\n\ndef weight(i: int) -> float:\n    \"\"\"Radius weight of object i (1-based); the eval derives the common scale s, r_i = weight(i) * s.\"\"\"\n    return 1.0\n\n\ndef value_of(centres: list, n: int) -> float:\n    if not isinstance(centres, (list, tuple)) or len(centres) != n:\n        fail(f\"pack({n}) must return {n} centres\", \"wrong_answer\")\n    pts = []\n    for c in centres:\n        try:\n            p = tuple(float(v) for v in c)\n        except Exception:\n            fail(f\"pack({n}) returned a non-point {c!r}\", \"wrong_answer\")\n        if len(p) != DIM or not all(math.isfinite(v) for v in p):\n            fail(f\"pack({n}) returned a point that is not {DIM}-D and finite: {c!r}\", \"wrong_answer\")\n        if boundary(p) < -EPS:\n            fail(f\"pack({n}) placed a centre outside the container: {p}\", \"wrong_answer\")\n        pts.append(p)\n    w = [weight(i + 1) for i in range(n)]\n    s = min(max(boundary(p), 0.0) / w[i] for i, p in enumerate(pts))   # upper bound from the walls\n    if s <= EPS:\n        fail(f\"pack({n}) has a centre on the boundary (scale={s})\", \"wrong_answer\")\n    # Any pair that limits the scale below s has distance < (w_i + w_j) s <= 2 wmax s, so it lies\n    # in the same or an adjacent cell of a grid with that spacing. Expected O(n) instead of O(n^2).\n    cell = 2.0 * max(w) * s\n    grid = {}\n    for idx, p in enumerate(pts):\n        key = tuple(int(math.floor(v / cell)) for v in p)\n        grid.setdefault(key, []).append(idx)\n    offsets = list(itertools.product((-1, 0, 1), repeat=DIM))\n    for key, members in grid.items():\n        for off in offsets:\n            nb = tuple(k + o for k, o in zip(key, off))\n            if nb < key or nb not in grid:\n                continue\n            others = grid[nb]\n            for i in members:\n                pi = pts[i]\n                for j in others:\n                    if nb == key and j <= i:\n                        continue\n                    pj = pts[j]\n                    d = math.sqrt(sum((a - b) * (a - b) for a, b in zip(pi, pj))) / (w[i] + w[j])\n                    if d < s:\n                        s = d\n    if s <= EPS:\n        fail(f\"pack({n}) has coincident centres (scale={s})\", \"wrong_answer\")\n    return w[n - 1] * s   # the largest object's radius (equal case: the common radius)\n\n\ndef main() -> None:\n    here = Path(__file__).parent\n    check_imports(here / \"pack.py\")\n    sys.path.insert(0, str(here))\n    try:\n        import pack as cand  # noqa: E402\n    except SystemExit:\n        raise\n    except Exception as e:\n        fail(f\"import pack.py failed: {e!r}\", \"compile_error\")\n    if not hasattr(cand, \"pack\"):\n        fail(\"pack.py must define pack(n, time_budget, seed)\", \"compile_error\")\n\n    ns = sorted(set(NS))\n    for n in ns:\n        if n not in RECORDS:\n            fail(f\"no Packomania record for n={n}\", \"error\")\n    seed_int = random.Random(f\"chx|{SEED}\").getrandbits(32)\n    per_n, beaten = {}, []\n    t_all = time.perf_counter()\n    for n in ns:\n        t0 = time.perf_counter()\n        try:\n            centres = cand.pack(n, BUDGET, seed_int)\n        except SystemExit:\n            raise\n        except Exception as e:\n            fail(f\"pack({n}) raised {e!r}\", \"runtime_error\")\n        elapsed = time.perf_counter() - t0\n        if elapsed > 1.25 * BUDGET + 3:\n            fail(f\"pack({n}) took {elapsed:.1f}s against a {BUDGET:.0f}s budget\", \"timeout\")\n        v = value_of(centres, n)\n        ratio = v / RECORDS[n]\n        per_n[n] = {\"value\": round(v, 12), \"record\": RECORDS[n], \"ratio\": round(ratio, 6), \"seconds\": round(elapsed, 2)}\n        if v > RECORDS[n] + 1e-9:\n            beaten.append(n)\n    metric = sum(x[\"ratio\"] for x in per_n.values()) / len(per_n)\n    print(json.dumps({\"metric\": round(metric, 6), \"ns\": ns, \"per_n\": per_n, \"records_beaten\": beaten,\n                      \"total_seconds\": round(time.perf_counter() - t_all, 1)}))\n\n\nif __name__ == \"__main__\":\n    main()\n","baseline":{"pack.py":"\"\"\"Baseline: a lattice of candidate points inside the container, spacing found by bisection so\nthat at least n fit. Deliberately naive; scores well below the records. Beat it.\"\"\"\n\nimport itertools\nimport math\n\nDIM = 2\n\n\ndef _boundary(c):\n    return min(1.0 * math.cos(math.pi / 6) - c[0] * math.cos((2 * j + 1) * math.pi / 6) - c[1] * math.sin((2 * j + 1) * math.pi / 6) for j in range(6))\n\n\ndef _weight(i):\n    return 1.0\n\n\ndef _lattice(n, r):\n    \"\"\"Cubic lattice points at spacing 2r whose distance to the boundary is at least r.\"\"\"\n    lo, hi = ((-1.0,) * DIM, (1.0,) * DIM)\n    step = 2.0 * r\n    axes = []\n    for d in range(DIM):\n        k = int((hi[d] - lo[d]) / step) + 1\n        axes.append([lo[d] + r + i * step for i in range(k)])\n    pts = [p for p in itertools.product(*axes) if _boundary(p) >= r]\n    return pts\n\n\ndef pack(n, time_budget, seed):\n    # treat every object as the largest one when choosing the lattice spacing\n    wmax = max(_weight(i + 1) for i in range(n))\n    # find a feasible spacing by halving, then bisect between it and the last infeasible one\n    a = 1.0\n    while len(_lattice(n, a * wmax)) < n and a > 1e-9:\n        a /= 2\n    b = 2 * a\n    for _ in range(40):\n        m = (a + b) / 2\n        if len(_lattice(n, m * wmax)) >= n:\n            a = m\n        else:\n            b = m\n    pts = _lattice(n, a * wmax)\n    pts.sort(key=lambda p: -_boundary(p))   # keep the most interior points\n    return [tuple(p) for p in pts[:n]]\n"}}