Example #1
0
  public void randomwalk(int v) {
    // weighted random walk
    RandomWalk rw = new RandomWalk(p_w);
    int status = rw.stay_move();
    ArrayList<Integer> walkpath = new ArrayList<Integer>();
    Integer u = v;
    walkpath.add(v);
    while (status == 1) // keep moving until status = 0
    {
      u = rw.move(G, u);
      if (u == null) // no neighbors
      break;
      if (!walkpath.contains(u)) walkpath.add(u);
      status = rw.stay_move();
    }
    //		System.out.println("walking length: "+ Integer.toString(walkpath.size()));

    for (int i = 0; i < walkpath.size(); i++)
      for (int j = i + 1; j < walkpath.size(); j++) {
        int m = walkpath.get(i);
        int n = walkpath.get(j);
        if (!G.containsEdge(m, n)) {
          double distance = Math.abs(socialposition.get(m) - socialposition.get(n));
          double prob = linkprob(distance, (j - i));
          if (random.nextDouble() < prob) {
            G.setEdgeWeight((DefaultWeightedEdge) G.addEdge(m, n), 1);
          }
        } else {
          DefaultWeightedEdge edge = G.getEdge(m, n);
          G.setEdgeWeight(edge, G.getEdgeWeight(edge) + 1);
        }
      }
    return;
  }
  /**
   * Add edges connecting this cell and its 8 (or less) neighbors
   *
   * @param row
   * @param col
   */
  private void addEdges(int row, int col) {
    System.out.println("Adding (" + row + ", " + col + ")");
    // Add weighted edge
    // North
    if (row > 0 && g.containsVertex(cellContainer[row][col])) {
      g.addEdge(cellContainer[row][col], cellContainer[row - 1][col]);
      g.setEdgeWeight(g.getEdge(cellContainer[row][col], cellContainer[row - 1][col]), 1);
      // NE
      if (col < colCount - 1 && g.containsVertex(cellContainer[row - 1][col + 1])) {
        g.addEdge(cellContainer[row][col], cellContainer[row - 1][col + 1]);
        g.setEdgeWeight(
            g.getEdge(cellContainer[row][col], cellContainer[row - 1][col + 1]), Math.sqrt(2.0));
      }
    }

    // East
    if (col < colCount - 1 && g.containsVertex(cellContainer[row][col + 1])) {
      g.addEdge(cellContainer[row][col], cellContainer[row][col + 1]);
      g.setEdgeWeight(g.getEdge(cellContainer[row][col], cellContainer[row][col + 1]), 1);

      // SE
      if (row < rowCount - 1 && g.containsVertex(cellContainer[row + 1][col + 1])) {
        g.addEdge(cellContainer[row][col], cellContainer[row + 1][col + 1]);
        g.setEdgeWeight(
            g.getEdge(cellContainer[row][col], cellContainer[row + 1][col + 1]), Math.sqrt(2.0));
      }
    }

    // South
    if (row < rowCount - 1 && g.containsVertex(cellContainer[row + 1][col])) {
      g.addEdge(cellContainer[row][col], cellContainer[row + 1][col]);
      g.setEdgeWeight(g.getEdge(cellContainer[row][col], cellContainer[row + 1][col]), 1);

      // SW
      if (col > 0 && g.containsVertex(cellContainer[row + 1][col - 1])) {
        g.addEdge(cellContainer[row][col], cellContainer[row + 1][col - 1]);
        g.setEdgeWeight(
            g.getEdge(cellContainer[row][col], cellContainer[row + 1][col - 1]), Math.sqrt(2.0));
      }
    }

    // West
    if (col > 0 && g.containsVertex(cellContainer[row][col - 1])) {
      g.addEdge(cellContainer[row][col], cellContainer[row][col - 1]);
      g.setEdgeWeight(g.getEdge(cellContainer[row][col], cellContainer[row][col - 1]), 1);

      // NW
      if (row > 0 && g.containsVertex(cellContainer[row - 1][col - 1])) {
        g.addEdge(cellContainer[row][col], cellContainer[row - 1][col - 1]);
        g.setEdgeWeight(
            g.getEdge(cellContainer[row][col], cellContainer[row - 1][col - 1]), Math.sqrt(2.0));
      }
    }
  }
