File size: 2,264 Bytes
85ee9d8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python
"""Stage-by-stage wall time for ONE fb1 ORCA run: QRF fit / sampling / ranking /
stage-2 MILP (solve_orca_milp on the 500x20 exploration residual matrix)."""
import sys
import time

import numpy as np

sys.path.insert(0, "/work/orca_repro")
from run_realdata import (  # noqa: E402
    ALPHA, K, K_NEIGHBOR, N_CALIB, N_EXPLORE, N_TEST,
    draw_samples, fit_qrf, load_dataset,
    rank_samples_by_density, residual_matrix,
    solve_orca_milp,
)

t_all = time.time()
X, y = load_dataset("fb1")
print(f"[data] fb1 X{X.shape} y{y.shape}", flush=True)

rng = np.random.default_rng(0)  # run_id=0 seed stream, same order as one_run
perm = rng.permutation(len(y))
i_explore = perm[:N_EXPLORE]
i_calib = perm[N_EXPLORE:N_EXPLORE + N_CALIB]
i_test = perm[N_EXPLORE + N_CALIB:N_EXPLORE + N_CALIB + N_TEST]
i_train = perm[N_EXPLORE + N_CALIB + N_TEST:]

X_train, X_explore = X[i_train], X[i_explore]
y_train, y_explore = y[i_train], y[i_explore]
mu_x, sd_x = X_train.mean(axis=0), X_train.std(axis=0)
sd_x[sd_x == 0] = 1.0
Z = lambda A: (A - mu_x) / sd_x  # noqa: E731
X_train, X_explore = Z(X_train), Z(X_explore)

# (a) QRF fit
t0 = time.time()
model = fit_qrf(X_train, y_train, seed=0)
t_qrf = time.time() - t0
print(f"[a] QRF fit: {t_qrf:.2f}s (train n={len(y_train)})", flush=True)

# (b) sampling: shared-u draws (current draw_samples API, one rng)
t0 = time.time()
S_exp = draw_samples(model, X_explore, K, rng)
t_samp = time.time() - t0
print(f"[b] sampling: {t_samp:.2f}s (n={len(X_explore)}, K={K})",
      flush=True)

# (c) ranking
t0 = time.time()
ranked = rank_samples_by_density(S_exp, K_NEIGHBOR)
t_rank = time.time() - t0
E = residual_matrix(y_explore, ranked)
print(f"[c] ranking: {t_rank:.2f}s | E{E.shape}", flush=True)

# (d) stage-2 MILP
lv_counts = [len(np.unique(E[:, r])) for r in range(K)]
print(f"levels per rank: min={min(lv_counts)} max={max(lv_counts)} "
      f"sum={sum(lv_counts)}", flush=True)
t0 = time.time()
thr, beta_hat, det = solve_orca_milp(E, ALPHA, d=1)
t_milp = time.time() - t0
print(f"[d] solve_orca_milp wall: {t_milp:.2f}s details={det}", flush=True)
print(f"thresholds[:5]={np.round(thr[:5], 4)} all>0={bool(np.all(thr > 0))}",
      flush=True)
print(f"TOTAL: {time.time() - t_all:.2f}s", flush=True)