コード例 #1
0
ファイル: VrpXMLReader.java プロジェクト: heerad/jsprit
  private void readVehiclesAndTheirTypes(XMLConfiguration vrpProblem) {

    // read vehicle-types
    Map<String, VehicleType> types = new HashMap<String, VehicleType>();
    List<HierarchicalConfiguration> typeConfigs = vrpProblem.configurationsAt("vehicleTypes.type");
    for (HierarchicalConfiguration typeConfig : typeConfigs) {
      String typeId = typeConfig.getString("id");
      if (typeId == null) throw new IllegalStateException("typeId is missing.");

      String capacityString = typeConfig.getString("capacity");
      boolean capacityDimensionsExist = typeConfig.containsKey("capacity-dimensions.dimension(0)");
      if (capacityString == null && !capacityDimensionsExist) {
        throw new IllegalStateException("capacity of type 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'.");
      }

      VehicleTypeImpl.Builder typeBuilder;
      if (capacityString != null) {
        typeBuilder =
            VehicleTypeImpl.Builder.newInstance(typeId)
                .addCapacityDimension(0, Integer.parseInt(capacityString));
      } else {
        typeBuilder = VehicleTypeImpl.Builder.newInstance(typeId);
        List<HierarchicalConfiguration> dimensionConfigs =
            typeConfig.configurationsAt("capacity-dimensions.dimension");
        for (HierarchicalConfiguration dimension : dimensionConfigs) {
          Integer index = dimension.getInt("[@index]");
          Integer value = dimension.getInt("");
          typeBuilder.addCapacityDimension(index, value);
        }
      }
      Double fix = typeConfig.getDouble("costs.fixed");
      Double timeC = typeConfig.getDouble("costs.time");
      Double distC = typeConfig.getDouble("costs.distance");

      if (fix != null) typeBuilder.setFixedCost(fix);
      if (timeC != null) typeBuilder.setCostPerTime(timeC);
      if (distC != null) typeBuilder.setCostPerDistance(distC);
      VehicleType type = typeBuilder.build();
      String id = type.getTypeId();
      String penalty = typeConfig.getString("[@type]");
      if (penalty != null) {
        if (penalty.equals("penalty")) {
          String penaltyFactor = typeConfig.getString("[@penaltyFactor]");
          if (penaltyFactor != null) {
            type = new PenaltyVehicleType(type, Double.parseDouble(penaltyFactor));
          } else type = new PenaltyVehicleType(type);
          id = id + "_penalty";
        }
      }

      types.put(id, type);
    }

    // read vehicles
    List<HierarchicalConfiguration> vehicleConfigs =
        vrpProblem.configurationsAt("vehicles.vehicle");
    boolean doNotWarnAgain = false;
    for (HierarchicalConfiguration vehicleConfig : vehicleConfigs) {
      String vehicleId = vehicleConfig.getString("id");
      if (vehicleId == null) throw new IllegalStateException("vehicleId is missing.");
      Builder builder = VehicleImpl.Builder.newInstance(vehicleId);
      String typeId = vehicleConfig.getString("typeId");
      if (typeId == null) throw new IllegalStateException("typeId is missing.");
      String vType = vehicleConfig.getString("[@type]");
      if (vType != null) {
        if (vType.equals("penalty")) {
          typeId += "_penalty";
        }
      }
      VehicleType type = types.get(typeId);
      if (type == null)
        throw new IllegalStateException("vehicleType with typeId " + typeId + " is missing.");
      builder.setType(type);
      String locationId = vehicleConfig.getString("location.id");
      if (locationId == null) {
        locationId = vehicleConfig.getString("startLocation.id");
      }
      if (locationId == null) throw new IllegalStateException("location.id is missing.");
      builder.setStartLocationId(locationId);
      String coordX = vehicleConfig.getString("location.coord[@x]");
      String coordY = vehicleConfig.getString("location.coord[@y]");
      if (coordX == null || coordY == null) {
        coordX = vehicleConfig.getString("startLocation.coord[@x]");
        coordY = vehicleConfig.getString("startLocation.coord[@y]");
      }
      if (coordX == null || coordY == null) {
        if (!doNotWarnAgain) {
          logger.warn("location.coord is missing. will not warn you again.");
          doNotWarnAgain = true;
        }
      } else {
        Coordinate coordinate =
            Coordinate.newInstance(Double.parseDouble(coordX), Double.parseDouble(coordY));
        builder.setStartLocationCoordinate(coordinate);
      }

      String endLocationId = vehicleConfig.getString("endLocation.id");
      if (endLocationId != null) builder.setEndLocationId(endLocationId);
      String endCoordX = vehicleConfig.getString("endLocation.coord[@x]");
      String endCoordY = vehicleConfig.getString("endLocation.coord[@y]");
      if (endCoordX == null || endCoordY == null) {
        if (!doNotWarnAgain) {
          logger.warn("endLocation.coord is missing. will not warn you again.");
          doNotWarnAgain = true;
        }
      } else {
        Coordinate coordinate =
            Coordinate.newInstance(Double.parseDouble(endCoordX), Double.parseDouble(endCoordY));
        builder.setEndLocationCoordinate(coordinate);
      }

      String start = vehicleConfig.getString("timeSchedule.start");
      String end = vehicleConfig.getString("timeSchedule.end");
      if (start != null) builder.setEarliestStart(Double.parseDouble(start));
      if (end != null) builder.setLatestArrival(Double.parseDouble(end));
      String returnToDepot = vehicleConfig.getString("returnToDepot");
      if (returnToDepot != null) {
        builder.setReturnToDepot(vehicleConfig.getBoolean("returnToDepot"));
      }
      VehicleImpl vehicle = builder.build();
      vrpBuilder.addVehicle(vehicle);
      vehicleMap.put(vehicleId, vehicle);
    }
  }
