#!/usr/bin/env python3 # -*- coding: utf-8 -*- """Route 21 / job #1324: the SAME exact maximin LP as return #585, at a chosen slot count N. python3 run_maxmin_N.py Reuses route21_model.py and route21_maxmin_margin.py from return #585 byte-for-byte; the only change is that Instance is built with n=N slots instead of the module default 20 (solve_window looks the class up in its module at call time, so it is rebound here rather than edited). Prints one JSON record to stdout (the artifact); progress goes to stderr. Deterministic, stdlib only, no inputs. """ import functools, json, os, sys, time from fractions import Fraction as F HERE = os.path.dirname(os.path.abspath(__file__)); sys.path.insert(0, HERE) import route21_maxmin_margin as M from route21_model import Instance, Q_OLD, Q_NEW, LEAVING, ENTERING, W ws, N = int(sys.argv[1]), int(sys.argv[2]) M.Instance = functools.partial(Instance, n=N) # the one change against run_maxmin.py inst = Instance(ws, n=N) occ = {q: max(len(p) for p in pairs.values()) for q, pairs in inst.pairs_old.items()} print(f"window {ws} N={N} L={inst.span+1} sum_occ={sum(occ.values())}", file=sys.stderr, flush=True) t0 = time.time() _stdout = sys.stdout; sys.stdout = sys.stderr # solver's verbose lines -> stderr rec = M.solve_window(ws, verbose=True) sys.stdout = _stdout rec["sum_occ"] = sum(occ.values()); rec["occ"] = {str(k): v for k, v in occ.items()} out = {"window_start": ws, "N": N, "span_L": rec["span_L"], "sum_occ": rec["sum_occ"], "occ": rec["occ"], "exact": rec["exact"], "cuts": rec["cuts"], "iterations": rec["iterations"], "wall_s": round(time.time() - t0, 2), "slots": rec["slots"]} if rec["exact"]: w = [F(v) for v in rec["w"]] m = F(rec["max_min_margin"]); c = rec["argmin_phase"] delta, C = inst.delta(w) C47 = C[LEAVING] wE = sum(w[i] for i in inst.deleted[c]) Cp89 = inst.post_capacity(ENTERING, c, w)[0] D = sum(C[q] - inst.post_capacity(q, c, w)[0] for q in Q_OLD if q != LEAVING) # #567's identity at the argmin phase, and #585's collapsed form when the phase saturates C_47 ident = delta + (C47 - wE) + D - Cp89 avg_lb = 2 * (1 - C47) / 89 # C'_89(c) >= 2(1 - w(E_c))/89 at a saturating phase out.update({ "max_min_margin": str(m), "max_min_margin_float": float(m), "delta_at_optimum": str(delta), "argmin_phase": c, "positive_phases": rec["positive_phases"], "zero_phases": rec["zero_phases"], "max_margin": rec["max_margin"], "w": rec["w"], "w_denominator": rec["w_denominator"], "C_47": str(C47), "deleted_weight_at_argmin": str(wE), "argmin_saturates_C47": wE == C47, "Cprime_89_at_argmin": str(Cp89), "D_other_owners_loss_at_argmin": str(D), "identity_holds_at_argmin": ident == m, "collapsed_form_delta_minus_Cp89_minus_D": str(delta - Cp89 - D), "equals_delta_minus_Cp89_minus_D": (delta - Cp89 - D) == m, "avg_lower_bound_2(1-C47)/89": str(avg_lb), "avg_lower_bound_float": float(avg_lb), "Cp89_over_avg_lb": str(Cp89 / avg_lb) if avg_lb else None, "deficit_ratio_Cp89_over_delta": str(Cp89 / delta) if delta else None, "sum_C_old": str(sum(C.values())), "certificate_all_conditions": rec.get("restricted_lp_certificate", {}).get("all_conditions"), "profile": rec["profile"], }) else: out.update({k: rec[k] for k in ("stalled", "upper_bound_relaxation", "lower_bound_feasible")}) print(json.dumps(out, indent=1, sort_keys=True))