-
Notifications
You must be signed in to change notification settings - Fork 0
/
product.jl
198 lines (192 loc) · 7.35 KB
/
product.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
186
187
188
189
190
191
192
193
194
195
196
197
198
using ProbabilisticCircuits
using StatsFuns
include("utils.jl")
include("integrate.jl")
function product_circuit(m::ProbCircuit, n::ProbCircuit; cache = PCPairCache(),
m_log_prob::Bool = true, n_log_prob::Bool = true, compatible::Bool = false)
postprocess_node(node::ProbCircuit) = begin
if is⋀gate(node) && num_children(node) == 1
children(node)[1]
else
node
end
end
@inbounds get!(cache, Pair(m, n)) do
if is⋁gate(m) && is⋁gate(n)
node_cache = Vector{ProbCircuit}()
prob_cache = Vector{Float64}()
nchm = num_children(m)
nchn = num_children(n)
for cm_idx in 1 : nchm
for cn_idx in 1 : nchn
cmn = product_circuit(children(m)[cm_idx], children(n)[cn_idx]; cache, m_log_prob, n_log_prob, compatible)
if cmn !== nothing
push!(node_cache, postprocess_node(cmn))
m_prob = m_log_prob ? exp(m.log_probs[cm_idx]) : m.log_probs[cm_idx]
n_prob = n_log_prob ? exp(n.log_probs[cn_idx]) : n.log_probs[cn_idx]
push!(prob_cache, m_prob * n_prob)
end
end
end
if length(node_cache) > 0
pc = summate(node_cache...)
pc.log_probs .= prob_cache
pc
else
nothing
end
elseif is⋀gate(m) && is⋀gate(n)
node_cache = Vector{ProbCircuit}()
nchm = num_children(m)
nchn = num_children(n)
flag = true
if compatible
@assert nchm == nchn
for c_idx = 1 : nchm
if !flag
break
end
pc = product_circuit(children(m)[c_idx], children(n)[c_idx];
cache, m_log_prob, n_log_prob, compatible)
if pc !== nothing
push!(node_cache, pc)
else
flag = false
end
end
else
for cm_idx = 1 : nchm
for cn_idx = 1 : nchn
pc = product_circuit(children(m)[cm_idx], children(n)[cn_idx];
cache, m_log_prob, n_log_prob, compatible)
if pc !== nothing
push!(node_cache, pc)
end
end
end
end
if flag
multiply(node_cache...)
else
nothing
end
elseif is⋁gate(m) && is⋀gate(n)
node_cache = Vector{ProbCircuit}()
prob_cache = Vector{Float64}()
nchm = num_children(m)
for cm_idx = 1 : nchm
cmn = product_circuit(children(m)[cm_idx], n; cache, m_log_prob, n_log_prob, compatible)
if cmn !== nothing
push!(node_cache, postprocess_node(cmn))
m_prob = m_log_prob ? exp(m.log_probs[cm_idx]) : m.log_probs[cm_idx]
push!(prob_cache, m_prob)
end
end
if length(node_cache) > 0
pc = summate(node_cache...)
pc.log_probs .= prob_cache
pc
else
nothing
end
elseif is⋀gate(m) && is⋁gate(n)
node_cache = Vector{ProbCircuit}()
prob_cache = Vector{Float64}()
nchn = num_children(n)
for cn_idx = 1 : nchn
cmn = product_circuit(m, children(n)[cn_idx]; cache, m_log_prob, n_log_prob, compatible)
if cmn !== nothing
push!(node_cache, postprocess_node(cmn))
n_prob = n_log_prob ? exp(n.log_probs[cn_idx]) : n.log_probs[cn_idx]
push!(prob_cache, n_prob)
end
end
if length(node_cache) > 0
pc = summate(node_cache...)
pc.log_probs .= prob_cache
pc
else
nothing
end
elseif isliteralgate(m) && isliteralgate(n)
if m.literal == n.literal
PlainProbLiteralNode(m.literal)
elseif m.literal == -n.literal
nothing
else
multiply(
PlainProbLiteralNode(m.literal),
PlainProbLiteralNode(n.literal)
)
end
elseif is⋁gate(m) && isliteralgate(n)
node_cache = Vector{ProbCircuit}()
prob_cache = Vector{Float64}()
nchm = num_children(m)
for cm_idx in 1 : nchm
cmn = product_circuit(children(m)[cm_idx], n; cache, m_log_prob, n_log_prob, compatible)
if cmn !== nothing
push!(node_cache, postprocess_node(cmn))
m_prob = m_log_prob ? exp(m.log_probs[cm_idx]) : m.log_probs[cm_idx]
push!(prob_cache, m_prob)
end
end
if length(node_cache) > 0
pc = summate(node_cache...)
pc.log_probs .= prob_cache
pc
else
nothing
end
elseif isliteralgate(m) && is⋁gate(n)
node_cache = Vector{ProbCircuit}()
prob_cache = Vector{Float64}()
nchn = num_children(n)
for cn_idx in 1 : nchn
cmn = product_circuit(m, children(n)[cn_idx]; cache, m_log_prob, n_log_prob, compatible)
if cmn !== nothing
push!(node_cache, postprocess_node(cmn))
n_prob = n_log_prob ? exp(n.log_probs[cn_idx]) : n.log_probs[cn_idx]
push!(prob_cache, n_prob)
end
end
if length(node_cache) > 0
pc = summate(node_cache...)
pc.log_probs .= prob_cache
pc
else
nothing
end
elseif is⋀gate(m) && isliteralgate(n)
node_cache = Vector{ProbCircuit}()
nchm = num_children(m)
for cm_idx in 1 : nchm
cmn = product_circuit(children(m)[cm_idx], n; cache, m_log_prob, n_log_prob, compatible)
if cmn !== nothing
push!(node_cache, postprocess_node(cmn))
end
end
if length(node_cache) > 0
multiply(node_cache...)
else
nothing
end
elseif isliteralgate(m) && is⋀gate(n)
node_cache = Vector{ProbCircuit}()
nchn = num_children(n)
for cn_idx in 1 : nchn
cmn = product_circuit(m, children(n)[cn_idx]; cache, m_log_prob, n_log_prob, compatible)
if cmn !== nothing
push!(node_cache, postprocess_node(cmn))
end
end
if length(node_cache) > 0
multiply(node_cache...)
else
nothing
end
else
@assert false "unhandled situation: ($(typeof(m)), $(typeof(n)))."
end
end
end