wolffd@0
|
1 function [engine, loglik] = initialize_engine(engine)
|
wolffd@0
|
2 %initialize
|
wolffd@0
|
3 bnet = bnet_from_engine(engine);
|
wolffd@0
|
4 ns = bnet.node_sizes(:);
|
wolffd@0
|
5 N = length(bnet.dag);
|
wolffd@0
|
6
|
wolffd@0
|
7 pot_type = 'scg'
|
wolffd@0
|
8 check_for_cd_arcs([], bnet.cnodes, bnet.dag);
|
wolffd@0
|
9
|
wolffd@0
|
10 % Evaluate CPDs with evidence, and convert to potentials
|
wolffd@0
|
11 pot = cell(1, N);
|
wolffd@0
|
12 C = length(engine.cliques);
|
wolffd@0
|
13 inited = zeros(1, C);
|
wolffd@0
|
14 clpot = cell(1, C);
|
wolffd@0
|
15 evidence = cell(1, N);
|
wolffd@0
|
16 for n=1:N
|
wolffd@0
|
17 fam = family(bnet.dag, n);
|
wolffd@0
|
18 e = bnet.equiv_class(n);
|
wolffd@0
|
19 pot{n} = CPD_to_scgpot(bnet.CPD{e}, fam, ns, bnet.cnodes, evidence);
|
wolffd@0
|
20 cindex = engine.clq_ass_to_node(n);
|
wolffd@0
|
21 if inited(cindex)
|
wolffd@0
|
22 %clpot{cindex} = direct_combine_pots(clpot{cindex}, pot{n});
|
wolffd@0
|
23 clpot{cindex} = direct_combine_pots(pot{n}, clpot{cindex});
|
wolffd@0
|
24 else
|
wolffd@0
|
25 clpot{cindex} = pot{n};
|
wolffd@0
|
26 inited(cindex) = 1;
|
wolffd@0
|
27 end
|
wolffd@0
|
28 end
|
wolffd@0
|
29
|
wolffd@0
|
30 for i=1:C
|
wolffd@0
|
31 if inited(i) == 0
|
wolffd@0
|
32 clpot{i} = scgpot([], [], [], []);
|
wolffd@0
|
33 end
|
wolffd@0
|
34 end
|
wolffd@0
|
35
|
wolffd@0
|
36 seppot = cell(C, C);
|
wolffd@0
|
37 % separators are is not need to initialize
|
wolffd@0
|
38
|
wolffd@0
|
39 % collect to root (node to parents)
|
wolffd@0
|
40 for n=engine.postorder(1:end-1)
|
wolffd@0
|
41 for p=parents(engine.jtree, n)
|
wolffd@0
|
42 [margpot, comppot] = complement_pot(clpot{n}, engine.separator{p,n});
|
wolffd@0
|
43 margpot = marginalize_pot(clpot{n}, engine.separator{p,n});
|
wolffd@0
|
44 clpot{n} = comppot;
|
wolffd@0
|
45 %seppot{p, n} = margpot;
|
wolffd@0
|
46 clpot{p} = combine_pots(clpot{p}, margpot);
|
wolffd@0
|
47 %clpot{p} = combine_pots(margpot, clpot{p});
|
wolffd@0
|
48 end
|
wolffd@0
|
49 end
|
wolffd@0
|
50
|
wolffd@0
|
51 temppot = clpot;
|
wolffd@0
|
52 %temppot = clpot{engine.root};
|
wolffd@0
|
53 for n=engine.preorder
|
wolffd@0
|
54 for c=children(engine.jtree, n)
|
wolffd@0
|
55 seppot{n,c} = marginalize_pot(temppot{n}, engine.separator{n,c});
|
wolffd@0
|
56 %seppot{n,c} = marginalize_pot(clpot{n}, engine.separator{n,c});
|
wolffd@0
|
57 %clpot{c} = direct_combine_pots(clpot{c}, seppot{n,c});
|
wolffd@0
|
58 temppot{c} = direct_combine_pots(temppot{c}, seppot{n,c});
|
wolffd@0
|
59 end
|
wolffd@0
|
60 end
|
wolffd@0
|
61
|
wolffd@0
|
62 engine.clpot = clpot;
|
wolffd@0
|
63 engine.seppot = seppot;
|
wolffd@0
|
64
|
wolffd@0
|
65
|