コード例 #1
0
ファイル: RuinClusters.java プロジェクト: muzuro/jsprit
  private void ruin(
      Collection<VehicleRoute> vehicleRoutes, int nOfJobs2BeRemoved, List<Job> unassignedJobs) {
    if (vrp.getJobs().values().size() == 0) return;
    Map<Job, VehicleRoute> mappedRoutes = map(vehicleRoutes);
    int toRemove = nOfJobs2BeRemoved;

    Collection<Job> lastRemoved = new ArrayList<Job>();
    Set<VehicleRoute> ruined = new HashSet<VehicleRoute>();
    Set<Job> removed = new HashSet<Job>();
    Set<VehicleRoute> cycleCandidates = new HashSet<VehicleRoute>();
    while (toRemove > 0) {
      Job target;
      VehicleRoute targetRoute = null;
      if (lastRemoved.isEmpty()) {
        target = RandomUtils.nextJob(vrp.getJobs().values(), random);
        targetRoute = mappedRoutes.get(target);
      } else {
        target = RandomUtils.nextJob(lastRemoved, random);
        Iterator<Job> neighborIterator =
            jobNeighborhoods.getNearestNeighborsIterator(nOfJobs2BeRemoved, target);
        while (neighborIterator.hasNext()) {
          Job j = neighborIterator.next();
          if (!removed.contains(j) && !ruined.contains(mappedRoutes.get(j))) {
            targetRoute = mappedRoutes.get(j);
            break;
          }
        }
        lastRemoved.clear();
      }
      if (targetRoute == null) break;
      if (cycleCandidates.contains(targetRoute)) break;
      if (ruined.contains(targetRoute)) {
        cycleCandidates.add(targetRoute);
        break;
      }
      DBSCANClusterer dbscan = new DBSCANClusterer(vrp.getTransportCosts());
      dbscan.setRandom(random);
      dbscan.setMinPts(minPts);
      dbscan.setEpsFactor(epsFactor);
      List<Job> cluster = dbscan.getRandomCluster(targetRoute);
      for (Job j : cluster) {
        if (toRemove == 0) break;
        if (removeJob(j, vehicleRoutes)) {
          lastRemoved.add(j);
          unassignedJobs.add(j);
        }
        toRemove--;
      }
      ruined.add(targetRoute);
    }
  }
コード例 #2
0
 private Collection<Job> ruinRoutes_(
     Collection<VehicleRoute> vehicleRoutes,
     Job targetJob,
     int nOfJobs2BeRemoved,
     Set<Job> available) {
   List<Job> unassignedJobs = new ArrayList<Job>();
   int nNeighbors = nOfJobs2BeRemoved - 1;
   removeJob(targetJob, vehicleRoutes);
   unassignedJobs.add(targetJob);
   Iterator<Job> neighborhoodIterator =
       jobNeighborhoods.getNearestNeighborsIterator(nNeighbors, targetJob);
   while (neighborhoodIterator.hasNext()) {
     Job job = neighborhoodIterator.next();
     if (available != null) available.remove(job);
     if (removeJob(job, vehicleRoutes)) {
       unassignedJobs.add(job);
     }
   }
   return unassignedJobs;
 }