Heilbronn's triangle problem: record configurations
Place n points in a square or triangle so the smallest triangle they form is as large as possible; beat a record or prove one optimal.
| 1 | #!/usr/bin/env python3 |
| 2 | """The project's judge (the check `verify`): every candidate (*.json with "points") in the route's folder, checked |
| 3 | exactly by heilbronn.py; on the default branch, whose route folder is routes/, constructions/ too. Both files are in checks/judge/, which a check runs at the project's approved version. One line per candidate, then the score and the new records; |
| 4 | metrics.json in the working directory gets the score (a float and its exact value), each instance's best value and the |
| 5 | instances above their records. Fails when a candidate is unreadable or malformed.""" |
| 6 | import json |
| 7 | import os |
| 8 | import pathlib |
| 9 | import sys |
| 10 | |
| 11 | from heilbronn import INSTANCES, RECORD, best_of, decimal, gain, is_record, scan, score |
| 12 | |
| 13 | root = pathlib.Path(sys.argv[1]) |
| 14 | roots = [root] + ([root.parent / "constructions"] if os.environ.get("SS_ROUTE_DIR") == "routes" else []) |
| 15 | found, bad = [], 0 |
| 16 | for name, instance, v in scan(roots): |
| 17 | if instance is None: |
| 18 | print(f"{name}: {v}") |
| 19 | bad += 1 |
| 20 | continue |
| 21 | found.append((name, instance, v)) |
| 22 | print(f"{instance}: {decimal(v)} ({name}), {gain(v, RECORD[instance])} against the record{'; a new record' if is_record(instance, v) else ''}") |
| 23 | best = best_of(found) |
| 24 | if best: |
| 25 | s = score({k: v for k, (v, _) in best.items()}) |
| 26 | new = [k for k in INSTANCES if k in best and is_record(k, best[k][0])] |
| 27 | metrics = {"score": float(s), "score_exact": str(s), "subtasks": {k: float(best[k][0]) for k in INSTANCES if k in best}, "new_records": new} |
| 28 | pathlib.Path("metrics.json").write_text(json.dumps(metrics) + "\n") |
| 29 | print(f"score {float(s):.12f} over {len(INSTANCES)} instances; " + (f"new records: {', '.join(new)}" if new else "no new record")) |
| 30 | else: |
| 31 | print(f"no valid candidates: {bad} failed" if bad else "no candidates to check") |
| 32 | sys.exit(1 if bad else 0) |
