Search in sources :

Example 26 with GraphNode

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

the class XmlBifParser method buildBayesNetwork.

public static BayesNetwork buildBayesNetwork(Bif bif) {
    String name = bif.getNetwork().getName();
    String packageName = "default";
    List<String> props = bif.getNetwork().getProperties();
    if (props != null) {
        for (String prop : props) {
            prop = prop.trim();
            if (prop.startsWith("package")) {
                packageName = prop.substring(prop.indexOf('=') + 1).trim();
            }
        }
    }
    BayesNetwork graph = new BayesNetwork(name, packageName);
    Map<String, GraphNode<BayesVariable>> map = new HashMap<String, GraphNode<BayesVariable>>();
    for (Definition def : bif.getNetwork().getDefinitions()) {
        GraphNode<BayesVariable> node = graph.addNode();
        BayesVariable var = buildVariable(def, bif.getNetwork(), node.getId());
        node.setContent(var);
        map.put(var.getName(), node);
    }
    for (Entry<String, GraphNode<BayesVariable>> entry : map.entrySet()) {
        GraphNode<BayesVariable> node = entry.getValue();
        BayesVariable var = node.getContent();
        if (var.getGiven() != null && var.getGiven().length > 0) {
            for (String given : var.getGiven()) {
                GraphNode<BayesVariable> givenNode = map.get(given);
                EdgeImpl e = new EdgeImpl();
                e.setOutGraphNode(givenNode);
                e.setInGraphNode(node);
            }
        }
    }
    return graph;
}
Also used : BayesVariable(org.drools.beliefs.bayes.BayesVariable) HashMap(java.util.HashMap) EdgeImpl(org.drools.beliefs.graph.impl.EdgeImpl) GraphNode(org.drools.beliefs.graph.GraphNode) BayesNetwork(org.drools.beliefs.bayes.BayesNetwork)

Example 27 with GraphNode

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

the class BayesAbsorbtionTest method testAbsorption2.

@Test
public void testAbsorption2() {
    // Absorbs into node1 into sep. A, B and C are in node1. A and B are in the sep.
    // this tests a non separator var, after the vars
    BayesVariable a = new BayesVariable<String>("A", 0, new String[] { "A1", "A2" }, null);
    BayesVariable b = new BayesVariable<String>("B", 1, new String[] { "B1", "B2" }, null);
    BayesVariable c = new BayesVariable<String>("C", 2, new String[] { "C1", "C2" }, null);
    Graph<BayesVariable> graph = new BayesNetwork();
    GraphNode x0 = addNode(graph);
    GraphNode x1 = addNode(graph);
    GraphNode x2 = addNode(graph);
    x0.setContent(a);
    x1.setContent(b);
    x2.setContent(c);
    JunctionTreeClique node1 = new JunctionTreeClique(0, graph, bitSet("0111"));
    JunctionTreeClique node2 = new JunctionTreeClique(1, graph, bitSet("0011"));
    SeparatorState sep = new JunctionTreeSeparator(0, node1, node2, bitSet("0011"), graph).createState();
    BayesVariable[] vars = new BayesVariable[] { a, b, c };
    BayesVariable[] sepVars = new BayesVariable[] { a, b };
    int[] sepVarPos = PotentialMultiplier.createSubsetVarPos(vars, sepVars);
    int sepVarNumberOfStates = PotentialMultiplier.createNumberOfStates(sepVars);
    int[] sepVarMultipliers = PotentialMultiplier.createIndexMultipliers(sepVars, sepVarNumberOfStates);
    double v = 0.44;
    for (int i = 0; i < node1.getPotentials().length; i++) {
        node1.getPotentials()[i] = v;
        v += +0.4;
    }
    double[] oldSepPotentials = new double[sep.getPotentials().length];
    Arrays.fill(oldSepPotentials, 0.2);
    v = 0.5;
    for (int i = 0; i < sep.getPotentials().length; i++) {
        sep.getPotentials()[i] = v;
        v += +0.5;
    }
    BayesAbsorption p = new BayesAbsorption(sepVarPos, oldSepPotentials, sep.getPotentials(), sepVarMultipliers, vars, node1.getPotentials());
    p.absorb();
    assertArray(new double[] { 0.01, 0.019, 0.055, 0.073, 0.137, 0.163, 0.254, 0.289 }, scaleDouble(3, node1.getPotentials()));
}
Also used : GraphNode(org.drools.beliefs.graph.GraphNode) Test(org.junit.Test)

Example 28 with GraphNode

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

the class BayesAbsorbtionTest method testAbsorption1.

