cmu-phil / tetrad

Repository for the Tetrad Project, www.phil.cmu.edu/tetrad.
GNU General Public License v2.0
408 stars 111 forks source link

randomGraphRandomForwardEdges doesn't add edges when connected=true #102

Closed jmogarrio closed 8 years ago

jmogarrio commented 8 years ago

The following check at line 343 occurs before any edge insertion. When connected=true, the continue statement is always executed, so the output has no edges.

        if (connected && indegree == 0 && outdegree == 0) {
            i--;
            continue;
        }
jdramsey commented 8 years ago

Try changing it to this:

int trials = 0;
boolean added = false;

for (int i = 0; i < numEdges; i++) {
    int c1 = RandomUtil.getInstance().nextInt(nodes2.size());
    int c2 = RandomUtil.getInstance().nextInt(nodes2.size());

    if (++trials > 2 * numEdges) break;

    if (c1 == c2) {
        i--;
        continue;
    }

    if (c1 > c2) {
        int temp = c1;
        c1 = c2;
        c2 = temp;
    }

    Node n1 = nodes2.get(c1);
    Node n2 = nodes2.get(c2);

    if (dag.isAdjacentTo(n1, n2)) {
        i--;
    }

    final int indegree = dag.getIndegree(n2);
    final int outdegree = dag.getOutdegree(n1);

    if (indegree >= maxIndegree) {
        i--;
        continue;
    }

    if (outdegree >= maxOutdegree) {
        i--;
        continue;
    }

    if (dag.getIndegree(n1) + dag.getOutdegree(n1) + 1 > maxDegree) {
        i--;
        continue;
    }

    if (dag.getIndegree(n2) + dag.getOutdegree(n2) + 1 > maxDegree) {
        i--;
        continue;
    }

    if (added && connected && indegree == 0 && outdegree == 0) {
        i--;
        continue;
    }

    dag.addDirectedEdge(n1, n2);
    added = true;
}

fixLatents4(numLatentConfounders, dag);

GraphUtils.circleLayout(dag, 200, 200, 150);

return dag;

}

Joe

On Fri, Dec 11, 2015 at 4:37 PM, jmogarrio notifications@github.com wrote:

The following check at line 343 occurs before any edge insertion When connected=true, the continue statement is always executed, so the output has no edges

    if (connected && indegree == 0 && outdegree == 0) {
        i--;
        continue;
    }

— Reply to this email directly or view it on GitHub https://github.com/cmu-phil/tetrad/issues/102.

Joseph D. Ramsey Special Faculty and Director of Research Computing Department of Philosophy 135 Baker Hall Carnegie Mellon University Pittsburgh, PA 15213

jsph.ramsey@gmail.com Office: (412) 268-8063 http://www.andrew.cmu.edu/user/jdramsey

jmogarrio commented 8 years ago

The number of edges is really small. Running it with arguments (1000, 250, 1000, 30, 15, 15, true) returns graphs with number of edges in the order of 10.

I don't really need the connectedness, it just seems the method is not working as it seems it's supposed to.

Try changing it to this:

int trials = 0;
boolean added = false;

for (int i = 0; i < numEdges; i++) {
    int c1 = RandomUtil.getInstance().nextInt(nodes2.size());
    int c2 = RandomUtil.getInstance().nextInt(nodes2.size());

    if (++trials > 2 * numEdges) break;

    if (c1 == c2) {
        i--;
        continue;
    }

    if (c1 > c2) {
        int temp = c1;
        c1 = c2;
        c2 = temp;
    }

    Node n1 = nodes2.get(c1);
    Node n2 = nodes2.get(c2);

    if (dag.isAdjacentTo(n1, n2)) {
        i--;
    }

    final int indegree = dag.getIndegree(n2);
    final int outdegree = dag.getOutdegree(n1);

    if (indegree >= maxIndegree) {
        i--;
        continue;
    }

    if (outdegree >= maxOutdegree) {
        i--;
        continue;
    }

    if (dag.getIndegree(n1) + dag.getOutdegree(n1) + 1 > maxDegree) {
        i--;
        continue;
    }

    if (dag.getIndegree(n2) + dag.getOutdegree(n2) + 1 > maxDegree) {
        i--;
        continue;
    }

    if (added && connected && indegree == 0 && outdegree == 0) {
        i--;
        continue;
    }

    dag.addDirectedEdge(n1, n2);
    added = true;
}

fixLatents4(numLatentConfounders, dag);

GraphUtils.circleLayout(dag, 200, 200, 150);

return dag;

}

Joe

On Fri, Dec 11, 2015 at 4:37 PM, jmogarrio notifications@github.com wrote:

The following check at line 343 occurs before any edge insertion When connected=true, the continue statement is always executed, so the output has no edges

    if (connected && indegree == 0 && outdegree == 0) {
        i--;
        continue;
    }

— Reply to this email directly or view it on GitHub https://github.com/cmu-phil/tetrad/issues/102.

Joseph D. Ramsey Special Faculty and Director of Research Computing Department of Philosophy 135 Baker Hall Carnegie Mellon University Pittsburgh, PA 15213

jsph.ramsey@gmail.com Office: (412) 268-8063 http://www.andrew.cmu.edu/user/jdramsey


Reply to this email directly or view it on GitHub: https://github.com/cmu-phil/tetrad/issues/102#issuecomment-164054906

jdramsey commented 8 years ago

This is fixed, I think. There was also a problem in that the number of edges in the graph was not always what was requested, even for small graphs. This is also fixed.