Ejemplo n.º 1
0
  @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));
  }
Ejemplo n.º 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;
 }
Ejemplo n.º 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);
 }
Ejemplo n.º 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);
 }
Ejemplo n.º 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);
    }
  }
Ejemplo n.º 6
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);

    }
  }
Ejemplo n.º 7
0
  private void readShipments(XMLConfiguration config) {
    List<HierarchicalConfiguration> shipmentConfigs = config.configurationsAt("shipments.shipment");
    for (HierarchicalConfiguration shipmentConfig : shipmentConfigs) {
      String id = shipmentConfig.getString("[@id]");
      if (id == null) throw new IllegalStateException("shipment[@id] is missing.");

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

      Shipment.Builder builder;
      if (capacityString != null) {
        builder =
            Shipment.Builder.newInstance(id).addSizeDimension(0, Integer.parseInt(capacityString));
      } else {
        builder = Shipment.Builder.newInstance(id);
        List<HierarchicalConfiguration> dimensionConfigs =
            shipmentConfig.configurationsAt("capacity-dimensions.dimension");
        for (HierarchicalConfiguration dimension : dimensionConfigs) {
          Integer index = dimension.getInt("[@index]");
          Integer value = dimension.getInt("");
          builder.addSizeDimension(index, value);
        }
      }

      // pickup-locationId
      String pickupLocationId = shipmentConfig.getString("pickup.locationId");
      if (pickupLocationId != null) {
        builder.setPickupLocation(pickupLocationId);
      }

      // pickup-coord
      Coordinate pickupCoord = getCoord(shipmentConfig, "pickup.");
      if (pickupCoord != null) {
        builder.setPickupCoord(pickupCoord);
        if (pickupLocationId != null) {
          //					vrpBuilder.addLocation(pickupLocationId,pickupCoord);
        } else {
          //					vrpBuilder.addLocation(pickupCoord.toString(),pickupCoord);
          builder.setPickupLocation(pickupCoord.toString());
        }
      }
      // pickup-serviceTime
      String pickupServiceTime = shipmentConfig.getString("pickup.duration");
      if (pickupServiceTime != null)
        builder.setPickupServiceTime(Double.parseDouble(pickupServiceTime));

      // pickup-tw
      String pickupTWStart = shipmentConfig.getString("pickup.timeWindows.timeWindow(0).start");
      String pickupTWEnd = shipmentConfig.getString("pickup.timeWindows.timeWindow(0).end");
      if (pickupTWStart != null && pickupTWEnd != null) {
        TimeWindow pickupTW =
            TimeWindow.newInstance(
                Double.parseDouble(pickupTWStart), Double.parseDouble(pickupTWEnd));
        builder.setPickupTimeWindow(pickupTW);
      }

      // delivery-locationId
      String deliveryLocationId = shipmentConfig.getString("delivery.locationId");
      if (deliveryLocationId != null) {
        builder.setDeliveryLocation(deliveryLocationId);
      }

      // delivery-coord
      Coordinate deliveryCoord = getCoord(shipmentConfig, "delivery.");
      if (deliveryCoord != null) {
        builder.setDeliveryCoord(deliveryCoord);
        if (deliveryLocationId != null) {
          //					vrpBuilder.addLocation(deliveryLocationId,deliveryCoord);
        } else {
          //					vrpBuilder.addLocation(deliveryCoord.toString(),deliveryCoord);
          builder.setDeliveryLocation(deliveryCoord.toString());
        }
      }
      // delivery-serviceTime
      String deliveryServiceTime = shipmentConfig.getString("delivery.duration");
      if (deliveryServiceTime != null)
        builder.setDeliveryServiceTime(Double.parseDouble(deliveryServiceTime));

      // delivery-tw
      String delTWStart = shipmentConfig.getString("delivery.timeWindows.timeWindow(0).start");
      String delTWEnd = shipmentConfig.getString("delivery.timeWindows.timeWindow(0).end");
      if (delTWStart != null && delTWEnd != null) {
        TimeWindow delTW =
            TimeWindow.newInstance(Double.parseDouble(delTWStart), Double.parseDouble(delTWEnd));
        builder.setDeliveryTimeWindow(delTW);
      }

      Shipment shipment = builder.build();
      //			vrpBuilder.addJob(shipment);
      shipmentMap.put(shipment.getId(), shipment);
    }
  }
Ejemplo n.º 8
0
  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);
  }