diff options
Diffstat (limited to 'sourcecodes/bnt-master/BNT/inference/static/@belprop_inf_engine')
24 files changed, 762 insertions, 0 deletions
diff --git a/sourcecodes/bnt-master/BNT/inference/static/@belprop_inf_engine/CVS/Entries b/sourcecodes/bnt-master/BNT/inference/static/@belprop_inf_engine/CVS/Entries new file mode 100644 index 00000000..b2150de3 --- /dev/null +++ b/sourcecodes/bnt-master/BNT/inference/static/@belprop_inf_engine/CVS/Entries @@ -0,0 +1,7 @@ +/belprop_inf_engine.m/1.1.1.1/Tue Dec 31 19:00:06 2002// +/enter_evidence.m/1.1.1.1/Wed May 29 15:59:56 2002// +/find_mpe.m/1.1.1.1/Wed Jun 19 22:08:40 2002// +/loopy_converged.m/1.1.1.1/Wed May 29 15:59:56 2002// +/marginal_family.m/1.1.1.1/Wed May 29 15:59:56 2002// +/marginal_nodes.m/1.1.1.1/Wed May 29 15:59:56 2002// +D diff --git a/sourcecodes/bnt-master/BNT/inference/static/@belprop_inf_engine/CVS/Entries.Log b/sourcecodes/bnt-master/BNT/inference/static/@belprop_inf_engine/CVS/Entries.Log new file mode 100644 index 00000000..9c6f22e4 --- /dev/null +++ b/sourcecodes/bnt-master/BNT/inference/static/@belprop_inf_engine/CVS/Entries.Log @@ -0,0 +1,2 @@ +A D/Old//// +A D/private//// diff --git a/sourcecodes/bnt-master/BNT/inference/static/@belprop_inf_engine/CVS/Repository b/sourcecodes/bnt-master/BNT/inference/static/@belprop_inf_engine/CVS/Repository new file mode 100644 index 00000000..928be328 --- /dev/null +++ b/sourcecodes/bnt-master/BNT/inference/static/@belprop_inf_engine/CVS/Repository @@ -0,0 +1 @@ +FullBNT/BNT/inference/static/@belprop_inf_engine diff --git a/sourcecodes/bnt-master/BNT/inference/static/@belprop_inf_engine/CVS/Root b/sourcecodes/bnt-master/BNT/inference/static/@belprop_inf_engine/CVS/Root new file mode 100644 index 00000000..f3bd14a6 --- /dev/null +++ b/sourcecodes/bnt-master/BNT/inference/static/@belprop_inf_engine/CVS/Root @@ -0,0 +1 @@ +:ext:nsaunier@bnt.cvs.sourceforge.net:/cvsroot/bnt diff --git a/sourcecodes/bnt-master/BNT/inference/static/@belprop_inf_engine/Old/CVS/Entries b/sourcecodes/bnt-master/BNT/inference/static/@belprop_inf_engine/Old/CVS/Entries new file mode 100644 index 00000000..06598b7b --- /dev/null +++ b/sourcecodes/bnt-master/BNT/inference/static/@belprop_inf_engine/Old/CVS/Entries @@ -0,0 +1,6 @@ +/belprop_gdl_inf_engine.m/1.1.1.1/Wed May 29 15:59:56 2002// +/belprop_inf_engine_nostr.m/1.1.1.1/Wed May 29 15:59:56 2002// +/enter_evidence.m/1.1.1.1/Wed May 29 15:59:56 2002// +/enter_evidence1.m/1.1.1.1/Wed May 29 15:59:56 2002// +/marginal_domain.m/1.1.1.1/Wed May 29 15:59:56 2002// +D diff --git a/sourcecodes/bnt-master/BNT/inference/static/@belprop_inf_engine/Old/CVS/Repository b/sourcecodes/bnt-master/BNT/inference/static/@belprop_inf_engine/Old/CVS/Repository new file mode 100644 index 00000000..f6b12595 --- /dev/null +++ b/sourcecodes/bnt-master/BNT/inference/static/@belprop_inf_engine/Old/CVS/Repository @@ -0,0 +1 @@ +FullBNT/BNT/inference/static/@belprop_inf_engine/Old diff --git a/sourcecodes/bnt-master/BNT/inference/static/@belprop_inf_engine/Old/CVS/Root b/sourcecodes/bnt-master/BNT/inference/static/@belprop_inf_engine/Old/CVS/Root new file mode 100644 index 00000000..f3bd14a6 --- /dev/null +++ b/sourcecodes/bnt-master/BNT/inference/static/@belprop_inf_engine/Old/CVS/Root @@ -0,0 +1 @@ +:ext:nsaunier@bnt.cvs.sourceforge.net:/cvsroot/bnt diff --git a/sourcecodes/bnt-master/BNT/inference/static/@belprop_inf_engine/Old/belprop_gdl_inf_engine.m b/sourcecodes/bnt-master/BNT/inference/static/@belprop_inf_engine/Old/belprop_gdl_inf_engine.m new file mode 100644 index 00000000..f3b84925 --- /dev/null +++ b/sourcecodes/bnt-master/BNT/inference/static/@belprop_inf_engine/Old/belprop_gdl_inf_engine.m @@ -0,0 +1,67 @@ +function engine = belprop_gdl_inf_engine(gdl, varargin) +% BELPROP_GDL_INF_ENGINE Make a belief propagation inference engine for a GDL graph +% engine = belprop_gdl_inf_engine(gdl_graph, ...) +% +% If the GDL graph is a tree, this will give exact results. +% +% The following optional arguments can be specified in the form of name/value pairs: +% [default in brackets] +% e.g., engine = belprop_inf_engine(gdl, 'tol', 1e-2, 'max_iter', 10) +% +% protocol - 'tree' means send messages up then down the tree, +% 'parallel' means use synchronous updates ['parallel'] +% max_iter - max. num. iterations [ 2*num_nodes ] +% momentum - weight assigned to old message in convex combination (useful for damping oscillations) [0] +% tol - tolerance used to assess convergence [1e-3] +% maximize - 1 means use max-product, 0 means use sum-product [0] + + +engine = init_fields; +engine = class(engine, 'belprop_gdl_inf_engine'); + +% set default params +N = length(gdl.G); +engine.protocol = 'parallel'; +engine.max_iter = 2*N; +engine.momentum = 0; +engine.tol = 1e-3; +engine.maximize = 0; + +engine = set_params(engine, varargin); + +engine.gdl = gdl; + +if strcmp(engine.protocol, 'tree') + % Make a rooted tree, so there is a fixed message passing order. + root = N; + [engine.tree, engine.preorder, engine.postorder, height, cyclic] = mk_rooted_tree(gdl.G, root); + assert(~cyclic); +end + +% store results computed by enter_evidence here +ndoms = length(gdl.doms); +nvars = length(gdl.vars); +engine.marginal_domains = cell(1, ndoms); + +% to compute the marginal on each variable, we need to know which domain to marginalize +% and we want to choose the lightest. We compute the weight once we have seen the evidence. +engine.dom_weight = []; +engine.evidence = []; + + +%%%%%%%%% + +function engine = init_fields() + +engine.protocol = []; +engine.gdl = []; +engine.max_iter = []; +engine.momentum = []; +engine.tol = []; +engine.maximize = []; +engine.marginal_domains = []; +engine.evidence = []; +engine.tree = []; +engine.preorder = []; +engine.postorder = []; +engine.dom_weight = []; diff --git a/sourcecodes/bnt-master/BNT/inference/static/@belprop_inf_engine/Old/belprop_inf_engine_nostr.m b/sourcecodes/bnt-master/BNT/inference/static/@belprop_inf_engine/Old/belprop_inf_engine_nostr.m new file mode 100644 index 00000000..8219a868 --- /dev/null +++ b/sourcecodes/bnt-master/BNT/inference/static/@belprop_inf_engine/Old/belprop_inf_engine_nostr.m @@ -0,0 +1,31 @@ +function engine = belprop_inf_engine(fg, max_iter, momentum, tol, maximize) + +if nargin < 2, max_iter = length(fg.G); end +if nargin < 3, momentum = 0; end +if nargin < 4, tol = 1e-3; end +if nargin < 5, maximize = 0; end + +engine.fgraph = fg; +engine.max_iter = max_iter; +engine.momentum = momentum; +engine.tol = tol; +engine.maximize = maximize; + +% store results computed by enter_evidence here +ndoms = length(fg.doms); +nvars = length(fg.vars); +engine.marginal_domains = cell(1, ndoms); + +% to compute the marginal on each variable, we need to know which domain to marginalize +% so we represent each domain as a bit vector, and compute its (pre-evidence) weight +engine.dom_weight = []; + +% engine.dom_bitv = sparse(ndoms, nvars); +% ns = fg.node_sizes; +% for i=1:ndoms +% engine.dom_bitv(i, fg.doms{i}) = 1; +% engine.dom_weight(i) = prod(ns(fg.doms{i})); +% end + + +engine = class(engine, 'belprop_inf_engine'); diff --git a/sourcecodes/bnt-master/BNT/inference/static/@belprop_inf_engine/Old/enter_evidence.m b/sourcecodes/bnt-master/BNT/inference/static/@belprop_inf_engine/Old/enter_evidence.m new file mode 100644 index 00000000..54649557 --- /dev/null +++ b/sourcecodes/bnt-master/BNT/inference/static/@belprop_inf_engine/Old/enter_evidence.m @@ -0,0 +1,80 @@ +function engine = enter_evidence(engine, evidence) + +doms = engine.fg.doms; +ndoms = length(doms); +ns = engine.fg.node_sizes; +obs = find(~isemptycell(evidence)); +cobs = myintersect(obs, engine.fg.cnodes); +dobs = myintersect(obs, engine.fg.dnodes); +ns(cobs) = 0; +ns(dobs) = 1; + +% prime each local kernel with evidence (if any) +local_kernel = cell(1, ndoms); +for i=1:length(engine.fg.kernels_of_type) + u = engine.fg.kernels_of_type{i}; + local_kernel(u) = kernel_to_dpots(engine.fg.kernels{i}, evidence, engine.fg.domains_of_type{i}); +end + +% initialise all msgs to 1s +nedges = engine.fg.nedges; +msg = cell(1, nedges); +for i=1:nedges + msg{i} = dpot(engine.fg.sepset{i}, ns(engine.fg.sepset{i})); +end + +prod_of_msg = cell(1, ndoms); +bel = cell(1, ndoms); +old_bel = cell(1, ndoms); + +converged = 0; +iter = 1; +while ~converged & (iter <= engine.max_iter) + + % each node multiplies all its incoming msgs + for i=1:ndoms + prod_of_msg{i} = dpot(doms{i}, ns(doms{i})); + nbrs = engine.fg.nbrs{i}; + for j=1:length(nbrs) + ndx = engine.fg.edge_ndx(j,i); + prod_of_msg{i} = multiply_by_pot(prod_of_msg{i}, msg{ndx}); + end + end + old_msg = msg; + + % each node computes its local belief + for i=1:ndoms + bel{i} = normalize_pot(multiply_pots(prod_of_msg{i}, local_kernel{i})); + end + + % converged? + converged = 1; + for i=1:ndoms + if ~approxeq(bel{i}, old_bel{i}, engine.tol) + converged = 0; + break; + end + end + + if ~converged + % each node sends a msg to each of its neighbors + for i=1:ndoms + nbrs = engine.fg.nbrs{i}; + for j=1:length(nbrs) + % multiply all incoming msgs except from j + temp = prod_of_msg{i}; + ndx = engine.fg.edge_ndx(j,i); + temp = divide_by_pot(temp, old_msg{ndx}); + % send msg from i to j + temp = multiply_by_pot(temp, local_kernel{i}); + ndx = engine.fg.edge_ndx(i,j); + msg{ndx} = normalize_pot(marginalize_pot(temp, engine.fg.sepset{ndx})); + end + end + end + + iter = iter + 1; +end + + +engine.marginal = bel; diff --git a/sourcecodes/bnt-master/BNT/inference/static/@belprop_inf_engine/Old/enter_evidence1.m b/sourcecodes/bnt-master/BNT/inference/static/@belprop_inf_engine/Old/enter_evidence1.m new file mode 100644 index 00000000..b38cd3cb --- /dev/null +++ b/sourcecodes/bnt-master/BNT/inference/static/@belprop_inf_engine/Old/enter_evidence1.m @@ -0,0 +1,94 @@ +function engine = enter_evidence(engine, evidence) + +doms = engine.fgraph.doms; +ndoms = length(doms); +ns = engine.fgraph.node_sizes; +obs = find(~isemptycell(evidence)); +cobs = myintersect(obs, engine.fgraph.cnodes); +dobs = myintersect(obs, engine.fgraph.dnodes); +ns(cobs) = 0; +ns(dobs) = 1; + +% recompute the weight of each domain now that we know what nodes are observed +for i=1:ndoms + engine.dom_weight(i) = prod(ns(engine.fgraph.doms{i})); +end + +% prime each local kernel with evidence (if any) +local_kernel = cell(1, ndoms); +for i=1:length(engine.fgraph.kernels_of_type) + u = engine.fgraph.kernels_of_type{i}; + local_kernel(u) = kernel_to_dpots(engine.fgraph.kernels{i}, evidence, engine.fgraph.domains_of_type{i}); +end + +% initialise all msgs to 1s +msg = cell(ndoms, ndoms); +for i=1:ndoms + nbrs = engine.fgraph.nbrs{i}; + for j=nbrs(:)' + dom = engine.fgraph.sepset{i,j}; + msg{i,j} = dpot(dom, ns(dom)); + end +end + +prod_of_msg = cell(1, ndoms); +bel = cell(1, ndoms); +old_bel = cell(1, ndoms); + +converged = 0; +iter = 1; +while ~converged & (iter <= engine.max_iter) + + % each node multiplies all its incoming msgs + for i=1:ndoms + prod_of_msg{i} = dpot(doms{i}, ns(doms{i})); + nbrs = engine.fgraph.nbrs{i}; + for j=nbrs(:)' + prod_of_msg{i} = multiply_by_pot(prod_of_msg{i}, msg{j,i}); + end + end + + % each node computes its local belief + old_bel = bel; + for i=1:ndoms + bel{i} = normalize_pot(multiply_pots(prod_of_msg{i}, local_kernel{i})); + end + + % converged? + if iter==1 + converged = 0; + else + converged = 1; + for i=1:ndoms + belT = get_params(bel{i}, 'table'); + old_belT = get_params(old_bel{i}, 'table'); + if ~approxeq(belT, old_belT, engine.tol) + converged = 0; + break; + end + end + end + + if ~converged + old_msg = msg; + % each node sends a msg to each of its neighbors + for i=1:ndoms + nbrs = engine.fgraph.nbrs{i}; + for j=nbrs(:)' + % multiply all incoming msgs except from j + temp = prod_of_msg{i}; + temp = divide_by_pot(temp, old_msg{j,i}); + % send msg from i to j + temp = multiply_by_pot(temp, local_kernel{i}); + msg{i,j} = normalize_pot(marginalize_pot(temp, engine.fgraph.sepset{i,j})); + end + end + end + + iter = iter + 1 +end + +engine.marginal_domains = bel; +%for i=1:ndoms + %engine.marginal_domains{i} = get_params(bel{i}, 'table'); +%end diff --git a/sourcecodes/bnt-master/BNT/inference/static/@belprop_inf_engine/Old/marginal_domain.m b/sourcecodes/bnt-master/BNT/inference/static/@belprop_inf_engine/Old/marginal_domain.m new file mode 100644 index 00000000..49ad94c5 --- /dev/null +++ b/sourcecodes/bnt-master/BNT/inference/static/@belprop_inf_engine/Old/marginal_domain.m @@ -0,0 +1,5 @@ +function marginal = marginal_domain(engine, i) +% MARGINAL_DOMAIN Return the marginal on the specified domain (belprop) +% marginal = marginal_domain(engine, i) + +marginal = pot_to_marginal(engine.marginal_domains{i}); diff --git a/sourcecodes/bnt-master/BNT/inference/static/@belprop_inf_engine/belprop_inf_engine.m b/sourcecodes/bnt-master/BNT/inference/static/@belprop_inf_engine/belprop_inf_engine.m new file mode 100644 index 00000000..839af506 --- /dev/null +++ b/sourcecodes/bnt-master/BNT/inference/static/@belprop_inf_engine/belprop_inf_engine.m @@ -0,0 +1,90 @@ +function engine = belprop_inf_engine(bnet, varargin) +% BELPROP_INF_ENGINE Make a loopy belief propagation inference engine +% engine = belprop_inf_engine(bnet, ...) +% +% This is like pearl_inf_engine, except it uses potential objects, +% instead of lambda/pi structs. Hence it is slower. +% +% The following optional arguments can be specified in the form of name/value pairs: +% [default in brackets] +% +% protocol - 'tree' means send messages up then down the tree, +% 'parallel' means use synchronous updates ['parallel'] +% max_iter - max. num. iterations [ 2*num_nodes ] +% momentum - weight assigned to old message in convex combination (useful for damping oscillations) [0] +% tol - tolerance used to assess convergence [1e-3] +% maximize - 1 means use max-product, 0 means use sum-product [0] +% filename - name of file to write beliefs to after each iteration within enter_evidence [ [] ] +% +% e.g., engine = belprop_inf_engine(bnet, 'maximize', 1, 'max_iter', 10) + +% gdl = general distributive law +engine.gdl = bnet_to_gdl(bnet); + +% set default params +N = length(engine.gdl.G); +engine.protocol = 'parallel'; +engine.max_iter = 2*N; +engine.momentum = 0; +engine.tol = 1e-3; +engine.maximize = 0; +engine.filename = []; +engine.fid = []; + +args = varargin; +nargs = length(args); +for i=1:2:nargs + switch args{i}, + case 'max_iter', engine.max_iter = args{i+1}; + case 'momentum', engine.momentum = args{i+1}; + case 'tol', engine.tol = args{i+1}; + case 'protocol', engine.protocol = args{i+1}; + case 'filename', engine.filename = args{i+1}; + otherwise, + error(['invalid argument name ' args{i}]); + end +end + + +if strcmp(engine.protocol, 'tree') + % Make a rooted tree, so there is a fixed message passing order. + root = N; + [engine.tree, engine.preorder, engine.postorder, height, cyclic] = mk_rooted_tree(engine.gdl.G, root); + assert(~cyclic); +end + +% store results computed by enter_evidence here +engine.marginal_domains = cell(1, N); + +engine.niter = []; + +engine = class(engine, 'belprop_inf_engine', inf_engine(bnet)); + +%%%%%%%%% + +function gdl = bnet_to_gdl(bnet) + +gdl.G = mk_undirected(bnet.dag); +N = length(bnet.dag); +gdl.doms = cell(1,N); +for i=1:N + gdl.doms{i} = family(bnet.dag, i); +end + +% Compute a bit vector representation of the set of domains +% dom_bitv(i,j) = 1 iff variable j occurs in domain i +gdl.dom_bitv = zeros(N, N); +for i=1:N + gdl.dom_bitv(i, gdl.doms{i}) = 1; +end + +% compute the interesection of the domains on either side of each edge (separating set) +gdl.sepset = cell(N, N); +gdl.nbrs = cell(1,N); +for i=1:N + nbrs = neighbors(gdl.G, i); + gdl.nbrs{i} = nbrs; + for j = nbrs(:)' + gdl.sepset{i,j} = myintersect(gdl.doms{i}, gdl.doms{j}); + end +end diff --git a/sourcecodes/bnt-master/BNT/inference/static/@belprop_inf_engine/enter_evidence.m b/sourcecodes/bnt-master/BNT/inference/static/@belprop_inf_engine/enter_evidence.m new file mode 100644 index 00000000..88cce18e --- /dev/null +++ b/sourcecodes/bnt-master/BNT/inference/static/@belprop_inf_engine/enter_evidence.m @@ -0,0 +1,86 @@ +function [engine, ll, niter] = enter_evidence(engine, evidence, varargin) +% ENTER_EVIDENCE Propagate evidence using belief propagation +% [engine, ll, niter] = enter_evidence(engine, evidence, ...) +% +% The log-likelihood is not computed; ll = 0. +% niter contains the number of iterations used (if engine.protocol = 'parallel') +% +% The following optional arguments can be specified in the form of name/value pairs: +% [default value in brackets] +% +% maximize - 1 means use max-product, 0 means use sum-product [0] +% exclude - list of nodes whose potential will not be included in the joint [ [] ] +% +% e.g., engine = enter_evidence(engine, ev, 'maximize', 1) + +ll = 0; +exclude = []; +maximize = 0; + +if nargin >= 3 + args = varargin; + nargs = length(args); + for i=1:2:nargs + switch args{i}, + case 'exclude', exclude = args{i+1}; + case 'maximize', maximize = args{i+1}; + otherwise, + error(['invalid argument name ' args{i}]); + end + end +end + +engine.maximize = maximize; + +if ~isempty(engine.filename) + engine.fid = fopen(engine.filename, 'w'); + if engine.fid == 0 + error(['can''t open ' engine.filename]); + end +else + engine.fid = []; +end + +gdl = engine.gdl; +bnet = bnet_from_engine(engine); + +ndoms = length(gdl.doms); +ns = bnet.node_sizes; +onodes = find(~isemptycell(evidence)); +pot_type = determine_pot_type(bnet, onodes); + +% prime each local kernel with evidence (if any) +local_kernel = cell(1, ndoms); +for i=1:ndoms + if myismember(i, exclude) + local_kernel{i} = mk_initial_pot(pot_type, gdl.doms{i}, ns, bnet.cnodes, onodes); + else + e = bnet.equiv_class(i); + local_kernel{i} = convert_to_pot(bnet.CPD{e}, pot_type, gdl.doms{i}(:), evidence); + end +end + +% initialise all msgs to 1s +msg = cell(ndoms, ndoms); +for i=1:ndoms + nbrs = gdl.nbrs{i}; + for j=nbrs(:)' + dom = gdl.sepset{i,j}; + msg{i,j} = mk_initial_pot(pot_type, dom, ns, bnet.cnodes, onodes); + end +end + +switch engine.protocol + case 'parallel', + [engine.marginal_domains, niter] = parallel_protocol(engine, evidence, pot_type, local_kernel, msg); + case 'tree', + engine.marginal_domains = serial_protocol(engine, evidence, pot_type, local_kernel, msg); + niter = 1; +end +engine.niter = niter; + +%fprintf('just finished %d iterations of belprop\n', niter); + +if ~isempty(engine.filename) + fclose(engine.fid); +end diff --git a/sourcecodes/bnt-master/BNT/inference/static/@belprop_inf_engine/find_mpe.m b/sourcecodes/bnt-master/BNT/inference/static/@belprop_inf_engine/find_mpe.m new file mode 100644 index 00000000..73bd0abc --- /dev/null +++ b/sourcecodes/bnt-master/BNT/inference/static/@belprop_inf_engine/find_mpe.m @@ -0,0 +1,49 @@ +function mpe = find_mpe(engine, evidence, varargin) +% FIND_MPE Find the most probable explanation of the data (belprop) +% function mpe = find_mpe(engine, evidence,...) +% +% evidence{i} = [] if X(i) is hidden, and otherwise contains its observed value (scalar or column vector). +% +% This finds the marginally most likely value for each hidden node, +% and may give the wrong results even if the graph is acyclic, +% unless you set break_ties = 1. +% +% The following optional arguments can be specified in the form of name/value pairs: +% [default value in brackets] +% +% break_ties is optional. If 1, we will force ties to be broken consistently +% by calling enter_evidence N times. (see Jensen96, p106) Default = 1. + +break_ties = 1; + +% parse optional params +args = varargin; +nargs = length(args); +for i=1:2:nargs + switch args{i}, + case 'break_ties', break_ties = args{i+1}; + otherwise, + error(['invalid argument name ' args{i}]); + end +end + +engine = enter_evidence(engine, evidence, 'maximize', 1); + +observed = ~isemptycell(evidence); +evidence = evidence(:); % hack to handle unrolled DBNs +N = length(evidence); +mpe = cell(1,N); +for i=1:N + m = marginal_nodes(engine, i); + % observed nodes are all set to 1 inside the inference engine, so we must undo this + if observed(i) + mpe{i} = evidence{i}; + else + mpe{i} = argmax(m.T); + if break_ties + evidence{i} = mpe{i}; + [engine, ll] = enter_evidence(engine, evidence, 'maximize', 1); + end + end +end + diff --git a/sourcecodes/bnt-master/BNT/inference/static/@belprop_inf_engine/loopy_converged.m b/sourcecodes/bnt-master/BNT/inference/static/@belprop_inf_engine/loopy_converged.m new file mode 100644 index 00000000..fba4f2fd --- /dev/null +++ b/sourcecodes/bnt-master/BNT/inference/static/@belprop_inf_engine/loopy_converged.m @@ -0,0 +1,13 @@ +function niter = loopy_converged(engine) +% LOOPY_CONVERGED Did loopy belief propagation converge? 0 means no, eles we return the num. iterations. +% function niter = loopy_converged(engine) +% +% We use a simple heuristic: we say convergence occurred if the number of iterations +% used was less than the maximum allowed. + +if engine.niter == engine.max_iter + niter = 0; +else + niter = engine.niter; +end +%conv = (strcmp(engine.protocol, 'tree') | (engine.niter < engine.max_iter)); diff --git a/sourcecodes/bnt-master/BNT/inference/static/@belprop_inf_engine/marginal_family.m b/sourcecodes/bnt-master/BNT/inference/static/@belprop_inf_engine/marginal_family.m new file mode 100644 index 00000000..afe404a8 --- /dev/null +++ b/sourcecodes/bnt-master/BNT/inference/static/@belprop_inf_engine/marginal_family.m @@ -0,0 +1,6 @@ +function [marginal, pot] = marginal_family(engine, query) +% MARGINAL_NODES Compute the marginal on the family of the specified query node (belprop) +% [marginal, pot] = marginal_family(engine, query) + +pot = engine.marginal_domains{query}; +marginal = pot_to_marginal(pot); diff --git a/sourcecodes/bnt-master/BNT/inference/static/@belprop_inf_engine/marginal_nodes.m b/sourcecodes/bnt-master/BNT/inference/static/@belprop_inf_engine/marginal_nodes.m new file mode 100644 index 00000000..0c2b5d94 --- /dev/null +++ b/sourcecodes/bnt-master/BNT/inference/static/@belprop_inf_engine/marginal_nodes.m @@ -0,0 +1,14 @@ +function [marginal, pot] = marginal_nodes(engine, query) +% MARGINAL_NODES Compute the marginal on the specified query nodes (belprop) +% [marginal, pot] = marginal_nodes(engine, query) +% +% query must be a subset of a family + +if isempty(query) + big_pot = engine.marginal_domains{1}; % pick an arbitrary domain +else + big_pot = engine.marginal_domains{query(end)}; +end +pot = marginalize_pot(big_pot, query); +marginal = pot_to_marginal(pot); + diff --git a/sourcecodes/bnt-master/BNT/inference/static/@belprop_inf_engine/private/CVS/Entries b/sourcecodes/bnt-master/BNT/inference/static/@belprop_inf_engine/private/CVS/Entries new file mode 100644 index 00000000..938d9867 --- /dev/null +++ b/sourcecodes/bnt-master/BNT/inference/static/@belprop_inf_engine/private/CVS/Entries @@ -0,0 +1,4 @@ +/junk/1.1.1.1/Wed May 29 15:59:56 2002// +/parallel_protocol.m/1.1.1.1/Wed May 29 15:59:56 2002// +/tree_protocol.m/1.1.1.1/Wed May 29 15:59:56 2002// +D diff --git a/sourcecodes/bnt-master/BNT/inference/static/@belprop_inf_engine/private/CVS/Repository b/sourcecodes/bnt-master/BNT/inference/static/@belprop_inf_engine/private/CVS/Repository new file mode 100644 index 00000000..9681913e --- /dev/null +++ b/sourcecodes/bnt-master/BNT/inference/static/@belprop_inf_engine/private/CVS/Repository @@ -0,0 +1 @@ +FullBNT/BNT/inference/static/@belprop_inf_engine/private diff --git a/sourcecodes/bnt-master/BNT/inference/static/@belprop_inf_engine/private/CVS/Root b/sourcecodes/bnt-master/BNT/inference/static/@belprop_inf_engine/private/CVS/Root new file mode 100644 index 00000000..f3bd14a6 --- /dev/null +++ b/sourcecodes/bnt-master/BNT/inference/static/@belprop_inf_engine/private/CVS/Root @@ -0,0 +1 @@ +:ext:nsaunier@bnt.cvs.sourceforge.net:/cvsroot/bnt diff --git a/sourcecodes/bnt-master/BNT/inference/static/@belprop_inf_engine/private/junk b/sourcecodes/bnt-master/BNT/inference/static/@belprop_inf_engine/private/junk new file mode 100644 index 00000000..11438db0 --- /dev/null +++ b/sourcecodes/bnt-master/BNT/inference/static/@belprop_inf_engine/private/junk @@ -0,0 +1,68 @@ +fgraph +fgraph%fgraph%fgraph%fgraph%fgraph%fgraph%fgraph%fgraph%fgraph%fgraph%fgraph%fgraph +fgraph +fgraphffgraphufgraphnfgraphcfgraphtfgraphifgraphofgraphnfgraph fgraph[fgraphbfgraphefgraphlfgraph,fgraph fgraphifgraphtfgraphefgraphrfgraph]fgraph fgraph=fgraph fgraphpfgraphafgraphrfgraphafgraphlfgraphlfgraphefgraphlfgraph_fgraphpfgraphrfgraphofgraphtfgraphofgraphcfgraphofgraphlfgraph(fgraphefgraphnfgraphgfgraphifgraphnfgraphefgraph,fgraph fgraphefgraphvfgraphifgraphdfgraphefgraphnfgraphcfgraphefgraph,fgraph fgraphpfgraphofgraphtfgraph_fgraphtfgraphyfgraphpfgraphefgraph,fgraph fgraphlfgraphofgraphcfgraphafgraphlfgraph_fgraphkfgraphefgraphrfgraphnfgraphefgraphlfgraph,fgraph fgraphmfgraphsfgraphgfgraph)fgraph +fgraph +fgraphdfgraphofgraphmfgraphsfgraph fgraph=fgraph fgraphefgraphnfgraphgfgraphifgraphnfgraphefgraph.fgraphffgraphgfgraphrfgraphafgraphpfgraphhfgraph.fgraphdfgraphofgraphmfgraphsfgraph;fgraph +fgraphnfgraphdfgraphofgraphmfgraphsfgraph fgraph=fgraph fgraphlfgraphefgraphnfgraphgfgraphtfgraphhfgraph(fgraphdfgraphofgraphmfgraphsfgraph)fgraph;fgraph +fgraphnfgraphsfgraph fgraph=fgraph fgraphefgraphnfgraphgfgraphifgraphnfgraphefgraph.fgraphffgraphgfgraphrfgraphafgraphpfgraphhfgraph.fgraphnfgraphofgraphdfgraphefgraph_fgraphsfgraphifgraphzfgraphefgraphsfgraph;fgraph +fgraphofgraphnfgraphofgraphdfgraphefgraphsfgraph fgraph=fgraph fgraphffgraphifgraphnfgraphdfgraph(fgraph~fgraphifgraphsfgraphefgraphmfgraphpfgraphtfgraphyfgraphcfgraphefgraphlfgraphlfgraph(fgraphefgraphvfgraphifgraphdfgraphefgraphnfgraphcfgraphefgraph)fgraph)fgraph;fgraph +fgraphcfgraphnfgraphofgraphdfgraphefgraphsfgraph fgraph=fgraph fgraphefgraphnfgraphgfgraphifgraphnfgraphefgraph.fgraphffgraphgfgraphrfgraphafgraphpfgraphhfgraph.fgraphcfgraphnfgraphofgraphdfgraphefgraphsfgraph;fgraph +fgraph +fgraphpfgraphrfgraphofgraphdfgraph_fgraphofgraphffgraph_fgraphmfgraphsfgraphgfgraph fgraph=fgraph fgraphcfgraphefgraphlfgraphlfgraph(fgraph1fgraph,fgraph fgraphnfgraphdfgraphofgraphmfgraphsfgraph)fgraph;fgraph +fgraphbfgraphefgraphlfgraph fgraph=fgraph fgraphcfgraphefgraphlfgraphlfgraph(fgraph1fgraph,fgraph fgraphnfgraphdfgraphofgraphmfgraphsfgraph)fgraph;fgraph +fgraphofgraphlfgraphdfgraph_fgraphbfgraphefgraphlfgraph fgraph=fgraph fgraphcfgraphefgraphlfgraphlfgraph(fgraph1fgraph,fgraph fgraphnfgraphdfgraphofgraphmfgraphsfgraph)fgraph;fgraph +fgraph +fgraphcfgraphofgraphnfgraphvfgraphefgraphrfgraphgfgraphefgraphdfgraph fgraph=fgraph fgraph0fgraph;fgraph +fgraphifgraphtfgraphefgraphrfgraph fgraph=fgraph fgraph1fgraph;fgraph +fgraphwfgraphhfgraphifgraphlfgraphefgraph fgraph~fgraphcfgraphofgraphnfgraphvfgraphefgraphrfgraphgfgraphefgraphdfgraph fgraph&fgraph fgraph(fgraphifgraphtfgraphefgraphrfgraph fgraph<fgraph=fgraph fgraphefgraphnfgraphgfgraphifgraphnfgraphefgraph.fgraphmfgraphafgraphxfgraph_fgraphifgraphtfgraphefgraphrfgraph)fgraph +fgraph fgraph fgraph +fgraph fgraph fgraph%fgraph fgraphefgraphafgraphcfgraphhfgraph fgraphnfgraphofgraphdfgraphefgraph fgraphmfgraphufgraphlfgraphtfgraphifgraphpfgraphlfgraphifgraphefgraphsfgraph fgraphafgraphlfgraphlfgraph fgraphifgraphtfgraphsfgraph fgraphifgraphnfgraphcfgraphofgraphmfgraphifgraphnfgraphgfgraph fgraphmfgraphsfgraphgfgraphsfgraph fgraphafgraphnfgraphdfgraph fgraphcfgraphofgraphmfgraphpfgraphufgraphtfgraphefgraphsfgraph fgraphifgraphtfgraphsfgraph fgraphlfgraphofgraphcfgraphafgraphlfgraph fgraphbfgraphefgraphlfgraphifgraphefgraphffgraph +fgraph fgraph fgraphofgraphlfgraphdfgraph_fgraphbfgraphefgraphlfgraph fgraph=fgraph fgraphbfgraphefgraphlfgraph;fgraph +fgraph fgraph fgraphffgraphofgraphrfgraph fgraphifgraph=fgraph1fgraph:fgraphnfgraphdfgraphofgraphmfgraphsfgraph +fgraph fgraph fgraph fgraph fgraphpfgraphrfgraphofgraphdfgraph_fgraphofgraphffgraph_fgraphmfgraphsfgraphgfgraph{fgraphifgraph}fgraph fgraph=fgraph fgraphmfgraphkfgraph_fgraphifgraphnfgraphifgraphtfgraphifgraphafgraphlfgraph_fgraphpfgraphofgraphtfgraph(fgraphpfgraphofgraphtfgraph_fgraphtfgraphyfgraphpfgraphefgraph,fgraph fgraphdfgraphofgraphmfgraphsfgraph{fgraphifgraph}fgraph,fgraph fgraphnfgraphsfgraph,fgraph fgraphcfgraphnfgraphofgraphdfgraphefgraphsfgraph,fgraph fgraphofgraphnfgraphofgraphdfgraphefgraphsfgraph)fgraph;fgraph +fgraph fgraph fgraph fgraph fgraphnfgraphbfgraphrfgraphsfgraph fgraph=fgraph fgraphefgraphnfgraphgfgraphifgraphnfgraphefgraph.fgraphffgraphgfgraphrfgraphafgraphpfgraphhfgraph.fgraphnfgraphbfgraphrfgraphsfgraph{fgraphifgraph}fgraph;fgraph +fgraph fgraph fgraph fgraph fgraphffgraphofgraphrfgraph fgraphjfgraph=fgraphnfgraphbfgraphrfgraphsfgraph(fgraph:fgraph)fgraph'fgraph +fgraph fgraph fgraph fgraph fgraph fgraph fgraphpfgraphrfgraphofgraphdfgraph_fgraphofgraphffgraph_fgraphmfgraphsfgraphgfgraph{fgraphifgraph}fgraph fgraph=fgraph fgraphmfgraphufgraphlfgraphtfgraphifgraphpfgraphlfgraphyfgraph_fgraphbfgraphyfgraph_fgraphpfgraphofgraphtfgraph(fgraphpfgraphrfgraphofgraphdfgraph_fgraphofgraphffgraph_fgraphmfgraphsfgraphgfgraph{fgraphifgraph}fgraph,fgraph fgraphmfgraphsfgraphgfgraph{fgraphjfgraph,fgraphifgraph}fgraph)fgraph;fgraph +fgraph fgraph fgraph fgraph fgraphefgraphnfgraphdfgraph +fgraph fgraph fgraph fgraph fgraphbfgraphefgraphlfgraph{fgraphifgraph}fgraph fgraph=fgraph fgraphnfgraphofgraphrfgraphmfgraphafgraphlfgraphifgraphzfgraphefgraph_fgraphpfgraphofgraphtfgraph(fgraphmfgraphufgraphlfgraphtfgraphifgraphpfgraphlfgraphyfgraph_fgraphpfgraphofgraphtfgraphsfgraph(fgraphpfgraphrfgraphofgraphdfgraph_fgraphofgraphffgraph_fgraphmfgraphsfgraphgfgraph{fgraphifgraph}fgraph,fgraph fgraphlfgraphofgraphcfgraphafgraphlfgraph_fgraphkfgraphefgraphrfgraphnfgraphefgraphlfgraph{fgraphifgraph}fgraph)fgraph)fgraph;fgraph +fgraph fgraph fgraphefgraphnfgraphdfgraph +fgraph +fgraph fgraph fgraph%fgraph fgraphcfgraphofgraphnfgraphvfgraphefgraphrfgraphgfgraphefgraphdfgraph?fgraph +fgraph fgraph fgraphifgraphffgraph fgraphifgraphtfgraphefgraphrfgraph=fgraph=fgraph1fgraph +fgraph fgraph fgraph fgraph fgraphcfgraphofgraphnfgraphvfgraphefgraphrfgraphgfgraphefgraphdfgraph fgraph=fgraph fgraph0fgraph;fgraph +fgraph fgraph fgraphefgraphlfgraphsfgraphefgraph +fgraph fgraph fgraph fgraph fgraphcfgraphofgraphnfgraphvfgraphefgraphrfgraphgfgraphefgraphdfgraph fgraph=fgraph fgraph1fgraph;fgraph +fgraph fgraph fgraph fgraph fgraphffgraphofgraphrfgraph fgraphifgraph=fgraph1fgraph:fgraphnfgraphdfgraphofgraphmfgraphsfgraph +fgraph fgraph fgraph fgraph fgraph fgraph fgraphifgraphffgraph fgraph~fgraphafgraphpfgraphpfgraphrfgraphofgraphxfgraphefgraphqfgraph_fgraphpfgraphofgraphtfgraph(fgraphbfgraphefgraphlfgraph{fgraphifgraph}fgraph,fgraph fgraphofgraphlfgraphdfgraph_fgraphbfgraphefgraphlfgraph{fgraphifgraph}fgraph,fgraph fgraphefgraphnfgraphgfgraphifgraphnfgraphefgraph.fgraphtfgraphofgraphlfgraph)fgraph +fgraph fgraphcfgraphofgraphnfgraphvfgraphefgraphrfgraphgfgraphefgraphdfgraph fgraph=fgraph fgraph0fgraph;fgraph +fgraph fgraphbfgraphrfgraphefgraphafgraphkfgraph;fgraph +fgraph fgraph fgraph fgraph fgraph fgraph fgraphefgraphnfgraphdfgraph +fgraph fgraph fgraph fgraph fgraphefgraphnfgraphdfgraph +fgraph fgraph fgraphefgraphnfgraphdfgraph +fgraph +fgraph fgraph fgraphifgraphffgraph fgraph~fgraphcfgraphofgraphnfgraphvfgraphefgraphrfgraphgfgraphefgraphdfgraph +fgraph fgraph fgraph fgraph fgraphofgraphlfgraphdfgraph_fgraphmfgraphsfgraphgfgraph fgraph=fgraph fgraphmfgraphsfgraphgfgraph;fgraph +fgraph fgraph fgraph fgraph fgraph%fgraph fgraphefgraphafgraphcfgraphhfgraph fgraphnfgraphofgraphdfgraphefgraph fgraphsfgraphefgraphnfgraphdfgraphsfgraph fgraphafgraph fgraphmfgraphsfgraphgfgraph fgraphtfgraphofgraph fgraphefgraphafgraphcfgraphhfgraph fgraphofgraphffgraph fgraphifgraphtfgraphsfgraph fgraphnfgraphefgraphifgraphgfgraphhfgraphbfgraphofgraphrfgraphsfgraph +fgraph fgraph fgraph fgraph fgraphffgraphofgraphrfgraph fgraphifgraph=fgraph1fgraph:fgraphnfgraphdfgraphofgraphmfgraphsfgraph +fgraph fgraph fgraph fgraph fgraph fgraph fgraphnfgraphbfgraphrfgraphsfgraph fgraph=fgraph fgraphefgraphnfgraphgfgraphifgraphnfgraphefgraph.fgraphffgraphgfgraphrfgraphafgraphpfgraphhfgraph.fgraphnfgraphbfgraphrfgraphsfgraph{fgraphifgraph}fgraph;fgraph +fgraph fgraph fgraph fgraph fgraph fgraph fgraphffgraphofgraphrfgraph fgraphjfgraph=fgraphnfgraphbfgraphrfgraphsfgraph(fgraph:fgraph)fgraph'fgraph +fgraph fgraph%fgraph fgraphmfgraphufgraphlfgraphtfgraphifgraphpfgraphlfgraphyfgraph fgraphafgraphlfgraphlfgraph fgraphifgraphnfgraphcfgraphofgraphmfgraphifgraphnfgraphgfgraph fgraphmfgraphsfgraphgfgraphsfgraph fgraphefgraphxfgraphcfgraphefgraphpfgraphtfgraph fgraphffgraphrfgraphofgraphmfgraph fgraphjfgraph +fgraph fgraphtfgraphefgraphmfgraphpfgraph fgraph=fgraph fgraphpfgraphrfgraphofgraphdfgraph_fgraphofgraphffgraph_fgraphmfgraphsfgraphgfgraph{fgraphifgraph}fgraph;fgraph +fgraph fgraphtfgraphefgraphmfgraphpfgraph fgraph=fgraph fgraphdfgraphifgraphvfgraphifgraphdfgraphefgraph_fgraphbfgraphyfgraph_fgraphpfgraphofgraphtfgraph(fgraphtfgraphefgraphmfgraphpfgraph,fgraph fgraphofgraphlfgraphdfgraph_fgraphmfgraphsfgraphgfgraph{fgraphjfgraph,fgraphifgraph}fgraph)fgraph;fgraph +fgraph fgraph%fgraph fgraphsfgraphefgraphnfgraphdfgraph fgraphmfgraphsfgraphgfgraph fgraphffgraphrfgraphofgraphmfgraph fgraphifgraph fgraphtfgraphofgraph fgraphjfgraph +fgraph fgraphtfgraphefgraphmfgraphpfgraph fgraph=fgraph fgraphmfgraphufgraphlfgraphtfgraphifgraphpfgraphlfgraphyfgraph_fgraphbfgraphyfgraph_fgraphpfgraphofgraphtfgraph(fgraphtfgraphefgraphmfgraphpfgraph,fgraph fgraphlfgraphofgraphcfgraphafgraphlfgraph_fgraphkfgraphefgraphrfgraphnfgraphefgraphlfgraph{fgraphifgraph}fgraph)fgraph;fgraph +fgraph fgraphifgraphffgraph fgraphefgraphnfgraphgfgraphifgraphnfgraphefgraph.fgraphmfgraphafgraphxfgraphifgraphmfgraphifgraphzfgraphefgraph +fgraph fgraph fgraph fgraphtfgraphefgraphmfgraphpfgraph2fgraph fgraph=fgraph fgraphmfgraphafgraphrfgraphgfgraphifgraphnfgraphafgraphlfgraphifgraphzfgraphefgraph_fgraphpfgraphofgraphtfgraph_fgraphmfgraphafgraphxfgraph(fgraphtfgraphefgraphmfgraphpfgraph,fgraph fgraphefgraphnfgraphgfgraphifgraphnfgraphefgraph.fgraphffgraphgfgraphrfgraphafgraphpfgraphhfgraph.fgraphsfgraphefgraphpfgraphsfgraphefgraphtfgraph{fgraphifgraph,fgraphjfgraph}fgraph)fgraph;fgraph +fgraph fgraphefgraphlfgraphsfgraphefgraph +fgraph fgraph fgraph fgraphtfgraphefgraphmfgraphpfgraph2fgraph fgraph=fgraph fgraphmfgraphafgraphrfgraphgfgraphifgraphnfgraphafgraphlfgraphifgraphzfgraphefgraph_fgraphpfgraphofgraphtfgraph(fgraphtfgraphefgraphmfgraphpfgraph,fgraph fgraphefgraphnfgraphgfgraphifgraphnfgraphefgraph.fgraphffgraphgfgraphrfgraphafgraphpfgraphhfgraph.fgraphsfgraphefgraphpfgraphsfgraphefgraphtfgraph{fgraphifgraph,fgraphjfgraph}fgraph)fgraph;fgraph +fgraph fgraphefgraphnfgraphdfgraph +fgraph fgraphmfgraphsfgraphgfgraph{fgraphifgraph,fgraphjfgraph}fgraph fgraph=fgraph fgraphnfgraphofgraphrfgraphmfgraphafgraphlfgraphifgraphzfgraphefgraph_fgraphpfgraphofgraphtfgraph(fgraphtfgraphefgraphmfgraphpfgraph2fgraph)fgraph;fgraph +fgraph fgraph fgraph fgraph fgraph fgraph fgraphefgraphnfgraphdfgraph +fgraph fgraph fgraph fgraph fgraphefgraphnfgraphdfgraph +fgraph fgraph fgraphefgraphnfgraphdfgraph +fgraph +fgraph fgraph fgraphifgraphtfgraphefgraphrfgraph fgraph=fgraph fgraphifgraphtfgraphefgraphrfgraph fgraph+fgraph fgraph1fgraph;fgraph +fgraphefgraphnfgraphdfgraph +fgraph +gdl diff --git a/sourcecodes/bnt-master/BNT/inference/static/@belprop_inf_engine/private/parallel_protocol.m b/sourcecodes/bnt-master/BNT/inference/static/@belprop_inf_engine/private/parallel_protocol.m new file mode 100644 index 00000000..3e702f7b --- /dev/null +++ b/sourcecodes/bnt-master/BNT/inference/static/@belprop_inf_engine/private/parallel_protocol.m @@ -0,0 +1,86 @@ +function [bel, niter] = parallel_protocol(engine, evidence, pot_type, local_kernel, msg) + +bnet = bnet_from_engine(engine); +ns = bnet.node_sizes; +onodes = find(~isemptycell(evidence)); + +ndoms = length(engine.gdl.doms); +prod_of_msg = cell(1, ndoms); +bel = cell(1, ndoms); +old_bel = cell(1, ndoms); + +converged = 0; +iter = 1; +while ~converged && (iter <= engine.max_iter) + + % each node multiplies all its incoming msgs and computes its local belief + old_bel = bel; + for i=1:ndoms + prod_of_msg{i} = mk_initial_pot(pot_type, engine.gdl.doms{i}, ns, bnet.cnodes, onodes); + nbrs = engine.gdl.nbrs{i}; + for j=nbrs(:)' + prod_of_msg{i} = multiply_by_pot(prod_of_msg{i}, msg{j,i}); + end + bel{i} = normalize_pot(multiply_by_pot(local_kernel{i}, prod_of_msg{i})); + end + + if ~isempty(engine.fid) + for i=1:ndoms + tmp = pot_to_marginal(bel{i}); + %fprintf(engine.fid, '%9.7f ', tmp.T(1)); + fprintf(engine.fid, '%9.7f ', tmp.U(1)); + end + %fprintf(engine.fid, ' U '); + %for i=1:ndoms + % tmp = pot_to_marginal(bel{i}); + % fprintf(engine.fid, '%9.7f ', tmp.U(1)); + %end + fprintf(engine.fid, '\n'); + end + + % converged? + if iter==1 + converged = 0; + else + converged = 1; + for i=1:ndoms + if ~approxeq_pot(bel{i}, old_bel{i}, engine.tol) + converged = 0; + break; + end + end + end + + if ~converged + old_msg = msg; + % each node sends a msg to each of its neighbors + for i=1:ndoms + nbrs = engine.gdl.nbrs{i}; + for j=nbrs(:)' + % multiply all incoming msgs except from j + temp = prod_of_msg{i}; + temp = divide_by_pot(temp, old_msg{j,i}); + % send msg from i to j + temp = multiply_by_pot(temp, local_kernel{i}); + temp2 = marginalize_pot(temp, engine.gdl.sepset{i,j}, engine.maximize); + msg{i,j} = normalize_pot(temp2); + end + end + end + + iter = iter + 1; +end + + +niter = iter-1; + +if 0 +for i=1:ndoms + prod_of_msg{i} = mk_initial_pot(pot_type, engine.gdl.doms{i}, ns, bnet.cnodes, onodes); + nbrs = engine.gdl.nbrs{i}; + for j=nbrs(:)' + prod_of_msg{i} = multiply_by_pot(prod_of_msg{i}, msg{j,i}); + end + bel{i} = normalize_pot(multiply_by_pot(local_kernel{i}, prod_of_msg{i})); +end +end diff --git a/sourcecodes/bnt-master/BNT/inference/static/@belprop_inf_engine/private/tree_protocol.m b/sourcecodes/bnt-master/BNT/inference/static/@belprop_inf_engine/private/tree_protocol.m new file mode 100644 index 00000000..940e74ae --- /dev/null +++ b/sourcecodes/bnt-master/BNT/inference/static/@belprop_inf_engine/private/tree_protocol.m @@ -0,0 +1,48 @@ +function bel = tree_protocol(engine, evidence, pot_type, local_kernel, msg) + +bnet = bnet_from_engine(engine); +ns = bnet.node_sizes; +onodes = find(~isemptycell(evidence)); + +ndoms = length(engine.gdl.doms); +prod_of_msg = cell(1, ndoms); +bel = cell(1, ndoms); + +% collect to root (node to parents) +for n=engine.postorder + % absorb msgs from children + prod_of_msg{n} = mk_initial_pot(pot_type, engine.gdl.doms{n}, ns, bnet.cnodes, onodes); + for c=children(engine.tree, n) + prod_of_msg{n} = multiply_by_pot(prod_of_msg{n}, msg{c,n}); + end + % send msg to parents + for p=parents(engine.tree, n) + if iter==1 + temp = prod_of_msg{n}; + else + temp = divide_by_pot(prod_of_msg{n}, old_msg{p,n}); + end + temp = multiply_by_pot(temp, local_kernel{n}); + temp2 = marginalize_pot(temp, engine.gdl.sepset{n,p}, engine.maximize); + %fprintf('%d sends %d\n', n, p); + msg{n,p} = normalize_pot(temp2); + end +end + +% distribute from root (node to children) +for n=engine.preorder + % absorb from parents + %prod_of_msg{n} = mk_initial_pot(pot_type, doms{n}, ns, cnodes, onodes); + for p=parents(engine.tree, n) + prod_of_msg{n} = multiply_by_pot(prod_of_msg{n}, msg{p,n}); + end + bel{n} = normalize_pot(multiply_pots(prod_of_msg{n}, local_kernel{n})); + % send msg to children + for c=children(engine.tree, n) + temp = divide_by_pot(prod_of_msg{n}, msg{c,n}); + temp = multiply_by_pot(temp, local_kernel{n}); + temp2 = marginalize_pot(temp, engine.gdl.sepset{n,c}, engine.maximize); + %fprintf('%d sends %d\n', n, c); + msg{n,c} = normalize_pot(temp2); + end +end |
