예제 #1
0
 public void read(String filename) {
   BufferedReader reader = getReader(filename);
   String line = null;
   boolean firstline = true;
   Coordinate depotCoord = null;
   int customerCount = 0;
   Integer nuOfCustomer = 0;
   while ((line = readLine(reader)) != null) {
     String trimedLine = line.trim();
     if (trimedLine.startsWith("//")) continue;
     String[] tokens = trimedLine.split("\\s+");
     if (firstline) {
       nuOfCustomer = Integer.parseInt(tokens[0]);
       customerCount = 0;
       firstline = false;
     } else if (customerCount <= nuOfCustomer) {
       if (customerCount == 0) {
         depotCoord =
             Coordinate.newInstance(Double.parseDouble(tokens[1]), Double.parseDouble(tokens[2]));
       } else {
         Service.Builder serviceBuilder =
             Service.Builder.newInstance(tokens[0])
                 .addSizeDimension(0, Integer.parseInt(tokens[3]));
         serviceBuilder.setCoord(
             Coordinate.newInstance(Double.parseDouble(tokens[1]), Double.parseDouble(tokens[2])));
         vrpBuilder.addJob(serviceBuilder.build());
       }
       customerCount++;
     } else if (trimedLine.startsWith("v")) {
       VehicleTypeImpl.Builder typeBuilder =
           VehicleTypeImpl.Builder.newInstance("type_" + tokens[1])
               .addCapacityDimension(0, Integer.parseInt(tokens[2]));
       int nuOfVehicles = 1;
       if (vrphType.equals(VrphType.FSMF)) {
         typeBuilder.setFixedCost(Double.parseDouble(tokens[3]));
       } else if (vrphType.equals(VrphType.FSMFD)) {
         typeBuilder.setFixedCost(Double.parseDouble(tokens[3]));
         if (tokens.length > 4) {
           typeBuilder.setCostPerDistance(Double.parseDouble(tokens[4]));
         } else
           throw new IllegalStateException(
               "option " + vrphType + " cannot be applied with this instance");
       } else if (vrphType.equals(VrphType.FSMD)) {
         if (tokens.length > 4) {
           typeBuilder.setCostPerDistance(Double.parseDouble(tokens[4]));
         } else
           throw new IllegalStateException(
               "option " + vrphType + " cannot be applied with this instance");
       } else if (vrphType.equals(VrphType.HVRPD)) {
         if (tokens.length > 4) {
           typeBuilder.setCostPerDistance(Double.parseDouble(tokens[4]));
           nuOfVehicles = Integer.parseInt(tokens[5]);
           vrpBuilder.setFleetSize(FleetSize.FINITE);
           vrpBuilder.addPenaltyVehicles(5.0, 5000);
         } else
           throw new IllegalStateException(
               "option " + vrphType + " cannot be applied with this instance");
       } else if (vrphType.equals(VrphType.HVRPFD)) {
         if (tokens.length > 4) {
           typeBuilder.setFixedCost(Double.parseDouble(tokens[3]));
           typeBuilder.setCostPerDistance(Double.parseDouble(tokens[4]));
           nuOfVehicles = Integer.parseInt(tokens[5]);
           vrpBuilder.setFleetSize(FleetSize.FINITE);
           vrpBuilder.addPenaltyVehicles(5.0, 5000);
         } else
           throw new IllegalStateException(
               "option " + vrphType + " cannot be applied with this instance");
       }
       for (int i = 0; i < nuOfVehicles; i++) {
         VehicleTypeImpl type = typeBuilder.build();
         Vehicle vehicle =
             VehicleImpl.Builder.newInstance("vehicle_" + tokens[1] + "_" + i)
                 .setStartLocationCoordinate(depotCoord)
                 .setType(type)
                 .build();
         vrpBuilder.addVehicle(vehicle);
       }
     }
   }
   closeReader(reader);
 }
  public static void main(String[] args) {
    VehicleRoutingProblem.Builder vrpBuilder = VehicleRoutingProblem.Builder.newInstance();
    new SolomonReader(vrpBuilder).read("input/C101_solomon.txt");
    VehicleRoutingProblem vrp = vrpBuilder.build();

    // y >= 50 skill1 otherwise skill2
    // two vehicles: v1 - skill1 #5; v2 - skill2 #6
    Vehicle solomonVehicle = vrp.getVehicles().iterator().next();
    VehicleType newType = solomonVehicle.getType();
    VehicleRoutingProblem.Builder skillProblemBuilder = VehicleRoutingProblem.Builder.newInstance();
    for (int i = 0; i < 5; i++) {
      VehicleImpl skill1Vehicle =
          VehicleImpl.Builder.newInstance("skill1_vehicle_" + i)
              .addSkill("skill1")
              .setStartLocation(
                  Location.Builder.newInstance()
                      .setId(solomonVehicle.getStartLocation().getId())
                      .setCoordinate(solomonVehicle.getStartLocation().getCoordinate())
                      .build())
              .setEarliestStart(solomonVehicle.getEarliestDeparture())
              .setType(newType)
              .build();
      VehicleImpl skill2Vehicle =
          VehicleImpl.Builder.newInstance("skill2_vehicle_" + i)
              .addSkill("skill2")
              .setStartLocation(
                  Location.Builder.newInstance()
                      .setId(solomonVehicle.getStartLocation().getId())
                      .setCoordinate(solomonVehicle.getStartLocation().getCoordinate())
                      .build())
              .setEarliestStart(solomonVehicle.getEarliestDeparture())
              .setType(newType)
              .build();
      skillProblemBuilder.addVehicle(skill1Vehicle).addVehicle(skill2Vehicle);
    }
    for (Job job : vrp.getJobs().values()) {
      Service service = (Service) job;
      Service.Builder skillServiceBuilder;
      if (service.getLocation().getCoordinate().getY() < 50.) {
        skillServiceBuilder =
            Service.Builder.newInstance(service.getId() + "_skill2")
                .setServiceTime(service.getServiceDuration())
                .setLocation(
                    Location.Builder.newInstance()
                        .setId(service.getLocation().getId())
                        .setCoordinate(service.getLocation().getCoordinate())
                        .build())
                .setTimeWindow(service.getTimeWindow())
                .addSizeDimension(0, service.getSize().get(0));
        skillServiceBuilder.addRequiredSkill("skill2");
      } else {
        skillServiceBuilder =
            Service.Builder.newInstance(service.getId() + "_skill1")
                .setServiceTime(service.getServiceDuration())
                .setLocation(
                    Location.Builder.newInstance()
                        .setId(service.getLocation().getId())
                        .setCoordinate(service.getLocation().getCoordinate())
                        .build())
                .setTimeWindow(service.getTimeWindow())
                .addSizeDimension(0, service.getSize().get(0));
        skillServiceBuilder.addRequiredSkill("skill1");
      }
      skillProblemBuilder.addJob(skillServiceBuilder.build());
    }
    skillProblemBuilder.setFleetSize(VehicleRoutingProblem.FleetSize.FINITE);
    VehicleRoutingProblem skillProblem = skillProblemBuilder.build();

    VehicleRoutingAlgorithmBuilder vraBuilder =
        new VehicleRoutingAlgorithmBuilder(skillProblem, "input/algorithmConfig_solomon.xml");
    vraBuilder.addCoreConstraints();
    vraBuilder.addDefaultCostCalculators();

    StateManager stateManager = new StateManager(skillProblem);
    stateManager.updateSkillStates();

    ConstraintManager constraintManager = new ConstraintManager(skillProblem, stateManager);
    constraintManager.addSkillsConstraint();

    VehicleRoutingAlgorithm vra =
        Jsprit.Builder.newInstance(skillProblem)
            .setStateAndConstraintManager(stateManager, constraintManager)
            .buildAlgorithm();

    Collection<VehicleRoutingProblemSolution> solutions = vra.searchSolutions();
    VehicleRoutingProblemSolution solution = Solutions.bestOf(solutions);

    SolutionPrinter.print(skillProblem, solution, SolutionPrinter.Print.VERBOSE);

    new Plotter(skillProblem, solution).plot("output/skill_solution", "solomon_with_skills");

    new VrpXMLWriter(skillProblem, solutions).write("output/solomon_with_skills");
  }
