% Converted to BNT from BIF by Web-based bif2bnt (2003-12-18T08:47:56)

node = struct('LVFAILURE', 1, ...
              'HISTORY', 2, ...
              'HYPOVOLEMIA', 3, ...
              'LVEDVOLUME', 4, ...
              'CVP', 5, ...
              'PCWP', 6, ...
              'STROKEVOLUME', 7, ...
              'ERRLOWOUTPUT', 8, ...
              'INTUBATION', 9, ...
              'KINKEDTUBE', 10, ...
              'DISCONNECT', 11, ...
              'MINVOLSET', 12, ...
              'VENTMACH', 13, ...
              'VENTTUBE', 14, ...
              'VENTLUNG', 15, ...
              'VENTALV', 16, ...
              'ARTCO2', 17, ...
              'INSUFFANESTH', 18, ...
              'FIO2', 19, ...
              'PVSAT', 20, ...
              'PULMEMBOLUS', 21, ...
              'SHUNT', 22, ...
              'SAO2', 23, ...
              'ANAPHYLAXIS', 24, ...
              'TPR', 25, ...
              'CATECHOL', 26, ...
              'HR', 27, ...
              'HRBP', 28, ...
              'ERRCAUTER', 29, ...
              'HREKG', 30, ...
              'HRSAT', 31, ...
              'EXPCO2', 32, ...
              'MINVOL', 33, ...
              'PAP', 34, ...
              'PRESS', 35, ...
              'CO', 36, ...
              'BP', 37);

adjacency = zeros(37);
adjacency([node.LVFAILURE], node.HISTORY) = 1;
adjacency([node.HYPOVOLEMIA node.LVFAILURE], node.LVEDVOLUME) = 1;
adjacency([node.LVEDVOLUME], node.CVP) = 1;
adjacency([node.LVEDVOLUME], node.PCWP) = 1;
adjacency([node.HYPOVOLEMIA node.LVFAILURE], node.STROKEVOLUME) = 1;
adjacency([node.MINVOLSET], node.VENTMACH) = 1;
adjacency([node.VENTMACH node.DISCONNECT], node.VENTTUBE) = 1;
adjacency([node.VENTTUBE node.KINKEDTUBE node.INTUBATION], node.VENTLUNG) = 1;
adjacency([node.VENTLUNG node.INTUBATION], node.VENTALV) = 1;
adjacency([node.VENTALV], node.ARTCO2) = 1;
adjacency([node.FIO2 node.VENTALV], node.PVSAT) = 1;
adjacency([node.PULMEMBOLUS node.INTUBATION], node.SHUNT) = 1;
adjacency([node.SHUNT node.PVSAT], node.SAO2) = 1;
adjacency([node.ANAPHYLAXIS], node.TPR) = 1;
adjacency([node.TPR node.SAO2 node.INSUFFANESTH node.ARTCO2], node.CATECHOL) = 1;
adjacency([node.CATECHOL], node.HR) = 1;
adjacency([node.HR node.ERRLOWOUTPUT], node.HRBP) = 1;
adjacency([node.ERRCAUTER node.HR], node.HREKG) = 1;
adjacency([node.ERRCAUTER node.HR], node.HRSAT) = 1;
adjacency([node.ARTCO2 node.VENTLUNG], node.EXPCO2) = 1;
adjacency([node.VENTLUNG node.INTUBATION], node.MINVOL) = 1;
adjacency([node.PULMEMBOLUS], node.PAP) = 1;
adjacency([node.VENTTUBE node.KINKEDTUBE node.INTUBATION], node.PRESS) = 1;
adjacency([node.HR node.STROKEVOLUME], node.CO) = 1;
adjacency([node.CO node.TPR], node.BP) = 1;

