Heilbronn's triangle problem
Heilbronn's triangle problem in the unit square: configurations of 11 to 16 points, every one checked in exact arithmetic

| 1 | """Try strip-cover moves from the n = 15 record at a low local CPU duty cycle.""" |
| 2 | |
| 3 | import argparse |
| 4 | import json |
| 5 | import time |
| 6 | from pathlib import Path |
| 7 | |
| 8 | import numpy as np |
| 9 | |
| 10 | from search import RECORD_NEXT_DIGIT, exact_fitness, run_track |
| 11 | from strip_moves import strip_neighbor |
| 12 | |
| 13 | |
| 14 | CPU_FRACTION = 0.04 |
| 15 | |
| 16 | |
| 17 | class ThrottledMoves: |
| 18 | def __init__(self, seconds): |
| 19 | self.started = time.monotonic() |
| 20 | self.cpu_started = time.process_time() |
| 21 | self.seconds = seconds |
| 22 | self.reported_at = self.started |
| 23 | |
| 24 | def __call__(self, points, rng, step): |
| 25 | cpu_used = time.process_time() - self.cpu_started |
| 26 | now = time.monotonic() |
| 27 | elapsed = now - self.started |
| 28 | delay = min(self.seconds - elapsed, cpu_used / CPU_FRACTION - elapsed) |
| 29 | if delay > 0: |
| 30 | time.sleep(delay) |
| 31 | if now - self.reported_at >= 30: |
| 32 | print(f"progress: {min(99, int(100 * elapsed / self.seconds))}% " |
| 33 | f"attempts={step} search_cpu={cpu_used:.1f}s", flush=True) |
| 34 | self.reported_at = now |
| 35 | return strip_neighbor(points, rng, step) |
| 36 | |
| 37 | |
| 38 | def main(): |
| 39 | parser = argparse.ArgumentParser(description=__doc__) |
| 40 | parser.add_argument("seconds", type=float) |
| 41 | parser.add_argument("seed", type=int) |
| 42 | parser.add_argument("--output-dir", type=Path, default=Path(".")) |
| 43 | args = parser.parse_args() |
| 44 | if args.seconds <= 0: |
| 45 | parser.error("seconds must be positive") |
| 46 | route = Path(__file__).resolve().parent |
| 47 | baseline = np.loadtxt(route / "reference15.dat") |
| 48 | moves = ThrottledMoves(args.seconds) |
| 49 | result = run_track("record", args.seed, args.seconds, None, (None, baseline), move=moves) |
| 50 | improved = exact_fitness(result["coordinates"]) >= RECORD_NEXT_DIGIT |
| 51 | result["search_cpu_seconds"] = round(time.process_time() - moves.cpu_started, 3) |
| 52 | result["wall_budget_seconds"] = args.seconds |
| 53 | result["cleared_next_check_digit"] = improved |
| 54 | args.output_dir.mkdir(parents=True, exist_ok=True) |
| 55 | (args.output_dir / "strip-summary.json").write_text(json.dumps(result, indent=2) + "\n") |
| 56 | if improved: |
| 57 | (args.output_dir / "n15.txt").write_text("\n".join(result["coordinates"]) + "\n") |
| 58 | print(f"progress: 100% attempts={result['attempts']} " |
| 59 | f"search_cpu={result['search_cpu_seconds']:.3f}s improved={improved}", flush=True) |
| 60 | |
| 61 | |
| 62 | if __name__ == "__main__": |
| 63 | main() |