#!/usr/bin/env python3 """Diagnostic (job #1843): why does the dynamic-order branch and bound explode? Runs three variants of the exact max-cover search on the SAME window (start 2207, the first capacity-33 window of [0,6000)) using the SAME masks, and reports node counts and the value found: A fixed order 41..73, bound over the unassigned suffix, incumbent leaves only (job1838-ext.py) B fixed order 41..73, bound over the unassigned suffix, incumbent at every node C DYNAMIC order (descending residual top), incumbent at every node (kstar_census.rs) Same masks, same bound values at each node; only the traversal order differs. Node counts print to stdout as a diagnostic, not as an artifact. """ import importlib.util import sys spec = importlib.util.spec_from_file_location("eng", "job1838-ext.py") eng = importlib.util.module_from_spec(spec) spec.loader.exec_module(eng) xs = eng.survivors(int(6000 * 5.9) + 1000) idx = {v: i for i, v in enumerate(xs)} j = idx[2207] masks = eng.kill_sets(xs, j) NQ, FULL, L = eng.NQ, eng.FULL, eng.L opts = [] for k in range(NQ): seen = {} for a, mm in enumerate(masks[k]): if mm and mm not in seen: seen[mm] = a opts.append(sorted(seen.items(), key=lambda t: -bin(t[0]).count("1"))) def tops(cov, klist): rem = FULL & ~cov out = [] for k in klist: t = 0 for mm, _ in opts[k]: c = bin(mm & rem).count("1") if c > t: t = c out.append((t, k)) return out def run_dynamic(): best = [0] nodes = [0] def rec(cov, used): nodes[0] += 1 c = bin(cov).count("1") if c > best[0]: best[0] = c order = [(t, k) for t, k in tops(cov, [k for k in range(NQ) if not used >> k & 1]) if t > 0] order.sort(key=lambda p: (-p[0], p[1])) bnd = c + sum(t for t, _ in order) if bnd <= best[0]: return for _, k in order: for mm, a in opts[k]: if mm & ~cov == 0: continue rec(cov | mm, used | 1 << k) rec(0, 0) return best[0], nodes[0] def run_fixed(incumbent_at_nodes): best = [0] nodes = [0] def bound(k, cov): b = bin(cov).count("1") return b + sum(t for t, _ in tops(cov, list(range(k, NQ)))) def rec(k, cov): nodes[0] += 1 if k == NQ: c = bin(cov).count("1") if c > best[0]: best[0] = c return if incumbent_at_nodes: c = bin(cov).count("1") if c > best[0]: best[0] = c if bound(k, cov) <= best[0]: return for mm, a in opts[k]: rec(k + 1, cov | mm) rec(0, 0) return best[0], nodes[0] def run_static(order): """Fixed prime order `order`, bound over the unassigned part of that order, node incumbent.""" best = [0] nodes = [0] def rec(i, cov): nodes[0] += 1 c = bin(cov).count("1") if c > best[0]: best[0] = c rest = order[i:] bnd = c + sum(t for t, _ in tops(cov, rest)) if bnd <= best[0]: return k = order[i] for mm, a in opts[k]: rec(i + 1, cov | mm) rec(0, 0) return best[0], nodes[0] VARIANTS = [("A", "fixed/leaf-incumbent (job1838-ext.py)", lambda: run_fixed(False)), ("B", "fixed/node-incumbent", lambda: run_fixed(True)), ("C", "dynamic order, node-incumbent", run_dynamic), ("D", "reversed fixed 73..41", lambda: run_static(list(reversed(range(NQ))))), ("E", "static by descending top", lambda: run_static( [k for _, k in sorted(tops(0, list(range(NQ))), key=lambda p: (-p[0], p[1]))])), ("F", "static by ascending top", lambda: run_static( [k for _, k in sorted(tops(0, list(range(NQ))), key=lambda p: (p[0], p[1]))]))] want = sys.argv[1] if len(sys.argv) > 1 else None for tag, name, fn in VARIANTS: if want and tag != want: continue val, nodes = fn() print("%-38s value=%d nodes=%d" % (tag + " " + name, val, nodes), flush=True)