value = {{'TRUE'; 'FALSE'}, ...
         {'TRUE'; 'FALSE'}, ...
         {'TRUE'; 'FALSE'}, ...
         {'LOW'; 'NORMAL'; 'HIGH'}, ...
         {'LOW'; 'NORMAL'; 'HIGH'}, ...
         {'LOW'; 'NORMAL'; 'HIGH'}, ...
         {'LOW'; 'NORMAL'; 'HIGH'}, ...
         {'TRUE'; 'FALSE'}, ...
         {'NORMAL'; 'ESOPHAGEAL'; 'ONESIDED'}, ...
         {'TRUE'; 'FALSE'}, ...
         {'TRUE'; 'FALSE'}, ...
         {'LOW'; 'NORMAL'; 'HIGH'}, ...
         {'ZERO'; 'LOW'; 'NORMAL'; 'HIGH'}, ...
         {'ZERO'; 'LOW'; 'NORMAL'; 'HIGH'}, ...
         {'ZERO'; 'LOW'; 'NORMAL'; 'HIGH'}, ...
         {'ZERO'; 'LOW'; 'NORMAL'; 'HIGH'}, ...
         {'LOW'; 'NORMAL'; 'HIGH'}, ...
         {'TRUE'; 'FALSE'}, ...
         {'LOW'; 'NORMAL'}, ...
         {'LOW'; 'NORMAL'; 'HIGH'}, ...
         {'TRUE'; 'FALSE'}, ...
         {'NORMAL'; 'HIGH'}, ...
         {'LOW'; 'NORMAL'; 'HIGH'}, ...
         {'TRUE'; 'FALSE'}, ...
         {'LOW'; 'NORMAL'; 'HIGH'}, ...
         {'NORMAL'; 'HIGH'}, ...
         {'LOW'; 'NORMAL'; 'HIGH'}, ...
         {'LOW'; 'NORMAL'; 'HIGH'}, ...
         {'TRUE'; 'FALSE'}, ...
         {'LOW'; 'NORMAL'; 'HIGH'}, ...
         {'LOW'; 'NORMAL'; 'HIGH'}, ...
         {'ZERO'; 'LOW'; 'NORMAL'; 'HIGH'}, ...
         {'ZERO'; 'LOW'; 'NORMAL'; 'HIGH'}, ...
         {'LOW'; 'NORMAL'; 'HIGH'}, ...
         {'ZERO'; 'LOW'; 'NORMAL'; 'HIGH'}, ...
         {'LOW'; 'NORMAL'; 'HIGH'}, ...
         {'LOW'; 'NORMAL'; 'HIGH'}};

