@Test
  public void maxCapacityShouldNotBeExceeded() {
    VehicleType type =
        VehicleTypeImpl.Builder.newInstance("type").addCapacityDimension(0, 100).build();
    VehicleImpl vehicle =
        VehicleImpl.Builder.newInstance("veh")
            .setStartLocationCoordinate(Coordinate.newInstance(0, 0))
            .setType(type)
            .setStartLocationId("start")
            .build();

    Shipment shipment =
        Shipment.Builder.newInstance("s")
            .setPickupLocationId("pick")
            .setDeliveryLocationId("del")
            .setPickupCoord(Coordinate.newInstance(10, 0))
            .setDeliveryCoord(Coordinate.newInstance(0, 10))
            .addSizeDimension(0, 100)
            .build();

    Shipment another_shipment =
        Shipment.Builder.newInstance("another_s")
            .setPickupLocationId("pick")
            .setDeliveryLocationId("del")
            .setPickupCoord(Coordinate.newInstance(10, 0))
            .setDeliveryCoord(Coordinate.newInstance(0, 10))
            .addSizeDimension(0, 50)
            .build();

    VehicleRoute iniRoute =
        VehicleRoute.Builder.newInstance(vehicle).addPickup(shipment).addDelivery(shipment).build();

    VehicleRoutingProblem vrp =
        VehicleRoutingProblem.Builder.newInstance()
            .addJob(shipment)
            .addVehicle(vehicle)
            .addJob(another_shipment)
            .setFleetSize(VehicleRoutingProblem.FleetSize.FINITE)
            .addInitialVehicleRoute(iniRoute)
            .build();

    VehicleRoutingAlgorithm vra = new GreedySchrimpfFactory().createAlgorithm(vrp);
    vra.setNuOfIterations(10);

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

    assertFalse(secondActIsPickup(solutions));
  }
Beispiel #2
0
 private static Coordinate getCoord(HierarchicalConfiguration serviceConfig, String prefix) {
   Coordinate pickupCoord = null;
   if (serviceConfig.getString(prefix + "coord[@x]") != null
       && serviceConfig.getString(prefix + "coord[@y]") != null) {
     double x = Double.parseDouble(serviceConfig.getString(prefix + "coord[@x]"));
     double y = Double.parseDouble(serviceConfig.getString(prefix + "coord[@y]"));
     pickupCoord = Coordinate.newInstance(x, y);
   }
   return pickupCoord;
 }
Beispiel #3
0
 @Test
 public void whenSettingLocationCoord_itShouldBeSetCorrectly() {
   Service s = Service.Builder.newInstance("s").setCoord(Coordinate.newInstance(1, 2)).build();
   assertEquals(1.0, s.getCoord().getX(), 0.01);
   assertEquals(2.0, s.getCoord().getY(), 0.01);
 }
Beispiel #4
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);
 }
Beispiel #5
0
  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);
    }
  }