#!/usr/bin/env python3 """bsearch.py for odd n under the diagonal reflection dg: mirrored pairs plus one point on x = y. Usage: dsearch.py 15 dg K STARTS SEED WORKERS HOPS > n15.txt The half-turn c2 cannot hold an odd number of points: its fixed point is the centre (1/2, 1/2), and the centre with any half-turn pair is collinear. The reflection dg maps (x, y) to (y, x); its fixed points lie on the diagonal x = y, where a fixed point forces no collinearity — only a pair (x, y), (y, x) together with the diagonal point (t, t) with t = (x + y)/2 would be, and the search steers away from it as from any zero area. So n = 2m + 1 points are m mirrored pairs with representatives (x, y) plus one parameter t: k = 2m + 1 coordinates. K of the pair representatives are pinned to x = 0 or x = 1 (their images (y, 0), (y, 1) stay on the boundary); at most 2 per side. Pinned coordinates never move; the final unsymmetric polish keeps every boundary point on the boundary. Needs numpy, scipy, and search.py and bsearch.py beside it (Pinned for the final polish). Structure otherwise that of bsearch.py, by @cogito-claude; this variant by make-chinese-model-great-again for issue #7, n = 15. """ import sys from multiprocessing import Pool import numpy as np from search import Problem, GROUPS, triples, signed, grad from bsearch import Pinned class DgProblem(Problem): """Odd n under dg: 2m + 1 parameters — m pair representatives (x, y) and one diagonal t.""" def __init__(self, n): assert n % 2 == 1, "odd n only: pairs plus one diagonal point" m = (n - 1) // 2 k = 2 * m + 1 M = np.zeros((2 * n, k)) for i in range(m): M[4 * i:4 * i + 2, 2 * i:2 * i + 2] = [(1, 0), (0, 1)] # (x, y) M[4 * i + 2:4 * i + 4, 2 * i:2 * i + 2] = [(0, 1), (1, 0)] # (y, x) M[2 * n - 2, k - 1] = 1 M[2 * n - 1, k - 1] = 1 # (t, t) self.n, self.T, self.M, self.o, self.k = n, triples(n), M, np.zeros(2 * n), k self.m = k self.units = [(2 * i, 2) for i in range(m)] + [(k - 1, 1)] # (start, width) per movable unit def anneal(self, z, rng, steps): now = best = self.value(z) kept = z.copy() for _ in range(steps): heat = 1e-3 ** (1 + _ / steps) step = 0.2 * 0.005 ** (_ / steps) start, w = self.units[rng.integers(len(self.units))] old = z[start:start + w].copy() z[start:start + w] = np.clip(old + rng.normal(0, step, w) * ~self.pin[start:start + w], 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[start:start + w] = old return kept def slp(self, z, iters=400, radius=0.02): from scipy.optimize import linprog 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, K, seed, hops = args rng = np.random.default_rng(seed) P = DgProblem(n) k = P.k z = rng.random(k) pin = np.zeros(k, bool) sides = [(0, 0.0), (0, 1.0)] # (coordinate 0 = x, value): images (y, 0) and (y, 1) stay on the boundary slots = [sd for sd in sides for _ in range(2)] pair = list(range((n - 1) // 2)) for i, j in zip(rng.choice(pair, K, replace=False), rng.permutation(len(slots))[:K]): c, val = slots[j] pin[2 * i + c] = True z[2 * i + c] = val P.pin = pin z = P.anneal(z, rng, steps=4000) z, v = P.slp(z) for _ in range(hops): q = z.copy() for start, w in [P.units[j] for j in rng.choice(len(P.units), rng.integers(1, 3), replace=False)]: q[start:start + w] = np.clip(q[start:start + w] + rng.normal(0, 0.08, w) * ~pin[start:start + w], 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]) assert sym == "dg" and n % 2 == 1, "dsearch.py: odd n under dg only" assert K <= 2 * len([(0, 0.0), (0, 1.0)]), "at most 2 pinned pairs per side" with Pool(workers) as pool: runs = pool.map(one, [(n, K, seed + k, hops) for k in range(starts)]) runs.sort(key=lambda r: -r[0]) for v, s, _ in runs[:5]: print(f"# dg 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 diagonal symmetry dg " f"with {K} pinned pairs, found by dsearch.py {n} dg {K} {starts} {seed} {workers} {hops}; best seed {s}") for x, y in p: print(f"{float(x)!r} {float(y)!r}")