Beispiel #1
0
 private void writeInitialRoutes(XMLConf xmlConfig) {
   if (vrp.getInitialVehicleRoutes().isEmpty()) return;
   String path = "initialRoutes.route";
   int routeCounter = 0;
   for (VehicleRoute route : vrp.getInitialVehicleRoutes()) {
     xmlConfig.setProperty(path + "(" + routeCounter + ").driverId", route.getDriver().getId());
     xmlConfig.setProperty(path + "(" + routeCounter + ").vehicleId", route.getVehicle().getId());
     xmlConfig.setProperty(path + "(" + routeCounter + ").start", route.getStart().getEndTime());
     int actCounter = 0;
     for (TourActivity act : route.getTourActivities().getActivities()) {
       xmlConfig.setProperty(
           path + "(" + routeCounter + ").act(" + actCounter + ")[@type]", act.getName());
       if (act instanceof JobActivity) {
         Job job = ((JobActivity) act).getJob();
         if (job instanceof Service) {
           xmlConfig.setProperty(
               path + "(" + routeCounter + ").act(" + actCounter + ").serviceId", job.getId());
         } else if (job instanceof Shipment) {
           xmlConfig.setProperty(
               path + "(" + routeCounter + ").act(" + actCounter + ").shipmentId", job.getId());
         } else {
           throw new IllegalStateException(
               "cannot write solution correctly since job-type is not know. make sure you use either service or shipment, or another writer");
         }
       }
       xmlConfig.setProperty(
           path + "(" + routeCounter + ").act(" + actCounter + ").arrTime", act.getArrTime());
       xmlConfig.setProperty(
           path + "(" + routeCounter + ").act(" + actCounter + ").endTime", act.getEndTime());
       actCounter++;
     }
     xmlConfig.setProperty(path + "(" + routeCounter + ").end", route.getEnd().getArrTime());
     routeCounter++;
   }
 }
 private static void printLowerBoundOfNuVehicles(VehicleRoutingProblem vrp, int vCap) {
   int demand = 0;
   for (Job j : vrp.getJobs().values()) {
     demand += j.getSize().get(0);
   }
   System.out.println("lowerBound=" + ((double) demand / (double) vCap));
 }
 private double getDeltaAbsoluteFixCost(VehicleRoute route, Vehicle newVehicle, Job job) {
   Capacity load = Capacity.addup(getCurrentMaxLoadInRoute(route), job.getSize());
   //		double load = getCurrentMaxLoadInRoute(route) + job.getCapacityDemand();
   double currentFix = 0.0;
   if (route.getVehicle() != null) {
     if (!(route.getVehicle() instanceof NoVehicle)) {
       currentFix += route.getVehicle().getType().getVehicleCostParams().fix;
     }
   }
   if (!newVehicle.getType().getCapacityDimensions().isGreaterOrEqual(load)) {
     return Double.MAX_VALUE;
   }
   return newVehicle.getType().getVehicleCostParams().fix - currentFix;
 }
 private double getDeltaRelativeFixCost(VehicleRoute route, Vehicle newVehicle, Job job) {
   Capacity currentLoad = getCurrentMaxLoadInRoute(route);
   //		int currentLoad = getCurrentMaxLoadInRoute(route);
   Capacity load = Capacity.addup(currentLoad, job.getSize());
   //		double load = currentLoad + job.getCapacityDemand();
   double currentRelFix = 0.0;
   if (route.getVehicle() != null) {
     if (!(route.getVehicle() instanceof NoVehicle)) {
       currentRelFix +=
           route.getVehicle().getType().getVehicleCostParams().fix
               * Capacity.divide(
                   currentLoad, route.getVehicle().getType().getCapacityDimensions());
     }
   }
   if (!newVehicle.getType().getCapacityDimensions().isGreaterOrEqual(load)) {
     return Double.MAX_VALUE;
   }
   double relativeFixCost =
       newVehicle.getType().getVehicleCostParams().fix
               * (Capacity.divide(load, newVehicle.getType().getCapacityDimensions()))
           - currentRelFix;
   return relativeFixCost;
 }
 static double score(
     Job unassignedJob,
     InsertionData best,
     InsertionData secondBest,
     ScoringFunction scoringFunction) {
   if (best == null) {
     throw new IllegalStateException("cannot insert job " + unassignedJob.getId());
   }
   double score;
   if (secondBest
       == null) { // either there is only one vehicle or there are more vehicles, but they cannot
     // load unassignedJob
     // if only one vehicle, I want the job to be inserted with min iCosts
     // if there are more vehicles, I want this job to be prioritized since there are no
     // alternatives
     score =
         Integer.MAX_VALUE - best.getInsertionCost() + scoringFunction.score(best, unassignedJob);
   } else {
     score =
         (secondBest.getInsertionCost() - best.getInsertionCost())
             + scoringFunction.score(best, unassignedJob);
   }
   return score;
 }
Beispiel #6
0
 private String getSkillString(Job job) {
   return createSkillString(job.getRequiredSkills());
 }
