示例#1
0
 private static List<String> shortestCycle(DirectedGraph<String, DefaultEdge> graph) {
   FloydWarshallShortestPaths<String, DefaultEdge> floyd = new FloydWarshallShortestPaths<>(graph);
   int minDistance = Integer.MAX_VALUE;
   String minSource = null;
   String minDestination = null;
   for (DefaultEdge edge : graph.edgeSet()) {
     String src = graph.getEdgeSource(edge);
     String dst = graph.getEdgeTarget(edge);
     int dist = (int) Math.round(floyd.shortestDistance(dst, src)); // from dst to src
     if (dist < 0) {
       continue;
     }
     if (dist < minDistance) {
       minDistance = dist;
       minSource = src;
       minDestination = dst;
     }
   }
   if (minSource == null) {
     return null;
   }
   GraphPath<String, DefaultEdge> shortestPath = floyd.getShortestPath(minDestination, minSource);
   List<String> pathVertexList = Graphs.getPathVertexList(shortestPath);
   // note: pathVertexList will be [a, a] instead of [a] when the shortest path is a loop edge
   if (!Objects.equals(shortestPath.getStartVertex(), shortestPath.getEndVertex())) {
     pathVertexList.add(pathVertexList.get(0));
   }
   return pathVertexList;
 }
  public static void main(String[] args) {

    TAP tap = TAP.BRAESS(TADriver.class);

    List<String> odpairs = new ArrayList<>(tap.getOdpairs().keySet());
    Collections.sort(odpairs);
    String header = "average_tt";
    String results = "";

    // calculate all-shortest-path
    FloydWarshallShortestPaths fws = new FloydWarshallShortestPaths(tap.getGraph());

    // update the edges cost
    for (String odPair : odpairs) {
      String origin = odPair.split("-")[0];
      String destination = odPair.split("-")[1];

      GraphPath path = fws.getShortestPath(origin, destination);

      for (Object e : path.getEdgeList()) {
        StandardEdge edge = (StandardEdge) e;
        edge.incrementTotalFlow(tap.getOdpairs().get(odPair).demandSize());
      }
    }

    // evaluate cost per OD pair
    for (String odPair : odpairs) {
      header += Params.COLUMN_SEPARATOR + odPair;

      String origin = odPair.split("-")[0];
      String destination = odPair.split("-")[1];

      results += Params.COLUMN_SEPARATOR + fws.getShortestPath(origin, destination).getWeight();
    }

    // print Result
    List<AbstractEdge> edges = new ArrayList<>(tap.getGraph().edgeSet());
    Collections.sort(edges);
    double cost = 0.0;

    for (AbstractEdge e : edges) {
      header += Params.COLUMN_SEPARATOR + e.getName();
      results += Params.COLUMN_SEPARATOR + e.getTotalFlow();
      cost += (e.getCost() * e.getTotalFlow()) / tap.getDrivers().size();
    }
    System.out.println(header);
    System.out.println(cost + results);
  }