Search in sources :

Example 6 with KnowledgeEdge

use of edu.cmu.tetrad.data.KnowledgeEdge in project tetrad by cmu-phil.

the class FciOrient method fciOrientbk.

/**
 * Orients according to background knowledge
 */
private void fciOrientbk(IKnowledge bk, Graph graph, List<Node> variables) {
    logger.log("info", "Starting BK Orientation.");
    for (Iterator<KnowledgeEdge> it = bk.forbiddenEdgesIterator(); it.hasNext(); ) {
        if (Thread.currentThread().isInterrupted()) {
            break;
        }
        KnowledgeEdge edge = it.next();
        // match strings to variables in the graph.
        Node from = SearchGraphUtils.translate(edge.getFrom(), variables);
        Node to = SearchGraphUtils.translate(edge.getTo(), variables);
        if (from == null || to == null) {
            continue;
        }
        if (graph.getEdge(from, to) == null) {
            continue;
        }
        // Orient to*->from
        graph.setEndpoint(to, from, Endpoint.ARROW);
        graph.setEndpoint(from, to, Endpoint.CIRCLE);
        changeFlag = true;
        logger.log("knowledgeOrientation", SearchLogUtils.edgeOrientedMsg("Knowledge", graph.getEdge(from, to)));
    }
    for (Iterator<KnowledgeEdge> it = bk.requiredEdgesIterator(); it.hasNext(); ) {
        if (Thread.currentThread().isInterrupted()) {
            break;
        }
        KnowledgeEdge edge = it.next();
        // match strings to variables in this graph
        Node from = SearchGraphUtils.translate(edge.getFrom(), variables);
        Node to = SearchGraphUtils.translate(edge.getTo(), variables);
        if (from == null || to == null) {
            continue;
        }
        if (graph.getEdge(from, to) == null) {
            continue;
        }
        graph.setEndpoint(to, from, Endpoint.TAIL);
        graph.setEndpoint(from, to, Endpoint.ARROW);
        changeFlag = true;
        logger.log("knowledgeOrientation", SearchLogUtils.edgeOrientedMsg("Knowledge", graph.getEdge(from, to)));
    }
    logger.log("info", "Finishing BK Orientation.");
}
Also used : KnowledgeEdge(edu.cmu.tetrad.data.KnowledgeEdge) Node(edu.cmu.tetrad.graph.Node)

Example 7 with KnowledgeEdge

use of edu.cmu.tetrad.data.KnowledgeEdge in project tetrad by cmu-phil.

the class KnowledgeBoxEditor method resetEdgeDisplay.

