OpenIslet

scripts/verify_iip0055_hygiene_safecell_minifh_orthogonality.py

OpenIslet Wave-6 W6-C1 (Provisional IIP-0055 / DEC-136 / GATE-34 / GY-033;

script Release v0.8.0 gen-eb955f8266035aba 417 KB · 30 sections

Use this file in any AI chat (paste-only or attachment-capable):

Download as text (.txt) Download section (.txt)

Pinned raw file: https://openislet.org/files/gen-eb955f8266035aba/scripts/verify_iip0055_hygiene_safecell_minifh_orthogonality.py · SHA-256: d5b98e2ba1310333…

    out = [(1.0 - phi) * d_ind[s] + phi * d_pos[s] for s in range(n_states)]
    require_cond(all(v >= -1e-12 for v in out), "Negative frechet_joint mass")
    out = [max(0.0, v) for v in out]
    require_cond(abs(sum(out) - 1.0) < 1e-12, "frechet_joint mass sum != 1")
    return out


def frechet_couple(base_dist: Sequence[float], u_new: float, phi: float, event_fn: Callable[[int], bool]) -> List[float]:
    n0 = len(base_dist)
    require_cond(0.0 <= u_new <= 1.0 and 0.0 <= phi <= 1.0, f"Invalid frechet_couple u={u_new} phi={phi}")
    require_cond(abs(sum(base_dist) - 1.0) < 1e-12, "frechet_couple base_dist sum != 1")
    p_e1 = sum(base_dist[mi] for mi in range(n0) if event_fn(mi))
    p_e0 = 1.0 - p_e1
    d_ind = [0.0] * (2 * n0)
    for mi in range(n0):
        w = base_dist[mi]
        d_ind[mi] = w * (1.0 - u_new)
        d_ind[mi | n0] = w * u_new
    d_pos = [0.0] * (2 * n0)
    p11 = min(p_e1, u_new)
    p10 = p_e1 - p11
    p01 = max(0.0, u_new - p_e1)
    p00 = p_e0 - p01
    for mi in range(n0):
        w = base_dist[mi]
        if w == 0.0:
            continue
        if event_fn(mi):
            frac = w / p_e1 if p_e1 > 0.0 else 0.0
            d_pos[mi] = p10 * frac
            d_pos[mi | n0] = p11 * frac
        else:
            frac = w / p_e0 if p_e0 > 0.0 else 0.0
            d_pos[mi] = p00 * frac
            d_pos[mi | n0] = p01 * frac
    out = [(1.0 - phi) * d_ind[k] + phi * d_pos[k] for k in range(2 * n0)]
    require_cond(all(v >= -1e-12 for v in out), "Negative frechet_couple mass")
    out = [max(0.0, v) for v in out]
    require_cond(abs(sum(out) - 1.0) < 1e-12, "frechet_couple mass sum != 1")
    return out


def _max_flow(cap: List[List[float]], src: int, snk: int) -> float:
    n = len(cap)
    flow = [[0.0] * n for _ in range(n)]
    total = 0.0
    while True:
        parent = [-1] * n
        parent[src] = src
        queue = [src]
        head = 0
        while head < len(queue) and parent[snk] == -1:
            u = queue[head]
            head += 1
            for v in range(n):
                if parent[v] == -1 and cap[u][v] - flow[u][v] > 1e-15:
                    parent[v] = u
                    queue.append(v)
        if parent[snk] == -1:
            break
        aug = float("inf")
        v = snk
        while v != src:
            u = parent[v]
            aug = min(aug, cap[u][v] - flow[u][v])
            v = u
        v = snk
        while v != src:
            u = parent[v]
            flow[u][v] += aug
            flow[v][u] -= aug
            v = u
        total += aug
    return total


def strassen_bipartite_bounds(
    v_h123: Sequence[float],
    dh4: Sequence[float],
    routes: Sequence[str],
    cell_classes: Sequence[str],
    compat_map: Mapping[str, FrozenSet[str]],
) -> Tuple[float, float]:
    n = len(routes)
    p_cell_cls: Dict[FrozenSet[str], float] = {}
    for mc, wc in enumerate(v_h123):
        if wc <= 1e-15:
            continue
        cs = frozenset(cell_classes[i] for i in range(n) if (mc >> i) & 1)
        p_cell_cls[cs] = p_cell_cls.get(cs, 0.0) + wc
    p_route_set: Dict[FrozenSet[str], float] = {}
    for mh, wh in enumerate(dh4):
        if wh <= 1e-15:
            continue
        rs = frozenset(routes[i] for i in range(n) if (mh >> i) & 1)
        p_route_set[rs] = p_route_set.get(rs, 0.0) + wh

    def is_compat(cs: FrozenSet[str], rs: FrozenSet[str]) -> bool:
        return any(bool(compat_map.get(cc, frozenset()) & rs) for cc in cs)

    left_keys = [k for k, v in p_cell_cls.items() if v > 1e-15]
    right_keys = [k for k, v in p_route_set.items() if v > 1e-15]
    nl, nr = len(left_keys), len(right_keys)
    src = 0
    snk = 1 + nl + nr

    def solve(use_compat: bool) -> float:
        cap = [[0.0] * (snk + 1) for _ in range(snk + 1)]
        for i, lk in enumerate(left_keys):
            cap[src][1 + i] = p_cell_cls[lk]
        for j, rk in enumerate(right_keys):
            cap[1 + nl + j][snk] = p_route_set[rk]
        for i, lk in enumerate(left_keys):
            for j, rk in enumerate(right_keys):
                if is_compat(lk, rk) == use_compat:
                    cap[1 + i][1 + nl + j] = 1.0
        return _max_flow(cap, src, snk)

    p_max = min(1.0, solve(True))
    p_min = max(0.0, 1.0 - solve(False))
    return p_min * 100.0, p_max * 100.0


def eval_exact_n_arm(
    arms: Sequence[Tuple[float, float, float, float]],
    dists: Sequence[List[float]],
    eff_ceils: Sequence[float],
    routes: Sequence[str],
    cell_classes: Sequence[str],
    compat_map: Mapping[str, FrozenSet[str]],
) -> Dict[str, Any]:
    n = len(arms)
    n_states = 1 << n
    # Enforce home-route GY-027(1) compatibility on BOTH locked and modular modes
    for i in range(n):
        allowed = compat_map.get(cell_classes[i], frozenset())
        require_cond(routes[i] in allowed, f"Home-route violation: arm {i} ({cell_classes[i]}) assigned {routes[i]} not in {sorted(allowed)}")

    for h in range(4):
        require_cond(len(dists[h]) == n_states, f"H{h + 1} len != {n_states}")
        require_cond(all(v >= -1e-12 for v in dists[h]), f"Negative mass in H{h + 1}")
        require_cond(abs(sum(dists[h]) - 1.0) < 1e-10, f"H{h + 1} distribution sum != 1")
        require_cond(eff_ceils[h] >= (1.0 - dists[h][0]) - 1e-12, f"Incoherent ceiling H{h + 1}: {eff_ceils[h]} < {1.0 - dists[h][0]}")
        for i in range(n):
            marg = sum(dists[h][m] for m in range(n_states) if (m >> i) & 1)
            require_cond(abs(marg - arms[i][h]) < 1e-9, f"Arm {i} H{h + 1} marginal {marg} != {arms[i][h]}")

    v = list(dists[0])
    for h in (1, 2):
        nxt = [0.0] * n_states
        dh = dists[h]
        for m1 in range(n_states):
            w1 = v[m1]
            if w1 == 0.0:
                continue
            for m2 in range(n_states):
                w2 = dh[m2]
                if w2 != 0.0:
                    nxt[m1 & m2] += w1 * w2
        v = nxt
    v_h123 = v
    dh4 = dists[3]

    locked = 0.0
    mod100 = 0.0
    for mc in range(1, n_states):
        wc = v_h123[mc]
        if wc == 0.0:
            continue
        allowed_r: Set[str] = set()
        for i in range(n):
            if (mc >> i) & 1:
                allowed_r.update(compat_map[cell_classes[i]])
        for mh in range(1, n_states):
            wh = dh4[mh]
            if wh == 0.0:
                continue
            if (mc & mh) != 0:
                locked += wc * wh
            if any(routes[j] in allowed_r for j in range(n) if (mh >> j) & 1):
                mod100 += wc * wh

    s_min_exact, s_max_exact = strassen_bipartite_bounds(v_h123, dh4, routes, cell_classes, compat_map)
    locked_ex = locked * 100.0
    mod_ex = mod100 * 100.0
    ceil_ex = math.prod(eff_ceils) * 100.0
    caps_ex = [x * 100.0 for x in eff_ceils]
    any_ex = [(1.0 - d[0]) * 100.0 for d in dists]
    return {
        "n_arms": n,
        "state_space": 1 << (4 * n),
        "locked_exact": locked_ex,
        "modular_exact": mod_ex,
        "ceiling_exact": ceil_ex,
        "locked_pct": round(locked_ex, 4),
        "mod90_pct": round((0.90 * mod100 + 0.10 * locked) * 100.0, 4),
        "modular_pct": round(mod_ex, 4),
        "ceiling_pct": round(ceil_ex, 4),
        "hurdle_caps_exact_pct": caps_ex,
        "hurdle_caps_pct": [round(x, 4) for x in caps_ex],
        "hurdle_any_success_exact_pct": any_ex,
        "hurdle_any_success_pct": [round(x, 4) for x in any_ex],
        "strassen_h123_h4_exact": [s_min_exact, s_max_exact],
        "strassen_h123_h4_pct": [round(s_min_exact, 4), round(s_max_exact, 4)],
    }


def eval_3arm_core_l0() -> Dict[str, Any]:
    arms = [(0.88, 0.78, 0.57, 0.69), (0.88, 0.79, 0.64, 0.70), (0.88, 0.82, 0.73, 0.69)]
    c1, c2, c3, c4 = 0.88, 0.87, 0.89, 0.85
    d1 = [0.0] * 8
    d1[0] = 1.0 - c1
    d1[7] = c1
    d2_raw = frechet_joint([a[1] / c2 for a in arms], 0.0)
    d2 = [c2 * x for x in d2_raw]
    d2[0] += 1.0 - c2
    d3_raw = frechet_joint([a[2] / c3 for a in arms], 0.0)
    d3 = [c3 * x for x in d3_raw]
    d3[0] += 1.0 - c3
    u_r1 = 0.69 / c4
    u_r2 = 0.70 / c4
    d4 = [0.0] * 8
    d4[0] = (1.0 - c4) + c4 * (1.0 - u_r1) * (1.0 - u_r2)
    d4[2] = c4 * (1.0 - u_r1) * u_r2
    d4[5] = c4 * u_r1 * (1.0 - u_r2)
    d4[7] = c4 * u_r1 * u_r2
    return eval_exact_n_arm(
        arms, [d1, d2, d3, d4], [c1, c2, c3, c4],
        ("R1", "R2", "R1"), ("INC", "INC", "INC"), {"INC": frozenset({"R1", "R2"})},
    )


def eval_5_or_6_arm_scenario(
    arms: Sequence[Tuple[float, float, float, float]],
    *,
    c1_allo: float = 0.88,
    c1_univ: Optional[float] = None,
    c1_z: Optional[float] = None,
    phi_z_h1: float = 0.55,
    c2_inc: float = 0.87,
    c2_y: float = 0.87,
    c2_univ: float = 0.95,
    c2_auto: float = 0.93,
    phi_z_h2: float = 0.80,
    c3: float = 0.89,
    phi_z_h3: float = 0.80,
    c4_univ: float = 0.92,
    c4_pouch: float = 0.85,
    c4_r3p: float = 0.85,
    c4_r4: Optional[float] = None,