Search in sources :

Example 36 with GraphNode

use of org.drools.beliefs.graph.GraphNode in project drools by kiegroup.

the class JunctionTreeBuilderTest method testPriorityQueueWithMaximalCliqueWeight.

@Test
public void testPriorityQueueWithMaximalCliqueWeight() {
    Graph<BayesVariable> graph = new BayesNetwork();
    GraphNode x0 = addNode(graph);
    GraphNode x1 = addNode(graph);
    GraphNode x2 = addNode(graph);
    GraphNode x3 = addNode(graph);
    GraphNode x4 = addNode(graph);
    GraphNode x5 = addNode(graph);
    GraphNode x6 = addNode(graph);
    GraphNode x7 = addNode(graph);
    GraphNode x8 = addNode(graph);
    GraphNode x9 = addNode(graph);
    GraphNode x10 = addNode(graph);
    GraphNode x11 = addNode(graph);
    GraphNode x12 = addNode(graph);
    connectParentToChildren(x2, x1);
    connectParentToChildren(x3, x1);
    connectParentToChildren(x4, x1);
    x1.setContent(new BayesVariable<String>("x1", x0.getId(), new String[] { "a", "b" }, new double[][] { { 0.1, 0.1 } }));
    x2.setContent(new BayesVariable<String>("x2", x0.getId(), new String[] { "a", "b" }, new double[][] { { 0.1, 0.1 } }));
    x3.setContent(new BayesVariable<String>("x3", x0.getId(), new String[] { "a", "b" }, new double[][] { { 0.1, 0.1 } }));
    x4.setContent(new BayesVariable<String>("x4", x0.getId(), new String[] { "a", "b" }, new double[][] { { 0.1, 0.1 } }));
    connectParentToChildren(x6, x5);
    connectParentToChildren(x7, x5);
    connectParentToChildren(x8, x5);
    x5.setContent(new BayesVariable<String>("x5", x0.getId(), new String[] { "a", "b", "c" }, new double[][] { { 0.1, 0.1, 0.1 } }));
    x6.setContent(new BayesVariable<String>("x6", x0.getId(), new String[] { "a", "b", "c" }, new double[][] { { 0.1, 0.1, 0.1 } }));
    x7.setContent(new BayesVariable<String>("x7", x0.getId(), new String[] { "a", "b", "c" }, new double[][] { { 0.1, 0.1, 0.1 } }));
    x8.setContent(new BayesVariable<String>("x8", x0.getId(), new String[] { "a", "b", "c" }, new double[][] { { 0.1, 0.1, 0.1 } }));
    connectParentToChildren(x10, x9);
    connectParentToChildren(x11, x9);
    connectParentToChildren(x12, x9);
    x9.setContent(new BayesVariable<String>("x9", x0.getId(), new String[] { "a" }, new double[][] { { 0.1 } }));
    x10.setContent(new BayesVariable<String>("x10", x0.getId(), new String[] { "a" }, new double[][] { { 0.1 } }));
    x11.setContent(new BayesVariable<String>("x11", x0.getId(), new String[] { "a", "b" }, new double[][] { { 0.1, 0.1 } }));
    x12.setContent(new BayesVariable<String>("x12", x0.getId(), new String[] { "a", "b" }, new double[][] { { 0.1, 0.1 } }));
    JunctionTreeBuilder jtBuilder = new JunctionTreeBuilder(graph);
    // jtBuilder.moralize(); // don't moralize, as we want to force a simpler construction for required edges, for the purposes of testing
    PriorityQueue<EliminationCandidate> p = new PriorityQueue<EliminationCandidate>(graph.size());
    EliminationCandidate elmCandVert = new EliminationCandidate(graph, jtBuilder.getAdjacencyMatrix(), x1);
    p.add(elmCandVert);
    elmCandVert = new EliminationCandidate(graph, jtBuilder.getAdjacencyMatrix(), x5);
    p.add(elmCandVert);
    elmCandVert = new EliminationCandidate(graph, jtBuilder.getAdjacencyMatrix(), x9);
    p.add(elmCandVert);
    EliminationCandidate v = p.remove();
    int id = v.getV().getId();
    assertEquals(9, id);
    assertEquals(4, v.getWeightRequired());
    v = p.remove();
    id = v.getV().getId();
    assertEquals(1, id);
    assertEquals(16, v.getWeightRequired());
    v = p.remove();
    id = v.getV().getId();
    assertEquals(5, id);
    assertEquals(81, v.getWeightRequired());
    assertEquals(0, p.size());
}
Also used : GraphNode(org.drools.beliefs.graph.GraphNode) PriorityQueue(java.util.PriorityQueue) Test(org.junit.Test)

