00001 /* 00002 * Created on Nov 2, 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 java.util.Vector; 00010 00011 import edu.tum.cs.inference.BasicSampledDistribution; 00012 import edu.tum.cs.inference.IParameterHandler; 00013 import edu.tum.cs.inference.ParameterHandler; 00014 import edu.tum.cs.inference.BasicSampledDistribution.DistributionComparison; 00015 import edu.tum.cs.inference.BasicSampledDistribution.DistributionEntryComparison; 00016 import edu.tum.cs.inference.BasicSampledDistribution.MeanSquaredError; 00017 import edu.tum.cs.util.Stopwatch; 00018 00019 public class TimeLimitedInference implements IParameterHandler { 00020 00021 protected ITimeLimitedInference inference; 00022 protected double time, interval; 00023 protected InferenceThread thread; 00024 protected BasicSampledDistribution referenceDistribution = null; 00028 protected Vector<Double> MSEs = null; 00029 protected Vector<Class<? extends DistributionEntryComparison>> comparisonClasses; 00030 protected ParameterHandler paramHandler; 00031 protected boolean verbose = true; 00032 00033 public TimeLimitedInference(ITimeLimitedInference inference, double time, double interval) throws Exception { 00034 this.inference = inference; 00035 this.time = time; 00036 this.interval = interval; 00037 comparisonClasses = new Vector<Class<? extends DistributionEntryComparison>>(); 00038 paramHandler = new ParameterHandler(this); 00039 paramHandler.add("verbose", "setVerbose"); 00040 } 00041 00042 public void setVerbose(boolean verbose) { 00043 this.verbose = verbose; 00044 } 00045 00046 public void setReferenceDistribution(BasicSampledDistribution dist) { 00047 referenceDistribution = dist; 00048 comparisonClasses.add(BasicSampledDistribution.MeanSquaredError.class); 00049 comparisonClasses.add(BasicSampledDistribution.HellingerDistance.class); 00050 MSEs = new Vector<Double>(); 00051 } 00052 00053 public SampledDistribution run() throws Exception { 00054 // start the inference thread 00055 thread = new InferenceThread(); 00056 thread.start(); 00057 // wait, repeatedly polling intermediate results 00058 Stopwatch sw = new Stopwatch(); 00059 sw.start(); 00060 boolean useIntervals = true; 00061 if(!useIntervals) 00062 Thread.sleep((int)(1000*time)); 00063 else { 00064 while(sw.getElapsedTimeSecs() < time && thread.isAlive()) { 00065 Thread.sleep((int)(1000*interval)); 00066 if(verbose) System.out.println("polling results after " + sw.getElapsedTimeSecs() + "s..."); 00067 SampledDistribution dist = pollResults(true); 00068 if(verbose && dist != null) System.out.printf("%d samples taken\n", dist.steps); 00069 if(referenceDistribution != null) { 00070 double mse; 00071 if(dist == null) 00072 mse = Double.POSITIVE_INFINITY; 00073 else { 00074 DistributionComparison dc = doComparison(dist); 00075 mse = dc.getResult(MeanSquaredError.class); 00076 } 00077 MSEs.add(mse); 00078 } 00079 } 00080 } 00081 // get final results, terminating the inference thread if it is still running 00082 SampledDistribution results = pollResults(false); 00083 if(thread.isAlive()) 00084 thread.stop(); 00085 return results; 00086 } 00087 00088 protected DistributionComparison doComparison(BasicSampledDistribution dist) throws Exception { 00089 DistributionComparison dc = new DistributionComparison(this.referenceDistribution, dist); 00090 for(Class<? extends DistributionEntryComparison> c : comparisonClasses) 00091 dc.addEntryComparison(c); 00092 dc.compare(); 00093 dc.printResults(); 00094 return dc; 00095 } 00096 00097 public SampledDistribution pollResults(boolean allowPrint) throws Exception { 00098 SampledDistribution dist = thread.pollResults(); 00099 if(allowPrint && verbose && dist != null) 00100 printResults(dist); 00101 return dist; 00102 } 00103 00104 protected void printResults(SampledDistribution dist) { 00105 // TODO 00106 } 00107 00112 public Vector<Double> getMSEs() { 00113 return MSEs; 00114 } 00115 00116 protected class InferenceThread extends Thread { 00117 00118 public void run() { 00119 try { 00120 inference.infer(); 00121 } 00122 catch(Exception e) { 00123 throw new RuntimeException(e); 00124 } 00125 } 00126 00127 public SampledDistribution pollResults() throws Exception { 00128 return inference.pollResults(); 00129 } 00130 } 00131 00132 @Override 00133 public ParameterHandler getParameterHandler() { 00134 return paramHandler; 00135 } 00136 }