From eaa0a9e5e4089b57589e0c5822ff26ec729eaa10 Mon Sep 17 00:00:00 2001 From: oblonski <4sschroeder@gmail.com> Date: Fri, 25 Apr 2014 23:32:29 +0200 Subject: [PATCH] modified core.problem.VehicleRoutingProblem.Builder to deal with jobs being part of initial routes i.e. these jobs do not appear in jobMap of problem since they are not variable anymore --- .../core/problem/VehicleRoutingProblem.java | 23 +++++++++++++++---- 1 file changed, 18 insertions(+), 5 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 d9d10d14..da97c9c5 100644 --- a/jsprit-core/src/main/java/jsprit/core/problem/VehicleRoutingProblem.java +++ b/jsprit-core/src/main/java/jsprit/core/problem/VehicleRoutingProblem.java @@ -161,6 +161,10 @@ public class VehicleRoutingProblem { private Map jobs; + private Map tentativeJobs = new HashMap(); + + private Set jobsInInitialRoutes = new HashSet(); + private Collection services; private Map coordinates; @@ -298,21 +302,25 @@ public class VehicleRoutingProblem { * @throws IllegalStateException if job is neither a shipment nor a service, or jobId has already been added. */ public Builder addJob(Job job) { - if(jobs.containsKey(job.getId())) throw new IllegalStateException("jobList already contains a job with id " + job.getId() + ". make sure you use unique ids for your jobs (i.e. service and shipments)"); + if(tentativeJobs.containsKey(job.getId())) throw new IllegalStateException("jobList already contains a job with id " + job.getId() + ". make sure you use unique ids for your jobs (i.e. service and shipments)"); + if(!(job instanceof Service || job instanceof Shipment)) throw new IllegalStateException("job must be either a service or a shipment"); + tentativeJobs.put(job.getId(), job); + return this; + } + + private void addJobToFinalJobMap(Job job){ if(job instanceof Service) { addService((Service) job); - return this; } else if(job instanceof Shipment){ addShipment((Shipment)job); - return this; } - throw new IllegalStateException("job must be either a service or a shipment"); } public Builder addInitialVehicleRoute(VehicleRoute route){ addVehicle(route.getVehicle()); for(Job job : route.getTourActivities().getJobs()){ + jobsInInitialRoutes.add(job.getId()); if(job instanceof Service) coordinates.put(((Service)job).getLocationId(), ((Service)job).getCoord()); if(job instanceof Shipment){ Shipment shipment = (Shipment)job; @@ -393,6 +401,11 @@ public class VehicleRoutingProblem { addPenaltyVehicles(); } } + for(Job job : tentativeJobs.values()){ + if(!jobsInInitialRoutes.contains(job.getId())){ + addJobToFinalJobMap(job); + } + } return new VehicleRoutingProblem(this); } @@ -528,7 +541,7 @@ public class VehicleRoutingProblem { * @return collection of jobs */ public Collection getAddedJobs(){ - return Collections.unmodifiableCollection(jobs.values()); + return Collections.unmodifiableCollection(tentativeJobs.values()); } /**