Search in sources :

Example 31 with GraphNode

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

the class JunctionTreeBuilderTest method testJunctionWithPruning1.

@Test
public void testJunctionWithPruning1() {
    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);
    List<OpenBitSet> list = new ArrayList<OpenBitSet>();
    OpenBitSet OpenBitSet1 = bitSet("00001111");
    OpenBitSet OpenBitSet2 = bitSet("00111100");
    // links to 2 and 1, but should still result in a single path. As the 3 -> 1 link, gets pruned
    OpenBitSet OpenBitSet3 = bitSet("11100001");
    OpenBitSet intersect1And2 = ((OpenBitSet) OpenBitSet2.clone());
    intersect1And2.and(OpenBitSet1);
    OpenBitSet intersect2And3 = ((OpenBitSet) OpenBitSet2.clone());
    intersect2And3.and(OpenBitSet3);
    list.add(OpenBitSet1);
    list.add(OpenBitSet2);
    list.add(OpenBitSet3);
    JunctionTreeBuilder jtBuilder = new JunctionTreeBuilder(graph);
    JunctionTreeClique jtNode = jtBuilder.junctionTree(list, false).getRoot();
    assertEquals(OpenBitSet1, jtNode.getBitSet());
    assertEquals(2, jtNode.getChildren().size());
    JunctionTreeSeparator sep = jtNode.getChildren().get(0);
    assertEquals(OpenBitSet1, sep.getParent().getBitSet());
    assertEquals(OpenBitSet2, sep.getChild().getBitSet());
    assertEquals(0, sep.getChild().getChildren().size());
    sep = jtNode.getChildren().get(1);
    assertEquals(OpenBitSet1, sep.getParent().getBitSet());
    assertEquals(OpenBitSet3, sep.getChild().getBitSet());
    assertEquals(0, sep.getChild().getChildren().size());
}
Also used : OpenBitSet(org.drools.core.util.bitmask.OpenBitSet) ArrayList(java.util.ArrayList) GraphNode(org.drools.beliefs.graph.GraphNode) Test(org.junit.Test)

Example 32 with GraphNode

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

the class JunctionTreeBuilderTest method testSepSetCompareWithDifferentCost.

@Test
public void testSepSetCompareWithDifferentCost() {
    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);
    x1.setContent(new BayesVariable<String>("x1", x0.getId(), new String[] { "a" }, new double[][] { { 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 } }));
    OpenBitSet OpenBitSet1_1 = bitSet("00001110");
    OpenBitSet OpenBitSet1_2 = bitSet("01101100");
    SeparatorSet s1 = new SeparatorSet(OpenBitSet1_1, 0, OpenBitSet1_2, 0, graph);
    OpenBitSet OpenBitSet2_1 = bitSet("00001110");
    OpenBitSet OpenBitSet2_2 = bitSet("00100110");
    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));
    // repeat, reversing the costs, to be sure no other factor is in play.
    x1.setContent(new BayesVariable<String>("x3", 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>("x1", x0.getId(), new String[] { "a" }, new double[][] { { 0.1 } }));
    s1 = new SeparatorSet(OpenBitSet1_1, 0, OpenBitSet1_2, 0, graph);
    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 33 with GraphNode

use of org.drools.beliefs.graph.GraphNode 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 34 with GraphNode

use of org.drools.beliefs.graph.GraphNode 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 35 with GraphNode

use of org.drools.beliefs.graph.GraphNode 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)

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