1
0
Fork 0
mirror of https://github.com/graphhopper/jsprit.git synced 2020-01-24 07:45:05 +01:00

add helper method to vrp - .getAllLocations(..)

This commit is contained in:
oblonski 2015-10-23 13:44:37 +02:00
parent 7166ac2515
commit 16069c9400
2 changed files with 45 additions and 19 deletions

View file

@ -137,6 +137,8 @@ public class VehicleRoutingProblem {
vehicleTypeIdIndexCounter++; vehicleTypeIdIndexCounter++;
} }
private Set<Location> allLocations = new HashSet<Location>();
/** /**
* Returns the unmodifiable map of collected locations (mapped by their location-id). * Returns the unmodifiable map of collected locations (mapped by their location-id).
* *
@ -146,6 +148,8 @@ public class VehicleRoutingProblem {
return Collections.unmodifiableMap(tentative_coordinates); return Collections.unmodifiableMap(tentative_coordinates);
} }
/** /**
* Returns the locations collected SO FAR by this builder. * Returns the locations collected SO FAR by this builder.
* <p/> * <p/>
@ -229,14 +233,25 @@ public class VehicleRoutingProblem {
private void addLocationToTentativeLocations(Job job) { private void addLocationToTentativeLocations(Job job) {
if (job instanceof Service) { 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) { } else if (job instanceof Shipment) {
Shipment shipment = (Shipment) job; Shipment shipment = (Shipment) job;
tentative_coordinates.put(shipment.getPickupLocation().getId(), shipment.getPickupLocation().getCoordinate()); Location pickupLocation = shipment.getPickupLocation();
tentative_coordinates.put(shipment.getDeliveryLocation().getId(), shipment.getDeliveryLocation().getCoordinate()); 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) { private void addJobToFinalJobMapAndCreateActivities(Job job) {
if (job instanceof Service) { if (job instanceof Service) {
Service service = (Service) job; Service service = (Service) job;
@ -282,7 +297,7 @@ public class VehicleRoutingProblem {
if (act instanceof TourActivity.JobActivity) { if (act instanceof TourActivity.JobActivity) {
Job job = ((TourActivity.JobActivity) act).getJob(); Job job = ((TourActivity.JobActivity) act).getJob();
jobsInInitialRoutes.add(job.getId()); jobsInInitialRoutes.add(job.getId());
registerLocation(job); addLocationToTentativeLocations(job);
registerJobAndActivity(abstractAct, job); registerJobAndActivity(abstractAct, job);
} }
} }
@ -290,15 +305,7 @@ public class VehicleRoutingProblem {
return this; 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) { private void registerJobAndActivity(AbstractActivity abstractAct, Job job) {
if (activityMap.containsKey(job)) activityMap.get(job).add(abstractAct); if (activityMap.containsKey(job)) activityMap.get(job).add(abstractAct);
@ -326,8 +333,9 @@ public class VehicleRoutingProblem {
if (jobs.containsKey(job.getId())) { if (jobs.containsKey(job.getId())) {
logger.warn("job " + job + " already in job list. overrides existing job."); logger.warn("job " + job + " already in job list. overrides existing job.");
} }
tentative_coordinates.put(job.getPickupLocation().getId(), job.getPickupLocation().getCoordinate()); addLocationToTentativeLocations(job);
tentative_coordinates.put(job.getDeliveryLocation().getId(), job.getDeliveryLocation().getCoordinate()); // tentative_coordinates.put(job.getPickupLocation().getId(), job.getPickupLocation().getCoordinate());
// tentative_coordinates.put(job.getDeliveryLocation().getId(), job.getDeliveryLocation().getCoordinate());
jobs.put(job.getId(), job); jobs.put(job.getId(), job);
} }
@ -368,9 +376,11 @@ public class VehicleRoutingProblem {
vehicleTypes.add(vehicle.getType()); vehicleTypes.add(vehicle.getType());
} }
String startLocationId = vehicle.getStartLocation().getId(); 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)) { 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; return this;
} }
@ -477,7 +487,8 @@ public class VehicleRoutingProblem {
} }
private Builder addService(Service service) { 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())) { if (jobs.containsKey(service.getId())) {
logger.warn("service " + service + " already in job list. overrides existing job."); logger.warn("service " + service + " already in job list. overrides existing job.");
} }
@ -530,6 +541,8 @@ public class VehicleRoutingProblem {
private final Collection<VehicleRoute> initialVehicleRoutes; private final Collection<VehicleRoute> initialVehicleRoutes;
private final Collection<Location> allLocations;
/** /**
* An enum that indicates type of fleetSize. By default, it is INFINTE * An enum that indicates type of fleetSize. By default, it is INFINTE
*/ */
@ -561,6 +574,7 @@ public class VehicleRoutingProblem {
this.locations = builder.getLocations(); this.locations = builder.getLocations();
this.activityMap = builder.activityMap; this.activityMap = builder.activityMap;
this.nuActivities = builder.activityIndexCounter; this.nuActivities = builder.activityIndexCounter;
this.allLocations = builder.allLocations;
logger.info("setup problem: {}", this); logger.info("setup problem: {}", this);
} }
@ -644,10 +658,15 @@ public class VehicleRoutingProblem {
/** /**
* @return returns all location, i.e. from vehicles and jobs. * @return returns all location, i.e. from vehicles and jobs.
*/ */
@Deprecated
public Locations getLocations() { public Locations getLocations() {
return locations; return locations;
} }
public Collection<Location> getAllLocations(){
return allLocations;
}
/** /**
* @param job for which the corresponding activities needs to be returned * @param job for which the corresponding activities needs to be returned
* @return associated activities * @return associated activities

View file

@ -75,6 +75,7 @@ public class VehicleRoutingProblemTest {
VehicleRoutingProblem vrp = builder.build(); VehicleRoutingProblem vrp = builder.build();
assertEquals(4, vrp.getVehicles().size()); assertEquals(4, vrp.getVehicles().size());
assertEquals(1, vrp.getAllLocations().size());
} }
@ -125,6 +126,7 @@ public class VehicleRoutingProblemTest {
assertEquals(2, vrp.getJobs().size()); assertEquals(2, vrp.getJobs().size());
assertEquals(s, vrp.getJobs().get("s")); assertEquals(s, vrp.getJobs().get("s"));
assertEquals(s2, vrp.getJobs().get("s2")); assertEquals(s2, vrp.getJobs().get("s2"));
assertEquals(2,vrp.getAllLocations().size());
} }
@Test @Test
@ -144,6 +146,7 @@ public class VehicleRoutingProblemTest {
assertEquals(2, vrp.getJobs().size()); assertEquals(2, vrp.getJobs().size());
assertEquals(s1, vrp.getJobs().get("s1")); assertEquals(s1, vrp.getJobs().get("s1"));
assertEquals(s2, vrp.getJobs().get("s2")); assertEquals(s2, vrp.getJobs().get("s2"));
assertEquals(1,vrp.getAllLocations().size());
} }
@ -304,6 +307,7 @@ public class VehicleRoutingProblemTest {
assertEquals(1, builder.getAddedVehicleTypes().size()); assertEquals(1, builder.getAddedVehicleTypes().size());
assertEquals(type, builder.getAddedVehicleTypes().iterator().next()); assertEquals(type, builder.getAddedVehicleTypes().iterator().next());
} }
@Test @Test
@ -383,6 +387,7 @@ public class VehicleRoutingProblemTest {
VehicleRoutingProblem vrp = vrpBuilder.build(); VehicleRoutingProblem vrp = vrpBuilder.build();
assertEquals(2, vrp.getInitialVehicleRoutes().size()); assertEquals(2, vrp.getInitialVehicleRoutes().size());
assertEquals(2,vrp.getAllLocations().size());
} }
@Test @Test
@ -410,6 +415,7 @@ public class VehicleRoutingProblemTest {
vrpBuilder.addInitialVehicleRoute(initialRoute); vrpBuilder.addInitialVehicleRoute(initialRoute);
VehicleRoutingProblem vrp = vrpBuilder.build(); VehicleRoutingProblem vrp = vrpBuilder.build();
assertFalse(vrp.getJobs().containsKey("myService")); assertFalse(vrp.getJobs().containsKey("myService"));
assertEquals(3,vrp.getAllLocations().size());
} }
@Test @Test
@ -420,10 +426,11 @@ public class VehicleRoutingProblemTest {
VehicleRoutingProblem.Builder vrpBuilder = VehicleRoutingProblem.Builder.newInstance(); VehicleRoutingProblem.Builder vrpBuilder = VehicleRoutingProblem.Builder.newInstance();
vrpBuilder.addJob(service); vrpBuilder.addJob(service);
vrpBuilder.addJob(shipment); vrpBuilder.addJob(shipment);
vrpBuilder.build(); VehicleRoutingProblem vrp = vrpBuilder.build();
assertEquals(1, service.getIndex()); assertEquals(1, service.getIndex());
assertEquals(2, shipment.getIndex()); assertEquals(2, shipment.getIndex());
assertEquals(3,vrp.getAllLocations().size());
} }