File size: 3,293 Bytes
40607c3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# 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