Download code/time_milp.py from Devya27/orca-table3-repro: direct link, hf CLI and curl.
- Browser
- Download file 2.26 kB
-
https://huggingface.co/datasets/Devya27/orca-table3-repro/resolve/main/code/time_milp.py
- Command line
-
hf download hf://datasets/Devya27/orca-table3-repro/code/time_milp.py
-
curl -L -o time_milp.py https://huggingface.co/datasets/Devya27/orca-table3-repro/resolve/main/code/time_milp.py
2.26 kB
| #!/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) | |