"""One 850 alpha4 three-arm trial with shared quotas/anchors, independent cuts.""" import argparse import hashlib import json from pathlib import Path import resource import time import numpy as np from scipy.sparse.csgraph import dijkstra if __package__: from .review_partition_scale import regions, histogram_control from .route_feasibility import graph_from_edges, partition_report, matched_acceptance from .capacity_partition_probe import capacity_assignment, boundary_summary else: from review_partition_scale import regions, histogram_control from route_feasibility import graph_from_edges, partition_report, matched_acceptance from capacity_partition_probe import capacity_assignment, boundary_summary def select_maximin(points, candidates, count, center, initial=()): selected=list(map(int,initial)) candidates=np.sort(np.asarray(candidates,int)) if len(selected)>count: raise ValueError('Initial selection exceeds count') while len(selected)0).sum(1)>1)), group_type_mass=th.tolist(),group_region_mass=rh.tolist()) def main(): p=argparse.ArgumentParser();p.add_argument('output',type=Path);out=p.parse_args().output cp=out/'execution_contract.json';contract=json.loads(cp.read_text())['O1'];case=contract['case'] with (out/'O1.started.json').open('x') as stream: json.dump({'contract_sha256':hashlib.sha256(cp.read_bytes()).hexdigest()},stream) start=time.monotonic() previous=Path('artifacts/routes-review-2026-11-13')/case cached=np.load(previous/'partitions.npz') sample=np.load(Path('artifacts/intake-2026-11-13/samples')/(case+'.npz')) olddir=Path('artifacts/capacity-projectors-2026-11-13')/case old_result=json.loads((olddir/'capacity_result.json').read_text());old_cuts=np.load(olddir/'candidate_partitions.npz') points,mass,edges=sample['points_raw'],sample['node_volume_raw'],sample['edges'] kind,old_seeds=cached['cleaned_type'],cached['seeds'] region,volume,_=regions(kind,edges,mass) new_seeds,changes=quota_anchors(points,mass,region,old_seeds,np.array(contract['quota'])) assert len(changes['new_nodes'])==9 and len(changes['retained_nodes'])==29 assert np.isclose(mass.sum()/len(new_seeds),contract['S']) np.savez_compressed(out/'common_quota_anchors.npz',old=old_seeds,new=new_seeds,region=region,quota=contract['quota']) (out/'anchor_changes.json').write_text(json.dumps(changes,indent=2)+'\n') length=np.linalg.norm(points[edges[:,0]]-points[edges[:,1]],axis=1) typed=length*(1+contract['alpha']*(kind[edges[:,0]]!=kind[edges[:,1]])) weights={'generic':length,'type_4':typed,'control_4':histogram_control(typed,length)} assert np.array_equal(np.sort(weights['type_4']),np.sort(weights['control_4'])) hashes={name:hashlib.sha256(w.tobytes()).hexdigest() for name,w in weights.items()} assert all(digest==old_result['records'][name]['cost_hash_before'] for name,digest in hashes.items()) (out/'O1_input_hashes.json').write_text(json.dumps(dict(cost_hashes=hashes,shared_anchor_sha256=hashlib.sha256(new_seeds.tobytes()).hexdigest(),shared_quota=contract['quota']),indent=2)+'\n') graph=graph_from_edges(len(points),edges,length) records,labels_by_arm={},{} for name,w in weights.items(): tick=time.monotonic() distance=dijkstra(graph_from_edges(len(points),edges,w),directed=False,indices=new_seeds) labels,solver=capacity_assignment(distance,mass,bound=contract['capacity_fraction'],maxiter=contract['maxiter']) assert hashlib.sha256(w.tobytes()).hexdigest()==hashes[name] complete=len(np.unique(labels))==contract['K'] report=partition_report(labels,graph,edges,points,mass,kind) if complete else dict(k=len(np.unique(labels)),all_connected=False) report.update(capacity=solver,own_anchor_in_cell=bool(np.array_equal(labels[new_seeds],np.arange(len(new_seeds)))), purity=purity_metrics(labels,mass,kind,region,new_seeds), old_anchor_purity=purity_metrics(old_cuts[name],mass,kind,region,old_seeds), seconds=time.monotonic()-tick,cost_hash_before=hashes[name],cost_hash_after=hashlib.sha256(w.tobytes()).hexdigest()) report['capacity_connectivity_anchor_pass']=bool(complete and solver['capacity_feasible'] and report['all_connected'] and report['own_anchor_in_cell']) report['boundary']=boundary_summary(labels,points,sample['boundary_triangles'],mass) if complete else None labels_by_arm[name]=labels;records[name]=report np.save(out/(name+'_labels.npy'),labels) (out/(name+'_result.json')).write_text(json.dumps(report,indent=2,allow_nan=False)+'\n') print(name,'feasible',report['capacity_connectivity_anchor_pass'],'exchange',report['purity']['anchor_region_exchange_mass_fraction'],'type purity',report['purity']['dominant_type_volume_purity'],flush=True) checks={} for first,second in [('type_4','generic'),('control_4','generic'),('type_4','control_4')]: if records[first]['k']==38 and records[second]['k']==38: checks[first+'__'+second]=matched_acceptance(records[first],records[second]) result=dict(contract=contract,records=records,matching=checks, all_defined_checks_pass=all(r['capacity_connectivity_anchor_pass'] for r in records.values()) and len(checks)==3 and all(c['pass_provisional'] for c in checks.values()), different_partition_node_fractions={a+'__'+b:float(np.mean(labels_by_arm[a]!=labels_by_arm[b])) for a,b in [('type_4','generic'),('control_4','generic'),('type_4','control_4')]}, wall_seconds=time.monotonic()-start,peak_rss_kib=resource.getrusage(resource.RUSAGE_SELF).ru_maxrss, full_acceptance='NOT_ESTABLISHED_missing_frozen_pool_items; purity is not model benefit') (out/'O1_result.json').write_text(json.dumps(result,indent=2,allow_nan=False)+'\n') if __name__=='__main__': main()