-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCNFs.jl
185 lines (158 loc) · 5.4 KB
/
CNFs.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
module CNFs
export CNF
using ..Gates
mutable struct CNF
clauses::Matrix{Int}
inputs::Dict{Symbol,Dict{Int,Int}}
size::Int
end
function Bool(cnf::CNF)
length(cnf.clauses) == 0 && return true
any(all.(iszero, eachcol(cnf.clauses))) && return false
throw(InexactError(:Bool, Bool, cnf))
end
"Tseytin transformation into 3CNF."
function CNF(x::Gate)
flat_clauses = Int[]
wires = IdDict{Gates.SymbolicBool,Int}()
inputs = Dict{Symbol,Dict{Int,Int}}()
walk(x::BoolVariable) = get!(wires, x) do
get!(inputs, x.name, Dict{Int,Int}())[x.bit] = length(wires) + 1
end
walk(gate::Gate) = get!(wires, gate) do
walked = walk.(gate.inputs)
c = length(wires) + 1
tseytin(::NotGate, a) = push!(flat_clauses,
-a, -c, 0,
a, c, 0)
tseytin(::AndGate, a, b) = push!(flat_clauses,
-a, -b, c,
a, -c, 0,
b, -c, 0)
tseytin(::OrGate, a, b) = push!(flat_clauses,
a, b, -c,
-a, c, 0,
-b, c, 0)
tseytin(::XorGate, a, b) = push!(flat_clauses,
-a, -b, -c,
a, b, -c,
a, -b, c,
-a, b, c)
tseytin(gate, walked...)
c
end
push!(flat_clauses, walk(x), 0, 0)
return CNF(reshape(flat_clauses, 3, :), inputs, length(wires))
end
function Base.show(io::IO, ::MIME"text/plain", cnf::CNF)
println(io, size(cnf.clauses, 2), "-clause, ", cnf.size, "-literal CNF:")
buffer = IOBuffer()
Base.print_matrix(IOContext(buffer), cnf.clauses', "", " ∨ ")
seek(buffer, 0)
join(io, (" ($clause)" for clause in eachline(buffer)), " ∧\n")
close(buffer)
end
function Base.map!(f, cnf::CNF)
map!(v -> copysign(f(abs(v)), v), cnf.clauses)
map!(input -> map!(f, values(input)), values(cnf.inputs))
return cnf
end
"Modify input CNF to remove variables that are not used in any clause."
function compress!(cnf::CNF)
vars = abs.(vec(cnf.clauses))
sort!(vars)
pushfirst!(vars, 0)
unique!(vars)
popfirst!(vars)
remapping = Dict(zip(vars, eachindex(vars)))
map!(v -> get(remapping, v, zero(v)), cnf)
map!(values(cnf.inputs)) do input
filter!(pair -> !iszero(pair.second), input)
end
cnf.size = length(vars)
return cnf
end
"Removes repeated variables in the same clause."
function deduplicate!(cnf::CNF)
clauses = eachcol(cnf.clauses)
remaining = trues(length(clauses))
for (i, clause) in enumerate(clauses),
(j, var) in enumerate(clause)
rest = @view clause[j + 1:end]
if -var in rest
remaining[i] = false
continue
end
replace!(rest, var => 0)
end
cnf.clauses = cnf.clauses[:, remaining]
if length(cnf.clauses) == 0
empty!(cnf.inputs)
cnf.size = 0
end
return cnf
end
"Simplification procedure I generalized from the 2SAT satisfiability algorithm. Guaranteed to solve instand"
function simplify!(cnf::CNF)
deduplicate!(cnf)
locations = Dict{Int,Vector{CartesianIndex}}()
for (location, v) in pairs(IndexCartesian(), cnf.clauses)
v != 0 || continue
push!(get!(locations, v, CartesianIndex[]), location)
end
remaining = trues(length(clauses))
assignments = Dict{Int,Bool}()
Base.@propagate_inbounds function dfs(clause::Int)
v = 0
for w in @view cnf.clauses[:, clause]
w != 0 || continue
v == 0 || return true
v = w
end
v != 0 || return false
get!(assignments, abs(v), v > 0) == (v > 0) || return false
remaining[getindex.(locations[v], 1)] .= false
for i in locations[-v]
col = i[1]
remaining[col] || continue
cnf.clauses[i] = 0
dfs(col) || return false
end
return true
end
@inbounds for i = axes(cnf.clauses, 2)
remaining[i] || continue
if !dfs(i)
cnf.clauses = zeros(Int, 1, 1)
empty!(cnf.inputs)
cnf.size = 0
return cnf, Dict{BoolVariable,Bool}()
end
end
cnf.clauses = cnf.clauses[:, remaining]
partial = Dict{BoolVariable,Bool}()
for (name, bits) in cnf.inputs,
(bit, v) in bits
assignment = get(assignments, v, missing)
!ismissing(assignment) || continue
partial[BoolVariable(name, bit)] = assignment
end
compress!(cnf)
return cnf, partial
end
using Random
function Random.shuffle!(rng::AbstractRNG, cnf::CNF)
perm = randperm(rng, cnf.size)
map!(v -> perm[v], cnf)
shuffle!.(rng, eachcol(cnf.clauses))
cnf.clauses .= cnf.clauses[:, randperm(rng, size(cnf.clauses, 2))]
return cnf
end
import PicoSAT
for f = (:solve, :itersolve)
@eval PicoSAT.$f(cnf::CNF; args...) = PicoSAT.$f(Iterators.map(clause -> Iterators.filter(!=(0), clause),
eachcol(cnf.clauses));
vars=cnf.size,
args...)
end
end