コード例 #1
0
 public void suggest(File locationFile, int locationListSize, File outputFile, int hubSize) {
   // WARNING: this code is VERY DIRTY.
   // It's JUST good enough to generate the hubs for Belgium once (because we only need to
   // determine them once).
   // Further research to generate good hubs is needed.
   List<AirLocation> locationList = readAirLocationFile(locationFile);
   if (locationListSize > locationList.size()) {
     throw new IllegalArgumentException(
         "The locationListSize ("
             + locationListSize
             + ") is larger than the locationList size ("
             + locationList.size()
             + ").");
   }
   locationList = subselectLocationList(locationListSize, locationList);
   Map<Point, Point> fromPointMap = new LinkedHashMap<Point, Point>(locationListSize * 10);
   Map<Point, Point> toPointMap = new LinkedHashMap<Point, Point>(locationListSize * 10);
   int rowIndex = 0;
   double maxAirDistance = 0.0;
   for (AirLocation fromAirLocation : locationList) {
     for (AirLocation toAirLocation : locationList) {
       double airDistance = fromAirLocation.getAirDistanceDouble(toAirLocation);
       if (airDistance > maxAirDistance) {
         maxAirDistance = airDistance;
       }
     }
   }
   double airDistanceThreshold = maxAirDistance / 10.0;
   for (AirLocation fromAirLocation : locationList) {
     for (AirLocation toAirLocation : locationList) {
       double distance;
       if (fromAirLocation != toAirLocation) {
         GHRequest request =
             new GHRequest(
                     fromAirLocation.getLatitude(),
                     fromAirLocation.getLongitude(),
                     toAirLocation.getLatitude(),
                     toAirLocation.getLongitude())
                 .setVehicle("car");
         GHResponse response = graphHopper.route(request);
         if (response.hasErrors()) {
           throw new IllegalStateException(
               "GraphHopper gave " + response.getErrors().size() + " errors. First error chained.",
               response.getErrors().get(0));
         }
         // Distance should be in km, not meter
         distance = response.getDistance() / 1000.0;
         if (distance == 0.0) {
           throw new IllegalArgumentException(
               "The fromAirLocation ("
                   + fromAirLocation
                   + ") and toAirLocation ("
                   + toAirLocation
                   + ") are the same.");
         }
         PointList ghPointList = response.getPoints();
         PointPart previousFromPointPart = null;
         PointPart previousToPointPart = null;
         double previousLatitude = Double.NaN;
         double previousLongitude = Double.NaN;
         for (int i = 0; i < ghPointList.size(); i++) {
           double latitude = ghPointList.getLatitude(i);
           double longitude = ghPointList.getLongitude(i);
           if (latitude == previousLatitude && longitude == previousLongitude) {
             continue;
           }
           if (calcAirDistance(
                   latitude,
                   longitude,
                   fromAirLocation.getLatitude(),
                   fromAirLocation.getLongitude())
               < airDistanceThreshold) {
             Point fromPoint = new Point(latitude, longitude);
             Point oldFromPoint = fromPointMap.get(fromPoint);
             if (oldFromPoint == null) {
               // Initialize fromPoint instance
               fromPoint.pointPartMap = new LinkedHashMap<AirLocation, PointPart>();
               fromPointMap.put(fromPoint, fromPoint);
             } else {
               // Reuse existing fromPoint instance
               fromPoint = oldFromPoint;
             }
             PointPart fromPointPart = fromPoint.pointPartMap.get(fromAirLocation);
             if (fromPointPart == null) {
               fromPointPart = new PointPart(fromPoint, fromAirLocation);
               fromPoint.pointPartMap.put(fromAirLocation, fromPointPart);
               fromPointPart.previousPart = previousFromPointPart;
             }
             fromPointPart.count++;
             previousFromPointPart = fromPointPart;
           }
           if (calcAirDistance(
                   latitude, longitude, toAirLocation.getLatitude(), toAirLocation.getLongitude())
               < airDistanceThreshold) {
             Point toPoint = new Point(latitude, longitude);
             Point oldToPoint = toPointMap.get(toPoint);
             if (oldToPoint == null) {
               // Initialize toPoint instance
               toPoint.pointPartMap = new LinkedHashMap<AirLocation, PointPart>();
               toPointMap.put(toPoint, toPoint);
             } else {
               // Reuse existing toPoint instance
               toPoint = oldToPoint;
             }
             // Basically do the same as fromPointPart, but while traversing in the other direction
             PointPart toPointPart = toPoint.pointPartMap.get(toAirLocation);
             boolean newToPointPart = false;
             if (toPointPart == null) {
               toPointPart = new PointPart(toPoint, toAirLocation);
               toPoint.pointPartMap.put(toAirLocation, toPointPart);
               newToPointPart = true;
             }
             if (previousToPointPart != null) {
               previousToPointPart.previousPart = toPointPart;
             }
             toPointPart.count++;
             if (newToPointPart) {
               previousToPointPart = toPointPart;
             } else {
               previousToPointPart = null;
             }
           }
           previousLatitude = latitude;
           previousLongitude = longitude;
         }
       }
     }
     logger.debug("  Finished routes for rowIndex {}/{}", rowIndex, locationList.size());
     rowIndex++;
   }
   Set<Point> hubPointList = new LinkedHashSet<Point>(20);
   extractFromHubs(new ArrayList<Point>(fromPointMap.values()), hubPointList, (hubSize + 1) / 2);
   extractFromHubs(new ArrayList<Point>(toPointMap.values()), hubPointList, hubSize / 2);
   logger.info("Writing hubs...");
   BufferedWriter vrpWriter = null;
   try {
     vrpWriter =
         new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outputFile), "UTF-8"));
     vrpWriter.write("HUB_COORD_SECTION\n");
     int id = 0;
     for (Point point : hubPointList) {
       vrpWriter.write("" + id + " " + point.latitude + " " + point.longitude + " " + id + "\n");
       id++;
     }
     vrpWriter.write("\n\nGOOGLE MAPS\n");
     for (Point point : hubPointList) {
       vrpWriter.write("" + id + "\t0\t" + point.latitude + "," + point.longitude + "\n");
       id++;
     }
   } catch (IOException e) {
     throw new IllegalArgumentException(
         "Could not read the locationFile ("
             + locationFile.getName()
             + ") or write the vrpOutputFile ("
             + outputFile.getName()
             + ").",
         e);
   } finally {
     IOUtils.closeQuietly(vrpWriter);
   }
   // Throw in google docs spreadsheet and use add-on Mapping Sheets to visualize.
 }
