Search in sources :

Example 16 with IKnowledge

use of edu.cmu.tetrad.data.IKnowledge in project tetrad by cmu-phil.

the class LayoutUtils method knowledgeLayout.

public static void knowledgeLayout(LayoutEditable layoutEditable) {
    Graph graph = new EdgeListGraph(layoutEditable.getGraph());
    try {
        for (Node node : new ArrayList<>(graph.getNodes())) {
            if (node.getNodeType() == NodeType.ERROR) {
                graph.removeNode(node);
            }
        }
        IKnowledge knowledge = layoutEditable.getKnowledge();
        SearchGraphUtils.arrangeByKnowledgeTiers(graph, knowledge);
        layoutEditable.layoutByGraph(graph);
    } catch (Exception e1) {
        JOptionPane.showMessageDialog(JOptionUtils.centeringComp(), e1.getMessage());
    }
    layout = Layout.knowledge;
}
Also used : IKnowledge(edu.cmu.tetrad.data.IKnowledge) ArrayList(java.util.ArrayList)

Example 17 with IKnowledge

use of edu.cmu.tetrad.data.IKnowledge in project tetrad by cmu-phil.

the class TestKnowledge method test1.

@Test
public final void test1() {
    List<Node> nodes1 = new ArrayList<>();
    for (int i1 = 0; i1 < 10; i1++) {
        nodes1.add(new ContinuousVariable("X" + (i1 + 1)));
    }
    Graph g = GraphUtils.randomGraph(nodes1, 0, 10, 3, 3, 3, false);
    g.getNode("X1").setName("X1.1");
    g.getNode("X2").setName("X2-1");
    List<Node> nodes = g.getNodes();
    List<String> varNames = new ArrayList<>();
    for (Node node : nodes) {
        varNames.add(node.getName());
    }
    IKnowledge knowledge = new Knowledge2(varNames);
    knowledge.addToTier(0, "X1.*1");
    knowledge.addToTier(0, "X2-1");
    knowledge.addToTier(1, "X3");
    knowledge.setForbidden("X4", "X5");
    knowledge.setRequired("X6", "X7");
    knowledge.setRequired("X7", "X8");
    assertTrue(knowledge.isForbidden("X4", "X5"));
    assertFalse(knowledge.isForbidden("X1.1", "X2-1"));
    assertTrue(knowledge.isForbidden("X3", "X2-1"));
    assertTrue(knowledge.isRequired("X6", "X7"));
    IKnowledge copy = knowledge.copy();
    assertTrue(copy.isForbidden("X4", "X5"));
    assertFalse(copy.isForbidden("X1", "X2-1"));
    assertTrue(copy.isForbidden("X3", "X2-1"));
    knowledge.setTierForbiddenWithin(0, true);
    assertTrue(knowledge.isForbidden("X1.1", "X2-1"));
    assertTrue(knowledge.isForbidden("X2-1", "X1.1"));
    assertFalse(knowledge.isForbidden("X1.1", "X1.1"));
    boolean found = false;
    for (Iterator i = knowledge.forbiddenEdgesIterator(); i.hasNext(); ) {
        KnowledgeEdge edge = (KnowledgeEdge) i.next();
        if (edge.getFrom().equals("X1.1") && edge.getTo().equals("X2-1")) {
            found = true;
        }
    }
    assertTrue(found);
    knowledge.setTierForbiddenWithin(0, false);
    assertFalse(knowledge.isForbidden("X1.1", "X2-1"));
    assertFalse(knowledge.isForbidden("X2-1", "X1.1"));
    assertFalse(knowledge.isForbidden("X1.1", "X1.1"));
}
Also used : KnowledgeEdge(edu.cmu.tetrad.data.KnowledgeEdge) Node(edu.cmu.tetrad.graph.Node) ArrayList(java.util.ArrayList) Knowledge2(edu.cmu.tetrad.data.Knowledge2) ContinuousVariable(edu.cmu.tetrad.data.ContinuousVariable) IKnowledge(edu.cmu.tetrad.data.IKnowledge) Graph(edu.cmu.tetrad.graph.Graph) Iterator(java.util.Iterator) Test(org.junit.Test)

Example 18 with IKnowledge

use of edu.cmu.tetrad.data.IKnowledge in project tetrad by cmu-phil.

the class TestKnowledge method test3.

