# +1 and -1 denote truth constants; all propositional variables are numbered from 2. mutable struct Encoding io::IOStream branch::Branch masks::Vector{Int} L::Array{Int,3} edge::Matrix{Int} seen::BitMatrix nvar::Int nclause::Int end nv(e)=e.nvar+=1 function clause(e, lits) 1 in lits && return for l in lits @assert l!=0 l == -1 || print(e.io,l,' ') end println(e.io,'0') e.nclause+=1 end function equalif(e,a,b,guard=()) clause(e,(guard...,-a,b)) clause(e,(guard...,a,-b)) end function geometry(c,masks,io) n=7+length(masks) e=Encoding(io,c,masks,zeros(Int,n,n,n),zeros(Int,n,n),falses(n,7),1,0) b=c.nd+c.h for a in 1:n-2,j in a+1:n-1,k in j+1:n # D then H is the cyclic ccw hull order, with H after D's last point. l = k<=b ? 1 : nv(e) e.L[a,j,k]=e.L[j,k,a]=e.L[k,a,j]=l e.L[j,a,k]=e.L[a,k,j]=e.L[k,j,a]=-l end for w in 8:n,s in 1:7 e.seen[w,s]=!iszero(masks[w-7]&(1<<(s-1))) end for a in 1:n,j in a+1:n v = a<=7e.seen[w,y],common) no=filter(w->!e.seen[w,y],common) for i in eachindex(yes),j in i+1:length(yes),z in no,σ in (-1,1) u,v=yes[i],yes[j] clause(e,(-σ*L[y,u,v],-σ*L[y,u,z],-σ*L[y,z,v])) end end function structural(e) L=e.L c=e.branch n=c.nd for w in 8:size(L,1), k in 1:n e.seen[w,k] && continue yes=findall(e.seen[w,:]) for i in eachindex(yes),j in i+1:length(yes),σ in (-1,1) a,b=yes[i],yes[j] clause(e,(-σ*L[w,a,k],-σ*L[a,b,k],-σ*L[b,w,k])) end end for (i,k) in enumerate(c.exits) wedge!(e,i+n,k+1,mod(k+1,n)+1) end pocket=[n;collect(n+1:n+c.h);1] for idx in 2:length(pocket)-1 f=pocket[idx] wedge!(e,f,n,1) wedge!(e,f,pocket[idx-1],pocket[idx+1]) index=findfirst(==(127⊻(1<<(f-1))),e.masks) isnothing(index) && continue z=index+7 for i in eachindex(pocket) i==idx && continue equalif(e,L[z,f,n],(i-σ*u,cond) g=nv(e) clause(e,(base...,-e.edge[v,q],g)) for u in yes clause(e,(base...,-σ*L[v,q,u],-σ*L[v,u,a],g)) end for x in 8:n e.seen[x,a] && e.seen[x,c] || continue clause(e,(-g,-σ*L[v,a,x])) clause(e,(-g,-σ*L[v,p,x])) e.seen[x,p] || continue clause(e,(-g,σ*L[p,x,a])) for y in 8:n e.seen[y,a] && e.seen[y,c] && !e.seen[y,p] || continue clause(e,(-g,σ*L[p,x,y])) end end end end end