got_q = sum( pr for m, pr in enumerate(d_out) if bool(m & bit_bc) == bool(bc) and bool(m & bit_yc) == bool(yc) ) require(abs(got_q - q[(bc, yc)]) <= 1e-12, f"H3 coated baseline 4-cell drift at ({bc},{yc})") return d_out def eval_exact_n_arm( arms: Sequence[tuple[float, float, float, float]], dists: Sequence[Sequence[float]], caps: Sequence[float], routes: Sequence[str], cell_classes: Sequence[str], compat_map: Mapping[str, FrozenSet[str]], *, coated_overlay_active: bool = False, coated_b_active: bool = False, coated_y_active: bool = False, coated_routes: FrozenSet[str] = frozenset(), coated_eligible_arm_mask: int = 0, compute_strassen: bool = True, compute_h23_lp: bool = False, ) -> dict[str, Any]: """ Exact 2^(4n) [and (n+2)-bit exposure-specific coated-overlay] portfolio evaluator. When coated_overlay_active=True: - d2 has (n+2) bits: bits 0..n-1 are EXACT unmodified uncoated H2 states; bit n = B_COAT (only pairs with R2_COAT); bit n+1 = Y_COAT (only pairs with R3P_COAT). - d4 has (n+2) bits: bits 0..n-1 are EXACT unmodified uncoated H4 routes; bit n = R2_COAT; bit n+1 = R3P_COAT. - cell_classes has (n+2) entries: 0..n-1 uncoated cell classes + n="COAT_B_ONLY" + n+1="COAT_Y_ONLY". - routes has (n+2) entries: 0..n-1 uncoated routes + n="ADV_COAT" + n+1="R3P_COAT". Guarantees ZERO spillover onto any uncoated pathway (Design A, X/D/Z, B/R1, Y/R4, R1&R4 joint)! """ n = len(arms) d1, d2, d3, d4 = dists n_arm_mask = (1 << n) - 1 for hi, dist in enumerate(dists): require(abs(sum(dist) - 1.0) <= 1e-11, f"H{hi+1} distribution sum != 1: {sum(dist)}") for i in range(n): got_m = sum(dist[m] for m in range(len(dist)) if (m >> i) & 1) require(abs(got_m - arms[i][hi]) <= 1e-11, f"Arm {i} H{hi+1} marginal mismatch: {got_m} vs {arms[i][hi]}") n_cbits = (n + 2) if coated_overlay_active else n n_rbits = len(routes) require(len(cell_classes) == n_cbits, f"cell_classes len {len(cell_classes)} != {n_cbits}") d12: dict[int, float] = {} bit_bc = 1 << n bit_yc = 1 << (n + 1) for m1, p1 in enumerate(d1): if p1 == 0.0: continue for m2, p2 in enumerate(d2): if p2 == 0.0: continue m = m1 & (m2 & n_arm_mask) if coated_overlay_active: if coated_b_active and (m1 & (1 << 1)) and (m2 & bit_bc): m |= bit_bc if coated_y_active and (m1 & (1 << 3)) and (m2 & bit_yc): m |= bit_yc if m: d12[m] = d12.get(m, 0.0) + p1 * p2 d3_has_coated_bits = coated_overlay_active and (len(d3) == (1 << (n + 2))) d123: dict[int, float] = {} for m12, p12 in d12.items(): for m3, p3 in enumerate(d3): if p3 == 0.0: continue m = (m12 & n_arm_mask) & (m3 & n_arm_mask) if coated_overlay_active: bc_m3 = bool(m3 & bit_bc) if d3_has_coated_bits else bool(m3 & (1 << 1)) yc_m3 = bool(m3 & bit_yc) if d3_has_coated_bits else bool(m3 & (1 << 3)) if (m12 & bit_bc) and bc_m3: m |= bit_bc if (m12 & bit_yc) and yc_m3: m |= bit_yc if m: d123[m] = d123.get(m, 0.0) + p12 * p3 p_cell_cls: dict[FrozenSet[str], float] = {} p_123_sum = 0.0 for m123, p123 in d123.items(): cls_set = frozenset(cell_classes[i] for i in range(n_cbits) if (m123 >> i) & 1) p_cell_cls[cls_set] = p_cell_cls.get(cls_set, 0.0) + p123 p_123_sum += p123 p_cell_cls[frozenset()] = max(0.0, 1.0 - p_123_sum) p_route_set: dict[FrozenSet[str], float] = {} d4_nz_rsets: list[tuple[int, float, FrozenSet[str]]] = [] for m4, p4 in enumerate(d4): if p4 == 0.0: continue r_set = frozenset(routes[k] for k in range(n_rbits) if (m4 >> k) & 1) p_route_set[r_set] = p_route_set.get(r_set, 0.0) + p4 d4_nz_rsets.append((m4, p4, r_set)) def is_mod_compat(cls_set: FrozenSet[str], r_set: FrozenSet[str]) -> bool: for cc in cls_set: if compat_map.get(cc, frozenset()) & r_set: return True return False p_locked = 0.0 p_modular = 0.0 p_modular_no_coated = 0.0 p_coated_only_by = 0.0 p_xdz_uncoated = 0.0 p_b_r1_uncoated = 0.0 p_a_only_with_coated_route = 0.0 p_non_eligible_coated_route_leak = 0.0 p_r1_and_r4_uncoated = 0.0 for m4, p4, r_set in d4_nz_rsets: if ("R1" in r_set) and ("R4" in r_set): p_r1_and_r4_uncoated += p4 xdz_mask = (1 << 2) | (1 << 4) | ((1 << 5) if n == 6 else 0) for m123, p123 in d123.items(): cls_set = frozenset(cell_classes[i] for i in range(n_cbits) if (m123 >> i) & 1) xdz_cls = frozenset(cell_classes[i] for i in range(n) if ((m123 & xdz_mask) >> i) & 1) b_unc_on = bool(m123 & (1 << 1)) for m4, p4, r_set in d4_nz_rsets: w = p123 * p4 locked_hit = bool((m123 & n_arm_mask) & (m4 & n_arm_mask)) if coated_overlay_active: if coated_b_active and (m123 & bit_bc) and (m4 & bit_bc): locked_hit = True if coated_y_active and (m123 & bit_yc) and (m4 & bit_yc): locked_hit = True if locked_hit: p_locked += w uncoated_r = r_set - coated_routes if is_mod_compat(cls_set, uncoated_r): p_modular_no_coated += w if coated_overlay_active: if (coated_b_active and (m123 & bit_bc) and ("ADV_COAT" in r_set)) or (coated_y_active and (m123 & bit_yc) and ("R3P_COAT" in r_set)): p_coated_only_by += w if xdz_cls and is_mod_compat(xdz_cls, uncoated_r): p_xdz_uncoated += w if b_unc_on and ("R1" in uncoated_r): p_b_r1_uncoated += w if is_mod_compat(cls_set, r_set): p_modular += w if coated_routes: if (m123 & n_arm_mask) == 1 and not (m123 & ~n_arm_mask) and not is_mod_compat(cls_set, uncoated_r): p_a_only_with_coated_route += w if not (m123 & coated_eligible_arm_mask) and not is_mod_compat(cls_set, uncoated_r): p_non_eligible_coated_route_leak += w require( p_a_only_with_coated_route == 0.0, f"Physical Design-A modular coated-route leak detected: {p_a_only_with_coated_route}", ) require( p_non_eligible_coated_route_leak == 0.0, f"Non-whitelisted product coated-route modular leak detected: {p_non_eligible_coated_route_leak}", ) p_ceiling = caps[0] * caps[1] * caps[2] * caps[3] a_m = [sum(dist[m] for m in range(len(dist)) if (m & 1)) for dist in dists] ad_h2 = [0.0, 0.0, 0.0, 0.0] for m, p in enumerate(d2): ad_h2[((m & 1) << 1) | ((m >> 4) & 1)] += p out: dict[str, Any] = { "locked_exact": p_locked * 100.0, "modular_exact": p_modular * 100.0, "ceiling_exact": p_ceiling * 100.0, "locked_pct": round(p_locked * 100.0, 4), "modular_pct": round(p_modular * 100.0, 4), "ceiling_pct": round(p_ceiling * 100.0, 4), "no_coated_routes_modular_exact": p_modular_no_coated * 100.0, "no_coated_routes_modular_pct": round(p_modular_no_coated * 100.0, 4), "coated_only_by_modular_exact": p_coated_only_by * 100.0, "coated_only_by_modular_pct": round(p_coated_only_by * 100.0, 4), "uncoated_xdz_only_modular_exact": p_xdz_uncoated * 100.0, "uncoated_xdz_only_modular_pct": round(p_xdz_uncoated * 100.0, 4), "uncoated_b_r1_marginal_exact": p_b_r1_uncoated * 100.0, "uncoated_b_r1_marginal_pct": round(p_b_r1_uncoated * 100.0, 4), "uncoated_r1_and_r4_joint_exact": p_r1_and_r4_uncoated * 100.0, "uncoated_r1_and_r4_joint_pct": round(p_r1_and_r4_uncoated * 100.0, 4), "hurdle_caps_exact_pct": [c * 100.0 for c in caps], "hurdle_caps_pct": [round(c * 100.0, 4) for c in caps], "hurdle_any_success_exact_pct": [(1.0 - d[0]) * 100.0 for d in dists], "hurdle_any_success_pct": [round((1.0 - d[0]) * 100.0, 4) for d in dists], "design_a_4_marginals_exact": a_m, "design_a_standalone_exact": a_m[0] * a_m[1] * a_m[2] * a_m[3], "design_a_d_h2_joint_4cell_exact": ad_h2, "design_a_only_coated_route_modular_mass_pp": 0.0, "non_eligible_product_coated_route_modular_leak_pp": 0.0, } if compute_strassen: s_min, s_max = bipartite_strassen_bounds(p_cell_cls, p_route_set, is_mod_compat) out["strassen_h123_h4_exact"] = [s_min * 100.0, s_max * 100.0] out["strassen_h123_h4_pct"] = [round(s_min * 100.0, 4), round(s_max * 100.0, 4)] if compute_h23_lp: # Build exact class-level H2 and H3 projections for sharp H2-H3 transportation LP d1_nz = [(m1, p1) for m1, p1 in enumerate(d1) if p1 > 1e-15] # Reduce d2 and d3 over nonzero states nz2 = [(m, p) for m, p in enumerate(d2) if p > 1e-15] nz3 = [(m, p) for m, p in enumerate(d3) if p > 1e-15] payoff_cache: dict[tuple[int, int], float] = {} c_min: list[list[float]] = [] c_max: list[list[float]] = [] for m2, _ in nz2: row_min: list[float] = [] row_max: list[float] = [] for m3, _ in nz3: key23 = (m2, m3) val = payoff_cache.get(key23) if val is None: val = 0.0 for m1, p1 in d1_nz: m123 = m1 & (m2 & n_arm_mask) & (m3 & n_arm_mask) if coated_overlay_active: bc_m3_lp = bool(m3 & bit_bc) if d3_has_coated_bits else bool(m3 & 2) yc_m3_lp = bool(m3 & bit_yc) if d3_has_coated_bits else bool(m3 & 8) if coated_b_active and (m1 & 2) and (m2 & bit_bc) and bc_m3_lp: m123 |= bit_bc if coated_y_active and (m1 & 8) and (m2 & bit_yc) and yc_m3_lp: m123 |= bit_yc if m123 == 0: continue cls_set = frozenset(cell_classes[i] for i in range(n_cbits) if (m123 >> i) & 1) p_r_ok = sum(pr for r_set, pr in p_route_set.items() if is_mod_compat(cls_set, r_set)) val += p1 * p_r_ok payoff_cache[key23] = val row_min.append(val) row_max.append(-val) c_min.append(row_min) c_max.append(row_max) sup = [p for _, p in nz2] dem = [p for _, p in nz3] h23_min = solve_transportation_min_cost(sup, dem, c_min) h23_max = -solve_transportation_min_cost(sup, dem, c_max) out["sharp_h23_lp_exact"] = [h23_min * 100.0, h23_max * 100.0] out["sharp_h23_lp_pct"] = [round(h23_min * 100.0, 4), round(h23_max * 100.0, 4)] return out def eval_wave7_portfolio( name: str, arms_uncoated: Sequence[tuple[float, float, float, float]], *,