From aacafa27badda93abdce75d9cea2bf52018a46d7 Mon Sep 17 00:00:00 2001 From: braktar Date: Thu, 19 May 2016 11:07:13 +0200 Subject: [PATCH] Refine Breaks XML --- .../jsprit/core/problem/io/VrpXMLReader.java | 95 +++++++++++-------- .../jsprit/core/problem/io/VrpXMLWriter.java | 6 +- .../problem/solution/route/VehicleRoute.java | 10 ++ .../core/reporting/SolutionPrinter.java | 12 ++- .../src/main/resources/vrp_xml_schema.xsd | 10 ++ 5 files changed, 88 insertions(+), 45 deletions(-) diff --git a/jsprit-core/src/main/java/com/graphhopper/jsprit/core/problem/io/VrpXMLReader.java b/jsprit-core/src/main/java/com/graphhopper/jsprit/core/problem/io/VrpXMLReader.java index 57c3d52a..1affa255 100644 --- a/jsprit-core/src/main/java/com/graphhopper/jsprit/core/problem/io/VrpXMLReader.java +++ b/jsprit-core/src/main/java/com/graphhopper/jsprit/core/problem/io/VrpXMLReader.java @@ -213,27 +213,33 @@ public class VrpXMLReader { if (endTimeS != null) endTime = Double.parseDouble(endTimeS); String serviceId = actConfig.getString("serviceId"); - if (serviceId != null) { - Service service = getService(serviceId); - if (service == null) - throw new IllegalStateException("service to serviceId " + serviceId + " is missing (reference in one of your initial routes). make sure you define the service you refer to here in ."); - //!!!since job is part of initial route, it does not belong to jobs in problem, i.e. variable jobs that can be assigned/scheduled - freezedJobIds.add(serviceId); - routeBuilder.addService(service); - } else { - String shipmentId = actConfig.getString("shipmentId"); - if (shipmentId == null) - throw new IllegalStateException("either serviceId or shipmentId is missing"); - Shipment shipment = getShipment(shipmentId); - if (shipment == null) - throw new IllegalStateException("shipment to shipmentId " + shipmentId + " is missing (reference in one of your initial routes). make sure you define the shipment you refer to here in ."); - freezedJobIds.add(shipmentId); - if (type.equals("pickupShipment")) { - routeBuilder.addPickup(shipment); - } else if (type.equals("deliverShipment")) { - routeBuilder.addDelivery(shipment); - } else - throw new IllegalStateException("type " + type + " is not supported. Use 'pickupShipment' or 'deliverShipment' here"); + if(type.equals("break")) { + Break currentbreak = getBreak(vehicleId); + routeBuilder.addBreak(currentbreak); + } + else { + if (serviceId != null) { + Service service = getService(serviceId); + if (service == null) + throw new IllegalStateException("service to serviceId " + serviceId + " is missing (reference in one of your initial routes). make sure you define the service you refer to here in ."); + //!!!since job is part of initial route, it does not belong to jobs in problem, i.e. variable jobs that can be assigned/scheduled + freezedJobIds.add(serviceId); + routeBuilder.addService(service); + } else { + String shipmentId = actConfig.getString("shipmentId"); + if (shipmentId == null) + throw new IllegalStateException("either serviceId or shipmentId is missing"); + Shipment shipment = getShipment(shipmentId); + if (shipment == null) + throw new IllegalStateException("shipment to shipmentId " + shipmentId + " is missing (reference in one of your initial routes). make sure you define the shipment you refer to here in ."); + freezedJobIds.add(shipmentId); + if (type.equals("pickupShipment")) { + routeBuilder.addPickup(shipment); + } else if (type.equals("deliverShipment")) { + routeBuilder.addDelivery(shipment); + } else + throw new IllegalStateException("type " + type + " is not supported. Use 'pickupShipment' or 'deliverShipment' here"); + } } } VehicleRoute route = routeBuilder.build(); @@ -276,24 +282,29 @@ public class VrpXMLReader { if (arrTimeS != null) arrTime = Double.parseDouble(arrTimeS); String endTimeS = actConfig.getString("endTime"); if (endTimeS != null) endTime = Double.parseDouble(endTimeS); - - String serviceId = actConfig.getString("serviceId"); - if (serviceId != null) { - Service service = getService(serviceId); - routeBuilder.addService(service); - } else { - String shipmentId = actConfig.getString("shipmentId"); - if (shipmentId == null) - throw new IllegalStateException("either serviceId or shipmentId is missing"); - Shipment shipment = getShipment(shipmentId); - if (shipment == null) - throw new IllegalStateException("shipment with id " + shipmentId + " does not exist."); - if (type.equals("pickupShipment")) { - routeBuilder.addPickup(shipment); - } else if (type.equals("deliverShipment")) { - routeBuilder.addDelivery(shipment); - } else - throw new IllegalStateException("type " + type + " is not supported. Use 'pickupShipment' or 'deliverShipment' here"); + if(type.equals("break")) { + Break currentbreak = getBreak(vehicleId); + routeBuilder.addBreak(currentbreak); + } + else { + String serviceId = actConfig.getString("serviceId"); + if (serviceId != null) { + Service service = getService(serviceId); + routeBuilder.addService(service); + } else { + String shipmentId = actConfig.getString("shipmentId"); + if (shipmentId == null) + throw new IllegalStateException("either serviceId or shipmentId is missing"); + Shipment shipment = getShipment(shipmentId); + if (shipment == null) + throw new IllegalStateException("shipment with id " + shipmentId + " does not exist."); + if (type.equals("pickupShipment")) { + routeBuilder.addPickup(shipment); + } else if (type.equals("deliverShipment")) { + routeBuilder.addDelivery(shipment); + } else + throw new IllegalStateException("type " + type + " is not supported. Use 'pickupShipment' or 'deliverShipment' here"); + } } } routes.add(routeBuilder.build()); @@ -324,6 +335,10 @@ public class VrpXMLReader { return vehicleMap.get(vehicleId); } + private Break getBreak(String vehicleId) { + return vehicleMap.get(vehicleId).getBreak(); + } + private void readProblemType(XMLConfiguration vrpProblem) { String fleetSize = vrpProblem.getString("problemType.fleetSize"); if (fleetSize == null) vrpBuilder.setFleetSize(FleetSize.INFINITE); @@ -673,7 +688,7 @@ public class VrpXMLReader { } // read break - List breakTWConfigs = vehicleConfig.configurationsAt("break.timeWindows.timeWindow"); + List breakTWConfigs = vehicleConfig.configurationsAt("breaks.timeWindows.timeWindow"); if (!breakTWConfigs.isEmpty()) { String breakDurationString = vehicleConfig.getString("breaks.duration"); Break.Builder current_break = Break.Builder.newInstance(vehicleId); diff --git a/jsprit-core/src/main/java/com/graphhopper/jsprit/core/problem/io/VrpXMLWriter.java b/jsprit-core/src/main/java/com/graphhopper/jsprit/core/problem/io/VrpXMLWriter.java index 506122d0..c2cf4cb4 100644 --- a/jsprit-core/src/main/java/com/graphhopper/jsprit/core/problem/io/VrpXMLWriter.java +++ b/jsprit-core/src/main/java/com/graphhopper/jsprit/core/problem/io/VrpXMLWriter.java @@ -354,10 +354,10 @@ public class VrpXMLWriter { if (vehicle.getBreak() != null) { Collection tws = vehicle.getBreak().getTimeWindows(); int index = 0; - xmlConfig.setProperty(vehiclePathString + "(" + counter + ").break.duration", vehicle.getBreak().getServiceDuration()); + xmlConfig.setProperty(vehiclePathString + "(" + counter + ").breaks.duration", vehicle.getBreak().getServiceDuration()); for(TimeWindow tw : tws) { - xmlConfig.setProperty(vehiclePathString + "(" + counter + ").break.timeWindows.timeWindow(" + index + ").start", tw.getStart()); - xmlConfig.setProperty(vehiclePathString + "(" + counter + ").break.timeWindows.timeWindow(" + index + ").end", tw.getEnd()); + xmlConfig.setProperty(vehiclePathString + "(" + counter + ").breaks.timeWindows.timeWindow(" + index + ").start", tw.getStart()); + xmlConfig.setProperty(vehiclePathString + "(" + counter + ").breaks.timeWindows.timeWindow(" + index + ").end", tw.getEnd()); ++index; } } diff --git a/jsprit-core/src/main/java/com/graphhopper/jsprit/core/problem/solution/route/VehicleRoute.java b/jsprit-core/src/main/java/com/graphhopper/jsprit/core/problem/solution/route/VehicleRoute.java index 1b8514dd..53074242 100644 --- a/jsprit-core/src/main/java/com/graphhopper/jsprit/core/problem/solution/route/VehicleRoute.java +++ b/jsprit-core/src/main/java/com/graphhopper/jsprit/core/problem/solution/route/VehicleRoute.java @@ -195,6 +195,16 @@ public class VehicleRoute { return this; } + public Builder addBreak(Break currentbreak) { + if (currentbreak == null) throw new IllegalArgumentException("break must not be null"); + return addBreak(currentbreak, currentbreak.getTimeWindow()); + } + + public Builder addBreak(Break currentbreak, TimeWindow timeWindow) { + if (currentbreak == null) throw new IllegalArgumentException("break must not be null"); + return addService(currentbreak,timeWindow); + } + /** * Adds a pickup to this route. * diff --git a/jsprit-core/src/main/java/com/graphhopper/jsprit/core/reporting/SolutionPrinter.java b/jsprit-core/src/main/java/com/graphhopper/jsprit/core/reporting/SolutionPrinter.java index 023a70d5..7cb4905c 100644 --- a/jsprit-core/src/main/java/com/graphhopper/jsprit/core/reporting/SolutionPrinter.java +++ b/jsprit-core/src/main/java/com/graphhopper/jsprit/core/reporting/SolutionPrinter.java @@ -17,6 +17,7 @@ package com.graphhopper.jsprit.core.reporting; import com.graphhopper.jsprit.core.problem.VehicleRoutingProblem; +import com.graphhopper.jsprit.core.problem.job.Break; import com.graphhopper.jsprit.core.problem.job.Job; import com.graphhopper.jsprit.core.problem.job.Service; import com.graphhopper.jsprit.core.problem.job.Shipment; @@ -53,11 +54,13 @@ public class SolutionPrinter { private static class Jobs { int nServices; int nShipments; + int nBreaks; - public Jobs(int nServices, int nShipments) { + public Jobs(int nServices, int nShipments, int nBreaks) { super(); this.nServices = nServices; this.nShipments = nShipments; + this.nBreaks = nBreaks; } } @@ -113,6 +116,7 @@ public class SolutionPrinter { Jobs jobs = getNuOfJobs(problem); out.format(leftAlign, "noServices", jobs.nServices); out.format(leftAlign, "noShipments", jobs.nShipments); + out.format(leftAlign, "noBreaks", jobs.nBreaks); out.format(leftAlign, "fleetsize", problem.getFleetSize().toString()); out.format("+--------------------------+%n"); @@ -194,6 +198,7 @@ public class SolutionPrinter { private static Jobs getNuOfJobs(VehicleRoutingProblem problem) { int nShipments = 0; int nServices = 0; + int nBreaks = 0; for (Job j : problem.getJobs().values()) { if (j instanceof Shipment) { nShipments++; @@ -201,8 +206,11 @@ public class SolutionPrinter { if (j instanceof Service) { nServices++; } + if (j instanceof Break) { + nBreaks++; + } } - return new Jobs(nServices, nShipments); + return new Jobs(nServices, nShipments, nBreaks); } } diff --git a/jsprit-core/src/main/resources/vrp_xml_schema.xsd b/jsprit-core/src/main/resources/vrp_xml_schema.xsd index a621bcc9..b4c8a856 100644 --- a/jsprit-core/src/main/resources/vrp_xml_schema.xsd +++ b/jsprit-core/src/main/resources/vrp_xml_schema.xsd @@ -258,6 +258,7 @@ + @@ -297,6 +298,7 @@ + + + + + + + + +