use of org.drools.beliefs.graph.GraphNode in project drools by kiegroup.
the class JunctionTreeBuilderTest method testMoralize2.
@Test
public void testMoralize2() {
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, x3);
connectParentToChildren(x2, x4);
connectParentToChildren(x4, x5);
connectParentToChildren(x3, x5);
connectParentToChildren(x6, x5);
JunctionTreeBuilder jtBuilder = new JunctionTreeBuilder(graph);
jtBuilder.moralize();
assertLinkedNode(jtBuilder, new int[] { x1.getId(), 2, 3 });
assertLinkedNode(jtBuilder, new int[] { x2.getId(), 1, 4 });
assertLinkedNode(jtBuilder, new int[] { x3.getId(), 1, 4, 5, 6 });
assertLinkedNode(jtBuilder, new int[] { x4.getId(), 2, 3, 5, 6 });
assertLinkedNode(jtBuilder, new int[] { x5.getId(), 3, 4, 6 });
assertLinkedNode(jtBuilder, new int[] { x6.getId(), 3, 4, 5 });
}
use of org.drools.beliefs.graph.GraphNode in project drools by kiegroup.
the class JunctionTreeBuilderTest method testPriorityQueueWithMinimalNewEdges.
@Test
public void testPriorityQueueWithMinimalNewEdges() {
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);
GraphNode x8 = addNode(graph);
GraphNode x9 = addNode(graph);
GraphNode x10 = addNode(graph);
GraphNode x11 = addNode(graph);
GraphNode x12 = addNode(graph);
// 3 new edges
connectParentToChildren(x2, x1);
connectParentToChildren(x3, x1);
connectParentToChildren(x4, x1);
// 1 new edge
// we give this a high weight, to show required new edges is compared first
connectParentToChildren(x6, x5);
connectParentToChildren(x7, x5);
x5.setContent(new BayesVariable<String>("x5", x0.getId(), new String[] { "a", "b", "c" }, new double[][] { { 0.1, 0.1, 0.1 } }));
x6.setContent(new BayesVariable<String>("x6", x0.getId(), new String[] { "a", "b", "c" }, new double[][] { { 0.1, 0.1, 0.1 } }));
x7.setContent(new BayesVariable<String>("x7", x0.getId(), new String[] { "a", "b", "c" }, new double[][] { { 0.1, 0.1, 0.1 } }));
// 6 new edges
connectParentToChildren(x9, x8);
connectParentToChildren(x10, x8);
connectParentToChildren(x11, x8);
connectParentToChildren(x12, x8);
JunctionTreeBuilder jtBuilder = new JunctionTreeBuilder(graph);
// jtBuilder.moralize(); // don't moralize, as we want to force a simpler construction for required edges, for the purposes of testing
PriorityQueue<EliminationCandidate> p = new PriorityQueue<EliminationCandidate>(graph.size());
EliminationCandidate elmCandVert = new EliminationCandidate(graph, jtBuilder.getAdjacencyMatrix(), x1);
p.add(elmCandVert);
elmCandVert = new EliminationCandidate(graph, jtBuilder.getAdjacencyMatrix(), x5);
p.add(elmCandVert);
elmCandVert = new EliminationCandidate(graph, jtBuilder.getAdjacencyMatrix(), x8);
p.add(elmCandVert);
EliminationCandidate v = p.remove();
int id = v.getV().getId();
assertEquals(5, id);
assertEquals(1, v.getNewEdgesRequired());
v = p.remove();
id = v.getV().getId();
assertEquals(1, id);
assertEquals(3, v.getNewEdgesRequired());
v = p.remove();
id = v.getV().getId();
assertEquals(8, id);
assertEquals(6, v.getNewEdgesRequired());
assertEquals(0, p.size());
}
use of org.drools.beliefs.graph.GraphNode 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()));
}
use of org.drools.beliefs.graph.GraphNode in project drools by kiegroup.
the class PassMessageTest method testPassMessage1.
@Test
public void testPassMessage1() {
BayesVariable a = new BayesVariable<String>("A", 0, new String[] { "A1", "A2" }, null);
Graph<BayesVariable> graph = new BayesNetwork();
GraphNode x0 = addNode(graph);
GraphNode x1 = addNode(graph);
x0.setContent(a);
JunctionTreeClique node1 = new JunctionTreeClique(0, graph, bitSet("0001"));
JunctionTreeClique node2 = new JunctionTreeClique(1, graph, bitSet("0001"));
JunctionTreeSeparator sep = new JunctionTreeSeparator(0, node1, node2, bitSet("0001"), graph);
node1.getPotentials()[0] = 0.2;
node1.getPotentials()[1] = 0.3;
node2.getPotentials()[0] = 0.6;
node2.getPotentials()[1] = 0.7;
JunctionTree jtree = new JunctionTree(graph, node1, new JunctionTreeClique[] { node1, node2 }, new JunctionTreeSeparator[] { sep });
BayesInstance bayesInstance = new BayesInstance(jtree);
SeparatorState sepState = bayesInstance.getSeparatorStates()[0];
sepState.getPotentials()[0] = 0.4;
sepState.getPotentials()[1] = 0.5;
bayesInstance.passMessage(node1, sep, node2);
assertArray(new double[] { 0.2, 0.3 }, scaleDouble(3, bayesInstance.getCliqueStates()[node1.getId()].getPotentials()));
assertArray(new double[] { 0.4, 0.6 }, scaleDouble(3, sepState.getPotentials()));
assertArray(new double[] { 0.417, 0.583 }, scaleDouble(3, bayesInstance.getCliqueStates()[node2.getId()].getPotentials()));
}
use of org.drools.beliefs.graph.GraphNode in project drools by kiegroup.
the class SprinkerTest method connectParentToChildren.
public static void connectParentToChildren(GraphNode parent, GraphNode... children) {
for (GraphNode child : children) {
EdgeImpl e = new EdgeImpl();
e.setOutGraphNode(parent);
e.setInGraphNode(child);
}
}
Aggregations