Search in sources :

Example 16 with OpenBitSet

use of org.drools.core.util.bitmask.OpenBitSet in project drools by kiegroup.

the class JunctionTreeBuilderTest method testFullExample2.

@Test
public void testFullExample2() {
    // Bayesian Networks -  A Self-contained introduction with implementation remarks
    // http://www.mathcs.emory.edu/~whalen/Papers/BNs/Intros/BayesianNetworksTutorial.pdf
    Graph<BayesVariable> graph = new BayesNetwork();
    // 0
    GraphNode xElectricity = addNode(graph);
    // 1
    GraphNode xTelecom = addNode(graph);
    // 2
    GraphNode xRail = addNode(graph);
    // 3
    GraphNode xAirTravel = addNode(graph);
    // 4
    GraphNode xTransportation = addNode(graph);
    // 5
    GraphNode xUtilities = addNode(graph);
    // 6
    GraphNode xUSBanks = addNode(graph);
    // 7
    GraphNode xUSStocks = addNode(graph);
    connectParentToChildren(xElectricity, xRail, xAirTravel, xUtilities, xTelecom);
    connectParentToChildren(xTelecom, xUtilities, xUSBanks);
    connectParentToChildren(xRail, xTransportation);
    connectParentToChildren(xAirTravel, xTransportation);
    connectParentToChildren(xUtilities, xUSStocks);
    connectParentToChildren(xUSBanks, xUSStocks);
    connectParentToChildren(xTransportation, xUSStocks);
    // Utilities, Transportation, USBanks, UStocks
    OpenBitSet clique1 = bitSet("11110000");
    // Electricity, Transportation, Utilities, USBanks
    OpenBitSet clique2 = bitSet("01110001");
    // Electricity, Telecom, Utilities, USBanks
    OpenBitSet clique3 = bitSet("01100011");
    // Electricity, Rail, AirTravel, Transportation
    OpenBitSet clique4 = bitSet("00011101");
    // Utilities, Transportation, USBanks
    OpenBitSet clique1And2 = bitSet("01110000");
    // Electricity, Utilities, USBanks
    OpenBitSet clique2And3 = bitSet("01100001");
    // Electricity, Transportation
    OpenBitSet clique2And4 = bitSet("00010001");
    xElectricity.setContent(new BayesVariable<String>("Electricity", xElectricity.getId(), new String[] { "Working", "Reduced", "NotWorking" }, new double[][] { { 0.6, 0.3, 0.099 } }));
    xTelecom.setContent(new BayesVariable<String>("Telecom", xTelecom.getId(), new String[] { "Working", "Reduced", "NotWorking" }, new double[][] { { 0.544, 0.304, 0.151 } }));
    xRail.setContent(new BayesVariable<String>("Rail", xRail.getId(), new String[] { "Working", "Reduced", "NotWorking" }, new double[][] { { 0.579, 0.230, 0.190 } }));
    xAirTravel.setContent(new BayesVariable<String>("AirTravel", xAirTravel.getId(), new String[] { "Working", "Reduced", "NotWorking" }, new double[][] { { 0.449, 0.330, 0.219 } }));
    xTransportation.setContent(new BayesVariable<String>("Transportation", xTransportation.getId(), new String[] { "Working", "Moderate", "Severe", "Failure" }, new double[][] { { 0.658, 0.167, 0.097, 0.077 } }));
    xUtilities.setContent(new BayesVariable<String>("Utilities", xUtilities.getId(), new String[] { "Working", "Moderate", "Severe", "Failure" }, new double[][] { { 0.541, 0.272, 0.097, 0.088 } }));
    xUSBanks.setContent(new BayesVariable<String>("USBanks", xUSBanks.getId(), new String[] { "Working", "Reduced", "NotWorking" }, new double[][] { { 0.488, 0.370, 0.141 } }));
    xUSStocks.setContent(new BayesVariable<String>("USStocks", xUSStocks.getId(), new String[] { "Up", "Down", "Crash" }, new double[][] { { 0.433, 0.386, 0.179 } }));
    JunctionTreeBuilder jtBuilder = new JunctionTreeBuilder(graph);
    JunctionTreeClique root = jtBuilder.build(false).getRoot();
    // clique1
    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
    assertSame(sep, jtNode2.getParentSeparator());
    sep = jtNode2.getChildren().get(0);
    assertEquals(clique2And3, sep.getBitSet());
    JunctionTreeClique jtNode3 = sep.getChild();
    assertEquals(clique2, sep.getParent().getBitSet());
    assertEquals(clique3, jtNode3.getBitSet());
    assertEquals(0, jtNode3.getChildren().size());
    // clique4
    sep = jtNode2.getChildren().get(1);
    assertEquals(clique2And4, sep.getBitSet());
    JunctionTreeClique jtNode4 = sep.getChild();
    assertEquals(clique2, sep.getParent().getBitSet());
    assertEquals(clique4, jtNode4.getBitSet());
    assertEquals(0, jtNode4.getChildren().size());
}
Also used : OpenBitSet(org.drools.core.util.bitmask.OpenBitSet) GraphNode(org.drools.beliefs.graph.GraphNode) Test(org.junit.Test)

