Search in sources :

Example 41 with ChoiceGenerator

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

the class LayeredDrawing method numCrossings.

private int numCrossings(List<Node> tier1, List<Node> tier2, Graph graph) {
    if (tier2.size() < 2) {
        return 0;
    }
    ChoiceGenerator cg = new ChoiceGenerator(tier2.size(), 2);
    int[] choice;
    int numCrossings = 0;
    while ((choice = cg.next()) != null) {
        List<Node> list1 = graph.getAdjacentNodes(tier2.get(choice[0]));
        List<Node> list2 = graph.getAdjacentNodes(tier2.get(choice[1]));
        list1.retainAll(tier1);
        list2.retainAll(tier1);
        for (Node node0 : list1) {
            for (Node node1 : list2) {
                if (list1.indexOf(node0) > list1.indexOf(node1)) {
                    numCrossings++;
                }
            }
        }
    }
    return numCrossings;
}
Also used : ChoiceGenerator(edu.cmu.tetrad.util.ChoiceGenerator)

Example 42 with ChoiceGenerator

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

the class TestFci method ancestral.

private boolean ancestral(Node n, Node q, Graph pag) {
    if (n == q)
        return false;
    if (pag.isAncestorOf(n, q)) {
        return true;
    } else {
        List<Node> adj = uncoveredPotentiallyDirectedPathStarts(n, q, pag, new LinkedList<Node>());
        if (adj.size() >= 2) {
            ChoiceGenerator gen = new ChoiceGenerator(adj.size(), 2);
            int[] choice;
            boolean found = false;
            while ((choice = gen.next()) != null) {
                List<Node> c = GraphUtils.asList(choice, adj);
                Node n1 = c.get(0);
                Node n2 = c.get(1);
                if (!pag.isAdjacentTo(n1, n2)) {
                    if (pag.isDefNoncollider(n1, n, n2)) {
                        found = true;
                    }
                }
            }
            if (found) {
                return true;
            }
        }
    }
    return false;
}
Also used : ChoiceGenerator(edu.cmu.tetrad.util.ChoiceGenerator)

Example 43 with ChoiceGenerator

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

the class FasDeterministic method searchAtDepth.

private boolean searchAtDepth(List<Node> nodes, final IndependenceTest test, Map<Node, Set<Node>> adjacencies, int depth) {
    int numRemoved = 0;
    int count = 0;
    List<IndependenceFact> facts = new ArrayList<>();
    for (Node x : nodes) {
        if (verbose) {
            if (++count % 100 == 0)
                out.println("count " + count + " of " + nodes.size());
        }
        List<Node> adjx = new ArrayList<>(adjacencies.get(x));
        EDGE: for (Node y : adjx) {
            List<Node> _adjx = new ArrayList<>(adjacencies.get(x));
            _adjx.remove(y);
            List<Node> ppx = possibleParents(x, _adjx, knowledge);
            if (ppx.size() >= depth) {
                ChoiceGenerator cg = new ChoiceGenerator(ppx.size(), depth);
                int[] choice;
                while ((choice = cg.next()) != null) {
                    List<Node> condSet = GraphUtils.asList(choice, ppx);
                    IndependenceFact fact = new IndependenceFact(x, y, condSet);
                    if (facts.contains(fact))
                        continue;
                    facts.add(fact);
                    boolean independent;
                    try {
                        numIndependenceTests++;
                        independent = test.isIndependent(x, y, condSet);
                    // if (test.determines(condSet, x) || test.determines(condSet, y)) {
                    // continue;
                    // }
                    } catch (Exception e) {
                        independent = false;
                    }
                    if (independent) {
                        numIndependenceJudgements++;
                    } else {
                        numDependenceJudgement++;
                    }
                    boolean noEdgeRequired = knowledge.noEdgeRequired(x.getName(), y.getName());
                    if (independent && noEdgeRequired) {
                        adjacencies.get(x).remove(y);
                        adjacencies.get(y).remove(x);
                        numRemoved++;
                        getSepsets().set(x, y, condSet);
                        TetradLogger.getInstance().log("independencies", SearchLogUtils.independenceFact(x, y, condSet) + " p = " + nf.format(test.getPValue()));
                        if (verbose) {
                            out.println(SearchLogUtils.independenceFactMsg(x, y, condSet, test.getPValue()));
                        }
                        continue EDGE;
                    } else {
                    // if (verbose) {
                    // out.println(SearchLogUtils.dependenceFactMsg(x, y, condSet, test.getScore()));
                    // }
                    }
                }
            }
        }
    }
    return freeDegree(nodes, adjacencies) > depth;
}
Also used : ChoiceGenerator(edu.cmu.tetrad.util.ChoiceGenerator)

