Search in sources :

Example 16 with KnowledgeEdge

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

the class FciOrientT 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.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(); ) {
        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 17 with KnowledgeEdge

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

the class FgesMb method addRequiredEdges.

// Adds edges required by knowledge.
private void addRequiredEdges(Graph graph) {
    if (!existsKnowledge()) {
        return;
    }
    for (Iterator<KnowledgeEdge> it = getKnowledge().requiredEdgesIterator(); it.hasNext(); ) {
        KnowledgeEdge next = it.next();
        Node nodeA = graph.getNode(next.getFrom());
        Node nodeB = graph.getNode(next.getTo());
        if (!graph.isAncestorOf(nodeB, nodeA)) {
            graph.removeEdges(nodeA, nodeB);
            graph.addDirectedEdge(nodeA, nodeB);
            TetradLogger.getInstance().log("insertedEdges", "Adding edge by knowledge: " + graph.getEdge(nodeA, nodeB));
        }
    }
    for (Edge edge : graph.getEdges()) {
        if (Thread.currentThread().isInterrupted()) {
            break;
        }
        final String A = edge.getNode1().getName();
        final String B = edge.getNode2().getName();
        if (knowledge.isForbidden(A, B)) {
            Node nodeA = edge.getNode1();
            Node nodeB = edge.getNode2();
            if (nodeA == null || nodeB == null) {
                throw new NullPointerException();
            }
            if (graph.isAdjacentTo(nodeA, nodeB) && !graph.isChildOf(nodeA, nodeB)) {
                if (!graph.isAncestorOf(nodeA, nodeB)) {
                    graph.removeEdges(nodeA, nodeB);
                    graph.addDirectedEdge(nodeB, nodeA);
                    TetradLogger.getInstance().log("insertedEdges", "Adding edge by knowledge: " + graph.getEdge(nodeB, nodeA));
                }
            }
            if (!graph.isChildOf(nodeA, nodeB) && getKnowledge().isForbidden(nodeA.getName(), nodeB.getName())) {
                if (!graph.isAncestorOf(nodeA, nodeB)) {
                    graph.removeEdges(nodeA, nodeB);
                    graph.addDirectedEdge(nodeB, nodeA);
                    TetradLogger.getInstance().log("insertedEdges", "Adding edge by knowledge: " + graph.getEdge(nodeB, nodeA));
                }
            }
        } else if (knowledge.isForbidden(B, A)) {
            Node nodeA = edge.getNode2();
            Node nodeB = edge.getNode1();
            if (nodeA == null || nodeB == null) {
                throw new NullPointerException();
            }
            if (graph.isAdjacentTo(nodeA, nodeB) && !graph.isChildOf(nodeA, nodeB)) {
                if (!graph.isAncestorOf(nodeA, nodeB)) {
                    graph.removeEdges(nodeA, nodeB);
                    graph.addDirectedEdge(nodeB, nodeA);
                    TetradLogger.getInstance().log("insertedEdges", "Adding edge by knowledge: " + graph.getEdge(nodeB, nodeA));
                }
            }
            if (!graph.isChildOf(nodeA, nodeB) && getKnowledge().isForbidden(nodeA.getName(), nodeB.getName())) {
                if (!graph.isAncestorOf(nodeA, nodeB)) {
                    graph.removeEdges(nodeA, nodeB);
                    graph.addDirectedEdge(nodeB, nodeA);
                    TetradLogger.getInstance().log("insertedEdges", "Adding edge by knowledge: " + graph.getEdge(nodeB, nodeA));
                }
            }
        }
    }
}
Also used : KnowledgeEdge(edu.cmu.tetrad.data.KnowledgeEdge) KnowledgeEdge(edu.cmu.tetrad.data.KnowledgeEdge)

Example 18 with KnowledgeEdge

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

the class FgesD method addRequiredEdges.

// Adds edges required by knowledge.
private void addRequiredEdges(Graph graph) {
    if (!existsKnowledge())
        return;
    for (Iterator<KnowledgeEdge> it = getKnowledge().requiredEdgesIterator(); it.hasNext(); ) {
        KnowledgeEdge next = it.next();
        Node nodeA = graph.getNode(next.getFrom());
        Node nodeB = graph.getNode(next.getTo());
        if (!graph.isAncestorOf(nodeB, nodeA)) {
            graph.removeEdges(nodeA, nodeB);
            graph.addDirectedEdge(nodeA, nodeB);
            TetradLogger.getInstance().log("insertedEdges", "Adding edge by knowledge: " + graph.getEdge(nodeA, nodeB));
        }
    }
    for (Edge edge : graph.getEdges()) {
        final String A = edge.getNode1().getName();
        final String B = edge.getNode2().getName();
        if (knowledge.isForbidden(A, B)) {
            Node nodeA = edge.getNode1();
            Node nodeB = edge.getNode2();
            if (nodeA == null || nodeB == null)
                throw new NullPointerException();
            if (graph.isAdjacentTo(nodeA, nodeB) && !graph.isChildOf(nodeA, nodeB)) {
                if (!graph.isAncestorOf(nodeA, nodeB)) {
                    graph.removeEdges(nodeA, nodeB);
                    graph.addDirectedEdge(nodeB, nodeA);
                    TetradLogger.getInstance().log("insertedEdges", "Adding edge by knowledge: " + graph.getEdge(nodeB, nodeA));
                }
            }
            if (!graph.isChildOf(nodeA, nodeB) && getKnowledge().isForbidden(nodeA.getName(), nodeB.getName())) {
                if (!graph.isAncestorOf(nodeA, nodeB)) {
                    graph.removeEdges(nodeA, nodeB);
                    graph.addDirectedEdge(nodeB, nodeA);
                    TetradLogger.getInstance().log("insertedEdges", "Adding edge by knowledge: " + graph.getEdge(nodeB, nodeA));
                }
            }
        } else if (knowledge.isForbidden(B, A)) {
            Node nodeA = edge.getNode2();
            Node nodeB = edge.getNode1();
            if (nodeA == null || nodeB == null)
                throw new NullPointerException();
            if (graph.isAdjacentTo(nodeA, nodeB) && !graph.isChildOf(nodeA, nodeB)) {
                if (!graph.isAncestorOf(nodeA, nodeB)) {
                    graph.removeEdges(nodeA, nodeB);
                    graph.addDirectedEdge(nodeB, nodeA);
                    TetradLogger.getInstance().log("insertedEdges", "Adding edge by knowledge: " + graph.getEdge(nodeB, nodeA));
                }
            }
            if (!graph.isChildOf(nodeA, nodeB) && getKnowledge().isForbidden(nodeA.getName(), nodeB.getName())) {
                if (!graph.isAncestorOf(nodeA, nodeB)) {
                    graph.removeEdges(nodeA, nodeB);
                    graph.addDirectedEdge(nodeB, nodeA);
                    TetradLogger.getInstance().log("insertedEdges", "Adding edge by knowledge: " + graph.getEdge(nodeB, nodeA));
                }
            }
        }
    }
}
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