00001 /* 00002 * Created on Oct 15, 2009 00003 * 00004 * TODO To change the template for this generated file go to 00005 * Window - Preferences - Java - Code Style - Code Templates 00006 */ 00007 package edu.tum.cs.bayesnets.inference; 00008 00009 import edu.ksu.cis.bnj.ver3.core.CPF; 00010 import edu.ksu.cis.bnj.ver3.core.DiscreteEvidence; 00011 import edu.tum.cs.bayesnets.core.BeliefNetworkEx; 00012 00017 public class BNJInference extends Sampler { 00018 00019 Class<? extends edu.ksu.cis.bnj.ver3.inference.Inference> algorithmClass; 00020 00021 public BNJInference(BeliefNetworkEx bn, Class<? extends edu.ksu.cis.bnj.ver3.inference.Inference> algoClass) throws Exception { 00022 super(bn); 00023 this.algorithmClass = algoClass; 00024 } 00025 00026 @Override 00027 public SampledDistribution _infer() 00028 throws Exception { 00029 // set evidence 00030 for(int i = 0; i < evidenceDomainIndices.length; i++) 00031 if(evidenceDomainIndices[i] != -1) 00032 nodes[i].setEvidence(new DiscreteEvidence(evidenceDomainIndices[i])); 00033 00034 // run inference 00035 edu.ksu.cis.bnj.ver3.inference.Inference algo = algorithmClass.newInstance(); 00036 algo.run(bn.bn); 00037 00038 // retrieve results 00039 createDistribution(); 00040 for(int i = 0; i < nodes.length; i++) { 00041 CPF cpf = algo.queryMarginal(nodes[i]); 00042 for(int j = 0; j < dist.values[i].length; j++) 00043 dist.values[i][j] = cpf.getDouble(j); 00044 } 00045 dist.Z = 1.0; 00046 dist.steps = 1; 00047 dist.trials = 1; 00048 00049 // remove evidence 00050 bn.removeAllEvidences(); 00051 00052 return dist; 00053 } 00054 00055 }