Search in sources :

Example 1 with OpenBitSet

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

the class EliminationCandidate method update.

public void update() {
    // must use the adjacency matrix, and not the vertex.getEdges() for connections,
    // as it gets updated with new connections, during elimination
    weightRequired = (int) Math.abs(v.getContent().getOutcomes().length);
    newEdgesRequired = 0;
    cliqueBitSet = new OpenBitSet(adjMatrix.length);
    cliqueBitSet.set(v.getId());
    // determine new edges added, to ensure all adjacent nodes become neighbours in a cluster
    boolean[] adjList = adjMatrix[v.getId()];
    for (int i = 0; i < adjList.length; i++) {
        if (!adjList[i]) {
            // not connected to this vertex
            continue;
        }
        GraphNode<BayesVariable> relV = g.getNode(i);
        weightRequired *= Math.abs(relV.getContent().getOutcomes().length);
        cliqueBitSet.set(i);
        for (int j = i + 1; j < adjList.length; j++) {
            // i + 1, so it doesn't check if a node is connected with itself
            if (!adjList[j] || adjMatrix[i][j]) {
                // edge already exists
                continue;
            }
            newEdgesRequired++;
        }
    }
}
Also used : OpenBitSet(org.drools.core.util.bitmask.OpenBitSet)

Example 2 with OpenBitSet

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

the class JunctionTreeBuilder method mapNodeToCliqueFamily.

/**
 * Given the set of cliques, mapped via ID in a Bitset, for a given bayes node,
 * Find the best clique. Where best clique is one that contains all it's parents
 * with the smallest number of nodes in that clique. When there are no parents
 * then simply pick the clique with the smallest number nodes.
 * @param varNodeToCliques
 * @param jtNodes
 */
public void mapNodeToCliqueFamily(OpenBitSet[] varNodeToCliques, JunctionTreeClique[] jtNodes) {
    for (int i = 0; i < varNodeToCliques.length; i++) {
        GraphNode<BayesVariable> varNode = graph.getNode(i);
        // Get OpenBitSet for parents
        OpenBitSet parents = new OpenBitSet();
        int count = 0;
        for (Edge edge : varNode.getInEdges()) {
            parents.set(edge.getOutGraphNode().getId());
            count++;
        }
        if (count == 0) {
        // node has no parents, so simply find the smallest clique it's in.
        }
        OpenBitSet cliques = varNodeToCliques[i];
        if (cliques == null) {
            throw new IllegalStateException("Node exists, that is not part of a clique. " + varNode.toString());
        }
        int bestWeight = -1;
        int clique = -1;
        // finds the smallest node, that contains all the parents
        for (int j = cliques.nextSetBit(0); j >= 0; j = cliques.nextSetBit(j + 1)) {
            JunctionTreeClique jtNode = jtNodes[j];
            // If it has parents then is jtNode a supserset of parents, visa-vis is parents a subset of jtNode
            if ((count == 0 || OpenBitSet.andNotCount(parents, jtNode.getBitSet()) == 0) && (clique == -1 || jtNode.getBitSet().cardinality() < bestWeight)) {
                bestWeight = (int) jtNode.getBitSet().cardinality();
                clique = j;
            }
        }
        if (clique == -1) {
            throw new IllegalStateException("No clique for node found." + varNode.toString());
        }
        varNode.getContent().setFamily(clique);
        jtNodes[clique].addToFamily(varNode.getContent());
    }
}
Also used : OpenBitSet(org.drools.core.util.bitmask.OpenBitSet) Edge(org.drools.beliefs.graph.Edge)

Example 3 with OpenBitSet

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

the class JunctionTreeBuilderTest method testJunctionWithPruning3.

@Test
public void testJunctionWithPruning3() {
    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("00011110");
    OpenBitSet OpenBitSet3 = bitSet("11100000");
    OpenBitSet OpenBitSet4 = bitSet("01100001");
    OpenBitSet intersect1And2 = ((OpenBitSet) OpenBitSet2.clone());
    intersect1And2.and(OpenBitSet1);
    OpenBitSet intersect2And3 = ((OpenBitSet) OpenBitSet2.clone());
    intersect2And3.and(OpenBitSet3);
    OpenBitSet intersect1And4 = ((OpenBitSet) OpenBitSet1.clone());
    intersect1And4.and(OpenBitSet4);
    OpenBitSet intersect3And4 = ((OpenBitSet) OpenBitSet3.clone());
    intersect3And4.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(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(1, sep.getChild().getChildren().size());
    jtNode = sep.getChild();
    assertEquals(OpenBitSet4, jtNode.getBitSet());
    assertEquals(1, jtNode.getChildren().size());
    sep = jtNode.getChildren().get(0);
    assertEquals(OpenBitSet4, sep.getParent().getBitSet());
    assertEquals(OpenBitSet3, sep.getChild().getBitSet());
    assertEquals(intersect3And4, 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 4 with OpenBitSet

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

the class JunctionTreeBuilderTest method testSepSetCompareWithDifferentMass.

@Test
public void testSepSetCompareWithDifferentMass() {
    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("01101100");
    SeparatorSet s1 = new SeparatorSet(OpenBitSet1_1, 0, OpenBitSet1_2, 0, graph);
    OpenBitSet OpenBitSet2_1 = bitSet("00001110");
    OpenBitSet OpenBitSet2_2 = bitSet("00100100");
    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));
}
Also used : OpenBitSet(org.drools.core.util.bitmask.OpenBitSet) ArrayList(java.util.ArrayList) GraphNode(org.drools.beliefs.graph.GraphNode) Test(org.junit.Test)

Example 5 with OpenBitSet

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

the class JunctionTreeBuilderTest method testJunctionTree.

@Test
public void testJunctionTree() {
    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 = bitSet("00001110");
    OpenBitSet OpenBitSet2 = bitSet("00011100");
    OpenBitSet OpenBitSet3 = bitSet("00110000");
    OpenBitSet OpenBitSet4 = bitSet("01110000");
    List<OpenBitSet> cliques = new ArrayList<OpenBitSet>();
    cliques.add(OpenBitSet1);
    cliques.add(OpenBitSet2);
    cliques.add(OpenBitSet3);
    cliques.add(OpenBitSet4);
    List<SeparatorSet> separatorSets = new ArrayList<SeparatorSet>();
    for (int i = 0; i < cliques.size(); i++) {
        OpenBitSet ci = cliques.get(i);
        for (int j = i + 1; j < cliques.size(); j++) {
            OpenBitSet cj = cliques.get(j);
            if (ci.intersects(cj)) {
                SeparatorSet separatorSet = new SeparatorSet(ci, 0, cj, 0, graph);
            }
        }
    }
    Collections.sort(separatorSets);
}
Also used : OpenBitSet(org.drools.core.util.bitmask.OpenBitSet) ArrayList(java.util.ArrayList) 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