JustinTX's picture
Add files using upload-large-folder tool
40607c3 verified
# EVOLVE-BLOCK-START
"""Initial search baseline for the AlphaEvolve AC2 task."""
import time
import numpy as np
def evaluate_sequence(sequence: list[float]) -> float:
"""Evaluator objective. Higher is better."""
if not isinstance(sequence, list):
return float(np.inf)
if not sequence:
return float(np.inf)
for x in sequence:
if isinstance(x, bool) or not isinstance(x, (int, float)):
return float(np.inf)
if np.isnan(x) or np.isinf(x):
return float(np.inf)
seq = [float(x) for x in sequence]
seq = [max(0.0, min(1000.0, x)) for x in seq]
if np.sum(seq) < 0.01:
return float(np.inf)
conv = np.convolve(seq, seq)
n = len(conv)
x_points = np.linspace(-0.5, 0.5, n + 2)
dx = np.diff(x_points)
y = np.concatenate(([0.0], conv, [0.0]))
l2_sq = 0.0
for i in range(n + 1):
y1 = y[i]
y2 = y[i + 1]
h = dx[i]
l2_sq += (h / 3.0) * (y1 * y1 + y1 * y2 + y2 * y2)
norm_1 = float(np.sum(np.abs(conv)) / (len(conv) + 1))
norm_inf = float(np.max(np.abs(conv)))
if norm_1 <= 0.0 or norm_inf <= 0.0:
return float(np.inf)
return float(l2_sq / (norm_1 * norm_inf))
def _make_sparse_seed(rng: np.random.Generator, n: int) -> np.ndarray:
h = np.zeros(n, dtype=np.float64)
k = int(rng.integers(4, max(8, n // 32)))
idx = rng.choice(n, size=k, replace=False)
h[idx] = rng.uniform(0.3, 8.0, size=k)
return h
def run(seed: int = 42, budget_s: float = 10.0, **kwargs) -> list[float]:
"""
Return a coefficient sequence maximizing evaluate_sequence.
"""
del kwargs
rng = np.random.default_rng(seed)
deadline = time.time() + max(0.1, 0.98 * budget_s)
n = 2048
best = _make_sparse_seed(rng, n)
best_score = evaluate_sequence(best.tolist())
# Multi-start
for _ in range(24):
cand = _make_sparse_seed(rng, n)
score = evaluate_sequence(cand.tolist())
if score > best_score:
best, best_score = cand, score
while time.time() < deadline:
cand = best.copy()
if rng.random() < 0.6:
# local amplitude edits around active support
active = np.where(cand > 1e-12)[0]
if active.size > 0:
k = int(rng.integers(1, min(12, active.size + 1)))
edit_idx = rng.choice(active, size=k, replace=False)
cand[edit_idx] *= rng.uniform(0.95, 1.08, size=k)
if rng.random() < 0.35:
# move mass between nearby bins
i = int(rng.integers(0, n))
j = int(np.clip(i + rng.integers(-32, 33), 0, n - 1))
amount = 0.05 * max(0.0, cand[i])
cand[i] = max(0.0, cand[i] - amount)
cand[j] += amount
if rng.random() < 0.20:
# occasionally inject new sparse peaks
k = int(rng.integers(1, 6))
idx = rng.choice(n, size=k, replace=False)
cand[idx] += rng.uniform(0.01, 0.4, size=k)
cand = np.clip(cand, 0.0, 1000.0)
score = evaluate_sequence(cand.tolist())
if np.isfinite(score) and score > best_score:
best, best_score = cand, score
return [float(x) for x in best.tolist()]
# EVOLVE-BLOCK-END