| """Attack 8: Tile the best small-n solution to create larger sequences. |
| Tiling preserves local structure better than interpolation.""" |
| import time |
| import numpy as np |
| from scipy.signal import fftconvolve |
| from scipy.optimize import minimize |
|
|
| def actual_obj(a): |
| a = np.maximum(a, 0); n = len(a) |
| conv = fftconvolve(a, a); s = np.sum(a) |
| if s < 0.01: return float('inf') |
| return 2.0 * n * np.max(conv) / s**2 |
|
|
| def make_lp(n, p): |
| def f(a): |
| a = np.maximum(a, 1e-12); S = np.sum(a) |
| conv = fftconvolve(a, a); conv = np.maximum(conv, 1e-30) |
| lc = np.log(conv); lcm = np.max(lc); lcs = lc - lcm |
| e = np.exp(p * lcs); se = np.sum(e) |
| Lp = np.exp(lcm) * se ** (1.0/p) |
| obj = 2.0 * n * Lp / S**2 |
| w = (se ** ((1-p)/p)) * np.exp((p-1) * lcs) |
| G = fftconvolve(w, a[::-1], mode='valid') |
| G = G[:n] if len(G) >= n else np.pad(G, (0, n-len(G))) |
| return obj, 2.0 * n / S**2 * (2 * G - 2.0 * Lp / S) |
| return f |
|
|
| |
| a300 = np.load('/workspace/best_sequence.npy') |
| print(f"n=300 template: {actual_obj(a300):.7f}") |
|
|
| |
| strategies = [] |
|
|
| |
| for k in [4, 7, 10]: |
| a_tiled = np.tile(a300, k)[:2000] |
| strategies.append(("tile_"+str(k), a_tiled)) |
|
|
| |
| for k in [7, 10]: |
| a_tiled = np.tile(a300, k + 1) |
| |
| n_base = len(a300) |
| overlap = n_base // 4 |
| for t in range(1, k): |
| start = t * n_base - overlap |
| for j in range(2 * overlap): |
| w = j / (2 * overlap) |
| idx = start + j |
| if idx < len(a_tiled): |
| a_tiled[idx] = (1-w) * a_tiled[idx] + w * a_tiled[idx] |
| a_tiled = a_tiled[:2000] |
| strategies.append(("tile_blend_"+str(k), a_tiled)) |
|
|
| |
| a_upsampled = np.zeros(2000) |
| ratio = 2000 / 300 |
| for i in range(300): |
| j = int(i * ratio) |
| if j < 2000: |
| a_upsampled[j] = a300[i] |
| |
| from scipy.ndimage import uniform_filter1d |
| a_smooth = uniform_filter1d(a_upsampled, int(ratio) + 1) |
| a_smooth = np.maximum(a_smooth, 1e-10) |
| strategies.append(("zero_insert", a_smooth)) |
|
|
| |
| a_interp = np.interp(np.linspace(0, 1, 2000), np.linspace(0, 1, 300), a300) |
| strategies.append(("interp", a_interp)) |
|
|
| |
| best_val = float('inf') |
| best_a = None |
| bounds = [(1e-10, 1000.0)] * 2000 |
|
|
| for name, a0 in strategies: |
| a0 = np.maximum(a0, 1e-10) |
| init_obj = actual_obj(a0) |
| |
| t0 = time.time() |
| for p in [32, 128, 512, 2048, 8192, 32768, 131072, 524288]: |
| if time.time() - t0 > 30: break |
| res = minimize(make_lp(2000, p), a0, method='L-BFGS-B', jac=True, bounds=bounds, |
| options={'maxiter': 10000, 'ftol': 1e-16, 'gtol': 1e-15}) |
| a0 = np.maximum(res.x, 1e-10) |
| |
| val = actual_obj(a0) |
| print(f"{name:20s}: init={init_obj:.4f} → final={val:.7f} ({time.time()-t0:.0f}s)") |
| |
| if val < best_val: |
| best_val = val |
| best_a = a0.copy() |
|
|
| print(f"\nBest tiling: {best_val:.7f}") |
| print(f"Current best: {actual_obj(np.load('/workspace/best_n2000_ultrahigh.npy')):.7f}") |
|
|
| if best_val < actual_obj(np.load('/workspace/best_n2000_ultrahigh.npy')): |
| np.save('/workspace/best_tiled.npy', best_a) |
| print("NEW RECORD!") |
|
|