public void sprinklerNetwork() {
BeliefNetwork bn = new BeliefNetwork();
String[] boolDomain = { "true", "false" };
BNNode bnodeCloudy = new BNNode("Cloudy", boolDomain);
BNNode bnodeRain = new BNNode("Rain", boolDomain);
BNNode bnodeSprinkler = new BNNode("Sprinkler", boolDomain);
BNNode bnodeWetGrass = new BNNode("WetGrass", boolDomain);
bn.addBeliefNode(bnodeRain);
bn.addBeliefNode(bnodeCloudy);
bn.addBeliefNode(bnodeWetGrass);
bn.addBeliefNode(bnodeSprinkler);
// note the order of execution of connect().
// domains are added in sequence as parent nodes are connected
bn.addEdge(bnodeCloudy, bnodeRain);
bn.addEdge(bnodeCloudy, bnodeSprinkler);
bn.addEdge(bnodeSprinkler, bnodeWetGrass);
bn.addEdge(bnodeRain, bnodeWetGrass);
// this is simple
TabularCPD table = (TabularCPD) bnodeCloudy.getFunction();
table.setValue(0, 0.5);
table.setValue(1, 0.5);
// see image sprinkler.jpg to make sense out of the order
// plus mind the rder of execution of connect() above
table = (TabularCPD) bnodeSprinkler.getFunction();
table.setValue(0, 0.1);
table.setValue(1, 0.5);
table.setValue(2, 0.9);
table.setValue(3, 0.5);
// see image sprinkler.jpg to make sense out of the order
table = (TabularCPD) bnodeRain.getFunction();
table.setValue(0, 0.8);
table.setValue(1, 0.2);
table.setValue(2, 0.2);
table.setValue(3, 0.8);
// see image sprinkler.jpg to make sense out of the order
table = (TabularCPD) bnodeWetGrass.getFunction();
table.setValue(0, 0.99);
table.setValue(1, 0.9);
table.setValue(2, 0.9);
table.setValue(3, 0.0);
table.setValue(4, .01);
table.setValue(5, 0.1);
table.setValue(6, 0.1);
table.setValue(7, 1.0);
// Rain.setEvidence(new DiscreteEvidence((Discrete)Rain.getDomain(),
// "true"));;
// WetGrass.setEvidence(new
// DiscreteEvidence((Discrete)Sprinkler.getDomain(), "true"));;
// Rain.setEvidence(new PolyEvidence());
// // WetGrass.setEvidence(new PolyEvidence());
// System.out.println("Here are the CPFS");
// System.out.println("r:" + bnodeCloudy.getName() + ":\n"
// + bnodeCloudy.getCPF().toString() );
// System.out.println("r:" + bnodeRain.getName() + ":\n"
// + bnodeRain.getCPF().toString() );
// System.out.println("r:" + bnodeSprinkler.getName() + ":\n"
// + bnodeSprinkler.getCPF().toString() );
// System.out.println("r:" + bnodeWetGrass.getName() + ":\n"
// + bnodeWetGrass.getCPF().toString() );
JTInference inference = new JTInference();
try {
inference.initialize(bn, new JoinTreeCompiler());
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
inference.run();
// System.out.println("Here are the marginal after inference with Belief
// Elimination");
// System.out.println("r:"+ bnodeCloudy.getName() + ":\n" +
// cpf2string(VE.queryMarginal(bnodeCloudy)));
// System.out.println("r:"+ bnodeSprinkler.getName() + ":\n" +
// cpf2string(VE.queryMarginal(bnodeSprinkler)));
// System.out.println("r:"+ bnodeRain.getName() + ":\n" +
// cpf2string(VE.queryMarginal(bnodeRain)));
// System.out.println("r:"+ bnodeWetGrass.getName() + ":\n" +
// cpf2string(VE.queryMarginal(bnodeWetGrass)));
Set<BNNode> nodes1 = bn.vertexSet();
for (BNNode node : nodes1) {
System.out.println(
node.getName() + ":\n" + node.getMarginal().toString()
+ "\n");
}
//
System.out.println(
"Here are the marginal after inference with Belief Elimination");
System.out.println(
"r:" + bnodeCloudy.getName() + ":\n"
+ inference.getMarginal(bnodeCloudy));
System.out.println(
"r:" + bnodeSprinkler.getName() + ":\n"
+ inference.getMarginal(bnodeSprinkler));
System.out.println(
"r:" + bnodeRain.getName() + ":\n"
+ inference.getMarginal(bnodeRain));
System.out.println(
"r:" + bnodeWetGrass.getName() + ":\n"
+ inference.getMarginal(bnodeWetGrass));
}