wolffd@0: function [engine, loglik] = initialize_engine(engine) wolffd@0: %initialize wolffd@0: bnet = bnet_from_engine(engine); wolffd@0: ns = bnet.node_sizes(:); wolffd@0: N = length(bnet.dag); wolffd@0: wolffd@0: pot_type = 'scg' wolffd@0: check_for_cd_arcs([], bnet.cnodes, bnet.dag); wolffd@0: wolffd@0: % Evaluate CPDs with evidence, and convert to potentials wolffd@0: pot = cell(1, N); wolffd@0: C = length(engine.cliques); wolffd@0: inited = zeros(1, C); wolffd@0: clpot = cell(1, C); wolffd@0: evidence = cell(1, N); wolffd@0: for n=1:N wolffd@0: fam = family(bnet.dag, n); wolffd@0: e = bnet.equiv_class(n); wolffd@0: pot{n} = CPD_to_scgpot(bnet.CPD{e}, fam, ns, bnet.cnodes, evidence); wolffd@0: cindex = engine.clq_ass_to_node(n); wolffd@0: if inited(cindex) wolffd@0: %clpot{cindex} = direct_combine_pots(clpot{cindex}, pot{n}); wolffd@0: clpot{cindex} = direct_combine_pots(pot{n}, clpot{cindex}); wolffd@0: else wolffd@0: clpot{cindex} = pot{n}; wolffd@0: inited(cindex) = 1; wolffd@0: end wolffd@0: end wolffd@0: wolffd@0: for i=1:C wolffd@0: if inited(i) == 0 wolffd@0: clpot{i} = scgpot([], [], [], []); wolffd@0: end wolffd@0: end wolffd@0: wolffd@0: seppot = cell(C, C); wolffd@0: % separators are is not need to initialize wolffd@0: wolffd@0: % collect to root (node to parents) wolffd@0: for n=engine.postorder(1:end-1) wolffd@0: for p=parents(engine.jtree, n) wolffd@0: [margpot, comppot] = complement_pot(clpot{n}, engine.separator{p,n}); wolffd@0: margpot = marginalize_pot(clpot{n}, engine.separator{p,n}); wolffd@0: clpot{n} = comppot; wolffd@0: %seppot{p, n} = margpot; wolffd@0: clpot{p} = combine_pots(clpot{p}, margpot); wolffd@0: %clpot{p} = combine_pots(margpot, clpot{p}); wolffd@0: end wolffd@0: end wolffd@0: wolffd@0: temppot = clpot; wolffd@0: %temppot = clpot{engine.root}; wolffd@0: for n=engine.preorder wolffd@0: for c=children(engine.jtree, n) wolffd@0: seppot{n,c} = marginalize_pot(temppot{n}, engine.separator{n,c}); wolffd@0: %seppot{n,c} = marginalize_pot(clpot{n}, engine.separator{n,c}); wolffd@0: %clpot{c} = direct_combine_pots(clpot{c}, seppot{n,c}); wolffd@0: temppot{c} = direct_combine_pots(temppot{c}, seppot{n,c}); wolffd@0: end wolffd@0: end wolffd@0: wolffd@0: engine.clpot = clpot; wolffd@0: engine.seppot = seppot; wolffd@0: wolffd@0: