"""Two Gates, experiment 2. Python 3 standard library. No training on test edges.""" import random from collections import Counter from fractions import Fraction from experiment import train N, K, EPOCHS, ROUNDS = 16, 4, 4000, 12000 assert EPOCHS*(K-1) == ROUNDS def pool(seed): rng = random.Random(seed) def table(): return [[rng.uniform(-.01,.01) for _ in range(2)] for _ in range(2)] S, R = [table() for _ in range(K)], [table() for _ in range(K)] edges = [(i,j) for i in range(K) for j in range(K) if i!=j] for t in range(EPOCHS): eps = max(.05, .5 * (1 - t / EPOCHS)); rng.shuffle(edges) def act(row): return rng.randrange(2) if rng.random()row[0]) for i,j in edges: g=rng.randrange(2); s=S[i][g]; b=act(s); r=R[j][b]; a=act(r); w=int(a==g) for row,k in [(s,b),(r,a)]: row[k]+=.08*(w-row[k]) def freeze(tables): return [tuple(int(y>x) for x,y in q) for q in tables] return list(zip(freeze(S),freeze(R))) def score(groups, edges, reverse_receiver_labels=False): h=n=0 for p,i,q,j in edges: s,r=groups[p][i][0], groups[q][j][1] for g in (0,1): h+=r[s[g]^reverse_receiver_labels]==g; n+=1 return Fraction(h,n),h,n if __name__=='__main__': # Populations contain K scouts and K keepers in both treatments. independent=[[train(10000+p*K+i) for i in range(K)] for p in range(N)] rotating=[pool(20000+p) for p in range(N)] all_edges=[(p,i,q,j) for p in range(N) for i in range(K) for q in range(N) for j in range(K)] for name,groups in [('fixed', independent),('rotating',rotating)]: bins={'within: same index':[e for e in all_edges if e[0]==e[2] and e[1]==e[3]], 'within: different index':[e for e in all_edges if e[0]==e[2] and e[1]!=e[3]], 'independent pools':[e for e in all_edges if e[0]!=e[2]]} for k,edges in bins.items(): f,h,n=score(groups,edges);print(name,k, f'{h}/{n} = {float(f):.3%}') if name=='rotating': print('per-pool withheld accuracy:', Counter(score(groups,[e for e in bins['within: same index'] if e[0]==p])[0] for p in range(N))) print('scout codebooks by pool:', [dict(Counter(s for s,_ in group)) for group in groups]) f,_,_=score(groups,bins['independent pools']); rev,_,_=score(groups,bins['independent pools'],True) print('independent pools, randomized receiver bit labels:',float((f+rev)/2))