private void calculateArrowsForward(Node x, Node y, Graph graph) { clearArrow(x, y); if (!knowledgeEmpty()) { if (getKnowledge().isForbidden(x.getName(), y.getName())) { return; } } List<Node> naYX = getNaYX(x, y, graph); List<Node> t = getTNeighbors(x, y, graph); DepthChoiceGenerator gen = new DepthChoiceGenerator(t.size(), t.size()); int[] choice; while ((choice = gen.next()) != null) { List<Node> s = GraphUtils.asList(choice, t); if (!knowledgeEmpty()) { if (!validSetByKnowledge(y, s)) { continue; } } double bump = insertEval(x, y, s, naYX, graph); if (bump > 0.0) { Arrow arrow = new Arrow(bump, x, y, s, naYX); sortedArrows.add(arrow); addLookupArrow(x, y, arrow); } } }
/** Returns true iif the given set forms a clique in the given graph. */ private static boolean isClique(List<Node> nodes, Graph graph) { for (int i = 0; i < nodes.size() - 1; i++) { for (int j = i + 1; j < nodes.size(); j++) { if (!graph.isAdjacentTo(nodes.get(i), nodes.get(j))) { return false; } } } return true; }
private static double characteristicPathLength(Graph g) { List<Node> nodes = g.getNodes(); int total = 0; int count = 0; for (int i = 0; i < nodes.size(); i++) { for (int j = i; j < nodes.size(); j++) { int shortest = shortestPath(nodes.get(i), nodes.get(j), g); total += shortest; count++; } } return total / (double) count; }
private String clusterSizes(List<List<Node>> partition, List<List<Node>> trueClusters) { String s = ""; FOR: for (int i = 0; i < partition.size(); i++) { List<Node> cluster = partition.get(i); s += cluster.size(); for (List<Node> trueCluster : trueClusters) { if (trueCluster.containsAll(cluster)) { // Collections.sort(trueCluster); // Collections.sort(cluster); // System.out.println(trueCluster + " " + cluster); s += "p"; if (i < partition.size() - 1) { s += ","; } continue FOR; } } if (i < partition.size() - 1) { s += ","; } } return s; }
private int numClustered(List<List<Node>> partition) { int sum = 0; for (int i = 0; i < partition.size(); i++) { List<Node> cluster = partition.get(i); sum += cluster.size(); } return sum; }
/** Tests to see if d separation facts are symmetric. */ public void testDSeparation2() { EdgeListGraphSingleConnections graph = new EdgeListGraphSingleConnections( new Dag(GraphUtils.randomGraph(7, 0, 14, 30, 15, 15, true))); List<Node> nodes = graph.getNodes(); int depth = -1; for (int i = 0; i < nodes.size(); i++) { for (int j = i; j < nodes.size(); j++) { Node x = nodes.get(i); Node y = nodes.get(j); List<Node> theRest = new ArrayList<Node>(nodes); // theRest.remove(x); // theRest.remove(y); DepthChoiceGenerator gen = new DepthChoiceGenerator(theRest.size(), depth); int[] choice; while ((choice = gen.next()) != null) { List<Node> z = new LinkedList<Node>(); for (int k = 0; k < choice.length; k++) { z.add(theRest.get(choice[k])); } boolean dConnectedTo = graph.isDConnectedTo(x, y, z); boolean dConnectedTo1 = graph.isDConnectedTo(y, x, z); if (dConnectedTo != dConnectedTo1) { System.out.println(x + " d connected to " + y + " given " + z); System.out.println(graph); System.out.println("dconnectedto = " + dConnectedTo); System.out.println("dconnecteto1 = " + dConnectedTo1); fail(); } } } } }
// Invalid if then nodes or graph changes. private void calculateArrowsBackward(Node x, Node y, Graph graph) { if (x == y) { return; } if (!graph.isAdjacentTo(x, y)) { return; } if (!knowledgeEmpty()) { if (!getKnowledge().noEdgeRequired(x.getName(), y.getName())) { return; } } List<Node> naYX = getNaYX(x, y, graph); clearArrow(x, y); List<Node> _naYX = new ArrayList<Node>(naYX); DepthChoiceGenerator gen = new DepthChoiceGenerator(_naYX.size(), _naYX.size()); int[] choice; while ((choice = gen.next()) != null) { List<Node> H = GraphUtils.asList(choice, _naYX); if (!knowledgeEmpty()) { if (!validSetByKnowledge(y, H)) { continue; } } double bump = deleteEval(x, y, H, naYX, graph); if (bump > 0.0) { Arrow arrow = new Arrow(bump, x, y, H, naYX); sortedArrows.add(arrow); addLookupArrow(x, y, arrow); } } }
/** Tests to see if d separation facts are symmetric. */ public void testDSeparation() { EdgeListGraphSingleConnections graph = new EdgeListGraphSingleConnections( new Dag(GraphUtils.randomGraph(7, 0, 7, 30, 15, 15, true))); System.out.println(graph); List<Node> nodes = graph.getNodes(); int depth = -1; for (int i = 0; i < nodes.size(); i++) { for (int j = i + 1; j < nodes.size(); j++) { Node x = nodes.get(i); Node y = nodes.get(j); List<Node> theRest = new ArrayList<Node>(nodes); theRest.remove(x); theRest.remove(y); DepthChoiceGenerator gen = new DepthChoiceGenerator(theRest.size(), depth); int[] choice; while ((choice = gen.next()) != null) { List<Node> z = new LinkedList<Node>(); for (int k = 0; k < choice.length; k++) { z.add(theRest.get(choice[k])); } if (graph.isDSeparatedFrom(x, y, z) != graph.isDSeparatedFrom(y, x, z)) { fail( SearchLogUtils.independenceFact(x, y, z) + " should have same d-sep result as " + SearchLogUtils.independenceFact(y, x, z)); } } } } }
private static int weightedRandom(List<Node> nodes, Graph graph) { int total = 0; int n = nodes.size(); for (int b = 0; b < n; b++) { total = weight(nodes, graph, total, b); } int r = RandomUtil.getInstance().nextInt(total); int count = 0; int index = 0; for (int b = 0; b < n; b++) { count = weight(nodes, graph, count, b); if (r <= count) { index = b; break; } } return index; }