@Test
public void testAbsorption1() {
    // Absorbs into node1 into sep. A and B are in node1. A and B are in the sep.
    // this is a straight forward projection
    BayesVariable a = new BayesVariable<String>("A", 0, new String[] { "A1", "A2" }, null);
    BayesVariable b = new BayesVariable<String>("B", 1, new String[] { "B1", "B2" }, null);
    Graph<BayesVariable> graph = new BayesNetwork();
    GraphNode x0 = addNode(graph);
    GraphNode x1 = addNode(graph);
    x0.setContent(a);
    x1.setContent(b);
    JunctionTreeClique node1 = new JunctionTreeClique(0, graph, bitSet("0011"));
    JunctionTreeClique node2 = new JunctionTreeClique(1, graph, bitSet("0011"));
    SeparatorState sep = new JunctionTreeSeparator(0, node1, node2, bitSet("0011"), graph).createState();
    BayesVariable[] vars = new BayesVariable[] { a, b };
    BayesVariable[] sepVars = new BayesVariable[] { a, b };
    int[] sepVarPos = PotentialMultiplier.createSubsetVarPos(vars, sepVars);
    int sepVarNumberOfStates = PotentialMultiplier.createNumberOfStates(sepVars);
    int[] sepVarMultipliers = PotentialMultiplier.createIndexMultipliers(sepVars, sepVarNumberOfStates);
    double v = 0.44;
    for (int i = 0; i < node1.getPotentials().length; i++) {
        node1.getPotentials()[i] = v;
        v += +0.4;
    }
    double[] oldSepPotentials = new double[sep.getPotentials().length];
    Arrays.fill(oldSepPotentials, 0.2);
    v = 0.5;
    for (int i = 0; i < sep.getPotentials().length; i++) {
        sep.getPotentials()[i] = v;
        v += +0.5;
    }
    BayesAbsorption p = new BayesAbsorption(sepVarPos, oldSepPotentials, sep.getPotentials(), sepVarMultipliers, vars, node1.getPotentials());
    p.absorb();
    assertArray(new double[] { 0.035, 0.135, 0.3, 0.529 }, scaleDouble(3, node1.getPotentials()));
}
Also used : GraphNode(org.drools.beliefs.graph.GraphNode) Test(org.junit.Test)

Example 29 with GraphNode

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

the class BayesProjectionTest method testProjection4.

@Test
public void testProjection4() {
    // Projects from node1 into sep. A, B and C are in node1. B and C are in the sep.
    // this tests a non separator var, is before the vars
    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 } });
    Graph<BayesVariable> graph = new BayesNetwork();
    GraphNode x0 = addNode(graph);
    GraphNode x1 = addNode(graph);
    GraphNode x2 = addNode(graph);
    GraphNode x3 = addNode(graph);
    x0.setContent(a);
    x1.setContent(b);
    x2.setContent(c);
    JunctionTreeClique node1 = new JunctionTreeClique(0, graph, bitSet("0111"));
    JunctionTreeClique node2 = new JunctionTreeClique(1, graph, bitSet("0110"));
    SeparatorState sep = new JunctionTreeSeparator(0, node1, node2, bitSet("0101"), graph).createState();
    double v = 0.1;
    for (int i = 0; i < node1.getPotentials().length; i++) {
        node1.getPotentials()[i] = v;
        v = scaleDouble(3, v + 0.1);
    }
    BayesVariable[] vars = new BayesVariable[] { a, b, c };
    BayesVariable[] sepVars = new BayesVariable[] { b, c };
    int[] sepVarPos = PotentialMultiplier.createSubsetVarPos(vars, sepVars);
    int sepVarNumberOfStates = PotentialMultiplier.createNumberOfStates(sepVars);
    int[] sepVarMultipliers = PotentialMultiplier.createIndexMultipliers(sepVars, sepVarNumberOfStates);
    double[] projectedSepPotentials = new double[sep.getPotentials().length];
    BayesProjection p = new BayesProjection(vars, node1.getPotentials(), sepVarPos, sepVarMultipliers, projectedSepPotentials);
    p.project();
    // remember it's been normalized, from 0.6 0.8 1.0 1.2
    assertArray(new double[] { 0.167, 0.222, 0.278, 0.333 }, scaleDouble(3, projectedSepPotentials));
}
Also used : GraphNode(org.drools.beliefs.graph.GraphNode) Test(org.junit.Test)

Example 30 with GraphNode

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

the class BayesProjectionTest method testProjection1.

@Test
public void testProjection1() {
    // Projects from node1 into sep. A and B are in node1. A and B are in the sep.
    // this is a straight forward projection
    BayesVariable a = new BayesVariable<String>("A", 0, new String[] { "A1", "A2" }, null);
    BayesVariable b = new BayesVariable<String>("B", 1, new String[] { "B1", "B2" }, null);
    Graph<BayesVariable> graph = new BayesNetwork();
    GraphNode x0 = addNode(graph);
    GraphNode x1 = addNode(graph);
    x0.setContent(a);
    x1.setContent(b);
    JunctionTreeClique node1 = new JunctionTreeClique(0, graph, bitSet("0011"));
    JunctionTreeClique node2 = new JunctionTreeClique(1, graph, bitSet("0011"));
    SeparatorState sep = new JunctionTreeSeparator(0, node1, node2, bitSet("0011"), graph).createState();
    double v = 0.1;
    for (int i = 0; i < node1.getPotentials().length; i++) {
        node1.getPotentials()[i] = v;
        v += +0.1;
    }
    BayesVariable[] vars = new BayesVariable[] { a, b };
    BayesVariable[] sepVars = new BayesVariable[] { a, b };
    int[] sepVarPos = PotentialMultiplier.createSubsetVarPos(vars, sepVars);
    int sepVarNumberOfStates = PotentialMultiplier.createNumberOfStates(sepVars);
    int[] sepVarMultipliers = PotentialMultiplier.createIndexMultipliers(sepVars, sepVarNumberOfStates);
    double[] projectedSepPotentials = new double[sep.getPotentials().length];
    BayesProjection p = new BayesProjection(vars, node1.getPotentials(), sepVarPos, sepVarMultipliers, projectedSepPotentials);
    p.project();
    assertArray(new double[] { 0.1, 0.2, 0.3, 0.4 }, scaleDouble(3, projectedSepPotentials));
}
Also used : 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