Search in sources :

Example 56 with ChoiceGenerator

use of edu.cmu.tetrad.util.ChoiceGenerator in project tetrad by cmu-phil.

the class MeekRulesRestricted method meekR3.

/**
 * Meek's rule R3. If a--b, a--c, a--d, c-->b, d-->b, then orient a-->b.
 */
private void meekR3(Node a, Graph graph, IKnowledge knowledge) {
    List<Node> adjacentNodes = graph.getAdjacentNodes(a);
    visitedNodes.add(a);
    if (adjacentNodes.size() < 3) {
        return;
    }
    for (Node b : adjacentNodes) {
        List<Node> otherAdjacents = new LinkedList<>(adjacentNodes);
        otherAdjacents.remove(b);
        if (!graph.isUndirectedFromTo(a, b)) {
            continue;
        }
        ChoiceGenerator cg = new ChoiceGenerator(otherAdjacents.size(), 2);
        int[] combination;
        while ((combination = cg.next()) != null) {
            Node c = otherAdjacents.get(combination[0]);
            Node d = otherAdjacents.get(combination[1]);
            if (graph.isAdjacentTo(c, d)) {
                continue;
            }
            if (!graph.isDirectedFromTo(c, a)) {
                continue;
            }
            if (!graph.isDirectedFromTo(d, a)) {
                continue;
            }
            if (graph.isUndirectedFromTo(b, c) && graph.isUndirectedFromTo(b, d)) {
                if (isArrowpointAllowed(b, a, knowledge, graph) && !createsCycle(b, a, graph)) {
                    if (!isUnshieldedNoncollider(c, b, d, graph)) {
                        continue;
                    }
                    Edge after = direct(b, a, graph);
                    Node x = after.getNode1();
                    Node y = after.getNode2();
                    rule1Queue.add(y);
                    rule2Queue.add(y);
                    rule3Queue.add(x);
                    if (useRule4) {
                        rule4Queue.add(x);
                    }
                    TetradLogger.getInstance().log("impliedOrientations", SearchLogUtils.edgeOrientedMsg("Meek R3", graph.getEdge(a, b)));
                // continue;
                }
            }
        }
    }
}
Also used : ChoiceGenerator(edu.cmu.tetrad.util.ChoiceGenerator)

Example 57 with ChoiceGenerator

use of edu.cmu.tetrad.util.ChoiceGenerator in project tetrad by cmu-phil.

the class MeekRulesRestricted method meekR1Locally.

// private List<Node> getColliderNodes(Graph graph) {
// if (colliderNodes != null) {
// List<Node> nodes = new ArrayList<Node>();
// 
// for (Node node : colliderNodes) {
// nodes.add(node);
// }
// 
// return nodes;
// }
// 
// List<Node> colliderNodes = new ArrayList<Node>();
// 
// NODES:
// for (Node y : graph.getNodes()) {
// List<Node> adj = graph.getAdjacentNodes(y);
// 
// int numInto = 0;
// 
// for (Node x : adj) {
// if (graph.isDirectedFromTo(x, y)) numInto++;
// if (numInto == 2) {
// colliderNodes.add(y);
// continue NODES;
// }
// }
// }
// 
// return colliderNodes;
// }
/**
 * Meek's rule R1: if b-->a, a---c, and a not adj to c, then a-->c
 */
private void meekR1Locally(Node a, Graph graph, IKnowledge knowledge) {
    List<Node> adjacentNodes = graph.getAdjacentNodes(a);
    visitedNodes.add(a);
    if (adjacentNodes.size() < 2) {
        return;
    }
    ChoiceGenerator cg = new ChoiceGenerator(adjacentNodes.size(), 2);
    int[] combination;
    while ((combination = cg.next()) != null) {
        Node b = adjacentNodes.get(combination[0]);
        Node c = adjacentNodes.get(combination[1]);
        // Skip triples that are shielded.
        if (graph.isAdjacentTo(b, c)) {
            continue;
        }
        if (graph.isDirectedFromTo(b, a) && graph.isUndirectedFromTo(a, c)) {
            if (!isUnshieldedNoncollider(b, a, c, graph)) {
                continue;
            }
            if (isArrowpointAllowed(a, c, knowledge, graph) && !createsCycle(a, c, graph)) {
                Edge after = direct(a, c, graph);
                Node x = after.getNode1();
                Node y = after.getNode2();
                // rule2Queue.add(x);
                // rule3Queue.add(x);
                rule1Queue.add(y);
                rule2Queue.add(y);
                rule3Queue.add(x);
                if (useRule4) {
                    rule4Queue.add(x);
                }
                TetradLogger.getInstance().log("impliedOrientations", SearchLogUtils.edgeOrientedMsg("Meek R1 triangle (" + b + "-->" + a + "---" + c + ")", graph.getEdge(a, c)));
            }
        } else if (graph.isDirectedFromTo(c, a) && graph.isUndirectedFromTo(a, b)) {
            if (!isUnshieldedNoncollider(b, a, c, graph)) {
                continue;
            }
            if (isArrowpointAllowed(a, b, knowledge, graph) && !createsCycle(a, b, graph)) {
                Edge after = direct(a, b, graph);
                Node x = after.getNode1();
                Node y = after.getNode2();
                rule1Queue.add(y);
                rule2Queue.add(y);
                rule3Queue.add(x);
                if (useRule4) {
                    rule4Queue.add(x);
                }
                TetradLogger.getInstance().log("impliedOrientations", SearchLogUtils.edgeOrientedMsg("Meek R1 (" + c + "-->" + a + "---" + b + ")", graph.getEdge(a, b)));
            }
        }
    }
}
Also used : ChoiceGenerator(edu.cmu.tetrad.util.ChoiceGenerator)

