From f662e5a4699b3eef06db78b382a5dd621275e79d Mon Sep 17 00:00:00 2001 From: oblonski <4sschroeder@gmail.com> Date: Fri, 29 Nov 2013 08:27:30 +0100 Subject: [PATCH] modify reader/writer to deal with open routes and add examples --- .../core/algorithm/recreate/Inserter.java | 10 +- .../jsprit/core/problem/io/VrpXMLReader.java | 13 +- .../jsprit/core/problem/io/VrpXMLWriter.java | 1 + .../problem/solution/route/VehicleRoute.java | 35 +- .../src/main/resources/vrp_xml_schema.xsd | 2 +- jsprit-examples/input/algorithmConfig_fix.xml | 49 + .../input/deliveries_solomon_open_c101.xml | 1236 +++++++++++++++++ .../examples/SimpleExampleOpenRoutes.java | 5 +- .../jsprit/examples/SolomonOpenExample.java | 101 ++ 9 files changed, 1434 insertions(+), 18 deletions(-) create mode 100755 jsprit-examples/input/algorithmConfig_fix.xml create mode 100644 jsprit-examples/input/deliveries_solomon_open_c101.xml create mode 100644 jsprit-examples/src/main/java/jsprit/examples/SolomonOpenExample.java diff --git a/jsprit-core/src/main/java/jsprit/core/algorithm/recreate/Inserter.java b/jsprit-core/src/main/java/jsprit/core/algorithm/recreate/Inserter.java index aed0becb..9ea0f15f 100644 --- a/jsprit-core/src/main/java/jsprit/core/algorithm/recreate/Inserter.java +++ b/jsprit-core/src/main/java/jsprit/core/algorithm/recreate/Inserter.java @@ -60,13 +60,13 @@ class Inserter { @Override public void handleJobInsertion(Job job, InsertionData iData, VehicleRoute route) { if(job instanceof Service){ - route.getTourActivities().addActivity(iData.getDeliveryInsertionIndex(), this.activityFactory.createActivity((Service)job)); - route.setDepartureTime(iData.getVehicleDepartureTime()); if(!iData.getSelectedVehicle().isReturnToDepot()){ if(iData.getDeliveryInsertionIndex()>=route.getTourActivities().getActivities().size()){ setEndLocation(route,(Service)job); } } + route.getTourActivities().addActivity(iData.getDeliveryInsertionIndex(), this.activityFactory.createActivity((Service)job)); + route.setDepartureTime(iData.getVehicleDepartureTime()); } else delegator.handleJobInsertion(job, iData, route); } @@ -93,14 +93,14 @@ class Inserter { if(job instanceof Shipment){ TourActivity pickupShipment = this.activityFactory.createPickup((Shipment)job); TourActivity deliverShipment = this.activityFactory.createDelivery((Shipment)job); - route.getTourActivities().addActivity(iData.getDeliveryInsertionIndex(), deliverShipment); - route.getTourActivities().addActivity(iData.getPickupInsertionIndex(), pickupShipment); - route.setDepartureTime(iData.getVehicleDepartureTime()); if(!iData.getSelectedVehicle().isReturnToDepot()){ if(iData.getDeliveryInsertionIndex()>=route.getTourActivities().getActivities().size()){ setEndLocation(route,(Shipment)job); } } + route.getTourActivities().addActivity(iData.getDeliveryInsertionIndex(), deliverShipment); + route.getTourActivities().addActivity(iData.getPickupInsertionIndex(), pickupShipment); + route.setDepartureTime(iData.getVehicleDepartureTime()); } else delegator.handleJobInsertion(job, iData, route); } diff --git a/jsprit-core/src/main/java/jsprit/core/problem/io/VrpXMLReader.java b/jsprit-core/src/main/java/jsprit/core/problem/io/VrpXMLReader.java index b6e15ab6..ab45c336 100644 --- a/jsprit-core/src/main/java/jsprit/core/problem/io/VrpXMLReader.java +++ b/jsprit-core/src/main/java/jsprit/core/problem/io/VrpXMLReader.java @@ -230,14 +230,19 @@ public class VrpXMLReader{ if(vehicle == null) throw new IllegalStateException("vehicle is missing."); String start = routeConfig.getString("start"); if(start == null) throw new IllegalStateException("route start-time is missing."); + double departureTime = Double.parseDouble(start); + String end = routeConfig.getString("end"); if(end == null) throw new IllegalStateException("route end-time is missing."); - Start startAct = Start.newInstance(vehicle.getLocationId(), vehicle.getEarliestDeparture(), vehicle.getLatestArrival()); - startAct.setEndTime(Double.parseDouble(start)); + +// Start startAct = Start.newInstance(vehicle.getLocationId(), vehicle.getEarliestDeparture(), vehicle.getLatestArrival()); +// startAct.setEndTime(Double.parseDouble(start)); End endAct = End.newInstance(vehicle.getLocationId(), vehicle.getEarliestDeparture(), vehicle.getLatestArrival()); endAct.setArrTime(Double.parseDouble(end)); VehicleRoute.Builder routeBuilder = VehicleRoute.Builder.newInstance(vehicle, driver); + routeBuilder.setDepartureTime(departureTime); + routeBuilder.setRouteEndArrivalTime(Double.parseDouble(end)); List actConfigs = routeConfig.configurationsAt("act"); for(HierarchicalConfiguration actConfig : actConfigs){ String type = actConfig.getString("[@type]"); @@ -466,6 +471,10 @@ public class VrpXMLReader{ 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); diff --git a/jsprit-core/src/main/java/jsprit/core/problem/io/VrpXMLWriter.java b/jsprit-core/src/main/java/jsprit/core/problem/io/VrpXMLWriter.java index 6167b29f..64643d6c 100644 --- a/jsprit-core/src/main/java/jsprit/core/problem/io/VrpXMLWriter.java +++ b/jsprit-core/src/main/java/jsprit/core/problem/io/VrpXMLWriter.java @@ -234,6 +234,7 @@ public class VrpXMLWriter { xmlConfig.setProperty(vehiclePathString + "("+counter+").timeSchedule.start", vehicle.getEarliestDeparture()); xmlConfig.setProperty(vehiclePathString + "("+counter+").timeSchedule.end", vehicle.getLatestArrival()); + xmlConfig.setProperty(vehiclePathString + "("+counter+").returnToDepot", vehicle.isReturnToDepot()); counter++; } diff --git a/jsprit-core/src/main/java/jsprit/core/problem/solution/route/VehicleRoute.java b/jsprit-core/src/main/java/jsprit/core/problem/solution/route/VehicleRoute.java index 22695b9d..424aaa40 100644 --- a/jsprit-core/src/main/java/jsprit/core/problem/solution/route/VehicleRoute.java +++ b/jsprit-core/src/main/java/jsprit/core/problem/solution/route/VehicleRoute.java @@ -61,6 +61,8 @@ public class VehicleRoute { private Start start; + private End end; + private TourActivities tourActivities = new TourActivities(); private TourActivityFactory serviceActivityFactory = new DefaultTourActivityFactory(); @@ -79,6 +81,10 @@ public class VehicleRoute { /** * Constructs the route-builder. + * + *

Default startLocation is vehicle.getLocationId()
+ * Default departureTime is vehicle.getEarliestDeparture()
+ * Default endLocation is either vehicle.getLocationId() or (if !vehicle.isReturnToDepot()) last specified activityLocation * @param vehicle * @param driver */ @@ -88,7 +94,7 @@ public class VehicleRoute { this.driver = driver; start = Start.newInstance(vehicle.getLocationId(), vehicle.getEarliestDeparture(), vehicle.getLatestArrival()); start.setEndTime(vehicle.getEarliestDeparture()); - End.newInstance(vehicle.getLocationId(), vehicle.getEarliestDeparture(), vehicle.getLatestArrival()); + end = End.newInstance(vehicle.getLocationId(), vehicle.getEarliestDeparture(), vehicle.getLatestArrival()); } /** @@ -102,6 +108,11 @@ public class VehicleRoute { return this; } + public Builder setRouteEndArrivalTime(double endTime){ + end.setArrTime(endTime); + return this; + } + public Builder addService(Service service){ addService(service,0.0,0.0); return this; @@ -185,7 +196,12 @@ public class VehicleRoute { if(!openShipments.isEmpty()){ throw new IllegalStateException("there are still shipments that have not been delivered yet."); } - VehicleRoute route = VehicleRoute.newInstance(tourActivities, driver, vehicle); + if(!vehicle.isReturnToDepot()){ + if(!tourActivities.isEmpty()){ + end.setLocationId(tourActivities.getActivities().get(tourActivities.getActivities().size()-1).getLocationId()); + } + } + VehicleRoute route = new VehicleRoute(this); return route; } @@ -218,13 +234,14 @@ public class VehicleRoute { setStartAndEnd(vehicle, vehicle.getEarliestDeparture()); } -// private VehicleRoute(Builder builder){ -// this.tourActivities = builder.tour; -// this.vehicle = builder.vehicle; -// this.driver = builder.driver; -// this.start = builder.start; -// this.end = builder.end; -// } + + private VehicleRoute(Builder builder){ + this.tourActivities = builder.tourActivities; + this.vehicle = builder.vehicle; + this.driver = builder.driver; + this.start = builder.start; + this.end = builder.end; + } private void verify(TourActivities tour, Driver driver, Vehicle vehicle) { if(tour == null || driver == null || vehicle == null) throw new IllegalStateException("null is not allowed for tour, driver or vehicle. use emptyRoute. use Tour.emptyTour, DriverImpl.noDriver() and VehicleImpl.noVehicle() instead." + diff --git a/jsprit-core/src/main/resources/vrp_xml_schema.xsd b/jsprit-core/src/main/resources/vrp_xml_schema.xsd index 04edd787..f1722126 100644 --- a/jsprit-core/src/main/resources/vrp_xml_schema.xsd +++ b/jsprit-core/src/main/resources/vrp_xml_schema.xsd @@ -53,7 +53,7 @@ - + diff --git a/jsprit-examples/input/algorithmConfig_fix.xml b/jsprit-examples/input/algorithmConfig_fix.xml new file mode 100755 index 00000000..b7ffdd9c --- /dev/null +++ b/jsprit-examples/input/algorithmConfig_fix.xml @@ -0,0 +1,49 @@ + + + + + 2000 + + + + 1.0 + + + + + 1 + + + + + + + + + 0.5 + + + + + 0.5 + + + + + + + + + 0.3 + + + + + 0.5 + + + + + + diff --git a/jsprit-examples/input/deliveries_solomon_open_c101.xml b/jsprit-examples/input/deliveries_solomon_open_c101.xml new file mode 100644 index 00000000..6dd2ab3f --- /dev/null +++ b/jsprit-examples/input/deliveries_solomon_open_c101.xml @@ -0,0 +1,1236 @@ + + + + INFINITE + HOMOGENEOUS + + + + solomonVehicle + solomonType + + 0 + + + + 0.0 + 1236.0 + + false + + + + + solomonType + 200 + + 0.0 + 1.0 + + + + + + + [x=5.0][y=35.0] + + 10 + 90.0 + + + 283.0 + 344.0 + + + + + [x=5.0][y=45.0] + + 10 + 90.0 + + + 665.0 + 716.0 + + + + + [x=8.0][y=40.0] + + 40 + 90.0 + + + 87.0 + 158.0 + + + + + [x=8.0][y=45.0] + + 20 + 90.0 + + + 751.0 + 816.0 + + + + + [x=0.0][y=45.0] + + 20 + 90.0 + + + 567.0 + 624.0 + + + + + [x=2.0][y=40.0] + + 20 + 90.0 + + + 383.0 + 434.0 + + + + + [x=0.0][y=40.0] + + 30 + 90.0 + + + 479.0 + 522.0 + + + + + [x=33.0][y=35.0] + + 10 + 90.0 + + + 16.0 + 80.0 + + + + + [x=33.0][y=32.0] + + 20 + 90.0 + + + 68.0 + 149.0 + + + + + [x=35.0][y=32.0] + + 10 + 90.0 + + + 166.0 + 235.0 + + + + + [x=35.0][y=30.0] + + 10 + 90.0 + + + 264.0 + 321.0 + + + + + [x=28.0][y=52.0] + + 20 + 90.0 + + + 812.0 + 883.0 + + + + + [x=28.0][y=55.0] + + 10 + 90.0 + + + 732.0 + 777.0 + + + + + [x=25.0][y=50.0] + + 10 + 90.0 + + + 65.0 + 144.0 + + + + + [x=25.0][y=52.0] + + 40 + 90.0 + + + 169.0 + 224.0 + + + + + [x=25.0][y=55.0] + + 10 + 90.0 + + + 622.0 + 701.0 + + + + + [x=23.0][y=52.0] + + 10 + 90.0 + + + 261.0 + 316.0 + + + + + [x=23.0][y=55.0] + + 20 + 90.0 + + + 546.0 + 593.0 + + + + + [x=20.0][y=50.0] + + 10 + 90.0 + + + 358.0 + 405.0 + + + + + [x=42.0][y=66.0] + + 10 + 90.0 + + + 65.0 + 146.0 + + + + + [x=45.0][y=70.0] + + 30 + 90.0 + + + 825.0 + 870.0 + + + + + [x=45.0][y=68.0] + + 10 + 90.0 + + + 912.0 + 967.0 + + + + + [x=20.0][y=55.0] + + 10 + 90.0 + + + 449.0 + 504.0 + + + + + [x=40.0][y=66.0] + + 20 + 90.0 + + + 170.0 + 225.0 + + + + + [x=40.0][y=69.0] + + 20 + 90.0 + + + 621.0 + 702.0 + + + + + [x=42.0][y=65.0] + + 10 + 90.0 + + + 15.0 + 67.0 + + + + + [x=10.0][y=40.0] + + 30 + 90.0 + + + 31.0 + 100.0 + + + + + [x=42.0][y=68.0] + + 10 + 90.0 + + + 727.0 + 782.0 + + + + + [x=10.0][y=35.0] + + 20 + 90.0 + + + 200.0 + 237.0 + + + + + [x=38.0][y=70.0] + + 10 + 90.0 + + + 534.0 + 605.0 + + + + + [x=38.0][y=68.0] + + 20 + 90.0 + + + 255.0 + 324.0 + + + + + [x=15.0][y=80.0] + + 10 + 90.0 + + + 278.0 + 345.0 + + + + + [x=18.0][y=75.0] + + 20 + 90.0 + + + 99.0 + 148.0 + + + + + [x=15.0][y=75.0] + + 20 + 90.0 + + + 179.0 + 254.0 + + + + + [x=20.0][y=80.0] + + 40 + 90.0 + + + 384.0 + 429.0 + + + + + [x=20.0][y=85.0] + + 40 + 90.0 + + + 475.0 + 528.0 + + + + + [x=22.0][y=75.0] + + 30 + 90.0 + + + 30.0 + 92.0 + + + + + [x=22.0][y=85.0] + + 10 + 90.0 + + + 567.0 + 620.0 + + + + + [x=35.0][y=69.0] + + 10 + 90.0 + + + 448.0 + 505.0 + + + + + [x=25.0][y=85.0] + + 20 + 90.0 + + + 652.0 + 721.0 + + + + + [x=30.0][y=52.0] + + 20 + 90.0 + + + 914.0 + 965.0 + + + + + [x=30.0][y=50.0] + + 10 + 90.0 + + + 10.0 + 73.0 + + + + + [x=55.0][y=80.0] + + 10 + 90.0 + + + 743.0 + 820.0 + + + + + [x=55.0][y=85.0] + + 20 + 90.0 + + + 647.0 + 726.0 + + + + + [x=58.0][y=75.0] + + 20 + 90.0 + + + 30.0 + 84.0 + + + + + [x=60.0][y=85.0] + + 30 + 90.0 + + + 561.0 + 622.0 + + + + + [x=60.0][y=80.0] + + 10 + 90.0 + + + 95.0 + 156.0 + + + + + [x=62.0][y=80.0] + + 30 + 90.0 + + + 196.0 + 239.0 + + + + + [x=65.0][y=82.0] + + 10 + 90.0 + + + 285.0 + 336.0 + + + + + [x=65.0][y=85.0] + + 40 + 90.0 + + + 475.0 + 518.0 + + + + + [x=67.0][y=85.0] + + 20 + 90.0 + + + 368.0 + 441.0 + + + + + [x=60.0][y=60.0] + + 10 + 90.0 + + + 836.0 + 889.0 + + + + + [x=60.0][y=55.0] + + 10 + 90.0 + + + 20.0 + 84.0 + + + + + [x=35.0][y=66.0] + + 10 + 90.0 + + + 357.0 + 410.0 + + + + + [x=65.0][y=60.0] + + 30 + 90.0 + + + 645.0 + 708.0 + + + + + [x=63.0][y=58.0] + + 10 + 90.0 + + + 737.0 + 802.0 + + + + + [x=87.0][y=30.0] + + 10 + 90.0 + + + 668.0 + 731.0 + + + + + [x=88.0][y=35.0] + + 20 + 90.0 + + + 109.0 + 170.0 + + + + + [x=88.0][y=30.0] + + 10 + 90.0 + + + 574.0 + 643.0 + + + + + [x=75.0][y=55.0] + + 20 + 90.0 + + + 369.0 + 420.0 + + + + + [x=72.0][y=55.0] + + 10 + 90.0 + + + 265.0 + 338.0 + + + + + [x=85.0][y=25.0] + + 10 + 90.0 + + + 769.0 + 820.0 + + + + + [x=85.0][y=35.0] + + 30 + 90.0 + + + 47.0 + 124.0 + + + + + [x=66.0][y=55.0] + + 10 + 90.0 + + + 173.0 + 238.0 + + + + + [x=65.0][y=55.0] + + 20 + 90.0 + + + 85.0 + 144.0 + + + + + [x=70.0][y=58.0] + + 20 + 90.0 + + + 458.0 + 523.0 + + + + + [x=68.0][y=60.0] + + 30 + 90.0 + + + 555.0 + 612.0 + + + + + [x=47.0][y=40.0] + + 10 + 90.0 + + + 12.0 + 77.0 + + + + + [x=47.0][y=35.0] + + 10 + 90.0 + + + 826.0 + 875.0 + + + + + [x=45.0][y=35.0] + + 10 + 90.0 + + + 916.0 + 969.0 + + + + + [x=45.0][y=30.0] + + 10 + 90.0 + + + 734.0 + 777.0 + + + + + [x=95.0][y=30.0] + + 30 + 90.0 + + + 387.0 + 456.0 + + + + + [x=95.0][y=35.0] + + 20 + 90.0 + + + 293.0 + 360.0 + + + + + [x=53.0][y=30.0] + + 10 + 90.0 + + + 450.0 + 505.0 + + + + + [x=92.0][y=30.0] + + 10 + 90.0 + + + 478.0 + 551.0 + + + + + [x=53.0][y=35.0] + + 50 + 90.0 + + + 353.0 + 412.0 + + + + + [x=45.0][y=65.0] + + 20 + 90.0 + + + 997.0 + 1068.0 + + + + + [x=90.0][y=35.0] + + 10 + 90.0 + + + 203.0 + 260.0 + + + + + [x=38.0][y=15.0] + + 10 + 90.0 + + + 651.0 + 740.0 + + + + + [x=38.0][y=5.0] + + 30 + 90.0 + + + 471.0 + 534.0 + + + + + [x=40.0][y=15.0] + + 40 + 90.0 + + + 35.0 + 87.0 + + + + + [x=40.0][y=5.0] + + 30 + 90.0 + + + 385.0 + 436.0 + + + + + [x=42.0][y=15.0] + + 10 + 90.0 + + + 95.0 + 158.0 + + + + + [x=48.0][y=30.0] + + 10 + 90.0 + + + 632.0 + 693.0 + + + + + [x=48.0][y=40.0] + + 10 + 90.0 + + + 76.0 + 129.0 + + + + + [x=50.0][y=35.0] + + 20 + 90.0 + + + 262.0 + 317.0 + + + + + [x=50.0][y=40.0] + + 50 + 90.0 + + + 171.0 + 218.0 + + + + + [x=35.0][y=5.0] + + 20 + 90.0 + + + 562.0 + 629.0 + + + + + [x=50.0][y=30.0] + + 10 + 90.0 + + + 531.0 + 610.0 + + + + + [x=28.0][y=35.0] + + 10 + 90.0 + + + 1001.0 + 1066.0 + + + + + [x=28.0][y=30.0] + + 10 + 90.0 + + + 632.0 + 693.0 + + + + + [x=30.0][y=30.0] + + 10 + 90.0 + + + 541.0 + 600.0 + + + + + [x=32.0][y=30.0] + + 10 + 90.0 + + + 359.0 + 412.0 + + + + + [x=30.0][y=35.0] + + 10 + 90.0 + + + 1054.0 + 1127.0 + + + + + [x=30.0][y=32.0] + + 30 + 90.0 + + + 448.0 + 509.0 + + + + + [x=25.0][y=30.0] + + 10 + 90.0 + + + 725.0 + 786.0 + + + + + [x=25.0][y=35.0] + + 10 + 90.0 + + + 912.0 + 969.0 + + + + + [x=44.0][y=5.0] + + 20 + 90.0 + + + 286.0 + 347.0 + + + + + [x=42.0][y=10.0] + + 40 + 90.0 + + + 186.0 + 257.0 + + + + + [x=26.0][y=32.0] + + 10 + 90.0 + + + 815.0 + 880.0 + + + + + diff --git a/jsprit-examples/src/main/java/jsprit/examples/SimpleExampleOpenRoutes.java b/jsprit-examples/src/main/java/jsprit/examples/SimpleExampleOpenRoutes.java index 1bc409b7..21599f2b 100644 --- a/jsprit-examples/src/main/java/jsprit/examples/SimpleExampleOpenRoutes.java +++ b/jsprit-examples/src/main/java/jsprit/examples/SimpleExampleOpenRoutes.java @@ -23,6 +23,7 @@ import jsprit.analysis.toolbox.SolutionPlotter; import jsprit.analysis.toolbox.SolutionPrinter; import jsprit.core.algorithm.VehicleRoutingAlgorithm; import jsprit.core.algorithm.box.SchrimpfFactory; +import jsprit.core.algorithm.io.VehicleRoutingAlgorithms; import jsprit.core.problem.VehicleRoutingProblem; import jsprit.core.problem.io.VrpXMLWriter; import jsprit.core.problem.job.Service; @@ -54,6 +55,7 @@ public class SimpleExampleOpenRoutes { * get a vehicle type-builder and build a type with the typeId "vehicleType" and a capacity of 2 */ VehicleTypeImpl.Builder vehicleTypeBuilder = VehicleTypeImpl.Builder.newInstance("vehicleType", 2); + vehicleTypeBuilder.setFixedCost(100); VehicleType vehicleType = vehicleTypeBuilder.build(); /* @@ -63,6 +65,7 @@ public class SimpleExampleOpenRoutes { vehicleBuilder.setLocationCoord(Coordinate.newInstance(10, 10)); vehicleBuilder.setType(vehicleType); vehicleBuilder.setReturnToDepot(false); + Vehicle vehicle = vehicleBuilder.build(); /* @@ -84,7 +87,7 @@ public class SimpleExampleOpenRoutes { /* * get the algorithm out-of-the-box. */ - VehicleRoutingAlgorithm algorithm = new SchrimpfFactory().createAlgorithm(problem); + VehicleRoutingAlgorithm algorithm = VehicleRoutingAlgorithms.readAndCreateAlgorithm(problem, "input/algorithmConfig_fix.xml"); /* * and search a solution diff --git a/jsprit-examples/src/main/java/jsprit/examples/SolomonOpenExample.java b/jsprit-examples/src/main/java/jsprit/examples/SolomonOpenExample.java new file mode 100644 index 00000000..6cba5e5c --- /dev/null +++ b/jsprit-examples/src/main/java/jsprit/examples/SolomonOpenExample.java @@ -0,0 +1,101 @@ +/******************************************************************************* + * Copyright (C) 2013 Stefan Schroeder + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3.0 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library. If not, see . + ******************************************************************************/ +package jsprit.examples; + +import java.io.File; +import java.util.Collection; + +import jsprit.analysis.toolbox.SolutionPlotter; +import jsprit.analysis.toolbox.SolutionPrinter; +import jsprit.core.algorithm.VehicleRoutingAlgorithm; +import jsprit.core.algorithm.io.VehicleRoutingAlgorithms; +import jsprit.core.algorithm.selector.SelectBest; +import jsprit.core.problem.VehicleRoutingProblem; +import jsprit.core.problem.io.VrpXMLReader; +import jsprit.core.problem.solution.VehicleRoutingProblemSolution; +import jsprit.instance.reader.SolomonReader; + + +public class SolomonOpenExample { + + public static void main(String[] args) { + /* + * some preparation - create output folder + */ + File dir = new File("output"); + // if the directory does not exist, create it + if (!dir.exists()){ + System.out.println("creating directory ./output"); + boolean result = dir.mkdir(); + if(result) System.out.println("./output created"); + } + + /* + * Build the problem. + * + * But define a problem-builder first. + */ + VehicleRoutingProblem.Builder vrpBuilder = VehicleRoutingProblem.Builder.newInstance(); + + /* + * A solomonReader reads solomon-instance files, and stores the required information in the builder. + */ + new VrpXMLReader(vrpBuilder).read("input/deliveries_solomon_open_c101.xml"); + + /* + * Finally, the problem can be built. By default, transportCosts are crowFlyDistances (as usually used for vrp-instances). + */ + VehicleRoutingProblem vrp = vrpBuilder.build(); + + SolutionPlotter.plotVrpAsPNG(vrp, "output/solomon_C101_open.png", "C101"); + + /* + * Define the required vehicle-routing algorithms to solve the above problem. + * + * The algorithm can be defined and configured in an xml-file. + */ +// VehicleRoutingAlgorithm vra = new SchrimpfFactory().createAlgorithm(vrp); + VehicleRoutingAlgorithm vra = VehicleRoutingAlgorithms.readAndCreateAlgorithm(vrp, "input/algorithmConfig_fix.xml"); +// vra.setPrematureBreak(100); +// vra.getAlgorithmListeners().addListener(new AlgorithmSearchProgressChartListener("output/sol_progress.png")); + /* + * Solve the problem. + * + * + */ + Collection solutions = vra.searchSolutions(); + + /* + * Retrieve best solution. + */ + VehicleRoutingProblemSolution solution = new SelectBest().selectSolution(solutions); + + /* + * print solution + */ + SolutionPrinter.print(solution); + + /* + * Plot solution. + */ + SolutionPlotter.plotSolutionAsPNG(vrp, solution, "output/solomon_C101_open_solution.png","C101"); + + + + } + +}