#!/usr/bin/env python3 """Independent labeled-position enumeration for the new exact toy artifact.""" import collections import hashlib import itertools import json import math import resource import sys import time from fractions import Fraction from pathlib import Path def words_from_counts(counts,prefix=()): if not sum(counts.values()): yield prefix else: for value in sorted(counts): if counts[value]: remaining=dict(counts);remaining[value]-=1 yield from words_from_counts(remaining,prefix+(value,)) def direct_score(b): g=list(b)+[6]+list(b)[::-1] return max(a+c for a,c in zip(g,g[1:]+g[:1])) def labeled_distribution(b,T): free=[i for i in range(len(b)) if b[i]<=T] pool=[b[i] for i in free] count=collections.Counter() for labels in itertools.permutations(range(len(pool))): w=list(b) for i,j in zip(free,labels): w[i]=pool[j] count[direct_score(w)]+=1 multiplicity=math.prod(math.factorial(n) for n in collections.Counter(pool).values()) assert all(n%multiplicity==0 for n in count.values()) return {k:n//multiplicity for k,n in count.items()} def pair(f): return [f.numerator,f.denominator] def main(): resource.setrlimit(resource.RLIMIT_CPU,(15,20)) cpu0,t=time.process_time(),time.monotonic() path=Path(sys.argv[1]) if len(sys.argv)>1 else Path('conditional1058.json') x=json.loads(path.read_text()) assert x['job']==1058 and x['kind']=='exact_toy_not_arithmetic_data' wanted=[(6,18,6,24,12,30),(6,18,12,30,6,24),(6,24,18,6,12,30)] assert [tuple(r['half_word']) for r in x['profiles']]==wanted for r in x['profiles']: b=r['half_word'] assert r['full_word']==b+[6]+b[::-1] assert r['A1']==max(b)==30 and r['A2']==direct_score(b)==42 assert r['period']==2*sum(b)+6==198 assert r['half_counts']==[[k,n] for k,n in sorted(collections.Counter(b).items())] assert [p['threshold'] for p in r['profiles']]==[30,24,18,12,6] for a in r['profiles']: T=a['threshold'];counts=labeled_distribution(b,T);n=sum(counts.values()) assert a['fixed_positions']==[[i,v] for i,v in enumerate(b) if v>T] assert a['arrangements']==n assert a['score_counts']==[[k,counts[k]] for k in sorted(counts)] mean=Fraction(sum(k*v for k,v in counts.items()),n) var=Fraction(sum(k*k*v for k,v in counts.items()),n)-mean*mean assert a['mean']==pair(mean) and a['population_variance']==pair(var) assert a['exact_maximum_from_sparse_map']==max(counts) if var==0: assert a['z_status']=='degenerate' and a['z_sign'] is None and a['z_squared'] is None else: delta=42-mean assert a['z_status']=='defined' assert a['z_sign']==(0 if delta==0 else (1 if delta>0 else -1)) assert a['z_squared']==pair(delta*delta/var) # Different histogram or A2 would weaken the non-identifiability witness. assert len({json.dumps(r['half_counts']) for r in x['profiles']})==1 laws=[r['profiles'][3]['score_counts'] for r in x['profiles']] assert laws==[[[36,2],[42,1]],[[36,1],[42,2]],[[42,3]]] universe=list(words_from_counts(collections.Counter(wanted[0]))) assert len(universe)==x['complete_half_words']==360 complete=degenerate_words=cases=0 assert x['thresholds_checked']==[0,6,12,18,24,30] for b in universe: for T in x['thresholds_checked']: counts=labeled_distribution(b,T);n=sum(counts.values());complete+=n if T