Example 58 with ChoiceGenerator

use of edu.cmu.tetrad.util.ChoiceGenerator in project tetrad by cmu-phil.

the class MeekRulesRestricted method meekR4.

private void meekR4(Node a, Graph graph, IKnowledge knowledge) {
    if (!useRule4) {
        return;
    }
    List<Node> adjacentNodes = graph.getAdjacentNodes(a);
    visitedNodes.add(a);
    if (adjacentNodes.size() < 3) {
        return;
    }
    for (Node d : adjacentNodes) {
        if (!graph.isAdjacentTo(d, a)) {
            continue;
        }
        List<Node> otherAdjacents = new LinkedList<>(adjacentNodes);
        otherAdjacents.remove(d);
        ChoiceGenerator cg = new ChoiceGenerator(otherAdjacents.size(), 2);
        int[] combination;
        while ((combination = cg.next()) != null) {
            Node b = otherAdjacents.get(combination[0]);
            Node c = otherAdjacents.get(combination[1]);
            if (graph.isDirectedFromTo(b, a) && graph.isDirectedFromTo(a, c)) {
                if (graph.isUndirectedFromTo(d, b) && graph.isUndirectedFromTo(d, c)) {
                    if (!isUnshieldedNoncollider(c, d, b, graph)) {
                        continue;
                    }
                    if (isArrowpointAllowed(d, c, knowledge, graph) && !createsCycle(d, c, graph)) {
                        Edge after = direct(d, c, graph);
                        Node x = after.getNode1();
                        Node y = after.getNode2();
                        rule1Queue.add(y);
                        rule2Queue.add(y);
                        rule3Queue.add(x);
                        if (useRule4) {
                            rule4Queue.add(x);
                        }
                        TetradLogger.getInstance().log("impliedOientations", SearchLogUtils.edgeOrientedMsg("Meek T1", graph.getEdge(a, c)));
                    // continue;
                    }
                }
            } else if (graph.isDirectedFromTo(c, a) && graph.isDirectedFromTo(a, b)) {
                if (graph.isUndirectedFromTo(d, b) && graph.isUndirectedFromTo(d, c)) {
                    if (!isUnshieldedNoncollider(c, d, b, graph)) {
                        continue;
                    }
                    if (isArrowpointAllowed(d, c, knowledge, graph) && !createsCycle(d, c, graph)) {
                        Edge after = direct(d, c, graph);
                        Node x = after.getNode1();
                        Node y = after.getNode2();
                        rule1Queue.add(y);
                        rule2Queue.add(y);
                        rule3Queue.add(x);
                        if (useRule4) {
                            rule4Queue.add(x);
                        }
                        TetradLogger.getInstance().log("impliedOientations", SearchLogUtils.edgeOrientedMsg("Meek T1", graph.getEdge(a, c)));
                    // continue;
                    }
                }
            }
        }
    }
}
Also used : ChoiceGenerator(edu.cmu.tetrad.util.ChoiceGenerator)

Example 59 with ChoiceGenerator

use of edu.cmu.tetrad.util.ChoiceGenerator in project tetrad by cmu-phil.

the class IonJoeModifications method awayFromAncestorCycle.