Example 44 with ChoiceGenerator

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

the class FciOrient method rulesR1R2cycle.

// Does all 3 of these rules at once instead of going through all
// triples multiple times per iteration of doFinalOrientation.
public void rulesR1R2cycle(Graph graph) {
    List<Node> nodes = graph.getNodes();
    for (Node B : nodes) {
        if (Thread.currentThread().isInterrupted()) {
            break;
        }
        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 && !Thread.currentThread().isInterrupted()) {
            if (Thread.currentThread().isInterrupted()) {
                break;
            }
            Node A = adj.get(combination[0]);
            Node C = adj.get(combination[1]);
            // choice gen doesnt do diff orders, so must switch A & C around.
            ruleR1(A, B, C, graph);
            ruleR1(C, B, A, graph);
            ruleR2(A, B, C, graph);
            ruleR2(C, B, A, graph);
        }
    }
}
Also used : Node(edu.cmu.tetrad.graph.Node) ChoiceGenerator(edu.cmu.tetrad.util.ChoiceGenerator)

Example 45 with ChoiceGenerator

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

the class FciOrient method ruleR3.

// /**
// * Implements the double-triangle orientation rule, which states that if D*-oB, A*->B<-*C and A*-oDo-*C, then
// * D*->B.
// * <p/>
// * This is Zhang's rule R3.
// */
// private void ruleR3(Graph graph) {
// List<Node> nodes = graph.getNodes();
// 
// for (Node d : nodes) {
// final List<Node> adjacentNodes = graph.getAdjacentNodes(d);
// 
// if (adjacentNodes.size() < 3) {
// continue;
// }
// 
// ChoiceGenerator gen = new ChoiceGenerator(adjacentNodes.size(), 3);
// int[] choice;
// 
// while ((choice = gen.next()) != null) {
// List<Node> adj = DataGraphUtils.asList(choice, adjacentNodes);
// 
// Node a = adj.get(0);
// Node b = adj.get(1);
// Node c = adj.get(2);
// 
// if (graph.getEndpoint(a, b) == Endpoint.ARROW && graph.getEndpoint(c, b) == Endpoint.ARROW
// && graph.getEndpoint(a, d) == Endpoint.CIRCLE && graph.getEndpoint(c, d) == Endpoint.ARROW
// && !graph.isAdjacentTo(a, c) && graph.getEndpoint(d, b) == Endpoint.CIRCLE) {
// graph.setEndpoint(d, b, Endpoint.ARROW);
// }
// }
// }
// }
/**
 * Implements the double-triangle orientation rule, which states that if
 * D*-oB, A*->B<-*C and A*-oDo-*C, then
 * D*->B.
 * <p>
 * This is Zhang's rule R3.
 */
public void ruleR3(Graph graph) {
    List<Node> nodes = graph.getNodes();
    for (Node B : nodes) {
        if (Thread.currentThread().isInterrupted()) {
            break;
        }
        List<Node> intoBArrows = graph.getNodesInTo(B, Endpoint.ARROW);
        List<Node> intoBCircles = graph.getNodesInTo(B, Endpoint.CIRCLE);
        for (Node D : intoBCircles) {
            if (Thread.currentThread().isInterrupted()) {
                break;
            }
            if (intoBArrows.size() < 2) {
                continue;
            }
            ChoiceGenerator gen = new ChoiceGenerator(intoBArrows.size(), 2);
            int[] choice;
            while ((choice = gen.next()) != null && !Thread.currentThread().isInterrupted()) {
                Node A = intoBArrows.get(choice[0]);
                Node C = intoBArrows.get(choice[1]);
                if (graph.isAdjacentTo(A, C)) {
                    continue;
                }
                if (!graph.isAdjacentTo(A, D) || !graph.isAdjacentTo(C, D)) {
                    continue;
                }
                if (!sepsets.isNoncollider(A, D, C)) {
                    continue;
                }
                if (graph.getEndpoint(A, D) != Endpoint.CIRCLE) {
                    continue;
                }
                if (graph.getEndpoint(C, D) != Endpoint.CIRCLE) {
                    continue;
                }
                if (!isArrowpointAllowed(D, B, graph)) {
                    continue;
                }
                graph.setEndpoint(D, B, Endpoint.ARROW);
                if (verbose) {
                    logger.log("impliedOrientations", SearchLogUtils.edgeOrientedMsg("Double triangle", graph.getEdge(D, B)));
                    out.println(SearchLogUtils.edgeOrientedMsg("Double triangle", graph.getEdge(D, B)));
                }
                changeFlag = true;
            }
        }
    }
}
Also used : Node(edu.cmu.tetrad.graph.Node) ChoiceGenerator(edu.cmu.tetrad.util.ChoiceGenerator)

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