Example #1
0
 private static int weight(List<Node> nodes, Graph graph, int total, int b) {
   double p = 1;
   int degree = graph.getNumEdges(nodes.get(b));
   int t = degree + 1;
   total += pow((double) t, p);
   return total;
 }
Example #2
0
  public void testAlternativeGraphs() {

    //        UniformGraphGenerator gen = new UniformGraphGenerator(UniformGraphGenerator.ANY_DAG);
    //        gen.setNumNodes(100);
    //        gen.setMaxEdges(200);
    //        gen.setMaxDegree(30);
    //        gen.setMaxInDegree(30);
    //        gen.setMaxOutDegree(30);
    ////        gen.setNumIterations(3000000);
    //        gen.setResamplingDegree(10);
    //
    //        gen.generate();
    //
    //        Graph graph = gen.getDag();

    Graph graph = weightedRandomGraph(250, 400);

    List<Integer> degreeCounts = new ArrayList<Integer>();
    Map<Integer, Integer> degreeCount = new HashMap<Integer, Integer>();

    for (Node node : graph.getNodes()) {
      int degree = graph.getNumEdges(node);
      degreeCounts.add(degree);

      if (degreeCount.get(degree) == null) {
        degreeCount.put(degree, 0);
      }

      degreeCount.put(degree, degreeCount.get(degree) + 1);
    }

    Collections.sort(degreeCounts);
    System.out.println(degreeCounts);
    List<Integer> _degrees = new ArrayList<Integer>(degreeCount.keySet());
    Collections.sort(_degrees);

    for (int i : _degrees) {
      int j = degreeCount.get(i);
      //            System.out.println(i + " " + j);
      System.out.println(log(i + 1) + " " + log(j));
    }

    System.out.println("\nCPL = " + characteristicPathLength(graph));

    Graph erGraph = erdosRenyiGraph(200, 200);
    System.out.println("\n ER CPL = " + characteristicPathLength(erGraph));
  }
  /**
   * Transforms a DAG represented in graph <code>graph</code> into a maximally directed pattern
   * (PDAG) by modifying <code>g</code> itself. Based on the algorithm described in Chickering
   * (2002) "Optimal structure identification with greedy search" Journal of Machine Learning
   * Research. It works for both BayesNets and SEMs. R. Silva, June 2004
   */
  public static void dagToPdag(Graph graph) {
    // do topological sort on the nodes
    Graph graphCopy = new EdgeListGraph(graph);
    Node orderedNodes[] = new Node[graphCopy.getNodes().size()];
    int count = 0;
    while (graphCopy.getNodes().size() > 0) {
      Set<Node> exogenousNodes = new HashSet<Node>();

      for (Node next : graphCopy.getNodes()) {
        if (graphCopy.isExogenous(next)) {
          exogenousNodes.add(next);
          orderedNodes[count++] = graph.getNode(next.getName());
        }
      }

      graphCopy.removeNodes(new ArrayList<Node>(exogenousNodes));
    }
    // ordered edges - improvised, inefficient implementation
    count = 0;
    Edge edges[] = new Edge[graph.getNumEdges()];
    boolean edgeOrdered[] = new boolean[graph.getNumEdges()];
    Edge orderedEdges[] = new Edge[graph.getNumEdges()];

    for (Edge edge : graph.getEdges()) {
      edges[count++] = edge;
    }

    for (int i = 0; i < edges.length; i++) {
      edgeOrdered[i] = false;
    }

    while (count > 0) {
      for (Node orderedNode : orderedNodes) {
        for (int k = orderedNodes.length - 1; k >= 0; k--) {
          for (int q = 0; q < edges.length; q++) {
            if (!edgeOrdered[q]
                && edges[q].getNode1() == orderedNodes[k]
                && edges[q].getNode2() == orderedNode) {
              edgeOrdered[q] = true;
              orderedEdges[orderedEdges.length - count] = edges[q];
              count--;
            }
          }
        }
      }
    }

    // label edges
    boolean compelledEdges[] = new boolean[graph.getNumEdges()];
    boolean reversibleEdges[] = new boolean[graph.getNumEdges()];
    for (int i = 0; i < graph.getNumEdges(); i++) {
      compelledEdges[i] = false;
      reversibleEdges[i] = false;
    }
    for (int i = 0; i < graph.getNumEdges(); i++) {
      if (compelledEdges[i] || reversibleEdges[i]) {
        continue;
      }
      Node x = orderedEdges[i].getNode1();
      Node y = orderedEdges[i].getNode2();
      for (int j = 0; j < orderedEdges.length; j++) {
        if (orderedEdges[j].getNode2() == x && compelledEdges[j]) {
          Node w = orderedEdges[j].getNode1();
          if (!graph.isParentOf(w, y)) {
            for (int k = 0; k < orderedEdges.length; k++) {
              if (orderedEdges[k].getNode2() == y) {
                compelledEdges[k] = true;
                break;
              }
            }
          } else {
            for (int k = 0; k < orderedEdges.length; k++) {
              if (orderedEdges[k].getNode1() == w && orderedEdges[k].getNode2() == y) {
                compelledEdges[k] = true;
                break;
              }
            }
          }
        }
        if (compelledEdges[i]) {
          break;
        }
      }
      if (compelledEdges[i]) {
        continue;
      }
      boolean foundZ = false;

      for (Edge orderedEdge : orderedEdges) {
        Node z = orderedEdge.getNode1();
        if (z != x && orderedEdge.getNode2() == y && !graph.isParentOf(z, x)) {
          compelledEdges[i] = true;
          for (int k = i + 1; k < graph.getNumEdges(); k++) {
            if (orderedEdges[k].getNode2() == y && !reversibleEdges[k]) {
              compelledEdges[k] = true;
            }
          }
          foundZ = true;
          break;
        }
      }

      if (!foundZ) {
        reversibleEdges[i] = true;

        for (int j = i + 1; j < orderedEdges.length; j++) {
          if (!compelledEdges[j] && orderedEdges[j].getNode2() == y) {
            reversibleEdges[j] = true;
          }
        }
      }
    }

    // undirect edges that are reversible
    for (int i = 0; i < reversibleEdges.length; i++) {
      if (reversibleEdges[i]) {
        graph.setEndpoint(orderedEdges[i].getNode1(), orderedEdges[i].getNode2(), Endpoint.TAIL);
        graph.setEndpoint(orderedEdges[i].getNode2(), orderedEdges[i].getNode1(), Endpoint.TAIL);
      }
    }
  }