Beispiel #7
0
 private void writeSolutions(XMLConf xmlConfig) {
   if (solutions == null) return;
   String solutionPath = "solutions.solution";
   int counter = 0;
   for (VehicleRoutingProblemSolution solution : solutions) {
     xmlConfig.setProperty(solutionPath + "(" + counter + ").cost", solution.getCost());
     int routeCounter = 0;
     for (VehicleRoute route : solution.getRoutes()) {
       //				xmlConfig.setProperty(solutionPath + "(" + counter + ").routes.route(" + routeCounter
       // + ").cost", route.getCost());
       xmlConfig.setProperty(
           solutionPath + "(" + counter + ").routes.route(" + routeCounter + ").driverId",
           route.getDriver().getId());
       xmlConfig.setProperty(
           solutionPath + "(" + counter + ").routes.route(" + routeCounter + ").vehicleId",
           route.getVehicle().getId());
       xmlConfig.setProperty(
           solutionPath + "(" + counter + ").routes.route(" + routeCounter + ").start",
           route.getStart().getEndTime());
       int actCounter = 0;
       for (TourActivity act : route.getTourActivities().getActivities()) {
         xmlConfig.setProperty(
             solutionPath
                 + "("
                 + counter
                 + ").routes.route("
                 + routeCounter
                 + ").act("
                 + actCounter
                 + ")[@type]",
             act.getName());
         if (act instanceof JobActivity) {
           Job job = ((JobActivity) act).getJob();
           if (job instanceof Service) {
             xmlConfig.setProperty(
                 solutionPath
                     + "("
                     + counter
                     + ").routes.route("
                     + routeCounter
                     + ").act("
                     + actCounter
                     + ").serviceId",
                 job.getId());
           } else if (job instanceof Shipment) {
             xmlConfig.setProperty(
                 solutionPath
                     + "("
                     + counter
                     + ").routes.route("
                     + routeCounter
                     + ").act("
                     + actCounter
                     + ").shipmentId",
                 job.getId());
           } else {
             throw new IllegalStateException(
                 "cannot write solution correctly since job-type is not know. make sure you use either service or shipment, or another writer");
           }
         }
         xmlConfig.setProperty(
             solutionPath
                 + "("
                 + counter
                 + ").routes.route("
                 + routeCounter
                 + ").act("
                 + actCounter
                 + ").arrTime",
             act.getArrTime());
         xmlConfig.setProperty(
             solutionPath
                 + "("
                 + counter
                 + ").routes.route("
                 + routeCounter
                 + ").act("
                 + actCounter
                 + ").endTime",
             act.getEndTime());
         actCounter++;
       }
       xmlConfig.setProperty(
           solutionPath + "(" + counter + ").routes.route(" + routeCounter + ").end",
           route.getEnd().getArrTime());
       routeCounter++;
     }
     int unassignedJobCounter = 0;
     for (Job unassignedJob : solution.getUnassignedJobs()) {
       xmlConfig.setProperty(
           solutionPath
               + "("
               + counter
               + ").unassignedJobs.job("
               + unassignedJobCounter
               + ")[@id]",
           unassignedJob.getId());
       unassignedJobCounter++;
     }
     counter++;
   }
 }
Beispiel #8
0
 private static void printVerbose(
     PrintWriter out, VehicleRoutingProblem problem, VehicleRoutingProblemSolution solution) {
   String leftAlgin = "| %-7s | %-20s | %-21s | %-15s | %-15s | %-15s | %-15s |%n";
   out.format(
       "+--------------------------------------------------------------------------------------------------------------------------------+%n");
   out.printf(
       "| detailed solution                                                                                                              |%n");
   out.format(
       "+---------+----------------------+-----------------------+-----------------+-----------------+-----------------+-----------------+%n");
   out.printf(
       "| route   | vehicle              | activity              | job             | arrTime         | endTime         | costs           |%n");
   int routeNu = 1;
   for (VehicleRoute route : solution.getRoutes()) {
     out.format(
         "+---------+----------------------+-----------------------+-----------------+-----------------+-----------------+-----------------+%n");
     double costs = 0;
     out.format(
         leftAlgin,
         routeNu,
         getVehicleString(route),
         route.getStart().getName(),
         "-",
         "undef",
         Math.round(route.getStart().getEndTime()),
         Math.round(costs));
     TourActivity prevAct = route.getStart();
     for (TourActivity act : route.getActivities()) {
       String jobId;
       if (act instanceof JobActivity) {
         jobId = ((JobActivity) act).getJob().getId();
       } else {
         jobId = "-";
       }
       double c =
           problem
               .getTransportCosts()
               .getTransportCost(
                   prevAct.getLocation(),
                   act.getLocation(),
                   prevAct.getEndTime(),
                   route.getDriver(),
                   route.getVehicle());
       c +=
           problem
               .getActivityCosts()
               .getActivityCost(act, act.getArrTime(), route.getDriver(), route.getVehicle());
       costs += c;
       out.format(
           leftAlgin,
           routeNu,
           getVehicleString(route),
           act.getName(),
           jobId,
           Math.round(act.getArrTime()),
           Math.round(act.getEndTime()),
           Math.round(costs));
       prevAct = act;
     }
     double c =
         problem
             .getTransportCosts()
             .getTransportCost(
                 prevAct.getLocation(),
                 route.getEnd().getLocation(),
                 prevAct.getEndTime(),
                 route.getDriver(),
                 route.getVehicle());
     c +=
         problem
             .getActivityCosts()
             .getActivityCost(
                 route.getEnd(),
                 route.getEnd().getArrTime(),
                 route.getDriver(),
                 route.getVehicle());
     costs += c;
     out.format(
         leftAlgin,
         routeNu,
         getVehicleString(route),
         route.getEnd().getName(),
         "-",
         Math.round(route.getEnd().getArrTime()),
         "undef",
         Math.round(costs));
     routeNu++;
   }
   out.format(
       "+--------------------------------------------------------------------------------------------------------------------------------+%n");
   if (!solution.getUnassignedJobs().isEmpty()) {
     out.format("+----------------+%n");
     out.format("| unassignedJobs |%n");
     out.format("+----------------+%n");
     String unassignedJobAlgin = "| %-14s |%n";
     for (Job j : solution.getUnassignedJobs()) {
       out.format(unassignedJobAlgin, j.getId());
     }
     out.format("+----------------+%n");
   }
 }