Search in sources :

Example 51 with ChoiceGenerator

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

the class Dci 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 52 with ChoiceGenerator

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

the class CpcOrienter method orientUnshieldedTriples.

// public final Graph orientationForGraph(Dag trueGraph) {
// Graph graph = new EdgeListGraph(independenceTest.getVariable());
// 
// for (Edge edge : trueGraph.getEdges()) {
// Node nodeA = edge.getNode1();
// Node nodeB = edge.getNode2();
// 
// Node _nodeA = independenceTest.getVariable(nodeA.getNode());
// Node _nodeB = independenceTest.getVariable(nodeB.getNode());
// 
// graph.addUndirectedEdge(_nodeA, _nodeB);
// }
// 
// SearchGraphUtils.pcOrientbk(knowledge, graph, graph.getNodes());
// orientUnshieldedTriples(knowledge, getIndependenceTest(), depth);
// MeekRules meekRules = new MeekRules();
// meekRules.setKnowledge(knowledge);
// meekRules.orientImplied(graph);
// 
// return graph;
// }
// ==========================PRIVATE METHODS===========================//
@SuppressWarnings({ "SameParameterValue" })
private void orientUnshieldedTriples(IKnowledge knowledge, IndependenceTest test, int depth) {
    TetradLogger.getInstance().log("info", "Starting Collider Orientation:");
    colliderTriples = new HashSet<>();
    noncolliderTriples = new HashSet<>();
    ambiguousTriples = new HashSet<>();
    for (Node y : graph.getNodes()) {
        List<Node> adjacentNodes = graph.getAdjacentNodes(y);
        if (adjacentNodes.size() < 2) {
            continue;
        }
        ChoiceGenerator cg = new ChoiceGenerator(adjacentNodes.size(), 2);
        int[] combination;
        while ((combination = cg.next()) != null) {
            Node x = adjacentNodes.get(combination[0]);
            Node z = adjacentNodes.get(combination[1]);
            if (this.graph.isAdjacentTo(x, z)) {
                continue;
            }
            allTriples.add(new Triple(x, y, z));
            CpcOrienter.TripleType type = getTripleType(x, y, z, test, depth);
            System.out.println(new Triple(x, y, z) + " " + type);
            if (type == CpcOrienter.TripleType.COLLIDER) {
                if (colliderAllowed(x, y, z, knowledge)) {
                    graph.setEndpoint(x, y, Endpoint.ARROW);
                    graph.setEndpoint(z, y, Endpoint.ARROW);
                    TetradLogger.getInstance().log("colliderOrientations", SearchLogUtils.colliderOrientedMsg(x, y, z));
                }
                colliderTriples.add(new Triple(x, y, z));
            } else if (type == CpcOrienter.TripleType.AMBIGUOUS) {
                Triple triple = new Triple(x, y, z);
                ambiguousTriples.add(triple);
                graph.addAmbiguousTriple(triple.getX(), triple.getY(), triple.getZ());
            } else {
                noncolliderTriples.add(new Triple(x, y, z));
            }
        }
    }
    TetradLogger.getInstance().log("info", "Finishing Collider Orientation.");
}
Also used : ChoiceGenerator(edu.cmu.tetrad.util.ChoiceGenerator)

Example 53 with ChoiceGenerator

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

the class Ccd method doNodeCollider.

