public static boolean isArrowpointAllowed(Object from, Object to, IKnowledge knowledge) { if (knowledge == null) { return true; } return !knowledge.isRequired(to.toString(), from.toString()) && !knowledge.isForbidden(from.toString(), to.toString()); }
/** Orients according to background knowledge */ private void fciOrientbk(IKnowledge bk, Graph graph, List<Node> variables) { logger.log("info", "Starting BK Orientation."); for (Iterator<KnowledgeEdge> it = bk.forbiddenEdgesIterator(); it.hasNext(); ) { KnowledgeEdge edge = it.next(); // match strings to variables in the graph. Node from = SearchGraphUtils.translate(edge.getFrom(), variables); Node to = SearchGraphUtils.translate(edge.getTo(), variables); if (from == null || to == null) { continue; } if (graph.getEdge(from, to) == null) { continue; } // Orient to*->from graph.setEndpoint(to, from, Endpoint.ARROW); graph.setEndpoint(from, to, Endpoint.CIRCLE); changeFlag = true; logger.log( "knowledgeOrientation", SearchLogUtils.edgeOrientedMsg("Knowledge", graph.getEdge(from, to))); } for (Iterator<KnowledgeEdge> it = bk.requiredEdgesIterator(); it.hasNext(); ) { KnowledgeEdge edge = it.next(); // match strings to variables in this graph Node from = SearchGraphUtils.translate(edge.getFrom(), variables); Node to = SearchGraphUtils.translate(edge.getTo(), variables); if (from == null || to == null) { continue; } if (graph.getEdge(from, to) == null) { continue; } graph.setEndpoint(to, from, Endpoint.TAIL); graph.setEndpoint(from, to, Endpoint.ARROW); changeFlag = true; logger.log( "knowledgeOrientation", SearchLogUtils.edgeOrientedMsg("Knowledge", graph.getEdge(from, to))); } logger.log("info", "Finishing BK Orientation."); }
/** Sets a new Knowledge2 for the algorithm. */ public void setKnowledge(IKnowledge knowledge) { if (knowledge == null) { throw new NullPointerException("Cannot set a null knowledge."); } this.knowledge = knowledge.copy(); }
/** * Executes the algorithm, producing (at least) a result workbench. Must be implemented in the * extending class. */ public void execute() { IKnowledge knowledge = getParams().getKnowledge(); SearchParams searchParams = getParams(); FciIndTestParams indTestParams = (FciIndTestParams) searchParams.getIndTestParams(); // Cfci fciSearch = // new Cfci(getIndependenceTest(), knowledge); // fciSearch.setMaxIndegree(indTestParams.depth()); // Graph graph = fciSearch.search(); // // if (knowledge.isDefaultToKnowledgeLayout()) { // SearchGraphUtils.arrangeByKnowledgeTiers(graph, knowledge); // } // // setResultGraph(graph); Graph graph; if (indTestParams.isRFCI_Used()) { Rfci fci = new Rfci(getIndependenceTest()); fci.setKnowledge(knowledge); fci.setCompleteRuleSetUsed(indTestParams.isCompleteRuleSetUsed()); fci.setMaxPathLength(indTestParams.getMaxReachablePathLength()); fci.setDepth(indTestParams.getDepth()); graph = fci.search(); } else { Fci fci = new Fci(getIndependenceTest()); fci.setKnowledge(knowledge); fci.setCompleteRuleSetUsed(indTestParams.isCompleteRuleSetUsed()); fci.setPossibleDsepSearchDone(indTestParams.isPossibleDsepDone()); fci.setMaxPathLength(indTestParams.getMaxReachablePathLength()); fci.setDepth(indTestParams.getDepth()); graph = fci.search(); } if (getSourceGraph() != null) { GraphUtils.arrangeBySourceGraph(graph, getSourceGraph()); } else if (knowledge.isDefaultToKnowledgeLayout()) { SearchGraphUtils.arrangeByKnowledgeTiers(graph, knowledge); } else { GraphUtils.circleLayout(graph, 200, 200, 150); } setResultGraph(graph); }
/** * Helper method. Appears to check if an arrowpoint is permitted by background knowledge. * * @param x The possible other node. * @param y The possible point node. * @return Whether the arrowpoint is allowed. */ private boolean isArrowpointAllowed(Node x, Node y) { if (graph.getEndpoint(x, y) == Endpoint.ARROW) { return true; } if (graph.getEndpoint(x, y) == Endpoint.TAIL) { return false; } if (graph.getEndpoint(y, x) == Endpoint.ARROW) { if (!knowledge.isForbidden(x.getName(), y.getName())) return true; } if (graph.getEndpoint(y, x) == Endpoint.TAIL) { if (!knowledge.isForbidden(x.getName(), y.getName())) return true; } return graph.getEndpoint(y, x) == Endpoint.CIRCLE; }
/** * Executes the algorithm, producing (at least) a result workbench. Must be implemented in the * extending class. */ public void execute() { IKnowledge knowledge = getParams().getKnowledge(); SearchParams searchParams = getParams(); FciGesIndTestParams indTestParams = (FciGesIndTestParams) searchParams.getIndTestParams(); // Cfci fciSearch = // new Cfci(getIndependenceTest(), knowledge); // fciSearch.setDepth(indTestParams.depth()); // Graph graph = fciSearch.search(); // // if (knowledge.isDefaultToKnowledgeLayout()) { // SearchGraphUtils.arrangeByKnowledgeTiers(graph, knowledge); // } // // setResultGraph(graph); Graph graph; TFciGes fci = new TFciGes(getIndependenceTest()); fci.setKnowledge(knowledge); fci.setCompleteRuleSetUsed(indTestParams.isCompleteRuleSetUsed()); fci.setPossibleDsepSearchDone(indTestParams.isPossibleDsepDone()); fci.setMaxPathLength(indTestParams.getMaxReachablePathLength()); fci.setDepth(indTestParams.getDepth()); fci.setPenaltyDiscount(indTestParams.getPenaltyDiscount()); fci.setSamplePrior(indTestParams.getSamplePrior()); fci.setStructurePrior(indTestParams.getStructurePrior()); fci.setCompleteRuleSetUsed(false); fci.setPenaltyDiscount(indTestParams.getPenaltyDiscount()); fci.setFaithfulnessAssumed(indTestParams.isFaithfulnessAssumed()); graph = fci.search(); if (getSourceGraph() != null) { GraphUtils.arrangeBySourceGraph(graph, getSourceGraph()); } else if (knowledge.isDefaultToKnowledgeLayout()) { SearchGraphUtils.arrangeByKnowledgeTiers(graph, knowledge); } else { GraphUtils.circleLayout(graph, 200, 200, 150); } setResultGraph(graph); }
public void execute() { IKnowledge knowledge = getParams().getKnowledge(); PcSearchParams searchParams = (PcSearchParams) getParams(); PcIndTestParams indTestParams = (PcIndTestParams) searchParams.getIndTestParams(); VcpcAlt VcpcAlt = new VcpcAlt(getIndependenceTest()); VcpcAlt.setKnowledge(knowledge); VcpcAlt.setAggressivelyPreventCycles(this.isAggressivelyPreventCycles()); VcpcAlt.setDepth(indTestParams.getDepth()); Graph graph = VcpcAlt.search(); if (getSourceGraph() != null) { GraphUtils.arrangeBySourceGraph(graph, getSourceGraph()); } else if (knowledge.isDefaultToKnowledgeLayout()) { SearchGraphUtils.arrangeByKnowledgeTiers(graph, knowledge); } else { GraphUtils.circleLayout(graph, 200, 200, 150); } setResultGraph(graph); }
/** * Executes the algorithm, producing (at least) a result workbench. Must be implemented in the * extending class. */ public void execute() { IKnowledge knowledge = (IKnowledge) getParams().get("knowledge", new Knowledge2()); Parameters searchParams = getParams(); Parameters params = searchParams; Graph graph; IndependenceTest independenceTest = getIndependenceTest(); Score score = new ScoredIndTest(independenceTest); if (independenceTest instanceof IndTestDSep) { final DagToPag dagToPag = new DagToPag(((IndTestDSep) independenceTest).getGraph()); dagToPag.setCompleteRuleSetUsed(params.getBoolean("completeRuleSetUsed", false)); graph = dagToPag.convert(); } else { GFci fci = new GFci(independenceTest, score); fci.setKnowledge(knowledge); fci.setCompleteRuleSetUsed(params.getBoolean("completeRuleSetUsed", false)); fci.setMaxPathLength(params.getInt("maxReachablePathLength", -1)); fci.setMaxDegree(params.getInt("maxIndegree")); fci.setCompleteRuleSetUsed(false); fci.setFaithfulnessAssumed(params.getBoolean("faithfulnessAssumed", true)); graph = fci.search(); } if (getSourceGraph() != null) { GraphUtils.arrangeBySourceGraph(graph, getSourceGraph()); } else if (knowledge.isDefaultToKnowledgeLayout()) { SearchGraphUtils.arrangeByKnowledgeTiers(graph, knowledge); } else { GraphUtils.circleLayout(graph, 200, 200, 150); } setResultGraph(graph); }
private void addRequiredEdges(Graph graph) { if (true) return; if (knowledgeEmpty()) return; for (Iterator<KnowledgeEdge> it = getKnowledge().requiredEdgesIterator(); it.hasNext(); ) { KnowledgeEdge next = it.next(); Node nodeA = graph.getNode(next.getFrom()); Node nodeB = graph.getNode(next.getTo()); if (!graph.isAncestorOf(nodeB, nodeA)) { graph.removeEdges(nodeA, nodeB); graph.addDirectedEdge(nodeA, nodeB); TetradLogger.getInstance() .log("insertedEdges", "Adding edge by knowledge: " + graph.getEdge(nodeA, nodeB)); } } for (Edge edge : graph.getEdges()) { final String A = edge.getNode1().getName(); final String B = edge.getNode2().getName(); if (knowledge.isForbidden(A, B)) { Node nodeA = edge.getNode1(); Node nodeB = edge.getNode2(); if (nodeA != null && nodeB != null && graph.isAdjacentTo(nodeA, nodeB) && !graph.isChildOf(nodeA, nodeB)) { if (!graph.isAncestorOf(nodeA, nodeB)) { graph.removeEdges(nodeA, nodeB); graph.addDirectedEdge(nodeB, nodeA); TetradLogger.getInstance() .log("insertedEdges", "Adding edge by knowledge: " + graph.getEdge(nodeB, nodeA)); } } if (!graph.isChildOf(nodeA, nodeB) && getKnowledge().isForbidden(nodeA.getName(), nodeB.getName())) { if (!graph.isAncestorOf(nodeA, nodeB)) { graph.removeEdges(nodeA, nodeB); graph.addDirectedEdge(nodeB, nodeA); TetradLogger.getInstance() .log("insertedEdges", "Adding edge by knowledge: " + graph.getEdge(nodeB, nodeA)); } } } else if (knowledge.isForbidden(B, A)) { Node nodeA = edge.getNode2(); Node nodeB = edge.getNode1(); if (nodeA != null && nodeB != null && graph.isAdjacentTo(nodeA, nodeB) && !graph.isChildOf(nodeA, nodeB)) { if (!graph.isAncestorOf(nodeA, nodeB)) { graph.removeEdges(nodeA, nodeB); graph.addDirectedEdge(nodeB, nodeA); TetradLogger.getInstance() .log("insertedEdges", "Adding edge by knowledge: " + graph.getEdge(nodeB, nodeA)); } } if (!graph.isChildOf(nodeA, nodeB) && getKnowledge().isForbidden(nodeA.getName(), nodeB.getName())) { if (!graph.isAncestorOf(nodeA, nodeB)) { graph.removeEdges(nodeA, nodeB); graph.addDirectedEdge(nodeB, nodeA); TetradLogger.getInstance() .log("insertedEdges", "Adding edge by knowledge: " + graph.getEdge(nodeB, nodeA)); } } } } }
//////////////////////////////////////////// // RFCI Algorithm 4.4 (Colombo et al, 2012) // Orient colliders //////////////////////////////////////////// private void ruleR0_RFCI(List<Node[]> rTuples) { List<Node[]> lTuples = new ArrayList<Node[]>(); List<Node> nodes = graph.getNodes(); /////////////////////////////// // process tuples in rTuples while (!rTuples.isEmpty()) { Node[] thisTuple = rTuples.remove(0); Node i = thisTuple[0]; Node j = thisTuple[1]; Node k = thisTuple[2]; final List<Node> nodes1 = getSepset(i, k); if (nodes1 == null) continue; List<Node> sepSet = new ArrayList<Node>(nodes1); sepSet.remove(j); boolean independent1 = false; if (knowledge.noEdgeRequired(i.getName(), j.getName())) // if BK allows { try { independent1 = independenceTest.isIndependent(i, j, sepSet); } catch (Exception e) { independent1 = true; } } boolean independent2 = false; if (knowledge.noEdgeRequired(j.getName(), k.getName())) // if BK allows { try { independent2 = independenceTest.isIndependent(j, k, sepSet); } catch (Exception e) { independent2 = true; } } if (!independent1 && !independent2) { lTuples.add(thisTuple); } else { // set sepSets to minimal separating sets if (independent1) { setMinSepSet(sepSet, i, j); graph.removeEdge(i, j); } if (independent2) { setMinSepSet(sepSet, j, k); graph.removeEdge(j, k); } // add new unshielded tuples to rTuples for (Node thisNode : nodes) { List<Node> adjacentNodes = graph.getAdjacentNodes(thisNode); if (independent1) // <i, ., j> { if (adjacentNodes.contains(i) && adjacentNodes.contains(j)) { Node[] newTuple = {i, thisNode, j}; rTuples.add(newTuple); } } if (independent2) // <j, ., k> { if (adjacentNodes.contains(j) && adjacentNodes.contains(k)) { Node[] newTuple = {j, thisNode, k}; rTuples.add(newTuple); } } } // remove tuples involving either (if independent1) <i, j> // or (if independent2) <j, k> from rTuples Iterator<Node[]> iter = rTuples.iterator(); while (iter.hasNext()) { Node[] curTuple = iter.next(); if ((independent1 && (curTuple[1] == i) && ((curTuple[0] == j) || (curTuple[2] == j))) || (independent2 && (curTuple[1] == k) && ((curTuple[0] == j) || (curTuple[2] == j))) || (independent1 && (curTuple[1] == j) && ((curTuple[0] == i) || (curTuple[2] == i))) || (independent2 && (curTuple[1] == j) && ((curTuple[0] == k) || (curTuple[2] == k)))) { iter.remove(); } } // remove tuples involving either (if independent1) <i, j> // or (if independent2) <j, k> from lTuples iter = lTuples.iterator(); while (iter.hasNext()) { Node[] curTuple = iter.next(); if ((independent1 && (curTuple[1] == i) && ((curTuple[0] == j) || (curTuple[2] == j))) || (independent2 && (curTuple[1] == k) && ((curTuple[0] == j) || (curTuple[2] == j))) || (independent1 && (curTuple[1] == j) && ((curTuple[0] == i) || (curTuple[2] == i))) || (independent2 && (curTuple[1] == j) && ((curTuple[0] == k) || (curTuple[2] == k)))) { iter.remove(); } } } } /////////////////////////////////////////////////////// // orient colliders (similar to original FCI ruleR0) for (Node[] thisTuple : lTuples) { Node i = thisTuple[0]; Node j = thisTuple[1]; Node k = thisTuple[2]; List<Node> sepset = getSepset(i, k); if (sepset == null) { continue; } if (!sepset.contains(j) && graph.isAdjacentTo(i, j) && graph.isAdjacentTo(j, k)) { if (!isArrowpointAllowed(i, j)) { continue; } if (!isArrowpointAllowed(k, j)) { continue; } graph.setEndpoint(i, j, Endpoint.ARROW); graph.setEndpoint(k, j, Endpoint.ARROW); printWrongColliderMessage(i, j, k, "R0_RFCI"); } } }
/** * Executes the algorithm, producing (at least) a result workbench. Must be implemented in the * extending class. */ public void execute() { Object source = dataWrapper.getSelectedDataModel(); DataModel dataModel = (DataModel) source; IKnowledge knowledge = params2.getKnowledge(); if (initialGraph == null) { initialGraph = new EdgeListGraph(dataModel.getVariables()); } Graph graph2 = new EdgeListGraph(initialGraph); graph2 = GraphUtils.replaceNodes(graph2, dataModel.getVariables()); Bff search; if (dataModel instanceof DataSet) { DataSet dataSet = (DataSet) dataModel; if (getAlgorithmType() == AlgorithmType.BEAM) { search = new BffBeam(graph2, dataSet, knowledge); } else if (getAlgorithmType() == AlgorithmType.GES) { search = new BffGes(graph2, dataSet); search.setKnowledge(knowledge); } else { throw new IllegalStateException(); } } else if (dataModel instanceof CovarianceMatrix) { CovarianceMatrix covarianceMatrix = (CovarianceMatrix) dataModel; if (getAlgorithmType() == AlgorithmType.BEAM) { search = new BffBeam(graph2, covarianceMatrix, knowledge); } else if (getAlgorithmType() == AlgorithmType.GES) { throw new IllegalArgumentException( "GES method requires a dataset; a covariance matrix was provided."); // search = new BffGes(graph2, covarianceMatrix); // search.setKnowledge(knowledge); } else { throw new IllegalStateException(); } } else { throw new IllegalStateException(); } PcIndTestParams indTestParams = (PcIndTestParams) getParams().getIndTestParams(); search.setAlpha(indTestParams.getAlpha()); search.setBeamWidth(indTestParams.getBeamWidth()); search.setHighPValueAlpha(indTestParams.getZeroEdgeP()); this.graph = search.search(); // this.graph = search.getNewSemIm().getSemPm().getGraph(); setOriginalSemIm(search.getOriginalSemIm()); this.newSemIm = search.getNewSemIm(); fireGraphChange(graph); if (getSourceGraph() != null) { GraphUtils.arrangeBySourceGraph(graph, getSourceGraph()); } else if (knowledge.isDefaultToKnowledgeLayout()) { SearchGraphUtils.arrangeByKnowledgeTiers(graph, knowledge); } else { GraphUtils.circleLayout(graph, 200, 200, 150); } setResultGraph(SearchGraphUtils.patternForDag(graph, knowledge)); }