Search in sources :

Example 1 with JunctionTreeBuilder

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

the class SprinkerTest method setUp.

@Before
public void setUp() {
    connectParentToChildren(cloudyNode, sprinklerNode, rainNode);
    connectParentToChildren(sprinklerNode, wetGrassNode);
    connectParentToChildren(rainNode, wetGrassNode);
    cloudyNode.setContent(cloudy);
    sprinklerNode.setContent(sprinkler);
    rainNode.setContent(rain);
    wetGrassNode.setContent(wetGrass);
    JunctionTreeBuilder jtBuilder = new JunctionTreeBuilder(graph);
    jTree = jtBuilder.build();
}
Also used : JunctionTreeBuilder(org.drools.beliefs.bayes.JunctionTreeBuilder) Before(org.junit.Before)

Example 2 with JunctionTreeBuilder

use of org.drools.beliefs.bayes.JunctionTreeBuilder 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 3 with JunctionTreeBuilder

use of org.drools.beliefs.bayes.JunctionTreeBuilder 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 4 with JunctionTreeBuilder

use of org.drools.beliefs.bayes.JunctionTreeBuilder 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 5 with JunctionTreeBuilder

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

the class BayesAssemblerService method addResource.

@Override
public void addResource(Object kbuilder, Resource resource, ResourceType type, ResourceConfiguration configuration) throws Exception {
    BayesNetwork network;
    JunctionTreeBuilder builder;
    KnowledgeBuilder kb = (KnowledgeBuilder) kbuilder;
    Bif bif = XmlBifParser.loadBif(resource, kb.getErrors());
    if (bif == null) {
        return;
    }
    try {
        network = XmlBifParser.buildBayesNetwork(bif);
    } catch (Exception e) {
        kb.getErrors().add(new BayesNetworkAssemblerError(resource, "Unable to parse opening Stream:\n" + e.toString()));
        return;
    }
    try {
        builder = new JunctionTreeBuilder(network);
    } catch (Exception e) {
        kb.getErrors().add(new BayesNetworkAssemblerError(resource, "Unable to build Junction Tree:\n" + e.toString()));
        return;
    }
    KnowledgeBuilderImpl kbuilderImpl = (KnowledgeBuilderImpl) kbuilder;
    PackageRegistry pkgReg = kbuilderImpl.getOrCreatePackageRegistry(new PackageDescr(network.getPackageName()));
    InternalKnowledgePackage kpkgs = pkgReg.getPackage();
    Map<ResourceType, ResourceTypePackage> rpkg = kpkgs.getResourceTypePackages();
    BayesPackage bpkg = (BayesPackage) rpkg.get(ResourceType.BAYES);
    if (bpkg == null) {
        bpkg = new BayesPackage();
        rpkg.put(ResourceType.BAYES, bpkg);
    }
    bpkg.addJunctionTree(network.getName(), builder.build());
}
Also used : JunctionTreeBuilder(org.drools.beliefs.bayes.JunctionTreeBuilder) ResourceType(org.kie.api.io.ResourceType) BayesNetwork(org.drools.beliefs.bayes.BayesNetwork) KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) PackageRegistry(org.drools.compiler.compiler.PackageRegistry) KnowledgeBuilderImpl(org.drools.compiler.builder.impl.KnowledgeBuilderImpl) PackageDescr(org.drools.compiler.lang.descr.PackageDescr) ResourceTypePackage(org.kie.api.internal.io.ResourceTypePackage) Bif(org.drools.beliefs.bayes.model.Bif) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage)

Aggregations

JunctionTreeBuilder (org.drools.beliefs.bayes.JunctionTreeBuilder)6 BayesInstance (org.drools.beliefs.bayes.BayesInstance)4 JunctionTree (org.drools.beliefs.bayes.JunctionTree)3 JunctionTreeClique (org.drools.beliefs.bayes.JunctionTreeClique)3 Test (org.junit.Test)3 Before (org.junit.Before)2 BayesNetwork (org.drools.beliefs.bayes.BayesNetwork)1 Bif (org.drools.beliefs.bayes.model.Bif)1 KnowledgeBuilderImpl (org.drools.compiler.builder.impl.KnowledgeBuilderImpl)1 PackageRegistry (org.drools.compiler.compiler.PackageRegistry)1 PackageDescr (org.drools.compiler.lang.descr.PackageDescr)1 InternalKnowledgePackage (org.drools.core.definitions.InternalKnowledgePackage)1 ResourceTypePackage (org.kie.api.internal.io.ResourceTypePackage)1 ResourceType (org.kie.api.io.ResourceType)1 KnowledgeBuilder (org.kie.internal.builder.KnowledgeBuilder)1