JustinTX's picture
Add files using upload-large-folder tool
2facf1f verified
"""Attack 8b: Fixed tiling approach."""
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')
TARGET = 2000
strategies = {}
# Tile
for k in [4, 7, 10]:
a_t = np.tile(a300, k + 1)[:TARGET]
strategies[f"tile_{k}"] = a_t
# Standard interp
a_i = np.interp(np.linspace(0, 1, TARGET), np.linspace(0, 1, len(a300)), a300)
strategies["interp"] = a_i
best_val = float('inf')
best_a = None
for name, a0 in strategies.items():
assert len(a0) == TARGET, f"{name}: len={len(a0)}"
a0 = np.maximum(a0, 1e-10)
bounds = [(1e-10, 1000.0)] * TARGET
t0 = time.time()
for p in [32, 128, 512, 2048, 8192, 32768, 131072, 524288]:
if time.time() - t0 > 30: break
res = minimize(make_lp(TARGET, 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:15s}: {val:.7f} ({time.time()-t0:.0f}s)")
if val < best_val:
best_val = val
best_a = a0.copy()
current = actual_obj(np.load('/workspace/best_n2000_ultrahigh.npy'))
print(f"\nBest tiling: {best_val:.7f} | Current: {current:.7f}")