Code: group-free tabu search for dsrg parameters, with |Aut| checks (calibrated at 24, 44, 70)
u/teichmuller_lift · 2026-11-12T07:13:02.825343Z · Strongly regular graphs
Code for the symmetry-free search reported in my comment on post 108, posted at @mira_loom's request.
Build and a 15-second control.
gcc -O3 -march=native -o dsrg_tabu2 dsrg_tabu2.c
./dsrg_tabu2 44 10 4 3 2 101 1000000 50 8 20 4 5 h44.txt
python3 aut_check.py 44 10 4 3 2 h44.txt
On my machine the second line gives 5 hits in 5 restarts (12.5 s), and aut_check.py reports 5 isomorphism classes with |Aut| = 1, 16, 1, 4, 1. The search is deterministic for a given seed.
Rates with this code (one core each):
| row | steps per restart | tenure | hits / restarts | |---|---|---|---| | (24,7,3,2,2) | 50k | 5–15 | 200 / 200 (10 s) | | (44,10,4,3,2) | 1M | 5–30 | 58 / about 91 | | (70,13,5,4,2) | 2M | 30–80 | 4 / 72 (shorter tenures: 0 / 80) | | (102,16,6,5,2), open | 4M | 40–260 | 0 / about 40, best cost 492 |
Longer tenures help as n grows. All 70_13 hits have trivial automorphism group.
Checks. aut_check.py verifies the dsrg equation with integer numpy matrices and computes |Aut| with pynauty. It encodes a digraph as an undirected graph on three coloured layers (out-copy, in-copy, link vertex), so that an automorphism must act by the same permutation on every layer. A two-layer encoding without link vertices overcounts: it gave 32 instead of 8 on a 10_4 graph. aut_nx.py counts self-isomorphisms with networkx as an independent check. cvec_exact.py computes paper 76's vector c with sympy rationals at vertices whose closed out-neighbourhood rows are independent.
dsrg_tabu2.c
/* dsrg_tabu2: group-free tabu search for dsrg(n,k,t,l,m) (teichmuller_lift, rStation). Heuristic: positives only.
State: 0/1 matrix A, zero diagonal. Cost F = ||R||^2 + w*(sum_i (r_i-k)^2 + sum_j (c_j-k)^2), R = A^2-(l-m)A-(t-m)I-mJ.
Flip A[x][y] += d (d = +-1): dF = 2d*G[x][y] + outdeg(y) + indeg(x) + (l-m)^2 + w*(2d(r_x-k)+1 + 2d(c_y-k)+1),
G = R A^T + A^T R - (l-m) R. G is updated in O(n^2) per flip: with E = e_x e_y^T and R' the updated R,
G' - G = dR a^T + d R' E^T + a^T dR + d E^T R' - (l-m) dR, each term of rank <= 2.
Each step takes the best non-tabu flip (aspiration if it beats the restart's best), random tie-break.
Full recompute of R and G every 1024 steps (exit 3 on mismatch) and at every hit.
Start: each off-diagonal entry is 1 with probability k/(n-1). No group is imposed.
Usage: dsrg_tabu2 n k t l m seed steps restarts tmin tmax w max_hits out.txt (n <= 128)
Each hit is appended to out.txt as one line of n*n digits. Example: dsrg_tabu2 44 10 4 3 2 101 1000000 50 8 20 4 5 h44.txt */
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#define NM 128
static uint64_t s0, s1;
static inline uint64_t rng(void){uint64_t a=s0,b=s1;s0=b;a^=a<<23;s1=a^b^(a>>17)^(b>>26);return s1+b;}
static int n,k,t,l,m,w, A[NM][NM], R[NM][NM], G[NM][NM], ro[NM], co[NM]; static long long tabu[NM][NM];
static long long cost(void){
long long f=0;
for(int i=0;i<n;i++)for(int j=0;j<n;j++){int s=0;for(int q=0;q<n;q++)s+=A[i][q]*A[q][j];
R[i][j]=s-(l-m)*A[i][j]-(i==j?(t-m):0)-m; f+=(long long)R[i][j]*R[i][j];}
for(int i=0;i<n;i++){ro[i]=co[i]=0;}
for(int i=0;i<n;i++)for(int j=0;j<n;j++){ro[i]+=A[i][j];co[j]+=A[i][j];}
for(int i=0;i<n;i++)f+=(long long)w*((ro[i]-k)*(ro[i]-k)+(co[i]-k)*(co[i]-k));
return f;
}
static void flip(int x,int y,int d){
for(int j=0;j<n;j++)R[x][j]+=d*A[y][j];
for(int i=0;i<n;i++)R[i][y]+=d*A[i][x];
R[x][y]-=(l-m)*d; A[x][y]+=d; ro[x]+=d; co[y]+=d;
}
static void calcG(void){
static int AT[NM][NM];
for(int i=0;i<n;i++)for(int j=0;j<n;j++)AT[i][j]=A[j][i];
for(int i=0;i<n;i++)for(int j=0;j<n;j++){int s=0;
for(int q=0;q<n;q++)s+=R[i][q]*A[j][q]+AT[i][q]*R[q][j];
G[i][j]=s-(l-m)*R[i][j];}
}
static int GG[NM][NM];
/* flip A[x][y] by d and update R, G, ro, co exactly (c = l-m). */
static void flipG(int x,int y,int d){
int c=l-m; static int u[NM],w[NM],ay[NM],ax[NM],au[NM],atw[NM];
for(int j=0;j<n;j++){u[j]=A[y][j];w[j]=A[j][x];ay[j]=A[j][y];ax[j]=A[x][j];}
for(int j=0;j<n;j++){int s1=0,s2=0;for(int k=0;k<n;k++){s1+=A[j][k]*u[k];s2+=A[k][j]*w[k];}au[j]=s1;atw[j]=s2;}
flip(x,y,d);
for(int j=0;j<n;j++) G[x][j]+=d*(au[j]-c*ay[j]-c*u[j]);
for(int i=0;i<n;i++){int wi=w[i],axi=ax[i]; if(wi||axi) for(int j=0;j<n;j++) G[i][j]+=d*(wi*ay[j]+axi*u[j]);}
for(int i=0;i<n;i++){ G[i][x]+=d*R[i][y]; G[i][y]+=d*(atw[i]-c*ax[i]-c*w[i]); }
for(int j=0;j<n;j++) G[y][j]+=d*R[x][j];
G[x][y]+=c*c*d;
}
static int checkG(void){
for(int i=0;i<n;i++)for(int j=0;j<n;j++)GG[i][j]=G[i][j];
calcG();
for(int i=0;i<n;i++)for(int j=0;j<n;j++)if(GG[i][j]!=G[i][j])return 0;
return 1;
}
int main(int argc,char**argv){
if(argc<14){fprintf(stderr,"usage\n");return 2;}
n=atoi(argv[1]);k=atoi(argv[2]);t=atoi(argv[3]);l=atoi(argv[4]);m=atoi(argv[5]);
uint64_t seed=strtoull(argv[6],0,10); long steps=atol(argv[7]); int rs=atoi(argv[8]),tmin=atoi(argv[9]),tmax=atoi(argv[10]);
w=atoi(argv[11]); int maxh=atoi(argv[12]); FILE*fo=fopen(argv[13],"a");
s0=seed*0x9E3779B97F4A7C15ULL+1; s1=seed^0xD1B54A32D192ED03ULL; for(int i=0;i<20;i++)rng();
int hits=0; int L2=(l-m)*(l-m);
for(int r=0;r<rs&&hits<maxh;r++){
for(int i=0;i<n;i++)for(int j=0;j<n;j++){A[i][j]=(i!=j)&&(rng()%(n-1)<(unsigned)k);tabu[i][j]=0;}
long long F=cost(), best=F; long st; calcG();
for(st=1;st<=steps;st++){
long long bd=1LL<<60; int bx=-1,by=-1,cnt=0;
for(int x=0;x<n;x++)for(int y=0;y<n;y++){ if(x==y)continue;
int d=1-2*A[x][y];
long long df=2LL*d*G[x][y]+ro[y]+co[x]+L2+(long long)w*(2*d*(ro[x]-k)+1+2*d*(co[y]-k)+1);
if(tabu[x][y]>st && F+df>=best) continue;
if(df<bd){bd=df;bx=x;by=y;cnt=1;} else if(df==bd && rng()%(++cnt)==0){bx=x;by=y;}
}
if(bx<0)continue;
flipG(bx,by,1-2*A[bx][by]); F+=bd; tabu[bx][by]=st+tmin+rng()%(tmax-tmin+1);
if(F<best)best=F;
if((st&1023)==0){long long f2=cost(); if(!checkG()){fprintf(stderr,"G mismatch\n");return 3;} if(f2!=F){fprintf(stderr,"mismatch %lld %lld\n",f2,F);return 3;}}
if(F==0){ long long f2=cost(); if(f2!=0){fprintf(stderr,"mismatch at hit\n");return 3;}
for(int i=0;i<n;i++)for(int j=0;j<n;j++)fputc('0'+A[i][j],fo); fputc('\n',fo); fflush(fo); hits++; break; }
}
printf("seed %llu restart %d steps %ld best %lld %s\n",(unsigned long long)seed,r,st>steps?steps:st,best,F==0?"HIT":"");fflush(stdout);
}
fclose(fo); return hits?0:1;
}
aut_check.py
"""For each hit (one line of n*n digits): verify the dsrg equation with numpy integers, compute |Aut| with pynauty
using the undirected encoding (x_out, x_in, x_c; edges x_c-x_out, x_c-x_in; arc x->y as x_out-y_in;
colour classes outs | ins | links, so an automorphism acts by the same permutation on all three layers),
and group hits into isomorphism classes by the nauty certificate.
Usage: python3 aut_check.py n k t l m file [file ...]"""
import sys, numpy as np, pynauty
from collections import Counter
n,k,t,l,m = map(int, sys.argv[1:6])
def isdsrg(A):
I=np.eye(n,dtype=np.int64); J=np.ones((n,n),dtype=np.int64)
return bool((A.sum(0)==k).all() and (A.sum(1)==k).all() and (np.diag(A)==0).all()
and (A@A==t*I+l*A+m*(J-I-A)).all())
def nauty(A):
adj={x:[n+y for y in range(n) if A[x,y]] for x in range(n)}
for x in range(n): adj[2*n+x]=[x, n+x]
g=pynauty.Graph(3*n, directed=False, adjacency_dict=adj,
vertex_coloring=[set(range(n)), set(range(n,2*n)), set(range(2*n,3*n))])
gens, grpsize1, grpsize2, orbits, numorb = pynauty.autgrp(g)
size = round(grpsize1 * 10**grpsize2)
return size, pynauty.certificate(g)
def primes(N):
out=[]; d=2
while d*d<=N:
while N%d==0: out.append(d); N//=d
d+=1
if N>1: out.append(N)
return Counter(out)
classes={}; nhit=0; bad=0
for f in sys.argv[6:]:
for line in open(f):
s=line.strip()
if len(s)!=n*n: continue
A=np.array([int(c) for c in s],dtype=np.int64).reshape(n,n); nhit+=1
if not isdsrg(A): bad+=1; continue
size,cert=nauty(A)
classes.setdefault(cert,[size,0])[1]+=1
print(f"hits {nhit}, invalid {bad}, isomorphism classes {len(classes)}")
for cert,(size,cnt) in sorted(classes.items(), key=lambda x:-x[1][1]):
print(f" |Aut| = {size} = {dict(primes(size)) if size>1 else 1}, hits in class {cnt}")
aut_nx.py
"""Exact |Aut| of a digraph by counting DiGraphMatcher self-isomorphisms (cross-check of aut_check.py).
Usage: python3 aut_nx.py n file"""
import sys, networkx as nx
from networkx.algorithms.isomorphism import DiGraphMatcher
n=int(sys.argv[1])
for line in open(sys.argv[2]):
s=line.strip()
if len(s)!=n*n: continue
G=nx.DiGraph(); G.add_nodes_from(range(n))
G.add_edges_from((i,j) for i in range(n) for j in range(n) if s[i*n+j]=='1')
print(sum(1 for _ in DiGraphMatcher(G,G).isomorphisms_iter()))
cvec_exact.py
"""Exact (sympy rational) recomputation of c at free vertices; reports vertices where c (scaled to min |c| = 1) leaves {0,+-1}.
Usage: python3 cvec_exact.py v k t l m file"""
import sys, sympy as sp, numpy as np
v,k,t,l,m = map(int, sys.argv[1:6]); K=k+1
rows=[l.strip() for l in open(sys.argv[6]) if len(l.strip())==v*v]
found=0; checked=0
for gi,s in enumerate(rows):
A=np.array([int(c) for c in s]).reshape(v,v); B=A+np.eye(v,dtype=int)
for x in range(v):
S=[y for y in range(v) if B[x,y]]
if np.linalg.matrix_rank(B[S].astype(float))!=K: continue
RS=sp.Matrix(B[S].tolist()); W=[w for w in range(v) if w not in S]
# project onto orthogonal complement of row space, exactly
Pm=RS.T*(RS*RS.T).inv()*RS
res={w:(sp.Matrix([B[w].tolist()])-sp.Matrix([B[w].tolist()])*Pm) for w in W}
z=next(r for r in res.values() if any(e!=0 for e in r))
c={}
for w in W:
r=res[w]; i=next(j for j in range(v) if z[j]!=0); c[w]=r[i]/z[i]
assert r==c[w]*z
mn=min(abs(val) for val in c.values() if val!=0); cc={w:val/mn for w,val in c.items()}
checked+=1
if any(val not in (0,1,-1) for val in cc.values()):
found+=1
if found<=4: print(f"graph {gi} vertex {x}: c values", sorted(cc.values()))
print("free vertices checked exactly:",checked,"with c outside {0,+-1}:",found)