Example 17 with OpenBitSet

use of org.drools.core.util.bitmask.OpenBitSet in project drools by kiegroup.

the class JunctionTreeBuilderTest method testTriangulate2.

@Test
public void testTriangulate2() {
    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);
    // *
    // /
    // *
    // /  \
    // *   *
    // |   |
    // *   |
    // \  /
    // *
    connectParentToChildren(x1, x2);
    connectParentToChildren(x1, x3);
    connectParentToChildren(x2, x4);
    connectParentToChildren(x2, x6);
    connectParentToChildren(x3, x5);
    connectParentToChildren(x5, x6);
    JunctionTreeBuilder jtBuilder = new JunctionTreeBuilder(graph);
    jtBuilder.moralize();
    List<OpenBitSet> cliques = jtBuilder.triangulate();
    // assert all new edges
    assertLinkedVertex(jtBuilder.getAdjacencyMatrix(), new int[] { 1, 2, 3 });
    assertLinkedVertex(jtBuilder.getAdjacencyMatrix(), new int[] { 2, 1, 3, 4, 5, 6 });
    assertLinkedVertex(jtBuilder.getAdjacencyMatrix(), new int[] { 3, 1, 2, 5 });
    assertLinkedVertex(jtBuilder.getAdjacencyMatrix(), new int[] { 4, 2 });
    assertLinkedVertex(jtBuilder.getAdjacencyMatrix(), new int[] { 5, 2, 3, 6 });
    assertLinkedVertex(jtBuilder.getAdjacencyMatrix(), new int[] { 6, 2, 5 });
    // 5th is 0, which is just a dummy V to get numbers aligned
    assertEquals(5, cliques.size());
    // x1, x2, x3 //a, b, c
    assertTrue(cliques.contains(bitSet("1110")));
    // x2, x4
    assertTrue(cliques.contains(bitSet("10100")));
    // x2, x5, x6
    assertTrue(cliques.contains(bitSet("1100100")));
    // x2, x3, x5
    assertTrue(cliques.contains(bitSet("101100")));
}
Also used : OpenBitSet(org.drools.core.util.bitmask.OpenBitSet) GraphNode(org.drools.beliefs.graph.GraphNode) Test(org.junit.Test)

Example 18 with OpenBitSet

use of org.drools.core.util.bitmask.OpenBitSet in project drools by kiegroup.

the class JunctionTreeBuilderTest method testSepSetCompareWithSameIntersect.

