diff --git a/jsprit-core/src/main/java/com/graphhopper/jsprit/core/algorithm/VehicleRoutingAlgorithm.java b/jsprit-core/src/main/java/com/graphhopper/jsprit/core/algorithm/VehicleRoutingAlgorithm.java index 1155955b..f0c7a383 100644 --- a/jsprit-core/src/main/java/com/graphhopper/jsprit/core/algorithm/VehicleRoutingAlgorithm.java +++ b/jsprit-core/src/main/java/com/graphhopper/jsprit/core/algorithm/VehicleRoutingAlgorithm.java @@ -135,23 +135,28 @@ public class VehicleRoutingAlgorithm { this.objectiveFunction = objectiveFunction; } - /** - * Adds solution to the collection of initial solutions. - * - * @param solution the solution to be added - */ - public void addInitialSolution(VehicleRoutingProblemSolution solution) { + /** + * Adds solution to the collection of initial solutions. + * + * This method may lead to errors if tour activities in the solution are different to the + * ones in the VRP (including differences in indexing) + * + * @param solution the solution to be added + */ + public void addInitialSolution(VehicleRoutingProblemSolution solution) { // We will make changes so let's make a copy solution = VehicleRoutingProblemSolution.copyOf(solution); - verify(solution); + verifyAndAdaptSolution(solution); initialSolutions.add(solution); } - private void verify(VehicleRoutingProblemSolution solution) { - Set allJobs = new HashSet(problem.getJobs().values()); - allJobs.removeAll(solution.getUnassignedJobs()); + //this method may lead to errors if tour activities in the solution are different to the ones in the VRP + //(including differences in indexing) + private void verifyAndAdaptSolution(VehicleRoutingProblemSolution solution) { + Set jobsNotInSolution = new HashSet(problem.getJobs().values()); + jobsNotInSolution.removeAll(solution.getUnassignedJobs()); for (VehicleRoute route : solution.getRoutes()) { - allJobs.removeAll(route.getTourActivities().getJobs()); + jobsNotInSolution.removeAll(route.getTourActivities().getJobs()); if (route.getVehicle().getIndex() == 0) throw new IllegalStateException("vehicle used in initial solution has no index. probably a vehicle is used that has not been added to the " + " the VehicleRoutingProblem. only use vehicles that have already been added to the problem."); @@ -164,7 +169,9 @@ public class VehicleRoutingAlgorithm { } } - solution.getUnassignedJobs().addAll(allJobs); + //if solution is partial (not all jobs are considered), add these jobs to solution.unassignedJobs + solution.getUnassignedJobs().addAll(jobsNotInSolution); + //update the cost of solution (regardless if partial or not) solution.setCost(getObjectiveFunction().getCosts(solution)); // if (nuJobs != problem.getJobs().values().size()) {