Search in sources :

Example 16 with GraphNode

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

the class JunctionTreeBuilderTest method testJunctionWithPruning2.

@Test
public void testJunctionWithPruning2() {
    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");
    OpenBitSet OpenBitSet3 = bitSet("11100000");
    OpenBitSet OpenBitSet4 = bitSet("00100001");
    OpenBitSet intersect1And2 = ((OpenBitSet) OpenBitSet2.clone());
    intersect1And2.and(OpenBitSet1);
    OpenBitSet intersect2And3 = ((OpenBitSet) OpenBitSet2.clone());
    intersect2And3.and(OpenBitSet3);
    OpenBitSet intersect1And4 = ((OpenBitSet) OpenBitSet1.clone());
    intersect1And4.and(OpenBitSet4);
    list.add(OpenBitSet1);
    list.add(OpenBitSet2);
    list.add(OpenBitSet3);
    list.add(OpenBitSet4);
    JunctionTreeBuilder jtBuilder = new JunctionTreeBuilder(graph);
    JunctionTreeClique jtNode = jtBuilder.junctionTree(list, false).getRoot();
    JunctionTreeClique root = jtNode;
    assertEquals(OpenBitSet1, root.getBitSet());
    assertEquals(2, root.getChildren().size());
    JunctionTreeSeparator sep = root.getChildren().get(0);
    assertEquals(OpenBitSet1, sep.getParent().getBitSet());
    assertEquals(OpenBitSet2, sep.getChild().getBitSet());
    assertEquals(1, sep.getChild().getChildren().size());
    jtNode = sep.getChild();
    assertEquals(OpenBitSet2, jtNode.getBitSet());
    assertEquals(1, jtNode.getChildren().size());
    sep = jtNode.getChildren().get(0);
    assertEquals(OpenBitSet2, sep.getParent().getBitSet());
    assertEquals(OpenBitSet3, sep.getChild().getBitSet());
    assertEquals(intersect2And3, sep.getBitSet());
    assertEquals(0, sep.getChild().getChildren().size());
    sep = root.getChildren().get(1);
    assertEquals(OpenBitSet1, sep.getParent().getBitSet());
    assertEquals(OpenBitSet4, sep.getChild().getBitSet());
    assertEquals(intersect1And4, sep.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 17 with GraphNode

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

the class JunctionTreeBuilderTest method testMoralize1.

@Test
public void testMoralize1() {
    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);
    connectParentToChildren(x2, x1);
    connectParentToChildren(x3, x1);
    connectParentToChildren(x4, x1);
    JunctionTreeBuilder jtBuilder = new JunctionTreeBuilder(graph);
    assertLinkedNode(jtBuilder, new int[] { 1, 2, 3, 4 });
    assertLinkedNode(jtBuilder, new int[] { 2, 1 });
    assertLinkedNode(jtBuilder, new int[] { 3, 1 });
    assertLinkedNode(jtBuilder, new int[] { 4, 1 });
    jtBuilder.moralize();
    assertLinkedNode(jtBuilder, new int[] { 1, 2, 3, 4 });
    assertLinkedNode(jtBuilder, new int[] { 2, 1, 3, 4 });
    assertLinkedNode(jtBuilder, new int[] { 3, 1, 2, 4 });
    assertLinkedNode(jtBuilder, new int[] { 4, 1, 2, 3 });
}
Also used : GraphNode(org.drools.beliefs.graph.GraphNode) Test(org.junit.Test)

Example 18 with GraphNode

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

the class JunctionTreeBuilderTest method testIterativeEliminationUsingEdgeAndWeight.

@Test
public void testIterativeEliminationUsingEdgeAndWeight() {
    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(x1, x6);
    connectParentToChildren(x2, x4);
    connectParentToChildren(x3, x5);
    connectParentToChildren(x4, x6);
    connectParentToChildren(x5, x6);
    // need to ensure x5 followed by x4 are removed first
    x1.setContent(new BayesVariable<String>("x1", x0.getId(), new String[] { "a", "b", "c", "d", "e", "f" }, new double[][] { { 0.1, 0.1, 0.1, 0.1, 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 } }));
    x5.setContent(new BayesVariable<String>("x5", x0.getId(), new String[] { "a" }, new double[][] { { 0.1 } }));
    x6.setContent(new BayesVariable<String>("x6", 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 vertex elimination order and updates
    boolean[][] clonedAdjMatrix = jtBuilder.cloneAdjacencyMarix(jtBuilder.getAdjacencyMatrix());
    PriorityQueue<EliminationCandidate> p = new PriorityQueue<EliminationCandidate>(graph.size());
    Map<Integer, EliminationCandidate> elmVertMap = new HashMap<Integer, EliminationCandidate>();
    for (GraphNode<BayesVariable> v : graph) {
        if (v.getId() == 0) {
            continue;
        }
        EliminationCandidate elmCandVert = new EliminationCandidate(graph, clonedAdjMatrix, v);
        p.add(elmCandVert);
        elmVertMap.put(v.getId(), elmCandVert);
    }
    assertLinkedVertex(jtBuilder.getAdjacencyMatrix(), new int[] { 1, 2, 3, 6 });
    assertLinkedVertex(jtBuilder.getAdjacencyMatrix(), new int[] { 2, 1, 4 });
    assertLinkedVertex(jtBuilder.getAdjacencyMatrix(), new int[] { 3, 1, 5 });
    assertLinkedVertex(jtBuilder.getAdjacencyMatrix(), new int[] { 4, 2, 6 });
    assertLinkedVertex(jtBuilder.getAdjacencyMatrix(), new int[] { 5, 3, 6 });
    assertLinkedVertex(jtBuilder.getAdjacencyMatrix(), new int[] { 6, 1, 4, 5 });
    assertEquals(3, elmVertMap.get(1).getNewEdgesRequired());
    assertEquals(1, elmVertMap.get(2).getNewEdgesRequired());
    assertEquals(1, elmVertMap.get(3).getNewEdgesRequired());
    assertEquals(1, elmVertMap.get(4).getNewEdgesRequired());
    assertEquals(1, elmVertMap.get(5).getNewEdgesRequired());
    assertEquals(3, elmVertMap.get(6).getNewEdgesRequired());
    // 5 has the lowest new edges and weight
    EliminationCandidate v = p.remove();
    int id = v.getV().getId();
    assertEquals(5, id);
    Set<Integer> verticesToUpdate = new HashSet<Integer>();
    boolean[] adjList = clonedAdjMatrix[id];
    jtBuilder.createClique(5, clonedAdjMatrix, verticesToUpdate, adjList);
    assertEquals(4, verticesToUpdate.size());
    assertTrue(verticesToUpdate.containsAll(Arrays.asList(new Integer[] { 1, 3, 6 })));
    jtBuilder.eliminateVertex(p, elmVertMap, clonedAdjMatrix, adjList, verticesToUpdate, v);
    // assert all new edges
    assertLinkedVertex(jtBuilder.getAdjacencyMatrix(), new int[] { 1, 2, 3, 6 });
    assertLinkedVertex(jtBuilder.getAdjacencyMatrix(), new int[] { 2, 1, 4 });
    assertLinkedVertex(jtBuilder.getAdjacencyMatrix(), new int[] { 3, 1, 5, 6 });
    assertLinkedVertex(jtBuilder.getAdjacencyMatrix(), new int[] { 4, 2, 6 });
    assertLinkedVertex(jtBuilder.getAdjacencyMatrix(), new int[] { 5, 3, 6 });
    assertLinkedVertex(jtBuilder.getAdjacencyMatrix(), new int[] { 6, 1, 3, 4, 5 });
    // assert new edges were correctly recalculated
    assertEquals(2, elmVertMap.get(1).getNewEdgesRequired());
    assertEquals(1, elmVertMap.get(2).getNewEdgesRequired());
    assertEquals(0, elmVertMap.get(3).getNewEdgesRequired());
    assertEquals(2, elmVertMap.get(6).getNewEdgesRequired());
    assertEquals(1, elmVertMap.get(4).getNewEdgesRequired());
    // 3 next as it has no new edges now, after recalculation
    v = p.remove();
    id = v.getV().getId();
    assertEquals(3, id);
    verticesToUpdate = new HashSet<Integer>();
    jtBuilder.createClique(3, clonedAdjMatrix, verticesToUpdate, adjList);
    jtBuilder.eliminateVertex(p, elmVertMap, clonedAdjMatrix, adjList, verticesToUpdate, v);
    // 4 is next
    v = p.remove();
    id = v.getV().getId();
    assertEquals(4, id);
    verticesToUpdate = new HashSet<Integer>();
    adjList = clonedAdjMatrix[id];
    jtBuilder.createClique(4, clonedAdjMatrix, verticesToUpdate, adjList);
    assertEquals(3, verticesToUpdate.size());
    // don't forget 3 and 5 were already eliminated
    assertTrue(verticesToUpdate.containsAll(Arrays.asList(new Integer[] { 1, 2, 6 })));
    jtBuilder.eliminateVertex(p, elmVertMap, clonedAdjMatrix, adjList, verticesToUpdate, v);
    // assert all new edges
    assertLinkedVertex(jtBuilder.getAdjacencyMatrix(), new int[] { 1, 2, 3, 6 });
    assertLinkedVertex(jtBuilder.getAdjacencyMatrix(), new int[] { 2, 1, 4, 6 });
    assertLinkedVertex(jtBuilder.getAdjacencyMatrix(), new int[] { 3, 1, 5, 6 });
    assertLinkedVertex(jtBuilder.getAdjacencyMatrix(), new int[] { 4, 2, 6 });
    assertLinkedVertex(jtBuilder.getAdjacencyMatrix(), new int[] { 5, 3, 6 });
    assertLinkedVertex(jtBuilder.getAdjacencyMatrix(), new int[] { 6, 1, 2, 3, 4, 5 });
    // assert new edges were correctly recalculated
    assertEquals(0, elmVertMap.get(1).getNewEdgesRequired());
    assertEquals(0, elmVertMap.get(2).getNewEdgesRequired());
    assertEquals(0, elmVertMap.get(6).getNewEdgesRequired());
    // 1, 2 and 6 all have no new edges, and same cluster, so it uses id to ensure arbitrary is deterministic
    v = p.remove();
    id = v.getV().getId();
    assertEquals(1, id);
    verticesToUpdate = new HashSet<Integer>();
    jtBuilder.createClique(1, clonedAdjMatrix, verticesToUpdate, adjList);
    jtBuilder.eliminateVertex(p, elmVertMap, clonedAdjMatrix, adjList, verticesToUpdate, v);
    v = p.remove();
    id = v.getV().getId();
    assertEquals(2, id);
    verticesToUpdate = new HashSet<Integer>();
    jtBuilder.createClique(2, clonedAdjMatrix, verticesToUpdate, adjList);
    jtBuilder.eliminateVertex(p, elmVertMap, clonedAdjMatrix, adjList, verticesToUpdate, v);
    v = p.remove();
    id = v.getV().getId();
    assertEquals(6, id);
    verticesToUpdate = new HashSet<Integer>();
    jtBuilder.createClique(6, clonedAdjMatrix, verticesToUpdate, adjList);
    jtBuilder.eliminateVertex(p, elmVertMap, clonedAdjMatrix, adjList, verticesToUpdate, v);
    assertEquals(0, p.size());
}
Also used : HashMap(java.util.HashMap) GraphNode(org.drools.beliefs.graph.GraphNode) PriorityQueue(java.util.PriorityQueue) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 19 with GraphNode

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

the class JunctionTreeBuilderTest method testMapNodeToCliques.

@Test
public void testMapNodeToCliques() {
    Graph<BayesVariable> graph = new BayesNetwork();
    JunctionTreeBuilder tbuilder = new JunctionTreeBuilder(graph);
    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);
    OpenBitSet clique0 = bitSet("01010101");
    OpenBitSet clique1 = bitSet("10010001");
    OpenBitSet clique2 = bitSet("10111010");
    OpenBitSet[] nodeToCliques = new OpenBitSet[8];
    tbuilder.mapVarNodeToCliques(nodeToCliques, 0, clique0);
    tbuilder.mapVarNodeToCliques(nodeToCliques, 1, clique1);
    tbuilder.mapVarNodeToCliques(nodeToCliques, 2, clique2);
    assertEquals(bitSet("011"), nodeToCliques[0]);
    assertEquals(bitSet("100"), nodeToCliques[1]);
    assertEquals(bitSet("001"), nodeToCliques[2]);
    assertEquals(bitSet("100"), nodeToCliques[3]);
    assertEquals(bitSet("111"), nodeToCliques[4]);
    assertEquals(bitSet("100"), nodeToCliques[5]);
    assertEquals(bitSet("001"), nodeToCliques[6]);
    assertEquals(bitSet("110"), nodeToCliques[7]);
}
Also used : OpenBitSet(org.drools.core.util.bitmask.OpenBitSet) GraphNode(org.drools.beliefs.graph.GraphNode) Test(org.junit.Test)

Example 20 with GraphNode

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

the class JunctionTreeBuilderTest method testCreateClique.

@Test
public void testCreateClique() {
    Graph<BayesVariable> graph = new BayesNetwork();
    GraphNode dX0 = addNode(graph);
    GraphNode dX1 = addNode(graph);
    GraphNode dX2 = addNode(graph);
    GraphNode dX3 = addNode(graph);
    GraphNode dX4 = addNode(graph);
    GraphNode dX5 = addNode(graph);
    connectParentToChildren(dX1, dX2, dX3, dX4);
    JunctionTreeBuilder jtBuilder = new JunctionTreeBuilder(graph);
    // do not moralize, as we want to test just the clique creation through elimination of the provided vertices
    Set<Integer> vertices = new HashSet<Integer>();
    boolean[] adjList = new boolean[] { false, false, true, true, true, false };
    boolean[][] clonedAdjMatrix = JunctionTreeBuilder.cloneAdjacencyMarix(jtBuilder.getAdjacencyMatrix());
    jtBuilder.createClique(dX1.getId(), clonedAdjMatrix, vertices, adjList);
    assertEquals(3, vertices.size());
    assertTrue(vertices.containsAll(Arrays.asList(new Integer[] { 2, 3, 4 })));
    assertLinkedNode(jtBuilder, 1, 2, 3, 4);
    assertLinkedNode(jtBuilder, 2, 1, 3, 4);
    assertLinkedNode(jtBuilder, 3, 1, 2, 4);
    assertLinkedNode(jtBuilder, 4, 1, 2, 3);
}
Also used : GraphNode(org.drools.beliefs.graph.GraphNode) HashSet(java.util.HashSet) 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