forked from algorithmicsuperintelligence/openevolve
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinitial_program.py
More file actions
67 lines (53 loc) · 2.28 KB
/
Copy pathinitial_program.py
File metadata and controls
67 lines (53 loc) · 2.28 KB
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
# EVOLVE-BLOCK-START
"""
LoopBench Demo — Gradient Descent Optimizer
Adapted from a classic hand-written linear-regression gradient descent
(OmkarPathak/Python-Programs). The cost and gradient are computed with Python
for-loops over NumPy arrays — correct but slow. LoopBench will vectorize them
with NumPy while keeping the numerical results identical.
Also exercises automatic dependency handling: the sandbox detects `numpy` and
installs it before running.
"""
import numpy as np
def evaluate_cost(x: np.ndarray, y: np.ndarray, params: np.ndarray) -> float:
"""Mean squared error of a line params=[m, b] over points (naive loop)."""
total = 0.0
n = len(y)
for i in range(n):
pred = params[0] * x[i, 0] + params[1]
total += (y[i] - pred) ** 2
return total / n
def evaluate_gradient(x: np.ndarray, y: np.ndarray, params: np.ndarray) -> np.ndarray:
"""Gradient of the MSE w.r.t. [m, b] (naive loop)."""
m_grad = 0.0
b_grad = 0.0
n = len(y)
for i in range(n):
err = y[i] - (params[0] * x[i, 0] + params[1])
m_grad += -(2.0 / n) * x[i, 0] * err
b_grad += -(2.0 / n) * err
return np.array([m_grad, b_grad])
# EVOLVE-BLOCK-END
# ── Fixed section (never mutated) ─────────────────────────────────────────────
def run_gradient_descent(x, y, init_params, alpha=0.1, iterations=200):
"""Fit a line to (x, y) via gradient descent. Returns (params, final_cost)."""
params = np.array(init_params, dtype=float)
for _ in range(iterations):
grad = evaluate_gradient(x, y, params)
params = params - alpha * grad
return params, evaluate_cost(x, y, params)
def _make_data(n=2000, m=2.5, b=1.0, seed=0):
rng = np.random.default_rng(seed)
xs = rng.random(n)
x = np.ones((n, 2))
x[:, 0] = xs
y = m * xs + b + rng.normal(0, 0.01, n)
return x, y
if __name__ == "__main__":
import time
x, y = _make_data()
start = time.perf_counter()
params, cost = run_gradient_descent(x, y, [0.0, 0.0], alpha=0.5, iterations=200)
elapsed_ms = (time.perf_counter() - start) * 1000
print(f"fit m={params[0]:.3f} b={params[1]:.3f} cost={cost:.6f}")
print(f"LOOPBENCH_SPEED_MS={elapsed_ms:.4f}")