001 package aima.test.probabilitytest; 002 003 import java.util.List; 004 005 import junit.framework.TestCase; 006 import aima.probability.BayesNet; 007 import aima.probability.BayesNetNode; 008 009 /** 010 * @author Ravi Mohan 011 * 012 */ 013 014 public class BayesNetTest extends TestCase { 015 BayesNet net; 016 017 @Override 018 public void setUp() { 019 net = createBurglaryNetwork(); 020 } 021 022 public void testVariablesAreCorrectlyObtainedFromBayesNetwork() { 023 List variables = net.getVariables(); 024 assertEquals(5, variables.size()); 025 assertEquals("Burglary", (String) variables.get(0)); 026 assertEquals("EarthQuake", (String) variables.get(1)); 027 assertEquals("Alarm", (String) variables.get(2)); 028 assertEquals("JohnCalls", (String) variables.get(3)); 029 assertEquals("MaryCalls", (String) variables.get(4)); 030 } 031 032 private BayesNet createBurglaryNetwork() { 033 BayesNetNode burglary = new BayesNetNode("Burglary"); 034 BayesNetNode earthquake = new BayesNetNode("EarthQuake"); 035 BayesNetNode alarm = new BayesNetNode("Alarm"); 036 BayesNetNode johnCalls = new BayesNetNode("JohnCalls"); 037 BayesNetNode maryCalls = new BayesNetNode("MaryCalls"); 038 039 alarm.influencedBy(burglary, earthquake); 040 johnCalls.influencedBy(alarm); 041 maryCalls.influencedBy(alarm); 042 043 burglary.setProbability(true, 0.001); 044 earthquake.setProbability(true, 0.002); 045 046 alarm.setProbability(true, true, 0.95); 047 alarm.setProbability(true, false, 0.94); 048 alarm.setProbability(false, true, 0.29); 049 alarm.setProbability(false, false, 0.001); 050 051 johnCalls.setProbability(true, 0.90); 052 johnCalls.setProbability(false, 0.05); 053 054 maryCalls.setProbability(true, 0.70); 055 maryCalls.setProbability(false, 0.01); 056 057 BayesNet net = new BayesNet(burglary, earthquake); 058 return net; 059 } 060 }