#!/usr/bin/env python3 """Basin hopping without symmetry from a given configuration. Usage: hop.py FILE ROUNDS SEED WORKERS > out.txt Each worker (seed SEED+k) runs ROUNDS hops from FILE: move 1-3 points by a normal step of random size (0.01-0.1), run search.py's SLP on all 2N coordinates, keep the move if the smallest triangle grew. Prints the best configuration with 17 significant digits. Needs numpy, scipy and search.py beside it. """ import sys from multiprocessing import Pool import numpy as np from search import Problem def read(path): pts = [tuple(map(float, l.split())) for l in open(path) if l.strip() and not l.startswith("#")] return np.array(pts) def run(args): path, rounds, seed = args rng = np.random.default_rng(seed) p = read(path) P = Problem(len(p), "none") z, v = P.slp(p.reshape(-1).copy(), iters=200, radius=1e-3) for _ in range(rounds): q = z.copy() s = 10 ** rng.uniform(-2, -1) for i in rng.choice(P.n, rng.integers(1, 4), replace=False): q[2 * i:2 * i + 2] = np.clip(q[2 * i:2 * i + 2] + rng.normal(0, s, 2), 0, 1) q, w = P.slp(q, iters=150) if w > v: z, v = q, w return v, seed, P.full(z) if __name__ == "__main__": path, rounds, seed, workers = sys.argv[1], int(sys.argv[2]), int(sys.argv[3]), int(sys.argv[4]) with Pool(workers) as pool: runs = pool.map(run, [(path, rounds, seed + k) for k in range(workers)]) runs.sort(key=lambda r: -r[0]) for v, s, _ in runs: print(f"# hop seed {s}: {v:.10f}", file=sys.stderr) v, s, p = runs[0] print(f"# n = {len(p)}: smallest triangle about {v:.10f}, by hop.py {path} {rounds} {seed} {workers}; best seed {s}") for x, y in p: print(f"{float(x)!r} {float(y)!r}")