"""Is the paper's BitSequence "Top-100 reward" metric able to separate methods? Sec 4.2 + App C.1: BitSequence has length 8, so |X| = 256 terminal states, and "Mean Top-100 Reward" is "the average reward of the 100 highest-reward samples from a batch of 2048 generations". Drawing 2048 samples from a 256-state space visits essentially every state whatever the policy, so the top 100 *samples* are dominated by the top few states almost regardless of how the policy is shaped. We bracket the metric by evaluating it for policies spanning the whole achievable range: * target : the ideal GFlowNet policy P*(x) ∝ R(x) * uniform : an untrained policy * anti-target : a deliberately adversarial policy P(x) ∝ 1/R(x) * greedy : all mass on the single best state If even the adversarial policy scores far above the paper's reported TB value, the reported spread cannot come from the described protocol. """ from __future__ import annotations import itertools import json import numpy as np from envs import BitSequenceEnv def top_k_metric(rewards, probs, n_samples=2048, k=100, rng=None, reps=200): rng = rng or np.random.RandomState(0) vals = [] idx = np.arange(len(rewards)) for _ in range(reps): draw = rng.choice(idx, size=n_samples, p=probs) r = rewards[draw] vals.append(np.sort(r)[-k:].mean()) return float(np.mean(vals)), float(np.std(vals)) if __name__ == "__main__": env = BitSequenceEnv(length=8, p_fail=0.9) X = list(itertools.product([0, 1], repeat=8)) R = np.array([env.reward(x) for x in X]) RMAX = R.max() policies = { "target P*(x) ∝ R(x)": R / R.sum(), "uniform (untrained)": np.ones_like(R) / len(R), "anti-target P(x) ∝ 1/R(x)": (1.0 / R) / (1.0 / R).sum(), "greedy (all mass on best)": (R == R.max()).astype(float) / (R == R.max()).sum(), } out = {} print(f"BitSequence |X| = {len(X)}, reward range [{R.min():.3f}, {R.max():.3f}]") print(f"Metric: mean of top-100 rewards from 2048 samples, normalised by " f"R_max = {RMAX:.2f}\n") print(f"{'policy':30s} {'top-100 (norm)':>16s} {'mean reward':>14s}") for name, p in policies.items(): mu, sd = top_k_metric(R, p) mean_r = float((p * R).sum()) out[name] = {"top100_normalised": mu / RMAX, "std": sd / RMAX, "mean_reward_normalised": mean_r / RMAX} print(f"{name:30s} {mu/RMAX:16.3f} {mean_r/RMAX:14.3f}") lo = min(v["top100_normalised"] for v in out.values()) print(f"\nLowest achievable top-100 over all four policies: {lo:.3f}") print("Paper reports TB = 0.18 and ST-GFN = 0.86 on this metric.") print("A value of 0.18 is below what even an adversarial policy attains,") print("so the reported spread is not reachable under the stated protocol.") # how large does the state space have to be for the metric to discriminate? print("\nSame metric as sequence length grows (target vs uniform policy):") print(f"{'length':>7s} {'|X|':>8s} {'target':>9s} {'uniform':>9s} {'gap':>7s}") rows = [] for L in [8, 12, 16, 20]: e = BitSequenceEnv(length=L, p_fail=0.9) Xs = list(itertools.product([0, 1], repeat=L)) if L <= 16 else None if Xs is None: rng = np.random.RandomState(0) Xs = [tuple(rng.randint(0, 2, L)) for _ in range(200000)] Rl = np.array([e.reward(x) for x in Xs]) t, _ = top_k_metric(Rl, Rl / Rl.sum(), reps=40) u, _ = top_k_metric(Rl, np.ones_like(Rl) / len(Rl), reps=40) rows.append((L, len(Xs), t / Rl.max(), u / Rl.max())) print(f"{L:7d} {len(Xs):8d} {t/Rl.max():9.3f} {u/Rl.max():9.3f} " f"{(t-u)/Rl.max():7.3f}") out["length_scaling"] = [ {"length": L, "n_states": n, "target": t, "uniform": u} for L, n, t, u in rows ] with open("../outputs/bitseq_metric_range.json", "w") as f: json.dump(out, f, indent=2)