Exemplo n.º 1
0
 @Override
 public void handleJobInsertion(Job job, InsertionData iData, VehicleRoute route) {
   if (job instanceof Service) {
     route.setVehicleAndDepartureTime(
         iData.getSelectedVehicle(), iData.getVehicleDepartureTime());
     if (!iData.getSelectedVehicle().isReturnToDepot()) {
       if (iData.getDeliveryInsertionIndex()
           >= route.getTourActivities().getActivities().size()) {
         setEndLocation(route, (Service) job);
       }
     }
     TourActivity activity = vehicleRoutingProblem.copyAndGetActivities(job).get(0);
     route.getTourActivities().addActivity(iData.getDeliveryInsertionIndex(), activity);
   } else delegator.handleJobInsertion(job, iData, route);
 }
Exemplo n.º 2
0
 private void writeInitialRoutes(XMLConf xmlConfig) {
   if (vrp.getInitialVehicleRoutes().isEmpty()) return;
   String path = "initialRoutes.route";
   int routeCounter = 0;
   for (VehicleRoute route : vrp.getInitialVehicleRoutes()) {
     xmlConfig.setProperty(path + "(" + routeCounter + ").driverId", route.getDriver().getId());
     xmlConfig.setProperty(path + "(" + routeCounter + ").vehicleId", route.getVehicle().getId());
     xmlConfig.setProperty(path + "(" + routeCounter + ").start", route.getStart().getEndTime());
     int actCounter = 0;
     for (TourActivity act : route.getTourActivities().getActivities()) {
       xmlConfig.setProperty(
           path + "(" + routeCounter + ").act(" + actCounter + ")[@type]", act.getName());
       if (act instanceof JobActivity) {
         Job job = ((JobActivity) act).getJob();
         if (job instanceof Service) {
           xmlConfig.setProperty(
               path + "(" + routeCounter + ").act(" + actCounter + ").serviceId", job.getId());
         } else if (job instanceof Shipment) {
           xmlConfig.setProperty(
               path + "(" + routeCounter + ").act(" + actCounter + ").shipmentId", job.getId());
         } else {
           throw new IllegalStateException(
               "cannot write solution correctly since job-type is not know. make sure you use either service or shipment, or another writer");
         }
       }
       xmlConfig.setProperty(
           path + "(" + routeCounter + ").act(" + actCounter + ").arrTime", act.getArrTime());
       xmlConfig.setProperty(
           path + "(" + routeCounter + ").act(" + actCounter + ").endTime", act.getEndTime());
       actCounter++;
     }
     xmlConfig.setProperty(path + "(" + routeCounter + ").end", route.getEnd().getArrTime());
     routeCounter++;
   }
 }
 @Override
 public void visit(VehicleRoute route) {
   begin(route);
   Iterator<TourActivity> revIterator = route.getTourActivities().reverseActivityIterator();
   while (revIterator.hasNext()) {
     visit(revIterator.next());
   }
   finish();
 }
Exemplo n.º 4
0
  public void write(String filename) {
    if (!filename.endsWith(".xml")) filename += ".xml";
    log.info("write vrp: " + filename);
    XMLConf xmlConfig = new XMLConf();
    xmlConfig.setFileName(filename);
    xmlConfig.setRootElementName("problem");
    xmlConfig.setAttributeSplittingDisabled(true);
    xmlConfig.setDelimiterParsingDisabled(true);

    writeProblemType(xmlConfig);
    writeVehiclesAndTheirTypes(xmlConfig);

    // might be sorted?
    List<Job> jobs = new ArrayList<Job>();
    jobs.addAll(vrp.getJobs().values());
    for (VehicleRoute r : vrp.getInitialVehicleRoutes()) {
      jobs.addAll(r.getTourActivities().getJobs());
    }

    writeServices(xmlConfig, jobs);
    writeShipments(xmlConfig, jobs);

    writeInitialRoutes(xmlConfig);
    writeSolutions(xmlConfig);

    OutputFormat format = new OutputFormat();
    format.setIndenting(true);
    format.setIndent(5);

    try {
      Document document = xmlConfig.createDoc();

      Element element = document.getDocumentElement();
      element.setAttribute("xmlns", "http://www.w3schools.com");
      element.setAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance");
      element.setAttribute("xsi:schemaLocation", "http://www.w3schools.com vrp_xml_schema.xsd");

    } catch (ConfigurationException e) {
      logger.error("Exception:", e);
      e.printStackTrace();
      System.exit(1);
    }

    try {
      Writer out = new FileWriter(filename);
      XMLSerializer serializer = new XMLSerializer(out, format);
      serializer.serialize(xmlConfig.getDocument());
      out.close();
    } catch (IOException e) {
      logger.error("Exception:", e);
      e.printStackTrace();
      System.exit(1);
    }
  }