// Does only the ancestor and cycle rules of these repeatedly until no changes
private void awayFromAncestorCycle(Graph graph) {
    while (changeFlag) {
        changeFlag = false;
        List<Node> nodes = graph.getNodes();
        for (Node B : nodes) {
            List<Node> adj = graph.getAdjacentNodes(B);
            if (adj.size() < 2) {
                continue;
            }
            ChoiceGenerator cg = new ChoiceGenerator(adj.size(), 2);
            int[] combination;
            while ((combination = cg.next()) != null) {
                Node A = adj.get(combination[0]);
                Node C = adj.get(combination[1]);
                // choice gen doesnt do diff orders, so must switch A & C around.
                awayFromAncestor(graph, A, B, C);
                awayFromAncestor(graph, C, B, A);
                awayFromCycle(graph, A, B, C);
                awayFromCycle(graph, C, B, A);
            }
        }
    }
    changeFlag = true;
}
Also used : ChoiceGenerator(edu.cmu.tetrad.util.ChoiceGenerator)

Example 60 with ChoiceGenerator

use of edu.cmu.tetrad.util.ChoiceGenerator in project tetrad by cmu-phil.

the class Mbfs method getTripleType.

private TripleType getTripleType(Graph graph, Node x, Node y, Node z, IndependenceTest test, int depth) {
    boolean existsSepsetContainingY = false;
    boolean existsSepsetNotContainingY = false;
    Set<Node> __nodes = new HashSet<>(graph.getAdjacentNodes(x));
    __nodes.remove(z);
    List<Node> _nodes = new LinkedList<>(__nodes);
    int _depth = depth;
    if (_depth == -1) {
        _depth = Integer.MAX_VALUE;
    }
    _depth = Math.min(_depth, _nodes.size());
    for (int d = 0; d <= _depth; d++) {
        if (Thread.currentThread().isInterrupted()) {
            break;
        }
        ChoiceGenerator cg = new ChoiceGenerator(_nodes.size(), d);
        int[] choice;
        while ((choice = cg.next()) != null) {
            if (Thread.currentThread().isInterrupted()) {
                break;
            }
            List<Node> condSet = asList(choice, _nodes);
            if (independent(x, z, condSet)) {
                if (condSet.contains(y)) {
                    existsSepsetContainingY = true;
                } else {
                    existsSepsetNotContainingY = true;
                }
            }
        }
    }
    __nodes = new HashSet<>(graph.getAdjacentNodes(z));
    __nodes.remove(x);
    _nodes = new LinkedList<>(__nodes);
    _depth = depth;
    if (_depth == -1) {
        _depth = Integer.MAX_VALUE;
    }
    _depth = Math.min(_depth, _nodes.size());
    for (int d = 0; d <= _depth; d++) {
        if (Thread.currentThread().isInterrupted()) {
            break;
        }
        ChoiceGenerator cg = new ChoiceGenerator(_nodes.size(), d);
        int[] choice;
        while ((choice = cg.next()) != null) {
            if (Thread.currentThread().isInterrupted()) {
                break;
            }
            List<Node> condSet = asList(choice, _nodes);
            if (independent(x, z, condSet)) {
                if (condSet.contains(y)) {
                    existsSepsetContainingY = true;
                } else {
                    existsSepsetNotContainingY = true;
                }
            }
        }
    }
    if (existsSepsetContainingY == existsSepsetNotContainingY) {
        return TripleType.AMBIGUOUS;
    } else if (!existsSepsetNotContainingY) {
        return TripleType.NONCOLLIDER;
    } else {
        return TripleType.COLLIDER;
    }
}
Also used : ChoiceGenerator(edu.cmu.tetrad.util.ChoiceGenerator) DepthChoiceGenerator(edu.cmu.tetrad.util.DepthChoiceGenerator)

Aggregations

ChoiceGenerator (edu.cmu.tetrad.util.ChoiceGenerator)161 Node (edu.cmu.tetrad.graph.Node)37 ArrayList (java.util.ArrayList)20 DepthChoiceGenerator (edu.cmu.tetrad.util.DepthChoiceGenerator)15 List (java.util.List)11 LinkedList (java.util.LinkedList)10 ExecutorService (java.util.concurrent.ExecutorService)5 NumberFormat (java.text.NumberFormat)2 HashSet (java.util.HashSet)2 StringTokenizer (java.util.StringTokenizer)2 Test (org.junit.Test)2 DataSet (edu.cmu.tetrad.data.DataSet)1 Triple (edu.cmu.tetrad.graph.Triple)1 DeltaSextadTest (edu.cmu.tetrad.search.DeltaSextadTest)1 IndependenceTest (edu.cmu.tetrad.search.IndependenceTest)1 IntSextad (edu.cmu.tetrad.search.IntSextad)1 SemIm (edu.cmu.tetrad.sem.SemIm)1 IndependenceResult (edu.cmu.tetradapp.model.IndependenceResult)1 Iterator (java.util.Iterator)1 SortedSet (java.util.SortedSet)1