コード例 #2
0
ファイル: CordeauReader.java プロジェクト: homerquan/jsprit
  public void read(String fileName) {
    vrpBuilder.setFleetSize(FleetSize.FINITE);
    BufferedReader reader = getReader(fileName);
    int vrpType;
    int nOfDepots = 0;
    int nOfCustomers = 0;
    int nOfVehiclesAtEachDepot = 0;

    int counter = 0;
    String line;
    List<List<Builder>> vehiclesAtDepot = new ArrayList<List<Builder>>();
    int depotCounter = 0;
    while ((line = readLine(reader)) != null) {
      line = line.replace("\r", "");
      line = line.trim();
      String[] tokens = line.split("\\s+");
      if (counter == 0) {
        vrpType = Integer.parseInt(tokens[0].trim());
        if (vrpType != 2)
          throw new IllegalStateException("expect vrpType to be equal to 2 and thus to be MDVRP");
        nOfVehiclesAtEachDepot = Integer.parseInt(tokens[1].trim());
        nOfCustomers = Integer.parseInt(tokens[2].trim());
        nOfDepots = Integer.parseInt(tokens[3].trim());
      } else if (counter <= nOfDepots) {
        String depot = Integer.valueOf(counter).toString();
        int duration = Integer.parseInt(tokens[0].trim());
        if (duration == 0) duration = 999999;
        int capacity = Integer.parseInt(tokens[1].trim());
        VehicleTypeImpl vehicleType =
            VehicleTypeImpl.Builder.newInstance(counter + "_cordeauType")
                .addCapacityDimension(0, capacity)
                .setCostPerDistance(1.0)
                .setFixedCost(0)
                .build();
        List<Builder> builders = new ArrayList<VehicleImpl.Builder>();
        for (int vehicleCounter = 0; vehicleCounter < nOfVehiclesAtEachDepot; vehicleCounter++) {
          Builder vBuilder =
              VehicleImpl.Builder.newInstance(
                  depot + "_" + (vehicleCounter + 1) + "_cordeauVehicle");
          vBuilder.setLatestArrival(duration).setType(vehicleType);
          builders.add(vBuilder);
        }
        vehiclesAtDepot.add(builders);
      } else if (counter <= (nOfCustomers + nOfDepots)) {
        String id = tokens[0].trim();
        Coordinate customerCoord = makeCoord(tokens[1].trim(), tokens[2].trim());
        double serviceTime = Double.parseDouble(tokens[3].trim());
        int demand = Integer.parseInt(tokens[4].trim());
        Service service =
            Service.Builder.newInstance(id)
                .addSizeDimension(0, demand)
                .setServiceTime(serviceTime)
                .setLocation(
                    Location.Builder.newInstance().setId(id).setCoordinate(customerCoord).build())
                .build();
        vrpBuilder.addJob(service);
      } else if (counter <= (nOfCustomers + nOfDepots + nOfDepots)) {
        Coordinate depotCoord = makeCoord(tokens[1].trim(), tokens[2].trim());
        List<Builder> vBuilders = vehiclesAtDepot.get(depotCounter);
        for (Builder vBuilder : vBuilders) {
          vBuilder.setStartLocation(Location.newInstance(depotCoord.getX(), depotCoord.getY()));
          VehicleImpl vehicle = vBuilder.build();
          vrpBuilder.addVehicle(vehicle);
        }
        depotCounter++;
      } else {
        throw new IllegalStateException("there are more lines than expected in file.");
      }
      counter++;
    }
    close(reader);
  }