From 16069c9400dc9de7cad0cef3f10420a393a99810 Mon Sep 17 00:00:00 2001 From: oblonski Date: Fri, 23 Oct 2015 13:44:37 +0200 Subject: [PATCH] add helper method to vrp - .getAllLocations(..) --- .../core/problem/VehicleRoutingProblem.java | 55 +++++++++++++------ .../problem/VehicleRoutingProblemTest.java | 9 ++- 2 files changed, 45 insertions(+), 19 deletions(-) diff --git a/jsprit-core/src/main/java/jsprit/core/problem/VehicleRoutingProblem.java b/jsprit-core/src/main/java/jsprit/core/problem/VehicleRoutingProblem.java index b7877b3e..087be134 100644 --- a/jsprit-core/src/main/java/jsprit/core/problem/VehicleRoutingProblem.java +++ b/jsprit-core/src/main/java/jsprit/core/problem/VehicleRoutingProblem.java @@ -137,6 +137,8 @@ public class VehicleRoutingProblem { vehicleTypeIdIndexCounter++; } + private Set allLocations = new HashSet(); + /** * Returns the unmodifiable map of collected locations (mapped by their location-id). * @@ -146,6 +148,8 @@ public class VehicleRoutingProblem { return Collections.unmodifiableMap(tentative_coordinates); } + + /** * Returns the locations collected SO FAR by this builder. *

@@ -229,14 +233,25 @@ public class VehicleRoutingProblem { private void addLocationToTentativeLocations(Job job) { if (job instanceof Service) { - tentative_coordinates.put(((Service) job).getLocation().getId(), ((Service) job).getLocation().getCoordinate()); + Location location = ((Service) job).getLocation(); +// tentative_coordinates.put(location.getId(), location.getCoordinate()); + addLocationToTentativeLocations(location); } else if (job instanceof Shipment) { Shipment shipment = (Shipment) job; - tentative_coordinates.put(shipment.getPickupLocation().getId(), shipment.getPickupLocation().getCoordinate()); - tentative_coordinates.put(shipment.getDeliveryLocation().getId(), shipment.getDeliveryLocation().getCoordinate()); + Location pickupLocation = shipment.getPickupLocation(); + addLocationToTentativeLocations(pickupLocation); +// tentative_coordinates.put(pickupLocation.getId(), pickupLocation.getCoordinate()); + Location deliveryLocation = shipment.getDeliveryLocation(); + addLocationToTentativeLocations(deliveryLocation); +// tentative_coordinates.put(deliveryLocation.getId(), deliveryLocation.getCoordinate()); } } + private void addLocationToTentativeLocations(Location location) { + tentative_coordinates.put(location.getId(), location.getCoordinate()); + allLocations.add(location); + } + private void addJobToFinalJobMapAndCreateActivities(Job job) { if (job instanceof Service) { Service service = (Service) job; @@ -282,7 +297,7 @@ public class VehicleRoutingProblem { if (act instanceof TourActivity.JobActivity) { Job job = ((TourActivity.JobActivity) act).getJob(); jobsInInitialRoutes.add(job.getId()); - registerLocation(job); + addLocationToTentativeLocations(job); registerJobAndActivity(abstractAct, job); } } @@ -290,15 +305,7 @@ public class VehicleRoutingProblem { return this; } - private void registerLocation(Job job) { - if (job instanceof Service) - tentative_coordinates.put(((Service) job).getLocation().getId(), ((Service) job).getLocation().getCoordinate()); - if (job instanceof Shipment) { - Shipment shipment = (Shipment) job; - tentative_coordinates.put(shipment.getPickupLocation().getId(), shipment.getPickupLocation().getCoordinate()); - tentative_coordinates.put(shipment.getDeliveryLocation().getId(), shipment.getDeliveryLocation().getCoordinate()); - } - } + private void registerJobAndActivity(AbstractActivity abstractAct, Job job) { if (activityMap.containsKey(job)) activityMap.get(job).add(abstractAct); @@ -326,8 +333,9 @@ public class VehicleRoutingProblem { if (jobs.containsKey(job.getId())) { logger.warn("job " + job + " already in job list. overrides existing job."); } - tentative_coordinates.put(job.getPickupLocation().getId(), job.getPickupLocation().getCoordinate()); - tentative_coordinates.put(job.getDeliveryLocation().getId(), job.getDeliveryLocation().getCoordinate()); + addLocationToTentativeLocations(job); +// tentative_coordinates.put(job.getPickupLocation().getId(), job.getPickupLocation().getCoordinate()); +// tentative_coordinates.put(job.getDeliveryLocation().getId(), job.getDeliveryLocation().getCoordinate()); jobs.put(job.getId(), job); } @@ -368,9 +376,11 @@ public class VehicleRoutingProblem { vehicleTypes.add(vehicle.getType()); } String startLocationId = vehicle.getStartLocation().getId(); - tentative_coordinates.put(startLocationId, vehicle.getStartLocation().getCoordinate()); + addLocationToTentativeLocations(vehicle.getStartLocation()); +// tentative_coordinates.put(startLocationId, vehicle.getStartLocation().getCoordinate()); if (!vehicle.getEndLocation().getId().equals(startLocationId)) { - tentative_coordinates.put(vehicle.getEndLocation().getId(), vehicle.getEndLocation().getCoordinate()); + addLocationToTentativeLocations(vehicle.getEndLocation()); +// tentative_coordinates.put(vehicle.getEndLocation().getId(), vehicle.getEndLocation().getCoordinate()); } return this; } @@ -477,7 +487,8 @@ public class VehicleRoutingProblem { } private Builder addService(Service service) { - tentative_coordinates.put(service.getLocation().getId(), service.getLocation().getCoordinate()); +// tentative_coordinates.put(service.getLocation().getId(), service.getLocation().getCoordinate()); + addLocationToTentativeLocations(service); if (jobs.containsKey(service.getId())) { logger.warn("service " + service + " already in job list. overrides existing job."); } @@ -530,6 +541,8 @@ public class VehicleRoutingProblem { private final Collection initialVehicleRoutes; + private final Collection allLocations; + /** * An enum that indicates type of fleetSize. By default, it is INFINTE */ @@ -561,6 +574,7 @@ public class VehicleRoutingProblem { this.locations = builder.getLocations(); this.activityMap = builder.activityMap; this.nuActivities = builder.activityIndexCounter; + this.allLocations = builder.allLocations; logger.info("setup problem: {}", this); } @@ -644,10 +658,15 @@ public class VehicleRoutingProblem { /** * @return returns all location, i.e. from vehicles and jobs. */ + @Deprecated public Locations getLocations() { return locations; } + + public Collection getAllLocations(){ + return allLocations; + } /** * @param job for which the corresponding activities needs to be returned * @return associated activities diff --git a/jsprit-core/src/test/java/jsprit/core/problem/VehicleRoutingProblemTest.java b/jsprit-core/src/test/java/jsprit/core/problem/VehicleRoutingProblemTest.java index 6cb6afb3..62cbeaa7 100644 --- a/jsprit-core/src/test/java/jsprit/core/problem/VehicleRoutingProblemTest.java +++ b/jsprit-core/src/test/java/jsprit/core/problem/VehicleRoutingProblemTest.java @@ -75,6 +75,7 @@ public class VehicleRoutingProblemTest { VehicleRoutingProblem vrp = builder.build(); assertEquals(4, vrp.getVehicles().size()); + assertEquals(1, vrp.getAllLocations().size()); } @@ -125,6 +126,7 @@ public class VehicleRoutingProblemTest { assertEquals(2, vrp.getJobs().size()); assertEquals(s, vrp.getJobs().get("s")); assertEquals(s2, vrp.getJobs().get("s2")); + assertEquals(2,vrp.getAllLocations().size()); } @Test @@ -144,6 +146,7 @@ public class VehicleRoutingProblemTest { assertEquals(2, vrp.getJobs().size()); assertEquals(s1, vrp.getJobs().get("s1")); assertEquals(s2, vrp.getJobs().get("s2")); + assertEquals(1,vrp.getAllLocations().size()); } @@ -304,6 +307,7 @@ public class VehicleRoutingProblemTest { assertEquals(1, builder.getAddedVehicleTypes().size()); assertEquals(type, builder.getAddedVehicleTypes().iterator().next()); + } @Test @@ -383,6 +387,7 @@ public class VehicleRoutingProblemTest { VehicleRoutingProblem vrp = vrpBuilder.build(); assertEquals(2, vrp.getInitialVehicleRoutes().size()); + assertEquals(2,vrp.getAllLocations().size()); } @Test @@ -410,6 +415,7 @@ public class VehicleRoutingProblemTest { vrpBuilder.addInitialVehicleRoute(initialRoute); VehicleRoutingProblem vrp = vrpBuilder.build(); assertFalse(vrp.getJobs().containsKey("myService")); + assertEquals(3,vrp.getAllLocations().size()); } @Test @@ -420,10 +426,11 @@ public class VehicleRoutingProblemTest { VehicleRoutingProblem.Builder vrpBuilder = VehicleRoutingProblem.Builder.newInstance(); vrpBuilder.addJob(service); vrpBuilder.addJob(shipment); - vrpBuilder.build(); + VehicleRoutingProblem vrp = vrpBuilder.build(); assertEquals(1, service.getIndex()); assertEquals(2, shipment.getIndex()); + assertEquals(3,vrp.getAllLocations().size()); }