Search in sources :

Example 1 with KnowledgeEdge

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

the class Fges2 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)

Example 2 with KnowledgeEdge

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

the class GFciMax method fciOrientbk.

// ===========================================PRIVATE METHODS=======================================//
/**
 * Orients according to background knowledge
 */
private void fciOrientbk(IKnowledge knowledge, Graph graph, List<Node> variables) {
    logger.log("info", "Starting BK Orientation.");
    for (Iterator<KnowledgeEdge> it = knowledge.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);
        logger.log("knowledgeOrientation", SearchLogUtils.edgeOrientedMsg("Knowledge", graph.getEdge(from, to)));
    }
    for (Iterator<KnowledgeEdge> it = knowledge.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);
        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 3 with KnowledgeEdge

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

the class TsFciOrient 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 4 with KnowledgeEdge

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

the class TestKnowledge method test1.

@Test
public final void test1() {
    List<Node> nodes1 = new ArrayList<>();
    for (int i1 = 0; i1 < 10; i1++) {
        nodes1.add(new ContinuousVariable("X" + (i1 + 1)));
    }
    Graph g = GraphUtils.randomGraph(nodes1, 0, 10, 3, 3, 3, false);
    g.getNode("X1").setName("X1.1");
    g.getNode("X2").setName("X2-1");
    List<Node> nodes = g.getNodes();
    List<String> varNames = new ArrayList<>();
    for (Node node : nodes) {
        varNames.add(node.getName());
    }
    IKnowledge knowledge = new Knowledge2(varNames);
    knowledge.addToTier(0, "X1.*1");
    knowledge.addToTier(0, "X2-1");
    knowledge.addToTier(1, "X3");
    knowledge.setForbidden("X4", "X5");
    knowledge.setRequired("X6", "X7");
    knowledge.setRequired("X7", "X8");
    assertTrue(knowledge.isForbidden("X4", "X5"));
    assertFalse(knowledge.isForbidden("X1.1", "X2-1"));
    assertTrue(knowledge.isForbidden("X3", "X2-1"));
    assertTrue(knowledge.isRequired("X6", "X7"));
    IKnowledge copy = knowledge.copy();
    assertTrue(copy.isForbidden("X4", "X5"));
    assertFalse(copy.isForbidden("X1", "X2-1"));
    assertTrue(copy.isForbidden("X3", "X2-1"));
    knowledge.setTierForbiddenWithin(0, true);
    assertTrue(knowledge.isForbidden("X1.1", "X2-1"));
    assertTrue(knowledge.isForbidden("X2-1", "X1.1"));
    assertFalse(knowledge.isForbidden("X1.1", "X1.1"));
    boolean found = false;
    for (Iterator i = knowledge.forbiddenEdgesIterator(); i.hasNext(); ) {
        KnowledgeEdge edge = (KnowledgeEdge) i.next();
        if (edge.getFrom().equals("X1.1") && edge.getTo().equals("X2-1")) {
            found = true;
        }
    }
    assertTrue(found);
    knowledge.setTierForbiddenWithin(0, false);
    assertFalse(knowledge.isForbidden("X1.1", "X2-1"));
    assertFalse(knowledge.isForbidden("X2-1", "X1.1"));
    assertFalse(knowledge.isForbidden("X1.1", "X1.1"));
}
Also used : KnowledgeEdge(edu.cmu.tetrad.data.KnowledgeEdge) Node(edu.cmu.tetrad.graph.Node) ArrayList(java.util.ArrayList) Knowledge2(edu.cmu.tetrad.data.Knowledge2) ContinuousVariable(edu.cmu.tetrad.data.ContinuousVariable) IKnowledge(edu.cmu.tetrad.data.IKnowledge) Graph(edu.cmu.tetrad.graph.Graph) Iterator(java.util.Iterator) Test(org.junit.Test)

Example 5 with KnowledgeEdge

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

the class Fci 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);
        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);
        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)

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