00001 package edu.tum.cs.bayesnets.inference; 00002 00003 import java.io.FileNotFoundException; 00004 00005 import edu.ksu.cis.bnj.ver3.plugin.IOPlugInLoader; 00006 import edu.tum.cs.bayesnets.core.BeliefNetworkEx; 00007 00013 public class SmileInference extends Sampler { 00014 smile.Network net; 00015 int algorithmID; 00016 00017 public SmileInference(BeliefNetworkEx bn, int algorithmID) throws Exception { 00018 super(bn); 00019 this.algorithmID = algorithmID; 00020 00021 // obtain SMILE Network 00022 System.out.println("converting network..."); 00023 bn.save("temp.net", IOPlugInLoader.getInstance().GetExportersByExt(".net")); 00024 net = new smile.Network(); 00025 net.readFile("temp.net"); 00026 // new java.io.File("temp.net").delete(); 00027 } 00028 00029 public SampledDistribution _infer() throws Exception { 00030 net.setBayesianAlgorithm(algorithmID); 00031 net.setSampleCount(numSamples); 00032 00033 // set evidence 00034 System.out.println("setting evidence..."); 00035 for(int i = 0; i < evidenceDomainIndices.length; i++) { 00036 if(evidenceDomainIndices[i] != -1) 00037 net.setEvidence("N" + i, evidenceDomainIndices[i]); 00038 } 00039 00040 // inference 00041 System.out.println("sampling..."); 00042 net.updateBeliefs(); 00043 00044 // store results in distribution 00045 System.out.println("reading results..."); 00046 this.createDistribution(); 00047 for(int i = 0; i < nodes.length; i++) { 00048 double[] values = net.getNodeValue("N" + i); 00049 dist.values[i] = values; 00050 } 00051 dist.Z = 1.0; 00052 00053 return dist; 00054 } 00055 }