コード例 #2
0
 private void extractFromHubs(List<Point> pointList, Set<Point> hubPointList, int hubSize) {
   //        logger.info("Filtering points below threshold...");
   //        fromPointMap = null;
   //        int THRESHOLD = 10;
   //        for (Iterator<Point> it = pointList.iterator(); it.hasNext(); ) {
   //            Point point = it.next();
   //            if (point.pointPartMap.values().size() < THRESHOLD) {
   //                it.remove();
   //                point.removed = true;
   //            }
   //        }
   //        for (Point point : pointList) {
   //            for (PointPart pointPart : point.pointPartMap.values()) {
   //                PointPart previousPart = pointPart.previousPart;
   //                while (previousPart != null && previousPart.point.removed) {
   //                    previousPart = previousPart.previousPart;
   //                }
   //                pointPart.previousPart = previousPart;
   //            }
   //        }
   logger.info("Extracting hubs...");
   for (int i = 0; i < hubSize; i++) {
     logger.info("  {} / {} with remaining pointListSize ({})", i, hubSize, pointList.size());
     // Make the biggest merger of 2 big streams into 1 stream a hub.
     int maxCount = -1;
     Point maxCountPoint = null;
     for (Point point : pointList) {
       int count = 0;
       for (PointPart pointPart : point.pointPartMap.values()) {
         count += pointPart.count;
       }
       if (count > maxCount) {
         maxCount = count;
         maxCountPoint = point;
       }
     }
     if (maxCountPoint == null) {
       throw new IllegalStateException("No maxCountPoint (" + maxCountPoint + ") found.");
     }
     maxCountPoint.hub = true;
     pointList.remove(maxCountPoint);
     hubPointList.add(maxCountPoint);
     // Remove trailing parts
     for (Iterator<Point> pointIt = pointList.iterator(); pointIt.hasNext(); ) {
       Point point = pointIt.next();
       for (Iterator<PointPart> partIt = point.pointPartMap.values().iterator();
           partIt.hasNext(); ) {
         PointPart pointPart = partIt.next();
         if (pointPart.comesAfterHub()) {
           partIt.remove();
         }
       }
       if (point.pointPartMap.isEmpty()) {
         point.removed = true;
         pointIt.remove();
       }
     }
     // Subtract prefix parts
     for (PointPart pointPart : maxCountPoint.pointPartMap.values()) {
       PointPart ancestorPart = pointPart.previousPart;
       while (ancestorPart != null) {
         ancestorPart.count -= pointPart.count;
         //                    if (ancestorPart.count < 0) {
         //                        throw new IllegalStateException("Impossible state"); // TODO
         // FIXME Does happen! Probably because some paths hit the same point twice at different
         // elevation
         //                    }
         if (ancestorPart.count <= 0) {
           ancestorPart.point.pointPartMap.remove(ancestorPart.anchor);
         }
         ancestorPart = ancestorPart.previousPart;
       }
     }
   }
 }