-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathintegrate.jl
50 lines (45 loc) · 1.28 KB
/
integrate.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
using LogicCircuits, ProbabilisticCircuits
function evaluate_circuit(n::ProbCircuit, d::Vector{Bool}; log_prob::Bool = true)
f_con(n)::Float64 = 0.0
f_lit(n)::Float64 = begin
if n.literal > Lit(0)
d[Var(n.literal)] == true ? 1.0 : 0.0
else
d[Var(-n.literal)] == false ? 1.0 : 0.0
end
end
f_a(n, cn)::Float64 = reduce(*, [cn...])
f_o(n, cn)::Float64 = begin
s = 0.0
if log_prob
for (logp, cp) in zip(n.log_probs, cn)
s += cp * exp(logp)
end
else
for (p, cp) in zip(n.log_probs, cn)
s += cp * p
end
end
s
end
foldup_aggregate(n, f_con, f_lit, f_a, f_o, Float64)
end
function integrate_circuit(n::ProbCircuit; log_prob::Bool = true)
f_con(n)::Float64 = 1.0
f_lit(n)::Float64 = 1.0
f_a(n, cn)::Float64 = reduce(*, [cn...])
f_o(n, cn)::Float64 = begin
s = 0.0
if log_prob
for (logp, cp) in zip(n.log_probs, cn)
s += cp * exp(logp)
end
else
for (p, cp) in zip(n.log_probs, cn)
s += cp * p
end
end
s
end
foldup_aggregate(n, f_con, f_lit, f_a, f_o, Float64)
end