private void resetEdgeDisplay(JCheckBox checkBox) {
    IKnowledge knowledge = getKnowledge();
    KnowledgeGraph graph = new KnowledgeGraph(getKnowledge());
    getVarNames().forEach(e -> {
        knowledge.addVariable(e);
        graph.addNode(new KnowledgeModelNode(e));
    });
    if (this.showRequiredByGroups) {
        List<KnowledgeEdge> list = knowledge.getListOfRequiredEdges();
        if (list.size() > EDGE_LIMIT) {
            showRequiredByGroups = false;
            if (checkBox != null) {
                checkBox.setSelected(false);
            }
            String errMsg = String.format("The number of edges to show exceeds the limit %d.", EDGE_LIMIT);
            JOptionPane.showMessageDialog(this, errMsg, "Unable To Display Edges", JOptionPane.ERROR_MESSAGE);
        } else {
            list.forEach(e -> {
                String from = e.getFrom();
                String to = e.getTo();
                if (knowledge.isRequiredByGroups(from, to)) {
                    KnowledgeModelNode fromNode = (KnowledgeModelNode) graph.getNode(from);
                    KnowledgeModelNode toNode = (KnowledgeModelNode) graph.getNode(to);
                    graph.addEdge(new KnowledgeModelEdge(fromNode, toNode, KnowledgeModelEdge.REQUIRED_BY_GROUPS));
                }
            });
        }
    }
    if (this.showForbiddenByGroups) {
        List<KnowledgeEdge> list = knowledge.getListOfForbiddenEdges();
        if (list.size() > EDGE_LIMIT) {
            showForbiddenByGroups = false;
            if (checkBox != null) {
                checkBox.setSelected(false);
            }
            String errMsg = String.format("The number of edges to show exceeds the limit %d.", EDGE_LIMIT);
            JOptionPane.showMessageDialog(this, errMsg, "Unable To Display Edges", JOptionPane.ERROR_MESSAGE);
        } else {
            list.forEach(e -> {
                String from = e.getFrom();
                String to = e.getTo();
                if (knowledge.isForbiddenByGroups(from, to)) {
                    KnowledgeModelNode fromNode = (KnowledgeModelNode) graph.getNode(from);
                    KnowledgeModelNode toNode = (KnowledgeModelNode) graph.getNode(to);
                    graph.addEdge(new KnowledgeModelEdge(fromNode, toNode, KnowledgeModelEdge.FORBIDDEN_BY_GROUPS));
                }
            });
        }
    }
    if (showRequired) {
        List<KnowledgeEdge> list = knowledge.getListOfExplicitlyRequiredEdges();
        if (list.size() > EDGE_LIMIT) {
            showRequired = false;
            if (checkBox != null) {
                checkBox.setSelected(false);
            }
            String errMsg = String.format("The number of edges to show exceeds the limit %d.", EDGE_LIMIT);
            JOptionPane.showMessageDialog(this, errMsg, "Unable To Display Edges", JOptionPane.ERROR_MESSAGE);
        } else {
            list.forEach(e -> {
                String from = e.getFrom();
                String to = e.getTo();
                KnowledgeModelNode fromNode = (KnowledgeModelNode) graph.getNode(from);
                KnowledgeModelNode toNode = (KnowledgeModelNode) graph.getNode(to);
                if (!(fromNode == null || toNode == null)) {
                    graph.addEdge(new KnowledgeModelEdge(fromNode, toNode, KnowledgeModelEdge.REQUIRED));
                }
            });
        }
    }
    if (showForbiddenByTiers) {
        List<KnowledgeEdge> list = knowledge.getListOfForbiddenEdges();
        if (list.size() > EDGE_LIMIT) {
            showForbiddenByTiers = false;
            if (checkBox != null) {
                checkBox.setSelected(false);
            }
            String errMsg = String.format("The number of edges to show exceeds the limit %d.", EDGE_LIMIT);
            JOptionPane.showMessageDialog(this, errMsg, "Unable To Display Edges", JOptionPane.ERROR_MESSAGE);
        } else {
            list.forEach(e -> {
                String from = e.getFrom();
                String to = e.getTo();
                if (knowledge.isForbiddenByTiers(from, to)) {
                    KnowledgeModelNode fromNode = (KnowledgeModelNode) graph.getNode(from);
                    KnowledgeModelNode toNode = (KnowledgeModelNode) graph.getNode(to);
                    if (fromNode == null) {
                        graph.addNode(new KnowledgeModelNode(from));
                        fromNode = (KnowledgeModelNode) graph.getNode(from);
                    }
                    if (toNode == null) {
                        graph.addNode(new KnowledgeModelNode(to));
                        toNode = (KnowledgeModelNode) graph.getNode(to);
                    }
                    KnowledgeModelEdge knowledgeModelEdge = new KnowledgeModelEdge(fromNode, toNode, KnowledgeModelEdge.FORBIDDEN_BY_TIERS);
                    graph.addEdge(knowledgeModelEdge);
                }
            });
        }
    }
    if (showForbiddenExplicitly) {
        List<KnowledgeEdge> list = knowledge.getListOfExplicitlyForbiddenEdges();
        if (list.size() > EDGE_LIMIT) {
            showForbiddenExplicitly = false;
            if (checkBox != null) {
                checkBox.setSelected(false);
            }
            String errMsg = String.format("The number of edges to show exceeds the limit %d.", EDGE_LIMIT);
            JOptionPane.showMessageDialog(this, errMsg, "Unable To Display Edges", JOptionPane.ERROR_MESSAGE);
        } else {
            list.forEach(e -> {
                String from = e.getFrom();
                String to = e.getTo();
                KnowledgeModelNode fromNode = (KnowledgeModelNode) graph.getNode(from);
                KnowledgeModelNode toNode = (KnowledgeModelNode) graph.getNode(to);
                KnowledgeModelEdge edge = new KnowledgeModelEdge(fromNode, toNode, KnowledgeModelEdge.FORBIDDEN_EXPLICITLY);
                if (!graph.containsEdge(edge)) {
                    graph.addEdge(edge);
                }
            });
        }
    }
    boolean arrangedAll = GraphUtils.arrangeBySourceGraph(graph, edgeWorkbench.getGraph());
    if (!arrangedAll) {
        GraphUtils.circleLayout(graph, 200, 200, 150);
    }
    edgeWorkbench.setGraph(graph);
    if (knowledgeBoxModel != null) {
        notifyKnowledge();
    }
}
Also used : IKnowledge(edu.cmu.tetrad.data.IKnowledge) KnowledgeEdge(edu.cmu.tetrad.data.KnowledgeEdge)