bnet = mk_bnet(adjacency, [2 2 2 3 3 3 3 2 3 2 2 3 4 4 4 4 3 2 2 3 2 2 3 2 3 2 3 3 2 3 3 4 4 3 4 3 3]);
bnet.CPD{node.LVFAILURE} = tabular_CPD(bnet, node.LVFAILURE, [0.05 0.95]);
bnet.CPD{node.HISTORY} = tabular_CPD(bnet, node.HISTORY, [0.9 0.01 0.1 0.99]);
bnet.CPD{node.HYPOVOLEMIA} = tabular_CPD(bnet, node.HYPOVOLEMIA, [0.2 0.8]);
bnet.CPD{node.LVEDVOLUME} = tabular_CPD(bnet, node.LVEDVOLUME, [0.95 0.01 0.98 0.05 0.04 0.09 0.01 0.9 0.01 0.9 0.01 0.05]);
bnet.CPD{node.CVP} = tabular_CPD(bnet, node.CVP, [0.95 0.04 0.01 0.04 0.95 0.29 0.01 0.01 0.7]);
bnet.CPD{node.PCWP} = tabular_CPD(bnet, node.PCWP, [0.95 0.04 0.01 0.04 0.95 0.04 0.01 0.01 0.95]);
bnet.CPD{node.STROKEVOLUME} = tabular_CPD(bnet, node.STROKEVOLUME, [0.98 0.5 0.95 0.05 0.01 0.49 0.04 0.9 0.01 0.01 0.01 0.05]);
bnet.CPD{node.ERRLOWOUTPUT} = tabular_CPD(bnet, node.ERRLOWOUTPUT, [0.05 0.95]);
bnet.CPD{node.INTUBATION} = tabular_CPD(bnet, node.INTUBATION, [0.92 0.03 0.05]);
bnet.CPD{node.KINKEDTUBE} = tabular_CPD(bnet, node.KINKEDTUBE, [0.04 0.96]);
bnet.CPD{node.DISCONNECT} = tabular_CPD(bnet, node.DISCONNECT, [0.1 0.9]);
bnet.CPD{node.MINVOLSET} = tabular_CPD(bnet, node.MINVOLSET, [0.05 0.9 0.05]);
bnet.CPD{node.VENTMACH} = tabular_CPD(bnet, node.VENTMACH, [0.05 0.05 0.05 0.93 0.01 0.01 0.01 0.93 0.01 0.01 0.01 0.93]);
bnet.CPD{node.VENTTUBE} = tabular_CPD(bnet, node.VENTTUBE, [0.97 0.97 0.97 0.97 0.97 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.97 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.97 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.97]);
bnet.CPD{node.VENTLUNG} = tabular_CPD(bnet, node.VENTLUNG, [0.97 0.95000005 0.4 0.3 0.97 0.97 0.97 0.97 0.97 0.95000005 0.5 0.3 0.97 0.01 0.01 0.01 0.97 0.97 0.97 0.97 0.97 0.01 0.01 0.01 0.01 0.030000001 0.58 0.68 0.01 0.01 0.01 0.01 0.01 0.030000001 0.48 0.68 0.01 0.97 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.97 0.01 0.01 0.01 0.010000001 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.010000001 0.01 0.01 0.01 0.01 0.97 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.97 0.01 0.01 0.010000001 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.010000001 0.01 0.01 0.01 0.01 0.01 0.97 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.97]);
bnet.CPD{node.VENTALV} = tabular_CPD(bnet, node.VENTALV, [0.97 0.01 0.01 0.01 0.97 0.01 0.01 0.01 0.97 0.030000001 0.01 0.01 0.01 0.97 0.01 0.01 0.01 0.97 0.01 0.01 0.01 0.95000005 0.94 0.88 0.01 0.01 0.97 0.01 0.01 0.01 0.97 0.01 0.01 0.010000001 0.04 0.1 0.01 0.01 0.01 0.97 0.01 0.01 0.01 0.97 0.01 0.010000001 0.01 0.01]);
bnet.CPD{node.ARTCO2} = tabular_CPD(bnet, node.ARTCO2, [0.01 0.01 0.04 0.9 0.01 0.01 0.92 0.09 0.98 0.98 0.04 0.01]);
bnet.CPD{node.INSUFFANESTH} = tabular_CPD(bnet, node.INSUFFANESTH, [0.1 0.9]);
bnet.CPD{node.FIO2} = tabular_CPD(bnet, node.FIO2, [0.05 0.95]);
bnet.CPD{node.PVSAT} = tabular_CPD(bnet, node.PVSAT, [1.0 1.0 0.99 0.95 0.95 0.01 0.95 0.01 0.0 0.0 0.01 0.04 0.04 0.95 0.04 0.01 0.0 0.0 0.0 0.01 0.01 0.04 0.01 0.98]);
bnet.CPD{node.PULMEMBOLUS} = tabular_CPD(bnet, node.PULMEMBOLUS, [0.01 0.99]);
bnet.CPD{node.SHUNT} = tabular_CPD(bnet, node.SHUNT, [0.1 0.1 0.01 0.95 0.95 0.05 0.9 0.9 0.99 0.05 0.05 0.95]);
bnet.CPD{node.SAO2} = tabular_CPD(bnet, node.SAO2, [0.98 0.01 0.01 0.98 0.98 0.69 0.01 0.98 0.01 0.01 0.01 0.3 0.01 0.01 0.98 0.01 0.01 0.01]);
bnet.CPD{node.ANAPHYLAXIS} = tabular_CPD(bnet, node.ANAPHYLAXIS, [0.01 0.99]);
bnet.CPD{node.TPR} = tabular_CPD(bnet, node.TPR, [0.98 0.3 0.01 0.4 0.01 0.3]);
bnet.CPD{node.CATECHOL} = tabular_CPD(bnet, node.CATECHOL, [0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.05 0.05 0.01 0.01 0.01 0.01 0.05 0.05 0.01 0.05 0.05 0.01 0.05 0.05 0.01 0.05 0.05 0.01 0.05 0.05 0.01 0.7 0.7 0.1 0.7 0.7 0.1 0.7 0.7 0.1 0.95 0.99 0.3 0.95 0.99 0.3 0.95 0.99 0.3 0.99 0.99 0.99 0.99 0.99 0.99 0.99 0.99 0.99 0.99 0.99 0.99 0.99 0.99 0.99 0.95 0.95 0.99 0.99 0.99 0.99 0.95 0.95 0.99 0.95 0.95 0.99 0.95 0.95 0.99 0.95 0.95 0.99 0.95 0.95 0.99 0.3 0.3 0.9 0.3 0.3 0.9 0.3 0.3 0.9 0.05 0.01 0.7 0.05 0.01 0.7 0.05 0.01 0.7]);
bnet.CPD{node.HR} = tabular_CPD(bnet, node.HR, [0.05 0.01 0.9 0.09 0.05 0.9]);
bnet.CPD{node.HRBP} = tabular_CPD(bnet, node.HRBP, [0.98 0.4 0.3 0.98 0.01 0.01 0.01 0.59 0.4 0.01 0.98 0.01 0.01 0.01 0.3 0.01 0.01 0.98]);
bnet.CPD{node.ERRCAUTER} = tabular_CPD(bnet, node.ERRCAUTER, [0.1 0.9]);
bnet.CPD{node.HREKG} = tabular_CPD(bnet, node.HREKG, [0.33333334 0.98 0.33333334 0.01 0.33333334 0.01 0.33333334 0.01 0.33333334 0.98 0.33333334 0.01 0.33333334 0.01 0.33333334 0.01 0.33333334 0.98]);
bnet.CPD{node.HRSAT} = tabular_CPD(bnet, node.HRSAT, [0.33333334 0.98 0.33333334 0.01 0.33333334 0.01 0.33333334 0.01 0.33333334 0.98 0.33333334 0.01 0.33333334 0.01 0.33333334 0.01 0.33333334 0.98]);
bnet.CPD{node.EXPCO2} = tabular_CPD(bnet, node.EXPCO2, [0.97 0.97 0.97 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.97 0.01 0.01 0.97 0.01 0.01 0.97 0.01 0.01 0.01 0.01 0.01 0.01 0.97 0.01 0.01 0.97 0.01 0.01 0.97 0.01 0.01 0.01 0.01 0.01 0.01 0.97 0.01 0.01 0.97 0.01 0.01 0.97]);
bnet.CPD{node.MINVOL} = tabular_CPD(bnet, node.MINVOL, [0.97 0.01 0.01 0.01 0.97 0.6 0.5 0.5 0.97 0.01 0.01 0.01 0.01 0.97 0.01 0.01 0.01 0.38 0.48 0.48 0.01 0.97 0.01 0.01 0.01 0.01 0.97 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.97 0.01 0.01 0.01 0.01 0.97 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.97]);
bnet.CPD{node.PAP} = tabular_CPD(bnet, node.PAP, [0.01 0.05 0.19 0.9 0.8 0.05]);
bnet.CPD{node.PRESS} = tabular_CPD(bnet, node.PRESS, [0.97 0.01 0.01 0.01 0.97 0.1 0.05 0.01 0.97 0.01 0.01 0.01 0.97 0.01 0.01 0.01 0.97 0.4 0.2 0.2 0.97 0.010000001 0.01 0.01 0.01 0.3 0.01 0.01 0.01 0.84 0.25 0.15 0.01 0.29 0.01 0.01 0.01 0.97 0.01 0.01 0.01 0.58 0.75 0.7 0.01 0.90000004 0.01 0.01 0.01 0.49 0.08 0.01 0.01 0.05 0.25 0.25 0.01 0.3 0.08 0.01 0.01 0.01 0.97 0.01 0.01 0.01 0.04 0.09 0.01 0.080000006 0.38 0.01 0.01 0.2 0.9 0.97 0.01 0.01 0.45 0.59 0.01 0.4 0.9 0.97 0.01 0.01 0.01 0.97 0.01 0.01 0.01 0.01 0.01 0.010000001 0.6 0.97]);
bnet.CPD{node.CO} = tabular_CPD(bnet, node.CO, [0.98 0.95 0.3 0.95 0.04 0.01 0.8 0.01 0.01 0.01 0.04 0.69 0.04 0.95 0.3 0.19 0.04 0.01 0.01 0.01 0.01 0.01 0.01 0.69 0.01 0.95 0.98]);
bnet.CPD{node.BP} = tabular_CPD(bnet, node.BP, [0.98 0.98 0.3 0.98 0.1 0.05 0.9 0.05 0.01 0.01 0.01 0.6 0.01 0.85 0.4 0.09 0.2 0.09 0.01 0.01 0.1 0.01 0.05 0.55 0.01 0.75 0.9]);



