"""Check transport identities without running the flow solver. Independently enumerate block orbits, stream and balance every edge, and compare every primitive kernel at two integer points with a labelled-subset placement DP. Also check the capacity-six closed-set obstruction directly. """ import sys,math,json,time,argparse,gzip,hashlib from pathlib import Path from collections import Counter,defaultdict from functools import lru_cache from itertools import combinations from itertools import permutations from fractions import Fraction sys.path.insert(0,str(Path(__file__).resolve().parents[2]/'scripts')) from certify import orbit_shapes def splits(shape,l): counts=Counter(shape);vs=sorted(counts,reverse=True) def rec(j,left,up,low): if j==len(vs): if left==0:yield tuple(up),tuple(low) return v=vs[j];count=counts[v] for k in range(min(count,left)+1):yield from rec(j+1,left-k,up+[v]*k,low+[v]*(count-k)) return rec(0,l,[],[]) @lru_cache(None) def mono(values,exponents): # Independent labelled-row DP followed by exact equal-row factorial division. n=len(values);d={0:1} for r in exponents: out=defaultdict(int) for mask,v in d.items(): for i,p in enumerate(values): if not mask>>i&1:out[mask|1<>i&1:continue nxt=mask|1<>i&1] for overlap in combinations(labels,h): other=(full^mask)|sum(1<0 for e in [u,v]: assert len(e)==n and all(type(a) is int and a>=0 for a in e) and sum(e)==degree assert tuple(sorted(e[:l],reverse=True)+sorted(e[l:],reverse=True))==e assert sum(u[:l])-sum(v[:l])==1 assert all(a>=b for a,b in zip(u[:l],v[:l])) assert all(a<=b for a,b in zip(u[l:],v[l:])) balances[u]+=amount;balances[v]-=amount digest.update(line.encode());count+=1 assert {e:b for e,b in balances.items() if b}==masses assert count==record['positive_flows'] and digest.hexdigest()==record['flow_sha256'] return count def check_obstruction(path): record=json.loads(path.read_text())['records'][0] c,n,l=record['capacity'],record['support'],record['cut'] assert (c,n,l)==(6,12,1) masses=independent_masses(c,n,l);generators=record['generators'] # Every reachable state has upper partition componentwise smaller and lower # partition componentwise larger. This characterization proves closure. def contains(e): return any(all(a<=b for a,b in zip(e[:l],g[:l])) and all(a>=b for a,b in zip(e[l:],g[l:])) for g in generators) selected={e:b for e,b in masses.items() if contains(e)} assert selected=={tuple(e):b for e,b in record['nonzero_masses_in_closed_set']} assert sum(selected.values())==record['deficiency']==39916800 # Enumerate all states independently through a basic partition recursion. def partition(d,k,cap): if k==0: if d==0:yield () return for a in range(min(cap,d),-1,-1): if d-a>(k-1)*a:continue for rest in partition(d-a,k-1,a):yield (a,)+rest size=sum(contains((a,)+lower) for a in range(33) for lower in partition(32-a,11,32)) assert size==record['closed_set_size']==5048 for p in [list(range(12,0,-1)),[37,31,29,23,19,17,13,11,7,5,3,2]]: assert orbit_eval(c,n,l,p)==direct_eval(c,n,l,p) return {'capacity':6,'support':12,'cut':1,'closed_states':size, 'nonzero_masses':len(selected),'net_mass':sum(selected.values()), 'generators':len(generators),'independent_integer_points':2,'passed':True} def check_occupancy_asymmetry(): strengths=[1,2,3];states=[] for assignment in permutations(range(3),2): weight=math.prod(strengths[i]**(r+1) for r,i in enumerate(assignment)) states.append((assignment,weight)) normalizer=sum(w for _,w in states) def mean(fun):return sum(Fraction(w,normalizer)*fun(a) for a,w in states) def covariance(i,k): occupied=lambda a:int(k in a) score=lambda a:sum(r+1 for r,j in enumerate(a) if j==i) return mean(lambda a:occupied(a)*score(a))-mean(occupied)*mean(score) g12=-covariance(0,1)/strengths[0]/strengths[1] g21=-covariance(1,0)/strengths[1]/strengths[0] assert normalizer==48 and g12==Fraction(37,384) and g21==Fraction(38,384) return {'N':3,'C':2,'strengths':strengths,'normalizer':normalizer, 'minus_d1_pi2_divided_by_strength2':str(g12), 'minus_d2_pi1_divided_by_strength1':str(g21), 'symmetric':False,'passed':True} def main(): parser=argparse.ArgumentParser(description=__doc__) base=Path(__file__).parent parser.add_argument('--manifest',type=Path,default=base/'results/transport_certificates.json') parser.add_argument('--certificate-dir',type=Path,default=Path('work/zipf-transport')) parser.add_argument('--out',type=Path,default=base/'results/transport_verification.json') args=parser.parse_args() manifest=json.loads(args.manifest.read_text()) records=manifest['records'];maximum=manifest['max_capacity_requested'] expected={(c,n,l) for c in range(1,maximum+1) for n in range(c+1,2*c+1) for l in range(1,n//2+1)} assert {(r['capacity'],r['support'],r['cut']) for r in records}==expected flows=[] for record in records: count=check_certificate(record,args.certificate_dir/record['certificate_file']) flows.append({'capacity':record['capacity'],'support':record['support'], 'cut':record['cut'],'checked_flows':count,'passed':True}) print('checked flow',record['capacity'],record['support'],record['cut'],count,flush=True) probes=[];primes=[2,3,5,7,11,13,17,19,23,29,31,37,41] for c in range(1,maximum+1): for n in range(c+1,2*c+2): for l in range(1,n//2+1): for p in [list(range(n,0,-1)),primes[:n][::-1]]: a=orbit_eval(c,n,l,p);b=direct_eval(c,n,l,p) assert a==b and a>0,(c,n,l,a,b) probes.append({'capacity':c,'support':n,'cut':l,'integer_points':2,'passed':True}) mono.cache_clear() obstruction=check_obstruction(base/'results/transport_obstruction.json') result={'manifest_sha256':hashlib.sha256(args.manifest.read_bytes()).hexdigest(), 'flows':flows,'independent_placement_probes':probes,'obstruction':obstruction, 'occupancy_asymmetry':check_occupancy_asymmetry()} args.out.write_text(json.dumps(result,indent=2)+'\n') print(json.dumps({'flow_cases':len(flows),'positive_flows':sum(r['checked_flows'] for r in flows), 'integer_points':sum(r['integer_points'] for r in probes), 'obstruction':obstruction}),flush=True) if __name__=='__main__':main()