comparison toolboxes/FullBNT-1.0.7/bnt/inference/static/@belprop_inf_engine/belprop_inf_engine.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 engine = belprop_inf_engine(bnet, varargin)
2 % BELPROP_INF_ENGINE Make a loopy belief propagation inference engine
3 % engine = belprop_inf_engine(bnet, ...)
4 %
5 % This is like pearl_inf_engine, except it uses potential objects,
6 % instead of lambda/pi structs. Hence it is slower.
7 %
8 % The following optional arguments can be specified in the form of name/value pairs:
9 % [default in brackets]
10 %
11 % protocol - 'tree' means send messages up then down the tree,
12 % 'parallel' means use synchronous updates ['parallel']
13 % max_iter - max. num. iterations [ 2*num_nodes ]
14 % momentum - weight assigned to old message in convex combination (useful for damping oscillations) [0]
15 % tol - tolerance used to assess convergence [1e-3]
16 % maximize - 1 means use max-product, 0 means use sum-product [0]
17 % filename - name of file to write beliefs to after each iteration within enter_evidence [ [] ]
18 %
19 % e.g., engine = belprop_inf_engine(bnet, 'maximize', 1, 'max_iter', 10)
20
21 % gdl = general distributive law
22 engine.gdl = bnet_to_gdl(bnet);
23
24 % set default params
25 N = length(engine.gdl.G);
26 engine.protocol = 'parallel';
27 engine.max_iter = 2*N;
28 engine.momentum = 0;
29 engine.tol = 1e-3;
30 engine.maximize = 0;
31 engine.filename = [];
32 engine.fid = [];
33
34 args = varargin;
35 nargs = length(args);
36 for i=1:2:nargs
37 switch args{i},
38 case 'max_iter', engine.max_iter = args{i+1};
39 case 'momentum', engine.momentum = args{i+1};
40 case 'tol', engine.tol = args{i+1};
41 case 'protocol', engine.protocol = args{i+1};
42 case 'filename', engine.filename = args{i+1};
43 otherwise,
44 error(['invalid argument name ' args{i}]);
45 end
46 end
47
48
49 if strcmp(engine.protocol, 'tree')
50 % Make a rooted tree, so there is a fixed message passing order.
51 root = N;
52 [engine.tree, engine.preorder, engine.postorder, height, cyclic] = mk_rooted_tree(engine.gdl.G, root);
53 assert(~cyclic);
54 end
55
56 % store results computed by enter_evidence here
57 engine.marginal_domains = cell(1, N);
58
59 engine.niter = [];
60
61 engine = class(engine, 'belprop_inf_engine', inf_engine(bnet));
62
63 %%%%%%%%%
64
65 function gdl = bnet_to_gdl(bnet)
66
67 gdl.G = mk_undirected(bnet.dag);
68 N = length(bnet.dag);
69 gdl.doms = cell(1,N);
70 for i=1:N
71 gdl.doms{i} = family(bnet.dag, i);
72 end
73
74 % Compute a bit vector representation of the set of domains
75 % dom_bitv(i,j) = 1 iff variable j occurs in domain i
76 gdl.dom_bitv = zeros(N, N);
77 for i=1:N
78 gdl.dom_bitv(i, gdl.doms{i}) = 1;
79 end
80
81 % compute the interesection of the domains on either side of each edge (separating set)
82 gdl.sepset = cell(N, N);
83 gdl.nbrs = cell(1,N);
84 for i=1:N
85 nbrs = neighbors(gdl.G, i);
86 gdl.nbrs{i} = nbrs;
87 for j = nbrs(:)'
88 gdl.sepset{i,j} = myintersect(gdl.doms{i}, gdl.doms{j});
89 end
90 end