Mercurial > hg > camir-aes2014
comparison toolboxes/FullBNT-1.0.7/bnt/inference/static/@pearl_inf_engine/private/tree_protocol.m @ 0:e9a9cd732c1e tip
first hg version after svn
author | wolffd |
---|---|
date | Tue, 10 Feb 2015 15:05:51 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:e9a9cd732c1e |
---|---|
1 function msg = tree_protocol(engine, evidence, msg) | |
2 | |
3 bnet = bnet_from_engine(engine); | |
4 N = length(bnet.dag); | |
5 | |
6 % Send messages from leaves to root | |
7 for i=1:N-1 | |
8 n = engine.postorder(i); | |
9 above = parents(engine.adj_mat, n); | |
10 msg = send_msgs_to_some_neighbors(n, msg, above, bnet, engine.child_index, engine.parent_index, ... | |
11 engine.msg_type, evidence); | |
12 end | |
13 | |
14 % Process root | |
15 n = engine.root; | |
16 cs = children(bnet.dag, n); | |
17 %msg{n}.lambda = compute_lambda(n, cs, msg, engine.msg_type); | |
18 msg{n}.lambda = prod_lambda_msgs(n, cs, msg, engine.msg_type); | |
19 ps = parents(bnet.dag, n); | |
20 msg{n}.pi = CPD_to_pi(bnet.CPD{bnet.equiv_class(n)}, engine.msg_type, n, ps, msg, evidence); | |
21 | |
22 % Send messages from root to leaves | |
23 for i=1:N | |
24 n = engine.preorder(i); | |
25 below = children(engine.adj_mat, n); | |
26 msg = send_msgs_to_some_neighbors(n, msg, below, bnet, engine.child_index, engine.parent_index, ... | |
27 engine.msg_type, evidence); | |
28 end | |
29 | |
30 | |
31 %%%%%%%%%% | |
32 | |
33 function msg = send_msgs_to_some_neighbors(n, msg, valid_nbrs, bnet, child_index, parent_index, ... | |
34 msg_type, evidence) | |
35 | |
36 verbose = 0; | |
37 | |
38 ns = bnet.node_sizes; | |
39 dag = bnet.dag; | |
40 e = bnet.equiv_class(n); | |
41 CPD = bnet.CPD{e}; | |
42 | |
43 | |
44 cs = children(dag, n); | |
45 %msg{n}.lambda = compute_lambda(n, cs, msg); | |
46 msg{n}.lambda = prod_lambda_msgs(n, cs, msg, msg_type); | |
47 if verbose, fprintf('%d computes lambda\n', n); display(msg{n}.lambda); end | |
48 | |
49 ps = parents(dag, n); | |
50 msg{n}.pi = CPD_to_pi(CPD, msg_type, n, ps, msg, evidence); | |
51 if verbose, fprintf('%d computes pi\n', n); display(msg{n}.pi); end | |
52 | |
53 ps2 = myintersect(parents(dag, n), valid_nbrs); | |
54 for p=ps2(:)' | |
55 lam_msg = CPD_to_lambda_msg(CPD, msg_type, n, ps, msg, p, evidence); | |
56 j = child_index{p}(n); % n is p's j'th child | |
57 msg{p}.lambda_from_child{j} = lam_msg; | |
58 if verbose, fprintf('%d sends lambda to %d\n', n, p); display(lam_msg); end | |
59 end | |
60 | |
61 cs2 = myintersect(cs, valid_nbrs); | |
62 for c=cs2(:)' | |
63 %pi_msg = compute_pi_msg(n, cs, msg, c); | |
64 pi_msg = compute_bel(msg_type, msg{n}.pi, prod_lambda_msgs(n, cs, msg, msg_type, c)); | |
65 j = parent_index{c}(n); % n is c's j'th parent | |
66 msg{c}.pi_from_parent{j} = pi_msg; | |
67 if verbose, fprintf('%d sends pi to %d\n', n, c); display(pi_msg); end | |
68 end | |
69 | |
70 | |
71 |