Repository navigation
Expand file tree
/
Copy pathloop.py
More file actions
138 lines (114 loc) · 4.75 KB
/
Copy pathloop.py
File metadata and controls
138 lines (114 loc) · 4.75 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
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
"""Sequential design: random init, then expected improvement."""
from __future__ import annotations
import numpy as np
from medialoop.gp import expected_improvement, predict
from medialoop.space import FACTORS, NAMES
def _row(vector: np.ndarray) -> dict[str, float]:
return {name: float(value) for name, value in zip(NAMES, vector)}
def _sample(rng: np.random.Generator, n: int) -> np.ndarray:
lows = np.array([item["low"] for item in FACTORS])
highs = np.array([item["high"] for item in FACTORS])
return rng.uniform(lows, highs, size=(n, len(FACTORS)))
def batch_acquire(
x_train: np.ndarray,
y_train: np.ndarray,
pool: np.ndarray,
batch_size: int,
strategy: str = "kriging_believer",
noise: float = 0.02,
rng: np.random.Generator | None = None,
) -> list[int]:
"""Select candidate indices using Kriging Believer or Constant Liar.
Strategies:
- 'kriging_believer': Imputes the GP posterior mean mu(x) at each selected point.
- 'constant_liar_min': Imputes min(y) (pessimistic / exploratory).
- 'constant_liar_max': Imputes max(y) (optimistic).
- 'constant_liar_mean': Imputes mean(y).
"""
if batch_size <= 0:
raise ValueError("batch_size must be a positive integer")
if batch_size > len(pool):
raise ValueError("batch_size cannot exceed candidate pool size")
selected_indices: list[int] = []
available_mask = np.ones(len(pool), dtype=bool)
cur_x = np.copy(x_train)
cur_y = np.copy(y_train)
for b in range(batch_size):
avail_indices = np.flatnonzero(available_mask)
if len(avail_indices) == 0:
break
cand_x = pool[avail_indices]
mu, sigma = predict(cur_x, cur_y, cand_x, noise=noise)
ei = expected_improvement(mu, sigma, float(cur_y.max()))
# Resolve ties reproducibly
best_cand = np.flatnonzero(np.isclose(ei, ei.max(), rtol=1e-10, atol=1e-15))
if rng is not None:
local_idx = int(rng.choice(best_cand))
else:
local_idx = int(best_cand[0])
chosen_idx = int(avail_indices[local_idx])
selected_indices.append(chosen_idx)
available_mask[chosen_idx] = False
if b < batch_size - 1:
pick_x = pool[chosen_idx : chosen_idx + 1]
if strategy == "kriging_believer":
imputed_y = float(mu[local_idx])
elif strategy == "constant_liar_min":
imputed_y = float(cur_y.min())
elif strategy == "constant_liar_max":
imputed_y = float(cur_y.max())
elif strategy == "constant_liar_mean":
imputed_y = float(cur_y.mean())
else:
raise ValueError(f"unknown batch strategy: {strategy!r}")
cur_x = np.vstack([cur_x, pick_x])
cur_y = np.append(cur_y, imputed_y)
return selected_indices
def run(
objective,
n_init: int,
n_steps: int,
seed: int,
noise: float = 0.02,
candidates: int = 400,
batch_size: int = 1,
batch_strategy: str = "kriging_believer",
):
if batch_size <= 0:
raise ValueError("batch_size must be a positive integer")
rng = np.random.default_rng(seed)
x = _sample(rng, n_init)
true = np.array([objective(_row(row)) for row in x])
y = true + rng.normal(0.0, noise, size=n_init)
for _ in range(n_steps):
pool = _sample(rng, candidates)
if batch_size == 1:
mu, sigma = predict(x, y, pool, noise=noise)
score = expected_improvement(mu, sigma, float(y.max()))
pick = pool[int(np.argmax(score))]
value = objective(_row(pick))
x = np.vstack([x, pick])
true = np.append(true, value)
y = np.append(y, value + rng.normal(0.0, noise))
else:
picks_idx = batch_acquire(x, y, pool, batch_size=batch_size, strategy=batch_strategy, noise=noise, rng=rng)
for idx in picks_idx:
pick = pool[idx]
value = objective(_row(pick))
x = np.vstack([x, pick])
true = np.append(true, value)
y = np.append(y, value + rng.normal(0.0, noise))
return {"x": x, "true": true, "best_true": float(true.max())}
def random_search(objective, budget: int, seed: int):
rng = np.random.default_rng(seed)
x = _sample(rng, budget)
true = np.array([objective(_row(row)) for row in x])
return {"best_true": float(true.max())}
def inside_bounds(x: np.ndarray) -> bool:
x = np.asarray(x)
if x.ndim != 2 or x.shape[1] != len(FACTORS) or not np.isfinite(x).all():
return False
for col, item in enumerate(FACTORS):
if np.any(x[:, col] < item["low"] - 1e-9) or np.any(x[:, col] > item["high"] + 1e-9):
return False
return True