use of edu.cmu.tetrad.data.IKnowledge in project tetrad by cmu-phil.
the class MbSearchEditor method layoutByKnowledge.
public void layoutByKnowledge() {
GraphWorkbench resultWorkbench = getWorkbench();
Graph graph = resultWorkbench.getGraph();
IKnowledge knowledge = (IKnowledge) getAlgorithmRunner().getParams().get("knowledge", new Knowledge2());
SearchGraphUtils.arrangeByKnowledgeTiers(graph, knowledge);
// resultWorkbench.setGraph(graph);
}
use of edu.cmu.tetrad.data.IKnowledge in project tetrad by cmu-phil.
the class GeneralAlgorithmRunner method execute.
// ============================PUBLIC METHODS==========================//
@Override
public void execute() {
List<Graph> graphList = new ArrayList<>();
int i = 0;
if (getDataModelList().isEmpty()) {
if (getSourceGraph() != null) {
Algorithm algo = getAlgorithm();
if (algo instanceof HasKnowledge) {
((HasKnowledge) algo).setKnowledge(getKnowledge());
}
graphList.add(algo.search(null, parameters));
} else {
throw new IllegalArgumentException("The parent boxes did not include any datasets or graphs. Try opening\n" + "the editors for those boxes and loading or simulating them.");
}
} else {
if (getAlgorithm() instanceof MultiDataSetAlgorithm) {
for (int k = 0; k < parameters.getInt("numRuns"); k++) {
List<DataSet> dataSets = getDataModelList().stream().map(e -> (DataSet) e).collect(Collectors.toCollection(ArrayList::new));
if (dataSets.size() < parameters.getInt("randomSelectionSize")) {
throw new IllegalArgumentException("Sorry, the 'random selection size' is greater than " + "the number of data sets.");
}
Collections.shuffle(dataSets);
List<DataModel> sub = new ArrayList<>();
for (int j = 0; j < parameters.getInt("randomSelectionSize"); j++) {
sub.add(dataSets.get(j));
}
Algorithm algo = getAlgorithm();
if (algo instanceof HasKnowledge) {
((HasKnowledge) algo).setKnowledge(getKnowledge());
}
graphList.add(((MultiDataSetAlgorithm) algo).search(sub, parameters));
}
} else if (getAlgorithm() instanceof ClusterAlgorithm) {
for (int k = 0; k < parameters.getInt("numRuns"); k++) {
getDataModelList().forEach(dataModel -> {
if (dataModel instanceof ICovarianceMatrix) {
ICovarianceMatrix dataSet = (ICovarianceMatrix) dataModel;
graphList.add(algorithm.search(dataSet, parameters));
} else if (dataModel instanceof DataSet) {
DataSet dataSet = (DataSet) dataModel;
if (!dataSet.isContinuous()) {
throw new IllegalArgumentException("Sorry, you need a continuous dataset for a cluster algorithm.");
}
graphList.add(algorithm.search(dataSet, parameters));
}
});
}
} else {
getDataModelList().forEach(data -> {
IKnowledge knowledgeFromData = data.getKnowledge();
if (!(knowledgeFromData == null || knowledgeFromData.getVariables().isEmpty())) {
this.knowledge = knowledgeFromData;
}
Algorithm algo = getAlgorithm();
if (algo instanceof HasKnowledge) {
((HasKnowledge) algo).setKnowledge(getKnowledge());
}
DataType algDataType = algo.getDataType();
if (data.isContinuous() && (algDataType == DataType.Continuous || algDataType == DataType.Mixed)) {
graphList.add(algo.search(data, parameters));
} else if (data.isDiscrete() && (algDataType == DataType.Discrete || algDataType == DataType.Mixed)) {
graphList.add(algo.search(data, parameters));
} else if (data.isMixed() && algDataType == DataType.Mixed) {
graphList.add(algo.search(data, parameters));
} else {
throw new IllegalArgumentException("The type of data changed; try opening up the search editor and " + "running the algorithm there.");
}
});
}
}
if (getKnowledge().getVariablesNotInTiers().size() < getKnowledge().getVariables().size()) {
for (Graph graph : graphList) {
SearchGraphUtils.arrangeByKnowledgeTiers(graph, getKnowledge());
}
} else {
for (Graph graph : graphList) {
GraphUtils.circleLayout(graph, 225, 200, 150);
}
}
this.graphList = graphList;
}
use of edu.cmu.tetrad.data.IKnowledge in project tetrad by cmu-phil.
the class MbFanSearchRunner method execute.
// =================PUBLIC METHODS OVERRIDING ABSTRACT=================//
/**
* Executes the algorithm, producing (at least) a result workbench. Must be
* implemented in the extending class.
*/
public void execute() {
int pcDepth = getParams().getInt("depth", -1);
Mbfs mbfs = new Mbfs(getIndependenceTest(), pcDepth);
Parameters params = getParams();
if (params instanceof Parameters) {
mbfs.setAggressivelyPreventCycles(params.getBoolean("aggressivelyPreventCycles", false));
}
IKnowledge knowledge = (IKnowledge) getParams().get("knowledge", new Knowledge2());
mbfs.setKnowledge(knowledge);
String targetName = getParams().getString("targetName", null);
Graph graph = mbfs.search(targetName);
setResultGraph(graph);
if (getSourceGraph() != null) {
GraphUtils.arrangeBySourceGraph(graph, getSourceGraph());
} else if (knowledge.isDefaultToKnowledgeLayout()) {
SearchGraphUtils.arrangeByKnowledgeTiers(graph, knowledge);
} else {
GraphUtils.circleLayout(graph, 200, 200, 150);
}
this.mbfs = mbfs;
}
use of edu.cmu.tetrad.data.IKnowledge in project tetrad by cmu-phil.
the class MbfsRunner method execute.
// =================PUBLIC METHODS OVERRIDING ABSTRACT=================//
/**
* Executes the algorithm, producing (at least) a result workbench. Must be
* implemented in the extending class.
*/
public void execute() {
// int pcDepth = ((Parameters) getParameters()).getMaxIndegree();
// Mbfs mbfs = new Mbfs(getIndependenceTest(), pcDepth);
// Parameters params = getParameters();
// if (params instanceof Parameters) {
// mbfs.setAggressivelyPreventCycles(((Parameters) params)
// .isAggressivelyPreventCycles());
// }
IKnowledge knowledge = (IKnowledge) getParams().get("knowledge", new Knowledge2());
// mbfs.setKnowledge(knowledge);
String targetName = getParams().getString("targetName", null);
// Graph searchGraph = mbfs.search(targetName);
// setResultGraph(searchGraph);
DataSet dataSet = (DataSet) getDataModelList().get(0);
SemBicScore score = new SemBicScore(new CovarianceMatrixOnTheFly(dataSet));
score.setPenaltyDiscount(getParams().getDouble("alpha", 0.001));
FgesMb search = new FgesMb(score);
search.setFaithfulnessAssumed(true);
Graph searchGraph = search.search(dataSet.getVariable(targetName));
if (getSourceGraph() != null) {
GraphUtils.arrangeBySourceGraph(searchGraph, getSourceGraph());
} else if (knowledge.isDefaultToKnowledgeLayout()) {
SearchGraphUtils.arrangeByKnowledgeTiers(searchGraph, knowledge);
} else {
GraphUtils.circleLayout(searchGraph, 200, 200, 150);
}
// this.mbfs = mbfs;
setResultGraph(searchGraph);
}
use of edu.cmu.tetrad.data.IKnowledge in project tetrad by cmu-phil.
the class ForbiddenGraphModel method createKnowledge.
private void createKnowledge(Parameters params) {
IKnowledge knwl = getKnowledge();
if (knwl == null) {
return;
}
knwl.clear();
List<String> varNames = getVarNames();
getKnowledgeBoxInput().getVariableNames().stream().filter(e -> !e.startsWith("E_")).forEach(e -> {
varNames.add(e);
knwl.addVariable(e);
});
if (resultGraph == null) {
throw new NullPointerException("I couldn't find a parent graph.");
}
List<Node> nodes = resultGraph.getNodes();
int numOfNodes = nodes.size();
for (int i = 0; i < numOfNodes; i++) {
for (int j = i + 1; j < numOfNodes; j++) {
Node n1 = nodes.get(i);
Node n2 = nodes.get(j);
if (n1.getName().startsWith("E_") || n2.getName().startsWith("E_")) {
continue;
}
Edge edge = resultGraph.getEdge(n1, n2);
if (edge != null && edge.isDirected()) {
knwl.setForbidden(edge.getNode2().getName(), edge.getNode1().getName());
}
}
}
}
Aggregations