use of org.drools.beliefs.bayes.BayesVariable in project drools by kiegroup.
the class SprinkerTest method marginalize.
public static void marginalize(BayesVariableState varState, CliqueState cliqueState) {
JunctionTreeClique jtNode = cliqueState.getJunctionTreeClique();
new Marginalizer(jtNode.getValues().toArray(new BayesVariable[jtNode.getValues().size()]), cliqueState.getPotentials(), varState.getVariable(), varState.getDistribution());
System.out.print(varState.getVariable().getName() + " ");
for (double d : varState.getDistribution()) {
System.out.print(d);
System.out.print(" ");
}
System.out.println(" ");
}
use of org.drools.beliefs.bayes.BayesVariable in project drools by kiegroup.
the class XmlBifParser method buildBayesNetwork.
public static BayesNetwork buildBayesNetwork(Bif bif) {
String name = bif.getNetwork().getName();
String packageName = "default";
List<String> props = bif.getNetwork().getProperties();
if (props != null) {
for (String prop : props) {
prop = prop.trim();
if (prop.startsWith("package")) {
packageName = prop.substring(prop.indexOf('=') + 1).trim();
}
}
}
BayesNetwork graph = new BayesNetwork(name, packageName);
Map<String, GraphNode<BayesVariable>> map = new HashMap<String, GraphNode<BayesVariable>>();
for (Definition def : bif.getNetwork().getDefinitions()) {
GraphNode<BayesVariable> node = graph.addNode();
BayesVariable var = buildVariable(def, bif.getNetwork(), node.getId());
node.setContent(var);
map.put(var.getName(), node);
}
for (Entry<String, GraphNode<BayesVariable>> entry : map.entrySet()) {
GraphNode<BayesVariable> node = entry.getValue();
BayesVariable var = node.getContent();
if (var.getGiven() != null && var.getGiven().length > 0) {
for (String given : var.getGiven()) {
GraphNode<BayesVariable> givenNode = map.get(given);
EdgeImpl e = new EdgeImpl();
e.setOutGraphNode(givenNode);
e.setInGraphNode(node);
}
}
}
return graph;
}
use of org.drools.beliefs.bayes.BayesVariable in project drools by kiegroup.
the class ParserTest method testSprinklerBuildBayesNework.
@Test
public void testSprinklerBuildBayesNework() {
Bif bif = (Bif) XmlBifParser.loadBif(ParserTest.class.getResource("Garden.xmlbif"));
BayesNetwork network = XmlBifParser.buildBayesNetwork(bif);
Map<String, GraphNode<BayesVariable>> map = nodeToMap(network);
GraphNode<BayesVariable> node = map.get("WetGrass");
BayesVariable wetGrass = node.getContent();
assertEquals(Arrays.asList(new String[] { "false", "true" }), Arrays.asList(wetGrass.getOutcomes()));
assertEquals(2, wetGrass.getGiven().length);
assertEquals(Arrays.asList(wetGrass.getGiven()), Arrays.asList(new String[] { "Sprinkler", "Rain" }));
assertTrue(Arrays.deepEquals(new double[][] { { 1.0, 0.0 }, { 0.1, 0.9 }, { 0.1, 0.9 }, { 0.01, 0.99 } }, wetGrass.getProbabilityTable()));
node = map.get("Sprinkler");
BayesVariable sprinkler = node.getContent();
assertEquals(Arrays.asList(new String[] { "false", "true" }), Arrays.asList(sprinkler.getOutcomes()));
assertEquals(1, sprinkler.getGiven().length);
assertEquals("Cloudy", sprinkler.getGiven()[0]);
assertTrue(Arrays.deepEquals(new double[][] { { 0.5, 0.5 }, { 0.9, 0.1 } }, sprinkler.getProbabilityTable()));
node = map.get("Cloudy");
BayesVariable cloudy = node.getContent();
assertEquals(Arrays.asList(new String[] { "false", "true" }), Arrays.asList(cloudy.getOutcomes()));
assertEquals(0, cloudy.getGiven().length);
assertTrue(Arrays.deepEquals(new double[][] { { 0.5, 0.5 } }, cloudy.getProbabilityTable()));
node = map.get("Rain");
BayesVariable rain = node.getContent();
assertEquals(Arrays.asList(new String[] { "false", "true" }), Arrays.asList(rain.getOutcomes()));
assertEquals(0, rain.getGiven().length);
assertTrue(Arrays.deepEquals(new double[][] { { 0.5, 0.5 } }, rain.getProbabilityTable()));
}
Aggregations