Example 8 with KnowledgeEdge

use of edu.cmu.tetrad.data.KnowledgeEdge in project tetrad by cmu-phil.

the class ProbFci method fciOrientbk.

/**
 * Orients according to background knowledge
 */
private void fciOrientbk(IKnowledge bk, Graph graph, List<Node> variables) {
    logger.log("info", "Starting BK Orientation.");
    for (Iterator<KnowledgeEdge> it = bk.forbiddenEdgesIterator(); it.hasNext(); ) {
        KnowledgeEdge edge = it.next();
        // match strings to variables in the graph.
        Node from = SearchGraphUtils.translate(edge.getFrom(), variables);
        Node to = SearchGraphUtils.translate(edge.getTo(), variables);
        if (from == null || to == null) {
            continue;
        }
        if (graph.getEdge(from, to) == null) {
            continue;
        }
        // Orient to*->from
        graph.setEndpoint(to, from, Endpoint.CIRCLE);
        graph.setEndpoint(from, to, Endpoint.TAIL);
        changeFlag = true;
        logger.log("knowledgeOrientation", SearchLogUtils.edgeOrientedMsg("Knowledge", graph.getEdge(from, to)));
    }
    for (Iterator<KnowledgeEdge> it = bk.requiredEdgesIterator(); it.hasNext(); ) {
        KnowledgeEdge edge = it.next();
        // match strings to variables in this graph
        Node from = SearchGraphUtils.translate(edge.getFrom(), variables);
        Node to = SearchGraphUtils.translate(edge.getTo(), variables);
        if (from == null || to == null) {
            continue;
        }
        if (graph.getEdge(from, to) == null) {
            continue;
        }
        // Orient from*->to (?)
        // Orient from-->to
        // System.out.println("Rule R8: Orienting " + from + "-->" + to);
        graph.setEndpoint(to, from, Endpoint.TAIL);
        graph.setEndpoint(from, to, Endpoint.ARROW);
        changeFlag = true;
        logger.log("knowledgeOrientation", SearchLogUtils.edgeOrientedMsg("Knowledge", graph.getEdge(from, to)));
    }
    logger.log("info", "Finishing BK Orientation.");
}
Also used : KnowledgeEdge(edu.cmu.tetrad.data.KnowledgeEdge)

Example 9 with KnowledgeEdge

use of edu.cmu.tetrad.data.KnowledgeEdge in project tetrad by cmu-phil.

the class Rfci method fciOrientbk.

/**
 * Orients according to background knowledge
 */
private void fciOrientbk(IKnowledge bk, Graph graph, List<Node> variables) {
    logger.log("info", "Starting BK Orientation.");
    for (Iterator<KnowledgeEdge> it = bk.forbiddenEdgesIterator(); it.hasNext(); ) {
        if (Thread.currentThread().isInterrupted()) {
            break;
        }
        KnowledgeEdge edge = it.next();
        // match strings to variables in the graph.
        Node from = SearchGraphUtils.translate(edge.getFrom(), variables);
        Node to = SearchGraphUtils.translate(edge.getTo(), variables);
        if (from == null || to == null) {
            continue;
        }
        if (graph.getEdge(from, to) == null) {
            continue;
        }
        // Orient to*->from
        graph.setEndpoint(to, from, Endpoint.ARROW);
        graph.setEndpoint(from, to, Endpoint.CIRCLE);
        changeFlag = true;
        logger.log("knowledgeOrientation", SearchLogUtils.edgeOrientedMsg("Knowledge", graph.getEdge(from, to)));
    }
    for (Iterator<KnowledgeEdge> it = bk.requiredEdgesIterator(); it.hasNext(); ) {
        if (Thread.currentThread().isInterrupted()) {
            break;
        }
        KnowledgeEdge edge = it.next();
        // match strings to variables in this graph
        Node from = SearchGraphUtils.translate(edge.getFrom(), variables);
        Node to = SearchGraphUtils.translate(edge.getTo(), variables);
        if (from == null || to == null) {
            continue;
        }
        if (graph.getEdge(from, to) == null) {
            continue;
        }
        graph.setEndpoint(to, from, Endpoint.TAIL);
        graph.setEndpoint(from, to, Endpoint.ARROW);
        changeFlag = true;
        logger.log("knowledgeOrientation", SearchLogUtils.edgeOrientedMsg("Knowledge", graph.getEdge(from, to)));
    }
    logger.log("info", "Finishing BK Orientation.");
}
Also used : KnowledgeEdge(edu.cmu.tetrad.data.KnowledgeEdge)

