#!/usr/bin/env python3 """search.py with points pinned to the boundary of the square. Usage: bsearch.py N SYM K STARTS SEED WORKERS HOPS > n.txt Like search.py, but among the orbit representatives K are pinned to sides of the square (their other coordinate stays free): at most 2 per side (a third would make three collinear points), sides drawn at random. SYM is none or c2; under c2 a representative on x = 0 (y = 0) has its image on x = 1 (y = 1), so K <= 4 there, K <= 8 without symmetry. Pinned coordinates never move in the annealing, the SLP or the hops; the final unsymmetric polish keeps every point that lies on the boundary on it. Needs numpy, scipy and search.py beside it. """ import sys from multiprocessing import Pool import numpy as np from search import GROUPS, Problem class Pinned(Problem): def __init__(self, n, sym, pin): super().__init__(n, sym) self.pin = pin # boolean mask over the 2m representative coordinates def anneal(self, z, rng, steps): now = best = self.value(z) kept = z.copy() for k in range(steps): heat = 1e-3 ** (1 + k / steps) step = 0.2 * 0.005 ** (k / steps) i = rng.integers(self.m) old = z[2 * i:2 * i + 2].copy() move = rng.normal(0, step, 2) * ~self.pin[2 * i:2 * i + 2] z[2 * i:2 * i + 2] = np.clip(old + move, 0, 1) new = self.value(z) if new >= now or rng.random() < np.exp((new - now) / heat): now = new if new > best: best, kept = new, z.copy() else: z[2 * i:2 * i + 2] = old return kept def slp(self, z, iters=400, radius=0.02): from scipy.optimize import linprog from search import grad, signed best = self.value(z) k = len(z) for _ in range(iters): p = self.full(z) s = signed(p, self.T) sg = np.sign(s) sg[sg == 0] = 1 mn = np.abs(s).min() act = np.abs(s) < mn + 4 * radius G = (grad(p, self.T)[act] @ self.M) * sg[act, None] A = np.hstack([-G, np.ones((act.sum(), 1))]) bounds = [(0, 0) if f else (max(-radius, -zi), min(radius, 1 - zi)) for zi, f in zip(z, self.pin)] cost = np.zeros(k + 1) cost[-1] = -1 res = linprog(cost, A_ub=A, b_ub=np.abs(s[act]), bounds=bounds + [(None, None)], method="highs") if res.status != 0: radius /= 2 continue q = np.clip(z + res.x[:-1], 0, 1) v = self.value(q) if v > best: z, best = q, v radius = min(radius * 1.5, 0.05) else: radius /= 2 if radius < 1e-14: break return z, best def one(args): n, sym, K, seed, hops = args rng = np.random.default_rng(seed) m = n // len(GROUPS[sym]) z = rng.random(2 * m) pin = np.zeros(2 * m, bool) # sides as (coordinate, value): a side holds at most 2 points (a third would be collinear), so the K pinned # representatives and their images are spread evenly: under c2 a representative on x = 0 has its image on x = 1. sides = [(0, 0), (1, 0), (0, 1), (1, 1)] if sym == "none" else [(0, 0), (1, 0)] slots = [sd for sd in sides for _ in range(2)] for i, j in zip(rng.choice(m, K, replace=False), rng.permutation(len(slots))[:K]): c, val = slots[j] pin[2 * i + c] = True z[2 * i + c] = val P = Pinned(n, sym, pin) z = P.anneal(z, rng, steps=4000) z, v = P.slp(z) for _ in range(hops): q = z.copy() for i in rng.choice(m, rng.integers(1, 3), replace=False): q[2 * i:2 * i + 2] = np.clip(q[2 * i:2 * i + 2] + rng.normal(0, 0.08, 2) * ~pin[2 * i:2 * i + 2], 0, 1) q, w = P.slp(q, iters=150) if w > v: z, v = q, w full = P.full(z).reshape(-1) F = Pinned(n, "none", (full == 0) | (full == 1)) pz, pv = F.slp(full.copy(), iters=300, radius=1e-4) if pv >= v: return pv, seed, F.full(pz) return v, seed, P.full(z) if __name__ == "__main__": if len(sys.argv) < 8: sys.exit(__doc__.strip()) n, sym, K = int(sys.argv[1]), sys.argv[2], int(sys.argv[3]) starts, seed, workers, hops = map(int, sys.argv[4:8]) with Pool(workers) as pool: runs = pool.map(one, [(n, sym, K, seed + k, hops) for k in range(starts)]) runs.sort(key=lambda r: -r[0]) for v, s, _ in runs[:5]: print(f"# {sym} K={K} start seed {s}: {v:.10f}", file=sys.stderr) v, s, p = runs[0] print(f"# n = {n}: smallest triangle about {v:.10f}, best of {starts} starts under symmetry {sym} with {K} " f"pinned representatives, found by bsearch.py {n} {sym} {K} {starts} {seed} {workers} {hops}; best seed {s}") for x, y in p: print(f"{float(x)!r} {float(y)!r}")