private void doNodeCollider(Graph graph, Map<Triple, Double> colliders, Map<Triple, Double> noncolliders, Node b) {
    List<Node> adjacentNodes = graph.getAdjacentNodes(b);
    if (adjacentNodes.size() < 2) {
        return;
    }
    ChoiceGenerator cg = new ChoiceGenerator(adjacentNodes.size(), 2);
    int[] combination;
    while ((combination = cg.next()) != null) {
        Node a = adjacentNodes.get(combination[0]);
        Node c = adjacentNodes.get(combination[1]);
        // Skip triples that are shielded.
        if (graph.isAdjacentTo(a, c)) {
            continue;
        }
        List<Node> adja = graph.getAdjacentNodes(a);
        double score = Double.POSITIVE_INFINITY;
        List<Node> S = null;
        DepthChoiceGenerator cg2 = new DepthChoiceGenerator(adja.size(), -1);
        int[] comb2;
        while ((comb2 = cg2.next()) != null) {
            List<Node> s = GraphUtils.asList(comb2, adja);
            independenceTest.isIndependent(a, c, s);
            double _score = independenceTest.getScore();
            if (_score < score) {
                score = _score;
                S = s;
            }
        }
        List<Node> adjc = graph.getAdjacentNodes(c);
        DepthChoiceGenerator cg3 = new DepthChoiceGenerator(adjc.size(), -1);
        int[] comb3;
        while ((comb3 = cg3.next()) != null) {
            List<Node> s = GraphUtils.asList(comb3, adjc);
            independenceTest.isIndependent(c, a, s);
            double _score = independenceTest.getScore();
            if (_score < score) {
                score = _score;
                S = s;
            }
        }
        // This could happen if there are undefined values and such.
        if (S == null) {
            continue;
        }
        if (S.contains(b)) {
            noncolliders.put(new Triple(a, b, c), score);
        } else {
            colliders.put(new Triple(a, b, c), score);
        }
    }
}
Also used : DepthChoiceGenerator(edu.cmu.tetrad.util.DepthChoiceGenerator) ChoiceGenerator(edu.cmu.tetrad.util.ChoiceGenerator) DepthChoiceGenerator(edu.cmu.tetrad.util.DepthChoiceGenerator)

Example 54 with ChoiceGenerator

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

the class Cefs method prune.

/**
 * Tries node remove the edge node---from using adjacent nodes of node 'from', then tries node remove each other
 * edge adjacent node 'from' using remaining edges adjacent node 'from.' If the edge 'node' is removed, the method
 * immediately returns.
 */
private void prune(Node node, Graph graph, int depth) {
    TetradLogger.getInstance().log("pruning", "Trying to remove edges adjacent to node " + node + ", depth = " + depth + ".");
    // Otherwise, try removing all other edges adjacent node node. Return
    // true if more edges could be removed at the next depth.
    List<Node> a = new LinkedList<>(graph.getAdjacentNodes(node));
    NEXT_EDGE: for (Node y : a) {
        List<Node> adjNode = new LinkedList<>(graph.getAdjacentNodes(node));
        adjNode.remove(y);
        adjNode = possibleParents(node, adjNode);
        if (adjNode.size() < depth) {
            continue;
        }
        ChoiceGenerator cg = new ChoiceGenerator(adjNode.size(), depth);
        int[] choice;
        while ((choice = cg.next()) != null) {
            List<Node> condSet = GraphUtils.asList(choice, adjNode);
            if (independent(node, y, condSet) && !edgeRequired(node, y)) {
                graph.removeEdge(node, y);
                // The target itself must not be removed.
                if (graph.getEdges(y).isEmpty() && y != getTarget()) {
                    graph.removeNode(y);
                }
                continue NEXT_EDGE;
            }
        }
    }
    int numAdjacents = graph.getAdjacentNodes(node).size();
    noteMaxAtDepth(depth, numAdjacents, node);
}
Also used : ChoiceGenerator(edu.cmu.tetrad.util.ChoiceGenerator)

Example 55 with ChoiceGenerator

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

the class BffBeam method getRemoveTriangleMoves.

private List<Move> getRemoveTriangleMoves(Graph graph) {
    List<Move> moves = new ArrayList<>();
    for (Node b : graph.getNodes()) {
        List<Node> adj = graph.getAdjacentNodes(b);
        if (adj.size() < 2)
            continue;
        ChoiceGenerator gen = new ChoiceGenerator(adj.size(), 2);
        int[] choice;
        while ((choice = gen.next()) != null) {
            List<Node> set = GraphUtils.asList(choice, adj);
            Node a = set.get(0);
            Node c = set.get(1);
            Edge edge1 = graph.getEdge(a, b);
            Edge edge2 = graph.getEdge(b, c);
            Edge edge3 = graph.getEdge(a, c);
            if (edge1 != null && edge2 != null && edge3 != null && edge1.pointsTowards(a) && edge3.pointsTowards(c) && edge2.pointsTowards(c)) {
                moves.add(new Move(Edges.directedEdge(b, c), Edges.directedEdge(c, a), Move.Type.SWAP));
            } else if (edge1 != null && edge2 != null && edge3 != null && edge3.pointsTowards(a) && edge1.pointsTowards(b) && edge2.pointsTowards(b)) {
                moves.add(new Move(Edges.directedEdge(b, c), Edges.directedEdge(b, a), Move.Type.SWAP));
            }
        }
    }
    return moves;
}
Also used : 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