Exemplo n.º 5
0
 @Override
 public void handleJobInsertion(Job job, InsertionData iData, VehicleRoute route) {
   if (job instanceof Shipment) {
     List<AbstractActivity> acts = vehicleRoutingProblem.copyAndGetActivities(job);
     TourActivity pickupShipment = acts.get(0);
     TourActivity deliverShipment = acts.get(1);
     route.setVehicleAndDepartureTime(
         iData.getSelectedVehicle(), iData.getVehicleDepartureTime());
     if (!iData.getSelectedVehicle().isReturnToDepot()) {
       if (iData.getDeliveryInsertionIndex() >= route.getActivities().size()) {
         setEndLocation(route, (Shipment) job);
       }
     }
     route.getTourActivities().addActivity(iData.getDeliveryInsertionIndex(), deliverShipment);
     route.getTourActivities().addActivity(iData.getPickupInsertionIndex(), pickupShipment);
   } else delegator.handleJobInsertion(job, iData, route);
 }
Exemplo n.º 6
0
  @Test
  public void whenSolvingProblem2_nuJobsInSolutionShouldBe4() {

    VehicleRoutingProblem.Builder vrpBuilder = VehicleRoutingProblem.Builder.newInstance();
    new VrpXMLReader(vrpBuilder)
        .read("src/test/resources/simpleProblem_inclShipments_iniRoutes.xml");
    VehicleRoutingProblem vrp = vrpBuilder.build();

    VehicleRoutingAlgorithm vra = new SchrimpfFactory().createAlgorithm(vrp);
    Collection<VehicleRoutingProblemSolution> solutions = vra.searchSolutions();
    VehicleRoutingProblemSolution solution = Solutions.bestOf(solutions);

    SolutionPrinter.print(vrp, solution, SolutionPrinter.Print.VERBOSE);

    int jobsInSolution = 0;
    for (VehicleRoute r : solution.getRoutes()) {
      jobsInSolution += r.getTourActivities().jobSize();
    }
    assertEquals(4, jobsInSolution);
  }
Exemplo n.º 7
0
 private void writeSolutions(XMLConf xmlConfig) {
   if (solutions == null) return;
   String solutionPath = "solutions.solution";
   int counter = 0;
   for (VehicleRoutingProblemSolution solution : solutions) {
     xmlConfig.setProperty(solutionPath + "(" + counter + ").cost", solution.getCost());
     int routeCounter = 0;
     for (VehicleRoute route : solution.getRoutes()) {
       //				xmlConfig.setProperty(solutionPath + "(" + counter + ").routes.route(" + routeCounter
       // + ").cost", route.getCost());
       xmlConfig.setProperty(
           solutionPath + "(" + counter + ").routes.route(" + routeCounter + ").driverId",
           route.getDriver().getId());
       xmlConfig.setProperty(
           solutionPath + "(" + counter + ").routes.route(" + routeCounter + ").vehicleId",
           route.getVehicle().getId());
       xmlConfig.setProperty(
           solutionPath + "(" + counter + ").routes.route(" + routeCounter + ").start",
           route.getStart().getEndTime());
       int actCounter = 0;
       for (TourActivity act : route.getTourActivities().getActivities()) {
         xmlConfig.setProperty(
             solutionPath
                 + "("
                 + counter
                 + ").routes.route("
                 + routeCounter
                 + ").act("
                 + actCounter
                 + ")[@type]",
             act.getName());
         if (act instanceof JobActivity) {
           Job job = ((JobActivity) act).getJob();
           if (job instanceof Service) {
             xmlConfig.setProperty(
                 solutionPath
                     + "("
                     + counter
                     + ").routes.route("
                     + routeCounter
                     + ").act("
                     + actCounter
                     + ").serviceId",
                 job.getId());
           } else if (job instanceof Shipment) {
             xmlConfig.setProperty(
                 solutionPath
                     + "("
                     + counter
                     + ").routes.route("
                     + routeCounter
                     + ").act("
                     + actCounter
                     + ").shipmentId",
                 job.getId());
           } else {
             throw new IllegalStateException(
                 "cannot write solution correctly since job-type is not know. make sure you use either service or shipment, or another writer");
           }
         }
         xmlConfig.setProperty(
             solutionPath
                 + "("
                 + counter
                 + ").routes.route("
                 + routeCounter
                 + ").act("
                 + actCounter
                 + ").arrTime",
             act.getArrTime());
         xmlConfig.setProperty(
             solutionPath
                 + "("
                 + counter
                 + ").routes.route("
                 + routeCounter
                 + ").act("
                 + actCounter
                 + ").endTime",
             act.getEndTime());
         actCounter++;
       }
       xmlConfig.setProperty(
           solutionPath + "(" + counter + ").routes.route(" + routeCounter + ").end",
           route.getEnd().getArrTime());
       routeCounter++;
     }
     int unassignedJobCounter = 0;
     for (Job unassignedJob : solution.getUnassignedJobs()) {
       xmlConfig.setProperty(
           solutionPath
               + "("
               + counter
               + ").unassignedJobs.job("
               + unassignedJobCounter
               + ")[@id]",
           unassignedJob.getId());
       unassignedJobCounter++;
     }
     counter++;
   }
 }