Search in sources :

Example 1 with Edge

use of org.drools.beliefs.graph.Edge 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 2 with Edge

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

the class JunctionTreeBuilder method moralize.

public void moralize(GraphNode<BayesVariable> v, GraphNode v1) {
    for (Edge e2 : v.getInEdges()) {
        // moralize, by connecting each parent with each other
        GraphNode v2 = graph.getNode(e2.getOutGraphNode().getId());
        if (v1 == v2) {
            // don't connect to itself
            continue;
        }
        if (adjacencyMatrix[v1.getId()][v2.getId()]) {
            // already connected, continue
            continue;
        }
        connect(getAdjacencyMatrix(), v1.getId(), v2.getId());
    }
}
Also used : GraphNode(org.drools.beliefs.graph.GraphNode) Edge(org.drools.beliefs.graph.Edge)

Example 3 with Edge

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

the class JunctionTreeBuilder method moralize.

public void moralize() {
    for (GraphNode<BayesVariable> v : graph) {
        for (Edge e1 : v.getInEdges()) {
            GraphNode pV1 = graph.getNode(e1.getOutGraphNode().getId());
            moralize(v, pV1);
        }
    }
}
Also used : GraphNode(org.drools.beliefs.graph.GraphNode) Edge(org.drools.beliefs.graph.Edge)

Aggregations

Edge (org.drools.beliefs.graph.Edge)3 GraphNode (org.drools.beliefs.graph.GraphNode)2 OpenBitSet (org.drools.core.util.bitmask.OpenBitSet)1