Example 37 with GraphNode

use of org.drools.beliefs.graph.GraphNode in project drools by kiegroup.

the class JunctionTreeBuilderTest method testFullExample1.

@Test
public void testFullExample1() {
    // from "Bayesian Belief Network Propagation Engine In Java"
    // the result here is slightly different, due to ordering, but it's still correct.
    // http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.135.7921&rep=rep1&type=pdf
    Graph<BayesVariable> graph = new BayesNetwork();
    GraphNode xa = addNode(graph);
    GraphNode xb = addNode(graph);
    GraphNode xc = addNode(graph);
    GraphNode xd = addNode(graph);
    GraphNode xe = addNode(graph);
    GraphNode xf = addNode(graph);
    GraphNode xg = addNode(graph);
    GraphNode xh = addNode(graph);
    connectParentToChildren(xa, xb, xc);
    connectParentToChildren(xb, xd);
    connectParentToChildren(xc, xe, xg);
    connectParentToChildren(xd, xf);
    connectParentToChildren(xe, xf, xh);
    connectParentToChildren(xg, xh);
    // d, e, f
    OpenBitSet clique1 = bitSet("00111000");
    // c, d, e
    OpenBitSet clique2 = bitSet("00011100");
    // c, e, g
    OpenBitSet clique3 = bitSet("01010100");
    // e, g, h
    OpenBitSet clique4 = bitSet("11010000");
    // b, c, d
    OpenBitSet clique5 = bitSet("00001110");
    // a, b, c
    OpenBitSet clique6 = bitSet("00000111");
    // d, e
    OpenBitSet clique1And2 = bitSet("00011000");
    // c, e
    OpenBitSet clique2And3 = bitSet("00010100");
    // c, d
    OpenBitSet clique2And5 = bitSet("00001100");
    // e, g
    OpenBitSet clique3And4 = bitSet("01010000");
    // b, c
    OpenBitSet clique5And6 = bitSet("00000110");
    // clique1
    JunctionTreeBuilder jtBuilder = new JunctionTreeBuilder(graph);
    JunctionTreeClique root = jtBuilder.build(false).getRoot();
    assertEquals(clique1, root.getBitSet());
    assertEquals(1, root.getChildren().size());
    // clique2
    JunctionTreeSeparator sep = root.getChildren().get(0);
    assertEquals(clique1And2, sep.getBitSet());
    JunctionTreeClique jtNode2 = sep.getChild();
    assertEquals(clique1, sep.getParent().getBitSet());
    assertEquals(clique2, jtNode2.getBitSet());
    assertEquals(2, jtNode2.getChildren().size());
    // clique3
    sep = jtNode2.getChildren().get(0);
    assertEquals(clique2And3, sep.getBitSet());
    JunctionTreeClique jtNode3 = sep.getChild();
    assertEquals(clique2, sep.getParent().getBitSet());
    assertEquals(clique3, jtNode3.getBitSet());
    assertEquals(1, jtNode3.getChildren().size());
    // clique4
    sep = jtNode3.getChildren().get(0);
    assertEquals(clique3And4, sep.getBitSet());
    JunctionTreeClique jtNode4 = sep.getChild();
    assertEquals(clique3, sep.getParent().getBitSet());
    assertEquals(clique4, jtNode4.getBitSet());
    assertEquals(0, jtNode4.getChildren().size());
    // clique5
    sep = jtNode2.getChildren().get(1);
    assertEquals(clique2And5, sep.getBitSet());
    JunctionTreeClique jtNode5 = sep.getChild();
    assertEquals(clique2, sep.getParent().getBitSet());
    assertEquals(clique5, jtNode5.getBitSet());
    assertEquals(1, jtNode5.getChildren().size());
    // clique 6
    sep = jtNode5.getChildren().get(0);
    assertEquals(clique5And6, sep.getBitSet());
    JunctionTreeClique jtNode6 = sep.getChild();
    assertEquals(clique5, sep.getParent().getBitSet());
    assertEquals(clique6, jtNode6.getBitSet());
    assertEquals(0, jtNode6.getChildren().size());
}
Also used : OpenBitSet(org.drools.core.util.bitmask.OpenBitSet) GraphNode(org.drools.beliefs.graph.GraphNode) Test(org.junit.Test)

