001 package aima.test.probabilitytest; 002 003 import junit.framework.TestCase; 004 import aima.probability.BayesNet; 005 import aima.probability.BayesNetNode; 006 import aima.probability.EnumerationAsk; 007 import aima.probability.Query; 008 009 /** 010 * @author Ravi Mohan 011 * 012 */ 013 014 public class EnumerationAskTest extends TestCase { 015 016 public void testEnumerationAskAimaExample() { 017 Query q = new Query("Burglary", 018 new String[] { "JohnCalls", "MaryCalls" }, new boolean[] { 019 true, true }); 020 double[] probs = EnumerationAsk.ask(q, createBurglaryNetwork()); 021 assertEquals(0.284, probs[0], 0.001); 022 assertEquals(0.716, probs[1], 0.001); 023 } 024 025 public void testEnumerationAllVariablesExcludingQueryKnown() { 026 Query q = new Query("Alarm", new String[] { "Burglary", "EarthQuake", 027 "JohnCalls", "MaryCalls" }, new boolean[] { false, false, true, 028 true }); 029 030 double[] probs = EnumerationAsk.ask(q, createBurglaryNetwork()); 031 assertEquals(0.557, probs[0], 0.001); 032 assertEquals(0.442, probs[1], 0.001); 033 } 034 035 private BayesNet createBurglaryNetwork() { 036 BayesNetNode burglary = new BayesNetNode("Burglary"); 037 BayesNetNode earthquake = new BayesNetNode("EarthQuake"); 038 BayesNetNode alarm = new BayesNetNode("Alarm"); 039 BayesNetNode johnCalls = new BayesNetNode("JohnCalls"); 040 BayesNetNode maryCalls = new BayesNetNode("MaryCalls"); 041 042 alarm.influencedBy(burglary, earthquake); 043 johnCalls.influencedBy(alarm); 044 maryCalls.influencedBy(alarm); 045 046 burglary.setProbability(true, 0.001);// TODO behaviour changes if 047 // root node 048 earthquake.setProbability(true, 0.002); 049 050 alarm.setProbability(true, true, 0.95); 051 alarm.setProbability(true, false, 0.94); 052 alarm.setProbability(false, true, 0.29); 053 alarm.setProbability(false, false, 0.001); 054 055 johnCalls.setProbability(true, 0.90); 056 johnCalls.setProbability(false, 0.05); 057 058 maryCalls.setProbability(true, 0.70); 059 maryCalls.setProbability(false, 0.01); 060 061 BayesNet net = new BayesNet(burglary, earthquake); 062 return net; 063 } 064 }