wolffd@0: function engine = enter_evidence(engine, evidence) wolffd@0: wolffd@0: doms = engine.fg.doms; wolffd@0: ndoms = length(doms); wolffd@0: ns = engine.fg.node_sizes; wolffd@0: obs = find(~isemptycell(evidence)); wolffd@0: cobs = myintersect(obs, engine.fg.cnodes); wolffd@0: dobs = myintersect(obs, engine.fg.dnodes); wolffd@0: ns(cobs) = 0; wolffd@0: ns(dobs) = 1; wolffd@0: wolffd@0: % prime each local kernel with evidence (if any) wolffd@0: local_kernel = cell(1, ndoms); wolffd@0: for i=1:length(engine.fg.kernels_of_type) wolffd@0: u = engine.fg.kernels_of_type{i}; wolffd@0: local_kernel(u) = kernel_to_dpots(engine.fg.kernels{i}, evidence, engine.fg.domains_of_type{i}); wolffd@0: end wolffd@0: wolffd@0: % initialise all msgs to 1s wolffd@0: nedges = engine.fg.nedges; wolffd@0: msg = cell(1, nedges); wolffd@0: for i=1:nedges wolffd@0: msg{i} = dpot(engine.fg.sepset{i}, ns(engine.fg.sepset{i})); wolffd@0: end wolffd@0: wolffd@0: prod_of_msg = cell(1, ndoms); wolffd@0: bel = cell(1, ndoms); wolffd@0: old_bel = cell(1, ndoms); wolffd@0: wolffd@0: converged = 0; wolffd@0: iter = 1; wolffd@0: while ~converged & (iter <= engine.max_iter) wolffd@0: wolffd@0: % each node multiplies all its incoming msgs wolffd@0: for i=1:ndoms wolffd@0: prod_of_msg{i} = dpot(doms{i}, ns(doms{i})); wolffd@0: nbrs = engine.fg.nbrs{i}; wolffd@0: for j=1:length(nbrs) wolffd@0: ndx = engine.fg.edge_ndx(j,i); wolffd@0: prod_of_msg{i} = multiply_by_pot(prod_of_msg{i}, msg{ndx}); wolffd@0: end wolffd@0: end wolffd@0: old_msg = msg; wolffd@0: wolffd@0: % each node computes its local belief wolffd@0: for i=1:ndoms wolffd@0: bel{i} = normalize_pot(multiply_pots(prod_of_msg{i}, local_kernel{i})); wolffd@0: end wolffd@0: wolffd@0: % converged? wolffd@0: converged = 1; wolffd@0: for i=1:ndoms wolffd@0: if ~approxeq(bel{i}, old_bel{i}, engine.tol) wolffd@0: converged = 0; wolffd@0: break; wolffd@0: end wolffd@0: end wolffd@0: wolffd@0: if ~converged wolffd@0: % each node sends a msg to each of its neighbors wolffd@0: for i=1:ndoms wolffd@0: nbrs = engine.fg.nbrs{i}; wolffd@0: for j=1:length(nbrs) wolffd@0: % multiply all incoming msgs except from j wolffd@0: temp = prod_of_msg{i}; wolffd@0: ndx = engine.fg.edge_ndx(j,i); wolffd@0: temp = divide_by_pot(temp, old_msg{ndx}); wolffd@0: % send msg from i to j wolffd@0: temp = multiply_by_pot(temp, local_kernel{i}); wolffd@0: ndx = engine.fg.edge_ndx(i,j); wolffd@0: msg{ndx} = normalize_pot(marginalize_pot(temp, engine.fg.sepset{ndx})); wolffd@0: end wolffd@0: end wolffd@0: end wolffd@0: wolffd@0: iter = iter + 1; wolffd@0: end wolffd@0: wolffd@0: wolffd@0: engine.marginal = bel;