@Test
public void testSepSetCompareWithSameIntersect() {
    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);
    OpenBitSet OpenBitSet1_1 = bitSet("00001110");
    OpenBitSet OpenBitSet1_2 = bitSet("01000010");
    SeparatorSet s1 = new SeparatorSet(OpenBitSet1_1, 0, OpenBitSet1_2, 0, graph);
    OpenBitSet OpenBitSet2_1 = bitSet("00001110");
    OpenBitSet OpenBitSet2_2 = bitSet("01000100");
    SeparatorSet s2 = new SeparatorSet(OpenBitSet2_1, 0, OpenBitSet2_2, 0, graph);
    List<SeparatorSet> list = new ArrayList<SeparatorSet>();
    list.add(s1);
    list.add(s2);
    Collections.sort(list);
    assertEquals(s1, list.get(0));
    // reverse the bits, to show the arbitrary is deterministic
    OpenBitSet1_2 = bitSet("01000100");
    s1 = new SeparatorSet(OpenBitSet1_1, 0, OpenBitSet1_2, 0, graph);
    OpenBitSet2_2 = bitSet("01000010");
    s2 = new SeparatorSet(OpenBitSet2_1, 0, OpenBitSet2_2, 0, graph);
    list = new ArrayList<SeparatorSet>();
    list.add(s1);
    list.add(s2);
    Collections.sort(list);
    // was s1 before
    assertEquals(s2, list.get(0));
}
Also used : OpenBitSet(org.drools.core.util.bitmask.OpenBitSet) ArrayList(java.util.ArrayList) GraphNode(org.drools.beliefs.graph.GraphNode) Test(org.junit.Test)

Example 19 with OpenBitSet

use of org.drools.core.util.bitmask.OpenBitSet in project drools by kiegroup.

the class JunctionTreeBuilderTest method testMapNodeToClique.

@Test
public void testMapNodeToClique() {
    Graph<BayesVariable> graph = new BayesNetwork();
    JunctionTreeBuilder tbuilder = new JunctionTreeBuilder(graph);
    GraphNode<BayesVariable> x0 = addNode(graph);
    GraphNode<BayesVariable> x1 = addNode(graph);
    GraphNode<BayesVariable> x2 = addNode(graph);
    GraphNode<BayesVariable> x3 = addNode(graph);
    GraphNode<BayesVariable> x4 = addNode(graph);
    GraphNode<BayesVariable> x5 = addNode(graph);
    GraphNode<BayesVariable> x6 = addNode(graph);
    GraphNode<BayesVariable> x7 = addNode(graph);
    connectChildToParents(x1, x2, x3);
    connectChildToParents(x3, x6, x7);
    OpenBitSet clique0 = bitSet("01001110");
    OpenBitSet clique1 = bitSet("11001110");
    OpenBitSet clique2 = bitSet("11101000");
    OpenBitSet clique3 = bitSet("00010011");
    JunctionTreeClique jtNode0 = new JunctionTreeClique(0, graph, clique0);
    JunctionTreeClique jtNode1 = new JunctionTreeClique(1, graph, clique1);
    JunctionTreeClique jtNode2 = new JunctionTreeClique(2, graph, clique2);
    JunctionTreeClique jtNode3 = new JunctionTreeClique(3, graph, clique3);
    JunctionTreeClique[] jtNodes = new JunctionTreeClique[] { jtNode0, jtNode1, jtNode2, jtNode3 };
    OpenBitSet[] nodeToCliques = new OpenBitSet[8];
    tbuilder.mapVarNodeToCliques(nodeToCliques, 0, clique0);
    tbuilder.mapVarNodeToCliques(nodeToCliques, 1, clique1);
    tbuilder.mapVarNodeToCliques(nodeToCliques, 2, clique2);
    tbuilder.mapVarNodeToCliques(nodeToCliques, 3, clique3);
    // int[] nodeToClique = new int[8];
    tbuilder.mapNodeToCliqueFamily(nodeToCliques, jtNodes);
    assertEquals(3, x0.getContent().getFamily());
    assertEquals(0, x1.getContent().getFamily());
    assertEquals(0, x2.getContent().getFamily());
    assertEquals(2, x3.getContent().getFamily());
    assertEquals(3, x4.getContent().getFamily());
    assertEquals(2, x5.getContent().getFamily());
    assertEquals(0, x6.getContent().getFamily());
    assertEquals(2, x7.getContent().getFamily());
}
Also used : OpenBitSet(org.drools.core.util.bitmask.OpenBitSet) Test(org.junit.Test)

Example 20 with OpenBitSet

use of org.drools.core.util.bitmask.OpenBitSet 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)

Aggregations

OpenBitSet (org.drools.core.util.bitmask.OpenBitSet)20 Test (org.junit.Test)16 GraphNode (org.drools.beliefs.graph.GraphNode)13 ArrayList (java.util.ArrayList)9 Edge (org.drools.beliefs.graph.Edge)1