"""How many named relay assignments remain compatible with the observed events?"""
from fractions import Fraction as F
from itertools import permutations
import json
from experience import score,FIRST


def compatible(groups,teachers):
    if teachers is None: return list(permutations(range(4)))
    return [p for p in permutations(range(4)) if all({p[i] for i in group}==obs for group,obs in zip(groups,teachers))]


def experiment():
    groups=[(0,1),(2,),(3,)]
    targets=[{FIRST[i] for i in group} for group in groups]
    stages=[compatible(groups,None),compatible(groups,targets),compatible(groups+[(0,)],targets+[{FIRST[0]}])]
    accuracy=[]
    for options in stages:
        scores=[F(score([p.index(r) for r in range(4)],FIRST)['balanced']) for p in options]
        accuracy.append(str(sum(scores)/len(scores)))
    assert list(map(len,stages))==[24,2,1]
    return dict(compatible=list(map(len,stages)),mean_balanced=accuracy,confounded=stages[1],
                active_links=4,extra_observation=[[0],[FIRST[0]]])


if __name__=='__main__': print(json.dumps(experiment(),indent=2))