Example #4
0
  /** Do an actual deletion (Definition 13 from Chickering, 2002). */
  private void delete(Node x, Node y, List<Node> subset, Graph graph, double bump) {

    Edge trueEdge = null;

    if (trueGraph != null) {
      Node _x = trueGraph.getNode(x.getName());
      Node _y = trueGraph.getNode(y.getName());
      trueEdge = trueGraph.getEdge(_x, _y);
    }

    if (log && verbose) {
      Edge oldEdge = graph.getEdge(x, y);

      String label = trueGraph != null && trueEdge != null ? "*" : "";
      TetradLogger.getInstance()
          .log(
              "deletedEdges",
              (graph.getNumEdges() - 1)
                  + ". DELETE "
                  + oldEdge
                  + " "
                  + subset
                  + " ("
                  + bump
                  + ") "
                  + label);
      out.println(
          (graph.getNumEdges() - 1)
              + ". DELETE "
              + oldEdge
              + " "
              + subset
              + " ("
              + bump
              + ") "
              + label);
    } else {
      int numEdges = graph.getNumEdges() - 1;
      if (numEdges % 50 == 0) out.println(numEdges);
    }

    graph.removeEdge(x, y);

    for (Node h : subset) {
      Edge oldEdge = graph.getEdge(y, h);

      graph.removeEdge(y, h);
      graph.addDirectedEdge(y, h);

      if (log) {
        TetradLogger.getInstance()
            .log("directedEdges", "--- Directing " + oldEdge + " to " + graph.getEdge(y, h));
      }

      if (verbose) {
        out.println("--- Directing " + oldEdge + " to " + graph.getEdge(y, h));
      }

      if (Edges.isUndirectedEdge(graph.getEdge(x, h))) {
        if (!graph.isAdjacentTo(x, h))
          throw new IllegalArgumentException("Not adjacent: " + x + ", " + h);
        oldEdge = graph.getEdge(x, h);

        graph.removeEdge(x, h);
        graph.addDirectedEdge(x, h);

        if (log) {
          TetradLogger.getInstance()
              .log("directedEdges", "--- Directing " + oldEdge + " to " + graph.getEdge(x, h));
        }

        if (verbose) {
          out.println("--- Directing " + oldEdge + " to " + graph.getEdge(x, h));
        }
      }
    }
  }
Example #5
0
  // serial.
  private void insert(Node x, Node y, List<Node> t, Graph graph, double bump) {
    if (graph.isAdjacentTo(x, y)) {
      return; // The initial graph may already have put this edge in the graph.
      //            throw new IllegalArgumentException(x + " and " + y + " are already adjacent in
      // the graph.");
    }

    Edge trueEdge = null;

    if (trueGraph != null) {
      Node _x = trueGraph.getNode(x.getName());
      Node _y = trueGraph.getNode(y.getName());
      trueEdge = trueGraph.getEdge(_x, _y);
    }

    graph.addDirectedEdge(x, y);

    if (log) {
      String label = trueGraph != null && trueEdge != null ? "*" : "";
      TetradLogger.getInstance()
          .log(
              "insertedEdges",
              graph.getNumEdges()
                  + ". INSERT "
                  + graph.getEdge(x, y)
                  + " "
                  + t
                  + " "
                  + bump
                  + " "
                  + label);
    } else {
      int numEdges = graph.getNumEdges() - 1;
      if (verbose) {
        if (numEdges % 50 == 0) out.println(numEdges);
      }
    }

    if (verbose) {
      String label = trueGraph != null && trueEdge != null ? "*" : "";
      out.println(
          graph.getNumEdges()
              + ". INSERT "
              + graph.getEdge(x, y)
              + " "
              + t
              + " "
              + bump
              + " "
              + label);
    } else {
      int numEdges = graph.getNumEdges() - 1;
      if (verbose) {
        if (numEdges % 50 == 0) out.println(numEdges);
      }
    }

    for (Node _t : t) {
      Edge oldEdge = graph.getEdge(_t, y);

      if (oldEdge == null) throw new IllegalArgumentException("Not adjacent: " + _t + ", " + y);

      graph.removeEdge(_t, y);
      graph.addDirectedEdge(_t, y);

      if (log && verbose) {
        TetradLogger.getInstance()
            .log("directedEdges", "--- Directing " + oldEdge + " to " + graph.getEdge(_t, y));
        out.println("--- Directing " + oldEdge + " to " + graph.getEdge(_t, y));
      }
    }
  }