Example 10 with KnowledgeEdge

use of edu.cmu.tetrad.data.KnowledgeEdge in project tetrad by cmu-phil.

the class IonJoeModifications method screenForKnowledge.

private Graph screenForKnowledge(Graph pag) {
    for (Iterator<KnowledgeEdge> it = knowledge.forbiddenEdgesIterator(); it.hasNext(); ) {
        KnowledgeEdge next = it.next();
        Node y = pag.getNode(next.getFrom());
        Node x = pag.getNode(next.getTo());
        if (x == null || y == null) {
            continue;
        }
        Edge edge = pag.getEdge(x, y);
        if (edge == null) {
            continue;
        }
        if (edge.getProximalEndpoint(x) == Endpoint.ARROW && edge.getProximalEndpoint(y) == Endpoint.TAIL) {
            return null;
        } else if (edge.getProximalEndpoint(x) == Endpoint.ARROW && edge.getProximalEndpoint(y) == Endpoint.CIRCLE) {
            pag.removeEdge(edge);
            pag.addEdge(Edges.bidirectedEdge(x, y));
        } else if (edge.getProximalEndpoint(x) == Endpoint.CIRCLE && edge.getProximalEndpoint(y) == Endpoint.CIRCLE) {
            pag.removeEdge(edge);
            pag.addEdge(Edges.partiallyOrientedEdge(x, y));
        }
    }
    for (Iterator<KnowledgeEdge> it = knowledge.requiredEdgesIterator(); it.hasNext(); ) {
        KnowledgeEdge next = it.next();
        Node x = pag.getNode(next.getFrom());
        Node y = pag.getNode(next.getTo());
        if (x == null || y == null) {
            continue;
        }
        Edge edge = pag.getEdge(x, y);
        if (edge == null) {
            return null;
        } else if (edge.getProximalEndpoint(x) == Endpoint.ARROW && edge.getProximalEndpoint(y) == Endpoint.TAIL) {
            return null;
        } else if (edge.getProximalEndpoint(x) == Endpoint.ARROW && edge.getProximalEndpoint(y) == Endpoint.CIRCLE) {
            return null;
        } else if (edge.getProximalEndpoint(x) == Endpoint.CIRCLE && edge.getProximalEndpoint(y) == Endpoint.ARROW) {
            pag.removeEdge(edge);
            pag.addEdge(Edges.directedEdge(x, y));
        } else if (edge.getProximalEndpoint(x) == Endpoint.CIRCLE && edge.getProximalEndpoint(y) == Endpoint.CIRCLE) {
            pag.removeEdge(edge);
            pag.addEdge(Edges.directedEdge(x, y));
        }
    }
    // doFinalOrientation(pag);
    return pag;
}
Also used : KnowledgeEdge(edu.cmu.tetrad.data.KnowledgeEdge) KnowledgeEdge(edu.cmu.tetrad.data.KnowledgeEdge)

Aggregations

KnowledgeEdge (edu.cmu.tetrad.data.KnowledgeEdge)18 Node (edu.cmu.tetrad.graph.Node)6 IKnowledge (edu.cmu.tetrad.data.IKnowledge)3 ContinuousVariable (edu.cmu.tetrad.data.ContinuousVariable)1 Knowledge2 (edu.cmu.tetrad.data.Knowledge2)1 Graph (edu.cmu.tetrad.graph.Graph)1 ArrayList (java.util.ArrayList)1 Iterator (java.util.Iterator)1 Test (org.junit.Test)1