Search in sources :

Example 11 with OpenBitSet

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

the class JunctionTreeTest method testJunctionTreeInitialisation.

@Test
public void testJunctionTreeInitialisation() {
    // creates  JunctionTree where node1 has only B as a family memory.
    // node 2 has both c and d as family, and c is the parent of d.
    BayesVariable a = new BayesVariable<String>("A", 0, new String[] { "A1", "A2" }, new double[][] { { 0.1, 0.2 } });
    BayesVariable b = new BayesVariable<String>("B", 1, new String[] { "B1", "B2" }, new double[][] { { 0.1, 0.2 } });
    BayesVariable c = new BayesVariable<String>("C", 2, new String[] { "C1", "C2" }, new double[][] { { 0.1, 0.2 } });
    BayesVariable d = new BayesVariable<String>("D", 3, new String[] { "D1", "D2" }, new double[][] { { 0.1, 0.2 }, { 0.3, 0.4 } });
    Graph<BayesVariable> graph = new BayesNetwork();
    GraphNode x0 = addNode(graph);
    GraphNode x1 = addNode(graph);
    GraphNode x2 = addNode(graph);
    GraphNode x3 = addNode(graph);
    // connectParentToChildren(x0, x2);
    connectParentToChildren(x2, x3);
    x0.setContent(a);
    x1.setContent(b);
    x2.setContent(c);
    x3.setContent(d);
    JunctionTreeClique node1 = new JunctionTreeClique(0, graph, bitSet("0011"));
    JunctionTreeClique node2 = new JunctionTreeClique(1, graph, bitSet("1100"));
    new JunctionTreeSeparator(0, node1, node2, new OpenBitSet(), graph);
    node1.addToFamily(b);
    b.setFamily(node1.getId());
    node2.addToFamily(c);
    c.setFamily(node2.getId());
    node2.addToFamily(d);
    d.setFamily(node2.getId());
    JunctionTree jtree = new JunctionTree(graph, node1, new JunctionTreeClique[] { node1, node2 }, null);
    assertArray(new double[] { 0.1, 0.2, 0.1, 0.2 }, scaleDouble(3, node1.getPotentials()));
    assertArray(new double[] { 0.01, 0.02, 0.06, 0.08 }, scaleDouble(3, node2.getPotentials()));
}
Also used : OpenBitSet(org.drools.core.util.bitmask.OpenBitSet) GraphNode(org.drools.beliefs.graph.GraphNode) Test(org.junit.Test)

Example 12 with OpenBitSet

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

the class JunctionTreeBuilder method mapVarNodeToCliques.

/**
 * Maps each Bayes node to cliques it's in.
 * It uses a BitSet to map the ID of the cliques
 * @param nodeToCliques
 * @param id
 * @param clique
 */
public void mapVarNodeToCliques(OpenBitSet[] nodeToCliques, int id, OpenBitSet clique) {
    for (int i = clique.nextSetBit(0); i >= 0; i = clique.nextSetBit(i + 1)) {
        OpenBitSet cliques = nodeToCliques[i];
        if (cliques == null) {
            cliques = new OpenBitSet();
            nodeToCliques[i] = cliques;
        }
        cliques.set(id);
    }
}
Also used : OpenBitSet(org.drools.core.util.bitmask.OpenBitSet)

Example 13 with OpenBitSet

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

the class GraphTest method bitSet.

public static OpenBitSet bitSet(String s) {
    OpenBitSet bitSet = new OpenBitSet();
    bitSet.setBits(new long[] { Long.valueOf(s, 2) });
    return bitSet;
}
Also used : OpenBitSet(org.drools.core.util.bitmask.OpenBitSet)

Example 14 with OpenBitSet

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

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

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