Search in sources :

Example 6 with BayesInstance

use of org.drools.beliefs.bayes.BayesInstance in project drools by kiegroup.

the class SprinkerTest method testNoEvidence.

@Test
public void testNoEvidence() {
    JunctionTreeBuilder jtBuilder = new JunctionTreeBuilder(graph);
    JunctionTree jTree = jtBuilder.build();
    JunctionTreeClique jtNode = jTree.getRoot();
    BayesInstance bayesInstance = new BayesInstance(jTree);
    bayesInstance.globalUpdate();
    assertArray(new double[] { 0.5, 0.5 }, scaleDouble(3, bayesInstance.marginalize("Cloudy").getDistribution()));
    assertArray(new double[] { 0.5, 0.5 }, scaleDouble(3, bayesInstance.marginalize("Rain").getDistribution()));
    assertArray(new double[] { 0.7, 0.3 }, scaleDouble(3, bayesInstance.marginalize("Sprinkler").getDistribution()));
    assertArray(new double[] { 0.353, 0.647 }, scaleDouble(3, bayesInstance.marginalize("WetGrass").getDistribution()));
}
Also used : JunctionTreeBuilder(org.drools.beliefs.bayes.JunctionTreeBuilder) BayesInstance(org.drools.beliefs.bayes.BayesInstance) JunctionTreeClique(org.drools.beliefs.bayes.JunctionTreeClique) JunctionTree(org.drools.beliefs.bayes.JunctionTree) Test(org.junit.Test)

Example 7 with BayesInstance

use of org.drools.beliefs.bayes.BayesInstance in project drools by kiegroup.

the class SprinkerTest method testGrassWetEvidence.

@Test
public void testGrassWetEvidence() {
    JunctionTreeBuilder jtBuilder = new JunctionTreeBuilder(graph);
    JunctionTree jTree = jtBuilder.build();
    JunctionTreeClique jtNode = jTree.getRoot();
    BayesInstance bayesInstance = new BayesInstance(jTree);
    bayesInstance.setLikelyhood("WetGrass", new double[] { 1.0, 0.0 });
    bayesInstance.globalUpdate();
    assertArray(new double[] { 0.639, 0.361 }, scaleDouble(3, bayesInstance.marginalize("Cloudy").getDistribution()));
    assertArray(new double[] { 0.881, 0.119 }, scaleDouble(3, bayesInstance.marginalize("Rain").getDistribution()));
    assertArray(new double[] { 0.938, 0.062 }, scaleDouble(3, bayesInstance.marginalize("Sprinkler").getDistribution()));
    assertArray(new double[] { 1.0, 0.0 }, scaleDouble(3, bayesInstance.marginalize("WetGrass").getDistribution()));
}
Also used : JunctionTreeBuilder(org.drools.beliefs.bayes.JunctionTreeBuilder) BayesInstance(org.drools.beliefs.bayes.BayesInstance) JunctionTreeClique(org.drools.beliefs.bayes.JunctionTreeClique) JunctionTree(org.drools.beliefs.bayes.JunctionTree) Test(org.junit.Test)

Example 8 with BayesInstance

use of org.drools.beliefs.bayes.BayesInstance in project drools by kiegroup.

the class SprinkerTest method testSprinklerEvidence.

@Test
public void testSprinklerEvidence() {
    JunctionTreeBuilder jtBuilder = new JunctionTreeBuilder(graph);
    JunctionTree jTree = jtBuilder.build();
    JunctionTreeClique jtNode = jTree.getRoot();
    BayesInstance bayesInstance = new BayesInstance(jTree);
    bayesInstance.setLikelyhood("Sprinkler", new double[] { 1.0, 0.0 });
    bayesInstance.setLikelyhood("Cloudy", new double[] { 1.0, 0.0 });
    bayesInstance.globalUpdate();
    assertArray(new double[] { 1.0, 0.0 }, scaleDouble(3, bayesInstance.marginalize("Cloudy").getDistribution()));
    assertArray(new double[] { 0.8, 0.2 }, scaleDouble(3, bayesInstance.marginalize("Rain").getDistribution()));
    assertArray(new double[] { 1.0, 0.0 }, scaleDouble(3, bayesInstance.marginalize("Sprinkler").getDistribution()));
    assertArray(new double[] { 0.82, 0.18 }, scaleDouble(3, bayesInstance.marginalize("WetGrass").getDistribution()));
}
Also used : JunctionTreeBuilder(org.drools.beliefs.bayes.JunctionTreeBuilder) BayesInstance(org.drools.beliefs.bayes.BayesInstance) JunctionTreeClique(org.drools.beliefs.bayes.JunctionTreeClique) JunctionTree(org.drools.beliefs.bayes.JunctionTree) Test(org.junit.Test)

Example 9 with BayesInstance

use of org.drools.beliefs.bayes.BayesInstance in project drools by kiegroup.

the class EarthQuakeTest method setUp.

@Before
public void setUp() {
    connectParentToChildren(burglaryNode, alarmNode);
    connectParentToChildren(earthquakeNode, alarmNode);
    connectParentToChildren(alarmNode, johnCallsNode, maryCallsNode);
    burglaryNode.setContent(burglary);
    earthquakeNode.setContent(earthquake);
    alarmNode.setContent(alarm);
    johnCallsNode.setContent(johnCalls);
    maryCallsNode.setContent(maryCalls);
    JunctionTreeBuilder jtBuilder = new JunctionTreeBuilder(graph);
    jTree = jtBuilder.build();
    // jTree.initialize();
    jtNode1 = jTree.getRoot();
    jtNode2 = jtNode1.getChildren().get(0).getChild();
    jtNode3 = jtNode1.getChildren().get(1).getChild();
    bayesInstance = new BayesInstance(jTree);
    burglaryState = bayesInstance.getVarStates()[burglary.getId()];
    earthquakeState = bayesInstance.getVarStates()[earthquake.getId()];
    alarmState = bayesInstance.getVarStates()[alarm.getId()];
    johnCallsState = bayesInstance.getVarStates()[johnCalls.getId()];
    maryCallsState = bayesInstance.getVarStates()[maryCalls.getId()];
}
Also used : JunctionTreeBuilder(org.drools.beliefs.bayes.JunctionTreeBuilder) BayesInstance(org.drools.beliefs.bayes.BayesInstance) Before(org.junit.Before)

Aggregations

BayesInstance (org.drools.beliefs.bayes.BayesInstance)9 Test (org.junit.Test)7 JunctionTree (org.drools.beliefs.bayes.JunctionTree)4 JunctionTreeBuilder (org.drools.beliefs.bayes.JunctionTreeBuilder)4 JunctionTreeClique (org.drools.beliefs.bayes.JunctionTreeClique)3 BayesPackage (org.drools.beliefs.bayes.assembler.BayesPackage)1 InternalKnowledgePackage (org.drools.core.definitions.InternalKnowledgePackage)1 Before (org.junit.Before)1 ResourceTypePackage (org.kie.api.internal.io.ResourceTypePackage)1 ResourceType (org.kie.api.io.ResourceType)1