예제 #3
0
  private void readServices(XMLConfiguration vrpProblem) {
    List<HierarchicalConfiguration> serviceConfigs =
        vrpProblem.configurationsAt("services.service");
    for (HierarchicalConfiguration serviceConfig : serviceConfigs) {
      String id = serviceConfig.getString("[@id]");
      if (id == null) throw new IllegalStateException("service[@id] is missing.");
      String type = serviceConfig.getString("[@type]");
      if (type == null) type = "service";

      String capacityString = serviceConfig.getString("capacity-demand");
      boolean capacityDimensionsExist =
          serviceConfig.containsKey("capacity-dimensions.dimension(0)");
      if (capacityString == null && !capacityDimensionsExist) {
        throw new IllegalStateException(
            "capacity of service is not set. use 'capacity-dimensions'");
      }
      if (capacityString != null && capacityDimensionsExist) {
        throw new IllegalStateException(
            "either use capacity or capacity-dimension, not both. prefer the use of 'capacity-dimensions' over 'capacity'.");
      }

      Service.Builder builder;
      if (capacityString != null) {
        builder = serviceBuilderFactory.createBuilder(type, id, Integer.parseInt(capacityString));
      } else {
        builder = serviceBuilderFactory.createBuilder(type, id, null);
        List<HierarchicalConfiguration> dimensionConfigs =
            serviceConfig.configurationsAt("capacity-dimensions.dimension");
        for (HierarchicalConfiguration dimension : dimensionConfigs) {
          Integer index = dimension.getInt("[@index]");
          Integer value = dimension.getInt("");
          builder.addSizeDimension(index, value);
        }
      }
      String serviceLocationId = serviceConfig.getString("locationId");
      if (serviceLocationId != null) builder.setLocationId(serviceLocationId);
      Coordinate serviceCoord = getCoord(serviceConfig, "");
      if (serviceCoord != null) {
        builder.setCoord(serviceCoord);
        if (serviceLocationId != null) {
          //					vrpBuilder.addLocation(serviceLocationId,serviceCoord);
        } else {
          //					vrpBuilder.addLocation(serviceCoord.toString(),serviceCoord);
          builder.setLocationId(serviceCoord.toString());
        }
      }
      if (serviceConfig.containsKey("duration")) {
        builder.setServiceTime(serviceConfig.getDouble("duration"));
      }
      List<HierarchicalConfiguration> deliveryTWConfigs =
          serviceConfig.configurationsAt("timeWindows.timeWindow");
      if (!deliveryTWConfigs.isEmpty()) {
        for (HierarchicalConfiguration twConfig : deliveryTWConfigs) {
          builder.setTimeWindow(
              TimeWindow.newInstance(twConfig.getDouble("start"), twConfig.getDouble("end")));
        }
      }
      Service service = builder.build();
      serviceMap.put(service.getId(), service);
      //			vrpBuilder.addJob(service);

    }
  }