Example #1
0
 @Override
 public jsprit.core.problem.job.Service.Builder createBuilder(
     String serviceType, String id, Integer size) {
   if (serviceType.equals("pickup")) {
     if (size != null) return Pickup.Builder.newInstance(id).addSizeDimension(0, size);
     else return Pickup.Builder.newInstance(id);
   } else if (serviceType.equals("delivery")) {
     if (size != null) return Delivery.Builder.newInstance(id).addSizeDimension(0, size);
     else return Delivery.Builder.newInstance(id);
   } else {
     if (size != null) return Service.Builder.newInstance(id).addSizeDimension(0, size);
     else return Service.Builder.newInstance(id);
   }
 }
  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");
  }
Example #3
0
  @Before
  public void doBefore() {
    Vehicle vehicle = mock(Vehicle.class);
    VehicleType type = mock(VehicleType.class);
    when(type.getCapacityDimensions())
        .thenReturn(Capacity.Builder.newInstance().addDimension(0, 20).build());
    when(vehicle.getType()).thenReturn(type);

    VehicleRoutingProblem.Builder serviceProblemBuilder =
        VehicleRoutingProblem.Builder.newInstance();
    Service s1 =
        Service.Builder.newInstance("s").addSizeDimension(0, 10).setLocationId("loc").build();
    Service s2 =
        Service.Builder.newInstance("s2").addSizeDimension(0, 5).setLocationId("loc").build();
    serviceProblemBuilder.addJob(s1).addJob(s2);
    final VehicleRoutingProblem serviceProblem = serviceProblemBuilder.build();

    final VehicleRoutingProblem.Builder pdProblemBuilder =
        VehicleRoutingProblem.Builder.newInstance();
    Pickup pickup =
        (Pickup)
            Pickup.Builder.newInstance("pick").addSizeDimension(0, 10).setLocationId("loc").build();
    Delivery delivery =
        (Delivery)
            Delivery.Builder.newInstance("del").addSizeDimension(0, 5).setLocationId("loc").build();
    pdProblemBuilder.addJob(pickup).addJob(delivery);
    final VehicleRoutingProblem pdProblem = pdProblemBuilder.build();

    final VehicleRoutingProblem.Builder shipmentProblemBuilder =
        VehicleRoutingProblem.Builder.newInstance();
    Shipment shipment1 =
        Shipment.Builder.newInstance("s1")
            .addSizeDimension(0, 10)
            .setPickupLocation("pick")
            .setDeliveryLocation("del")
            .build();
    Shipment shipment2 =
        Shipment.Builder.newInstance("s2")
            .addSizeDimension(0, 5)
            .setPickupLocation("pick")
            .setDeliveryLocation("del")
            .build();
    shipmentProblemBuilder.addJob(shipment1).addJob(shipment2).build();
    final VehicleRoutingProblem shipmentProblem = shipmentProblemBuilder.build();

    VehicleRoute.Builder serviceRouteBuilder = VehicleRoute.Builder.newInstance(vehicle);
    serviceRouteBuilder.setJobActivityFactory(
        new JobActivityFactory() {

          @Override
          public List<AbstractActivity> createActivities(Job job) {
            return serviceProblem.copyAndGetActivities(job);
          }
        });
    serviceRoute = serviceRouteBuilder.addService(s1).addService(s2).build();

    VehicleRoute.Builder pdRouteBuilder = VehicleRoute.Builder.newInstance(vehicle);
    pdRouteBuilder.setJobActivityFactory(
        new JobActivityFactory() {
          @Override
          public List<AbstractActivity> createActivities(Job job) {
            return pdProblem.copyAndGetActivities(job);
          }
        });
    pickup_delivery_route = pdRouteBuilder.addService(pickup).addService(delivery).build();

    VehicleRoute.Builder shipmentRouteBuilder = VehicleRoute.Builder.newInstance(vehicle);
    shipmentRouteBuilder.setJobActivityFactory(
        new JobActivityFactory() {
          @Override
          public List<AbstractActivity> createActivities(Job job) {
            return shipmentProblem.copyAndGetActivities(job);
          }
        });
    shipment_route =
        shipmentRouteBuilder
            .addPickup(shipment1)
            .addPickup(shipment2)
            .addDelivery(shipment2)
            .addDelivery(shipment1)
            .build();

    stateManager = new StateManager(mock(VehicleRoutingProblem.class));
    stateManager.updateLoadStates();
  }
Example #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);
 }
Example #5
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);
  }