@Test
public final void test3() {
    List<String> vars = new ArrayList<>();
    final int numVars = 1000;
    for (int i = 0; i < numVars; i++) {
        vars.add("X" + i);
    }
    IKnowledge knowledge = new Knowledge2(vars);
    knowledge.setForbidden("X1*", "X2*");
    knowledge.setForbidden("X3*", "X4*");
    knowledge.setForbidden("X5*", "X6*");
    knowledge.addToTier(0, "X7*");
    knowledge.addToTier(1, "X8*");
    knowledge.addToTier(2, "X9*");
    for (int i = 0; i < numVars; i++) {
        assertTrue(knowledge.isForbidden("X11", "X22"));
    }
}
Also used : IKnowledge(edu.cmu.tetrad.data.IKnowledge) ArrayList(java.util.ArrayList) Knowledge2(edu.cmu.tetrad.data.Knowledge2) Test(org.junit.Test)

Example 19 with IKnowledge

use of edu.cmu.tetrad.data.IKnowledge in project tetrad by cmu-phil.

the class TestPcd method testSearch3.

/**
 * This will fail if the orientation loop doesn't continue after the first orientation.
 */
// @Test
public void testSearch3() {
    IKnowledge knowledge = new Knowledge2();
    knowledge.setForbidden("B", "D");
    knowledge.setForbidden("D", "B");
    checkWithKnowledge("A-->B,C-->B,B-->D", "A-->B,C-->B,D", knowledge);
}
Also used : IKnowledge(edu.cmu.tetrad.data.IKnowledge) Knowledge2(edu.cmu.tetrad.data.Knowledge2)

Example 20 with IKnowledge

use of edu.cmu.tetrad.data.IKnowledge in project tetrad by cmu-phil.

the class TimeLagGraphWrapper method getKnowledge.

public IKnowledge getKnowledge() {
    // need to fix this!
    int numLags = 1;
    List<Node> variables = graph.getNodes();
    List<Integer> laglist = new ArrayList<>();
    IKnowledge knowledge1 = new Knowledge2();
    int lag;
    for (Node node : variables) {
        String varName = node.getName();
        String tmp;
        if (varName.indexOf(':') == -1) {
            lag = 0;
            laglist.add(lag);
        } else {
            tmp = varName.substring(varName.indexOf(':') + 1, varName.length());
            lag = Integer.parseInt(tmp);
            laglist.add(lag);
        }
    }
    numLags = Collections.max(laglist);
    for (Node node : variables) {
        String varName = node.getName();
        String tmp;
        if (varName.indexOf(':') == -1) {
            lag = 0;
            laglist.add(lag);
        } else {
            tmp = varName.substring(varName.indexOf(':') + 1, varName.length());
            lag = Integer.parseInt(tmp);
            laglist.add(lag);
        }
        knowledge1.addToTier(numLags - lag, node.getName());
    }
    System.out.println("Knowledge in graph = " + knowledge1);
    return knowledge1;
}
Also used : IKnowledge(edu.cmu.tetrad.data.IKnowledge) ArrayList(java.util.ArrayList) Knowledge2(edu.cmu.tetrad.data.Knowledge2)

Aggregations

IKnowledge (edu.cmu.tetrad.data.IKnowledge)58 Knowledge2 (edu.cmu.tetrad.data.Knowledge2)46 Parameters (edu.cmu.tetrad.util.Parameters)18 Graph (edu.cmu.tetrad.graph.Graph)12 ArrayList (java.util.ArrayList)10 GraphWorkbench (edu.cmu.tetradapp.workbench.GraphWorkbench)8 IOException (java.io.IOException)7 EdgeListGraph (edu.cmu.tetrad.graph.EdgeListGraph)5 CharArrayWriter (java.io.CharArrayWriter)5 DataReader (edu.cmu.tetrad.data.DataReader)4 DataSet (edu.cmu.tetrad.data.DataSet)4 Node (edu.cmu.tetrad.graph.Node)4 WatchedProcess (edu.cmu.tetradapp.util.WatchedProcess)4 File (java.io.File)4 PrintWriter (java.io.PrintWriter)4 List (java.util.List)4 KnowledgeBoxInput (edu.cmu.tetrad.data.KnowledgeBoxInput)3 KnowledgeEdge (edu.cmu.tetrad.data.KnowledgeEdge)3 Test (org.junit.Test)3 BdeuScore (edu.cmu.tetrad.algcomparison.score.BdeuScore)2