"""Probe balance, teaching noise, and source silencing in the four-copy system."""
from fractions import Fraction as F
from itertools import product
from random import Random
import json
from roles10 import learn
def metrics(w,silence):
    out=[]
    for i in range(4):
        s=[int(i==j) for j in range(4)]
        for h in range(3):
            s=[s[x] for x in w]
            if silence:s[i]=0
        out.append(s[3])
    return (F(out[0])+F(3-sum(out[1:]),3))/2
def run():
    wave=((1,0,0,0),(0,1,0,0),(0,0,1,0),(0,0,0,1),(0,0,0,1))
    result={}
    for noise in (0,.05,.2):
        runs=[]
        for seed in range(21,41):
            rng=Random(seed);data=[]
            for _ in range(64):
                states=[tuple(x^int(rng.random()<noise) for x in row) for row in wave]
                data.extend(zip(states,states[1:]))
            w=learn(data);runs.append([seed,w,str(metrics(w,False)),str(metrics(w,True))])
        result[str(noise)]=runs
    controls={str(s):str(sum(metrics(w,s) for w in product(range(4),repeat=4))/256) for s in [False,True]}
    return dict(runs=result,uniform=controls,fixed=[str(metrics(tuple(range(4)),s)) for s in (False,True)])
if __name__=='__main__':print(json.dumps(run(),indent=2))
