Search in sources :

Example 1 with DagInPatternIterator

use of edu.cmu.tetrad.search.DagInPatternIterator in project tetrad by cmu-phil.

the class TestDagInPatternIterator method test4.

@Test
public void test4() {
    Graph pattern = new EdgeListGraph();
    Node x1 = new GraphNode("X1");
    Node x2 = new GraphNode("X2");
    Node x3 = new GraphNode("X3");
    Node x4 = new GraphNode("X4");
    Node x5 = new GraphNode("X5");
    Node x6 = new GraphNode("X6");
    pattern.addNode(x1);
    pattern.addNode(x2);
    pattern.addNode(x3);
    pattern.addNode(x4);
    pattern.addNode(x5);
    pattern.addNode(x6);
    pattern.addDirectedEdge(x5, x1);
    pattern.addDirectedEdge(x3, x1);
    pattern.addDirectedEdge(x3, x4);
    pattern.addDirectedEdge(x6, x5);
    pattern.addUndirectedEdge(x1, x6);
    pattern.addUndirectedEdge(x4, x6);
    DagInPatternIterator iterator = new DagInPatternIterator(pattern);
    int count = 0;
    while (iterator.hasNext()) {
        iterator.next();
        count++;
    }
    assertEquals(1, count);
}
Also used : DagInPatternIterator(edu.cmu.tetrad.search.DagInPatternIterator) Test(org.junit.Test)

Example 2 with DagInPatternIterator

use of edu.cmu.tetrad.search.DagInPatternIterator in project tetrad by cmu-phil.

the class TestDagInPatternIterator method test2.

@Test
public void test2() {
    Graph pattern = new EdgeListGraph();
    Node x = new GraphNode("X");
    Node y = new GraphNode("Y");
    pattern.addNode(x);
    pattern.addNode(y);
    pattern.addDirectedEdge(x, y);
    DagInPatternIterator iterator = new DagInPatternIterator(pattern);
    int count = 0;
    while (iterator.hasNext()) {
        iterator.next();
        count++;
    }
    assertEquals(1, count);
}
Also used : DagInPatternIterator(edu.cmu.tetrad.search.DagInPatternIterator) Test(org.junit.Test)

Example 3 with DagInPatternIterator

use of edu.cmu.tetrad.search.DagInPatternIterator in project tetrad by cmu-phil.

the class TestDagInPatternIterator method test1.

@Test
public void test1() {
    List<Node> nodes = new ArrayList<>();
    for (int i = 0; i < 10; i++) {
        nodes.add(new GraphNode("X" + (i + 1)));
    }
    RandomUtil.getInstance().setSeed(342233L);
    Graph graph = GraphUtils.randomGraphRandomForwardEdges(nodes, 0, 10, 3, 3, 3, false, true);
    Graph p = new EdgeListGraphSingleConnections(graph);
    Dag dag = new Dag(graph);
    Graph pattern = SearchGraphUtils.patternFromDag(graph);
    System.out.println(pattern);
    DagInPatternIterator iterator = new DagInPatternIterator(pattern);
    int count = 0;
    while (iterator.hasNext()) {
        iterator.next();
        count++;
    }
    assertEquals(6, count);
}
Also used : ArrayList(java.util.ArrayList) DagInPatternIterator(edu.cmu.tetrad.search.DagInPatternIterator) Test(org.junit.Test)

Example 4 with DagInPatternIterator

use of edu.cmu.tetrad.search.DagInPatternIterator in project tetrad by cmu-phil.

the class TestDagInPatternIterator method test5.

@Test
public void test5() {
    RandomUtil.getInstance().setSeed(34828384L);
    List<Node> nodes1 = new ArrayList<>();
    for (int i = 0; i < 3; i++) {
        nodes1.add(new ContinuousVariable("X" + (i + 1)));
    }
    Dag dag1 = new Dag(GraphUtils.randomGraph(nodes1, 0, 3, 30, 15, 15, false));
    Graph pattern = SearchGraphUtils.patternForDag(dag1);
    List<Node> nodes = pattern.getNodes();
    // Make random knowedge.
    int numTiers = 6;
    IKnowledge knowledge = new Knowledge2();
    for (Node node : nodes) {
        int tier = RandomUtil.getInstance().nextInt(numTiers);
        if (tier < 2)
            continue;
        knowledge.addToTier(tier, node.getName());
    }
    if (!knowledge.isViolatedBy(pattern)) {
        DagInPatternIterator iterator1 = new DagInPatternIterator(pattern);
        Graph dag0 = null;
        while (iterator1.hasNext()) {
            Graph dag = iterator1.next();
            if (!knowledge.isViolatedBy(dag)) {
                dag0 = dag;
            }
        }
        if (dag0 == null) {
            fail("Inconsistent knowledge.");
        }
    }
    if (!knowledge.isViolatedBy(pattern)) {
        DagInPatternIterator iterator2 = new DagInPatternIterator(pattern, knowledge);
        while (iterator2.hasNext()) {
            Graph dag = iterator2.next();
            if (knowledge.isViolatedBy(dag)) {
                throw new IllegalArgumentException("Knowledge violated");
            }
        }
    }
    DagInPatternIterator iterator3 = new DagInPatternIterator(pattern);
    int count = 0;
    while (iterator3.hasNext()) {
        iterator3.next();
        count++;
    }
    assertEquals(6, count);
}
Also used : ContinuousVariable(edu.cmu.tetrad.data.ContinuousVariable) IKnowledge(edu.cmu.tetrad.data.IKnowledge) ArrayList(java.util.ArrayList) Knowledge2(edu.cmu.tetrad.data.Knowledge2) DagInPatternIterator(edu.cmu.tetrad.search.DagInPatternIterator) Test(org.junit.Test)

Example 5 with DagInPatternIterator

use of edu.cmu.tetrad.search.DagInPatternIterator in project tetrad by cmu-phil.

the class TestDagInPatternIterator method test3.

@Test
public void test3() {
    Graph pattern = new EdgeListGraph();
    Node x1 = new GraphNode("X1");
    Node x2 = new GraphNode("X2");
    Node x3 = new GraphNode("X3");
    Node x4 = new GraphNode("X4");
    Node x5 = new GraphNode("X5");
    Node x6 = new GraphNode("X6");
    pattern.addNode(x1);
    pattern.addNode(x2);
    pattern.addNode(x3);
    pattern.addNode(x4);
    pattern.addNode(x5);
    pattern.addNode(x6);
    pattern.addDirectedEdge(x5, x1);
    pattern.addDirectedEdge(x3, x1);
    pattern.addDirectedEdge(x3, x4);
    pattern.addDirectedEdge(x6, x5);
    pattern.addUndirectedEdge(x1, x6);
    pattern.addUndirectedEdge(x4, x6);
    DagInPatternIterator iterator = new DagInPatternIterator(pattern);
    int count = 0;
    while (iterator.hasNext()) {
        iterator.next();
        count++;
    }
    assertEquals(1, count);
}
Also used : DagInPatternIterator(edu.cmu.tetrad.search.DagInPatternIterator) Test(org.junit.Test)

Aggregations

DagInPatternIterator (edu.cmu.tetrad.search.DagInPatternIterator)5 Test (org.junit.Test)5 ArrayList (java.util.ArrayList)2 ContinuousVariable (edu.cmu.tetrad.data.ContinuousVariable)1 IKnowledge (edu.cmu.tetrad.data.IKnowledge)1 Knowledge2 (edu.cmu.tetrad.data.Knowledge2)1