Example 38 with GraphNode

use of org.drools.beliefs.graph.GraphNode in project drools by kiegroup.

the class MarginalizerTest method test1.

@Test
public void test1() {
    BayesVariable a = new BayesVariable<String>("A", 0, new String[] { "A1", "A2", "A3" }, null);
    BayesVariable b = new BayesVariable<String>("B", 1, new String[] { "B1", "B2", "B3" }, null);
    BayesVariable c = new BayesVariable<String>("C", 2, new String[] { "C1", "C2", "C3" }, null);
    Graph<BayesVariable> graph = new BayesNetwork();
    GraphNode x0 = addNode(graph);
    GraphNode x1 = addNode(graph);
    GraphNode x2 = addNode(graph);
    x0.setContent(a);
    x1.setContent(b);
    x2.setContent(c);
    BayesVariable[] srcVars = new BayesVariable[] { a, b, c };
    int varNumberOfStates = PotentialMultiplier.createNumberOfStates(srcVars);
    System.out.println(varNumberOfStates);
    double[] srcDistribution = new double[varNumberOfStates];
    double x = 0.05;
    for (int i = 0; i < varNumberOfStates; i++) {
        srcDistribution[i] = x;
        x = x + 0.05;
    }
    srcDistribution = scaleDouble(3, srcDistribution);
    for (int i = 0; i < srcDistribution.length; i++) {
        System.out.print(srcDistribution[i] + " ");
    }
    System.out.println("");
    // public void Marginalizer(BayesVariable[]  srcVars, double[] srcPotentials, BayesVariable var,
    // double[] varDistribution,  BayesVariable[]  trgVars) {
    double[] trgDistribution = new double[b.getOutcomes().length];
    Marginalizer marginalizer = new Marginalizer(srcVars, srcDistribution, b, trgDistribution);
    for (int i = 0; i < trgDistribution.length; i++) {
        System.out.print(trgDistribution[i] + " ");
    }
    System.out.println("");
}
Also used : GraphNode(org.drools.beliefs.graph.GraphNode) Test(org.junit.Test)

Example 39 with GraphNode

use of org.drools.beliefs.graph.GraphNode 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()));
}
Also used : BayesVariable(org.drools.beliefs.bayes.BayesVariable) GraphNode(org.drools.beliefs.graph.GraphNode) BayesNetwork(org.drools.beliefs.bayes.BayesNetwork) Bif(org.drools.beliefs.bayes.model.Bif) Test(org.junit.Test)

Aggregations

GraphNode (org.drools.beliefs.graph.GraphNode)39 Test (org.junit.Test)32 OpenBitSet (org.drools.core.util.bitmask.OpenBitSet)13 ArrayList (java.util.ArrayList)8 EdgeImpl (org.drools.beliefs.graph.impl.EdgeImpl)4 PriorityQueue (java.util.PriorityQueue)3 HashMap (java.util.HashMap)2 HashSet (java.util.HashSet)2 BayesNetwork (org.drools.beliefs.bayes.BayesNetwork)2 BayesVariable (org.drools.beliefs.bayes.BayesVariable)2 Edge (org.drools.beliefs.graph.Edge)2 Bif (org.drools.beliefs.bayes.model.Bif)1 Graph (org.drools.beliefs.graph.Graph)1