示例#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);
 }
示例#2
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);

    }
  }