clear engine;

engine{1} = jtree_inf_engine(bnet);
engine{2} = var_elim_inf_engine(bnet);
engine{3} = belprop_inf_engine(bnet);

maxIter = 100;
numVars = 37;

evidence = cell(1,numVars);
E = length(engine);
ll = zeros(maxIter,3);
totalTimes = zeros(1,E);
times = zeros(maxIter,3);

marginalTimes = zeros(maxIter, 3);
totalMargTimes = zeros(1,E);

for i=1:maxIter
    for j=1:numVars
        evidence{j} = floor(rand*length(value{j})+1);
    end
    

    tic; [engine{1}, ll(i,1)] = enter_evidence(engine{1}, evidence); 
    times(i,1) = times(i,1) + toc; totalTimes(1) = totalTimes(1) + times(i,1);
    
    tic; [engine{2}, ll(i,2)] = enter_evidence(engine{2}, evidence); 
    times(i,2) = times(i,2) + toc; totalTimes(2) = totalTimes(2) + times(i,2);
    
    tic; [engine{3}, ll(i,3), niter(3)] = enter_evidence(engine{3}, evidence);
    times(i,3) = times(i,3) + toc; totalTimes(3) = totalTimes(3) + times(i,3);

    for j=1:numVars
        tic; marginal_nodes(engine{1}, j); 
        marginalTimes(i,1) = marginalTimes(i,1) + toc; totalMargTimes(1) = totalMargTimes(1) + marginalTimes(j,1);

        tic; marginal_nodes(engine{2}, j); 
        marginalTimes(i,2) = marginalTimes(i,2) + toc; totalMargTimes(2) = totalMargTimes(2) + marginalTimes(j,2);

        tic; marginal_nodes(engine{3}, j);
        marginalTimes(i,3) = marginalTimes(i,3) + toc; totalMargTimes(3) = totalMargTimes(3) + marginalTimes(j,3);
    end
    
end