Example #3
0
 // global attachment -- with prob 1
 public void globalattach(int v) throws IOException {
   Integer u = random.nextInt(N);
   while (u == v) u = random.nextInt(N);
   if (!G.containsEdge(v, u)) {
     G.setEdgeWeight((DefaultWeightedEdge) G.addEdge(v, u), 1);
   } else {
     DefaultWeightedEdge edge = G.getEdge(v, u);
     G.setEdgeWeight(edge, G.getEdgeWeight(edge) + 1);
   }
 }
  // Create shortest-path graph for every vertex by depth-first traversal algorithm
  private Multigraph<String, DefaultEdge> DijkstraAlgorithm(
      WeightedMultigraph<String, DefaultWeightedEdge> originalGraph, String thisVertex) {

    // 1. Simplify the multi-graph of the active power flow into a simple graph
    SimpleWeightedGraph<String, DefaultWeightedEdge> originalSimpleGraph =
        new SimpleWeightedGraph<String, DefaultWeightedEdge>(DefaultWeightedEdge.class);
    for (String curVertex : originalGraph.vertexSet()) originalSimpleGraph.addVertex(curVertex);
    for (DefaultWeightedEdge curEdge : originalGraph.edgeSet()) {
      String sourceVertex = originalGraph.getEdgeSource(curEdge);
      String targetVertex = originalGraph.getEdgeTarget(curEdge);
      if (originalSimpleGraph.containsEdge(sourceVertex, targetVertex)) {
        DefaultWeightedEdge modifiedEdge = originalSimpleGraph.getEdge(sourceVertex, targetVertex);
        double newEdgeWeight =
            originalSimpleGraph.getEdgeWeight(modifiedEdge) + originalGraph.getEdgeWeight(curEdge);
        originalSimpleGraph.setEdgeWeight(modifiedEdge, newEdgeWeight);
      } else {
        DefaultWeightedEdge newEdge = new DefaultWeightedEdge();
        originalSimpleGraph.addEdge(sourceVertex, targetVertex, newEdge);
        originalSimpleGraph.setEdgeWeight(newEdge, originalGraph.getEdgeWeight(curEdge));
      }
    }
    // Issue (2010/10/25): Maybe larger amount of active power transfer still means weaker
    // relationship between the two terminal buses of a certain branch,
    // thus originalSimpleGraph other than inverseGraph should be used here.
    // Use the inverse of active power to build a new weighted directed graph (the larger the active
    // power is, the close the two buses will be)
    //		SimpleDirectedWeightedGraph<String, DefaultWeightedEdge> inverseGraph =
    //			new SimpleDirectedWeightedGraph<String, DefaultWeightedEdge>(DefaultWeightedEdge.class);
    //		for (String curVertex : originalSimpleGraph.vertexSet())
    //			inverseGraph.addVertex(curVertex);
    //		for (DefaultWeightedEdge curEdge : originalSimpleGraph.edgeSet()) {
    //			String sourceVertex = originalSimpleGraph.getEdgeSource(curEdge);
    //			String targetVertex = originalSimpleGraph.getEdgeTarget(curEdge);
    //			DefaultWeightedEdge newEdge = new DefaultWeightedEdge();
    //			inverseGraph.addEdge(sourceVertex, targetVertex, newEdge);
    //			inverseGraph.setEdgeWeight(newEdge, 1 / originalSimpleGraph.getEdgeWeight(curEdge));
    //		}
    // 2. Initialize the map of vertices and the corresponding weights (distance from current vertex
    // to the first vertex)
    HashMap<String, Double> mapVertexShortestDistance = new HashMap<String, Double>();
    //		for (String thisOriginalVertex : inverseGraph.vertexSet())
    for (String thisOriginalVertex : originalSimpleGraph.vertexSet())
      mapVertexShortestDistance.put(thisOriginalVertex, 10E10);
    // The weight of the first vertex is zero
    mapVertexShortestDistance.put(thisVertex, 0.0);

    // 3. Depth-first traversing, update the shortest-path values
    Stack<String> bfiVertices =
        new Stack<String>(); // Stack to store passed vertices in a breadth-first traversing
    // The map of a weighted edge and the flag of having been visited
    //		HashMap<DefaultWeightedEdge, Boolean> mapEdgeVisited = new HashMap<DefaultWeightedEdge,
    // Boolean>();
    //		for (DefaultWeightedEdge thisEdge : inverseGraph.edgeSet())
    //			mapEdgeVisited.put(thisEdge, false);
    String currentVertex = thisVertex;
    bfiVertices.push(currentVertex);
    //		System.out.println(bfiVertices.toString());
    while (!bfiVertices.isEmpty()) {
      // Operate the following codes for those edges started with current vertex
      boolean hasNewEdge = false;
      //			for (DefaultWeightedEdge curEdge : inverseGraph.outgoingEdgesOf(currentVertex)) {
      for (DefaultWeightedEdge curEdge : originalSimpleGraph.edgesOf(currentVertex)) {
        //				if (!mapEdgeVisited.get(curEdge)) {	// Used for those edges that have not been treated
        // yet
        // 3.1. Mark current edge as already been visited
        //					mapEdgeVisited.put(curEdge, true);
        //				String nextVertex = inverseGraph.getEdgeTarget(curEdge);
        String nextVertex = originalSimpleGraph.getEdgeTarget(curEdge);
        // 3.2. Update shortest-path values
        double curSD = mapVertexShortestDistance.get(currentVertex);
        //					double edgeWeight = inverseGraph.getEdgeWeight(curEdge);
        double edgeWeight = originalSimpleGraph.getEdgeWeight(curEdge);
        double newSD = curSD + edgeWeight;
        if (mapVertexShortestDistance.get(nextVertex) > newSD) {
          hasNewEdge = true;
          mapVertexShortestDistance.put(nextVertex, newSD);
          // 3.3. Push the target vertex of current edge into the stack
          bfiVertices.push(nextVertex);
          //						System.out.println(bfiVertices.toString());
          break;
          //						System.out.println("New shortest path [" + nextVertex + "]: " + newSD);
        }
        //				}
      }
      if (!hasNewEdge) {
        bfiVertices.pop();
      }
      if (!bfiVertices.isEmpty()) currentVertex = bfiVertices.peek();
    }
    // 4. Create shortest-path digraph of current vertex
    // 4.1. Initialize the shortest-path digraph
    Multigraph<String, DefaultEdge> shortestPathGraph =
        new Multigraph<String, DefaultEdge>(DefaultEdge.class);
    // 4.2. Add all qualified edges
    //		for (DefaultWeightedEdge curEdge : inverseGraph.edgeSet()) {
    for (DefaultWeightedEdge curEdge : originalSimpleGraph.edgeSet()) {
      // 4.2.1. Evaluate if current edge is suitable
      //			String sourceVertex = inverseGraph.getEdgeSource(curEdge);
      //			String targetVertex = inverseGraph.getEdgeTarget(curEdge);
      String sourceVertex = originalSimpleGraph.getEdgeSource(curEdge);
      String targetVertex = originalSimpleGraph.getEdgeTarget(curEdge);
      //			if (Math.abs(inverseGraph.getEdgeWeight(curEdge) -
      if (originalSimpleGraph.getEdgeWeight(curEdge) > 1.0E-5) {
        if (Math.abs(
                originalSimpleGraph.getEdgeWeight(curEdge)
                    - (mapVertexShortestDistance.get(targetVertex)
                        - mapVertexShortestDistance.get(sourceVertex)))
            < 1.0E-5) {
          // 4.2.2. Add suitable edge that found just now
          DefaultEdge newEdge = new DefaultEdge();
          if (!shortestPathGraph.containsVertex(sourceVertex))
            shortestPathGraph.addVertex(sourceVertex);
          if (!shortestPathGraph.containsVertex(targetVertex))
            shortestPathGraph.addVertex(targetVertex);
          shortestPathGraph.addEdge(sourceVertex, targetVertex, newEdge);
        }
      }
    }
    return shortestPathGraph;
  }