This repository has been archived by the owner on Jan 29, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
lib_doSim.jl
234 lines (230 loc) · 7.91 KB
/
lib_doSim.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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
#
# Code released under the MIT license, see corresponding LICENSE file
# (c) 2015, Thibaut Lienart
#
# --------------------------------------------------------------------------------------------------
#
function doLBPD()
_start_lbpd = time()
println("LBPD sim ($expname::$Ngrid)")
# > pre-allocation of storage space
global messages = ones(2*nedges,Ngrid)
global beliefs = zeros(nnodes,Ngrid)
# > initial beliefs: just node pot (mess=1)
for node=1:nnodes
neighbors = get_neighbors(node)
cur_beliefs = eval_node_pot(node,grid)
beliefs[node,:] = cur_beliefs/sum(cur_beliefs)
end
# > pre-computation in case of homogeneous edge pot
global edge_pot_grid = []
if HOMOG_EDGE_POT
edge_pot_grid = zeros(Ngrid,Ngrid)
for gi=1:Ngrid
for gj=1:Ngrid
edge_pot_grid[gi,gj]=eval_edge_pot(0,0,grid[gi],grid[gj])
end
end
end
# > keep init belief, useful for later comp
global init_beliefs = copy(beliefs)
#
for loop = 1:nloops
print(">loop: ",loop)
_start_loop = time()
for i=1:length(scheduling)
lbpd_node_update(scheduling[i])
end
println(" [completed in ",get_time(_start_loop),"s]")
end
println("LBPD completed in ",get_time(_start_lbpd),"s.")
#
writecsv("$expname/$expname\_lbpd_origbel_np$Ngrid.dat",init_beliefs)
writecsv("$expname/$expname\_lbpd_grid_np$Ngrid.dat", grid)
writecsv("$expname/$expname\_lbpd_beliefs_np$Ngrid.dat",beliefs)
end
function doEPBP()
_start_epbp = time()
println("EPBP sim ($expname::$N) [run::$R]")
# > pre-allocation of storage space
global particles = zeros(nnodes,N)
global b_weights = zeros(nnodes,N)
global b_evals = zeros(nnodes,N)
global q_moments = zeros(nnodes,2)
global e_weights = zeros(2*nedges,N)
global eta_moments = zeros(2*nedges,2)
global eta_node_moments = zeros(nnodes,2)
#
# > initial proposals & particles [!USER!]
for node = 1:nnodes
mu_node = obs_values[node]
q_moments[node,:] = [ mu_node s_init ]
particles[node,:] = mu_node + s_init*randn(N,1)
end
#
# > initial edge weights
for edge = 1:2*nedges
from = edge_list[edge,1]
weights = eval_node_pot(from,particles[from,:])
weights /= sum(weights)
#
e_weights[edge,:] = weights
end
#
orig_q_moments = copy(q_moments)
orig_eta_moments = copy(eta_moments)
#
for loop = 1:nloops
print(">loop: ",loop)
_start_loop = time()
for i=1:length(scheduling)
epbp_node_update(scheduling[i])
end
println(" [completed in ",get_time(_start_loop),"s]")
end
println("EPBP completed in ",get_time(_start_epbp),"s.")
print("...eval est. beliefs on mesh...")
_start_epbp_estbel = time()
epbp_est_beliefs = zeros(nnodes,Ngrid)
for node = 1:nnodes
t1,t2 = epbp_eval_belief(node,grid)
epbp_est_beliefs[node,:] = t2
end
println(" [done in ",get_time(_start_epbp_estbel),"s]")
#
writecsv("$expname/$expname\_epbp_est_beliefs_np$N\_r$R.dat",epbp_est_beliefs)
writecsv("$expname/$expname\_epbp_particles_np$N\_r$R.dat", particles)
writecsv("$expname/$expname\_epbp_weights_np$N\_r$R.dat", b_weights)
writecsv("$expname/$expname\_epbp_evals_np$N\_r$R.dat", b_evals)
writecsv("$expname/$expname\_epbp_qmom_np$N\_r$R.dat", q_moments)
end
function doFEPBP()
_start_fepbp = time()
println("FEPBP sim ($expname::$N/$C) [run::$R]")
# > pre-allocation of storage space
global particles = zeros(nnodes,N)
global b_weights = zeros(nnodes,N)
global b_evals = zeros(nnodes,N)
global q_moments = zeros(nnodes,2)
global e_weights = zeros(2*nedges,N)
global eta_moments = zeros(2*nedges,2)
global eta_node_moments = zeros(nnodes,2)
#
# > initial proposals & particles [!USER!]
for node = 1:nnodes
mu_node = obs_values[node]
q_moments[node,:] = [ mu_node s_init ]
particles[node,:] = mu_node + s_init*randn(N,1)
end
#
# > initial edge weights
for edge = 1:2*nedges
from = edge_list[edge,1]
weights = eval_node_pot(from,particles[from,:])
weights /= sum(weights)
#
e_weights[edge,:] = weights
end
#
orig_q_moments = copy(q_moments)
orig_eta_moments = copy(eta_moments)
#
for loop = 1:nloops
print(">loop: ",loop)
_start_loop = time()
for i=1:length(scheduling)
if NODE_PROGRESS
println(">> node >> ", scheduling[i])
end
epbp_node_update(scheduling[i],true) # with fastmode
end
println(" [completed in ",get_time(_start_loop),"s]")
end
println("FEPBP completed in ",get_time(_start_fepbp),"s.")
print("...eval est. beliefs on mesh...")
_start_fepbp_estbel = time()
fepbp_est_beliefs = zeros(nnodes,Ngrid)
for node = 1:nnodes
t1,t2 = epbp_eval_belief(node,grid) # complete eval (not fast)
fepbp_est_beliefs[node,:] = t2
end
println(" [done in ",get_time(_start_fepbp_estbel),"s]")
#
writecsv("$expname/$expname\_fepbp_est_beliefs_np$N\_nc$C\_r$R.dat",fepbp_est_beliefs)
writecsv("$expname/$expname\_fepbp_particles_np$N\_nc$C\_r$R.dat", particles)
writecsv("$expname/$expname\_fepbp_weights_np$N\_nc$C\_r$R.dat", b_weights)
writecsv("$expname/$expname\_fepbp_evals_np$N\_nc$C\_r$R.dat", b_evals)
writecsv("$expname/$expname\_fepbp_qmom_np$N\_nc$C\_r$R.dat", q_moments)
end
function doPBP()
_start_pbp = time()
println("PBP sim ($expname::$N) [run::$R]")
#
# > pre-allocation of storage space
global particles = zeros(nnodes,N)
global b_evals = zeros(nnodes,N)
global messages = ones(2*nedges,N)
#
for node=1:nnodes
node_p = obs_values[node]+s_init*randn(1,N)
bel_p = eval_node_pot(node,node_p)
particles[node,:] = node_p
b_evals[node,:] = bel_p/sum(bel_p)
end
#
for loop=1:nloops
print(">loop: ",loop)
_start_loop = time()
if PARACHAINS
for i=1:length(scheduling)
pbp_node_update(scheduling[i])
end
else
for i=1:length(scheduling)
pbp_node_update2(scheduling[i])
end
end
println(" [completed in ",get_time(_start_loop),"s]")
end
#
println("PBP completed in ",get_time(_start_pbp),"s.")
print("...eval est. beliefs on mesh...")
_start_pbp_estbel = time()
pbp_est_beliefs = zeros(nnodes,Ngrid)
for node = 1:nnodes
pbp_est_beliefs[node,:] = pbp_eval_belief(node,grid)
end
println(" [done in ",get_time(_start_pbp_estbel),"s]")
#
writecsv("$expname/$expname\_pbp_est_beliefs_np$N\_r$R.dat",pbp_est_beliefs)
writecsv("$expname/$expname\_pbp_particles_np$N\_r$R.dat", particles)
writecsv("$expname/$expname\_pbp_evals_np$N\_r$R.dat", b_evals)
end
function doEP()
_start_ep = time()
println("EP sim ...")
#
global eta_moments = zeros(2*nedges,2)
global eta_node_moments = zeros(nnodes,2)
global q_moments = zeros(nnodes,2)
#
for node=1:nnodes
q_moments[node,:] = [obs_values[node] s_init]
end
for loop=1:nEPloops
print(">loop ",loop)
_start_loop = time()
old_moms = copy(q_moments)
for i = 1:length(scheduling)
if NODE_PROGRESS
println(">> node >> ", scheduling[i])
end
ep_node_update(scheduling[i])
end
println(" [completed in ",get_time(_start_loop),"s; raw diff ",
round(norm(q_moments-old_moms),3),"]")
end
println("EP completed in ",get_time(_start_ep),"s.")
#
writecsv("$expname/$expname\_ep_qmoments_nepl$nEPloops.dat",q_moments)
end