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

remove unrelated breaks from unassigned jobs

This commit is contained in:
Kandel Irina 2018-02-08 14:15:20 +02:00
parent 92afffcf1e
commit 82a1a1117d
2 changed files with 63 additions and 3 deletions

View file

@ -23,8 +23,10 @@ import com.graphhopper.jsprit.core.algorithm.recreate.InsertionStrategy;
import com.graphhopper.jsprit.core.algorithm.recreate.listener.InsertionListener;
import com.graphhopper.jsprit.core.algorithm.ruin.RuinStrategy;
import com.graphhopper.jsprit.core.algorithm.ruin.listener.RuinListener;
import com.graphhopper.jsprit.core.problem.job.Break;
import com.graphhopper.jsprit.core.problem.job.Job;
import com.graphhopper.jsprit.core.problem.solution.VehicleRoutingProblemSolution;
import com.graphhopper.jsprit.core.problem.solution.route.VehicleRoute;
import java.util.Collection;
import java.util.HashSet;
@ -49,14 +51,30 @@ public class RuinAndRecreateModule implements SearchStrategyModule {
@Override
public VehicleRoutingProblemSolution runAndGetSolution(VehicleRoutingProblemSolution vrpSolution) {
Collection<Job> ruinedJobs = ruin.ruin(vrpSolution.getRoutes());
Set<Job> ruinedJobSet = new HashSet<Job>();
Set<Job> ruinedJobSet = new HashSet<>();
ruinedJobSet.addAll(ruinedJobs);
ruinedJobSet.addAll(vrpSolution.getUnassignedJobs());
Collection<Job> unassignedJobs = insertion.insertJobs(vrpSolution.getRoutes(), ruinedJobSet);
vrpSolution.getUnassignedJobs().clear();
vrpSolution.getUnassignedJobs().addAll(unassignedJobs);
return vrpSolution;
vrpSolution.getUnassignedJobs().addAll(getUnassignedJobs(vrpSolution, unassignedJobs));
return vrpSolution;
}
static Set<Job> getUnassignedJobs(VehicleRoutingProblemSolution vrpSolution, Collection<Job> unassignedJobs) {
final Set<Job> unassigned = new HashSet<>();
for (Job job : unassignedJobs) {
if (!(job instanceof Break))
unassigned.add(job);
}
for (VehicleRoute route : vrpSolution.getRoutes()) {
Break aBreak = route.getVehicle().getBreak();
if(aBreak != null && !route.getTourActivities().servesJob(aBreak)) {
unassigned.add(aBreak);
}
}
return unassigned;
}
@Override

View file

@ -0,0 +1,42 @@
package com.graphhopper.jsprit.core.algorithm.module;
import com.graphhopper.jsprit.core.problem.job.Break;
import com.graphhopper.jsprit.core.problem.job.Job;
import com.graphhopper.jsprit.core.problem.solution.VehicleRoutingProblemSolution;
import com.graphhopper.jsprit.core.problem.solution.route.VehicleRoute;
import com.graphhopper.jsprit.core.problem.solution.route.activity.TourActivities;
import com.graphhopper.jsprit.core.problem.vehicle.Vehicle;
import org.junit.Test;
import java.util.*;
import static com.graphhopper.jsprit.core.algorithm.module.RuinAndRecreateModule.getUnassignedJobs;
import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
public class RuinAndRecreateModuleTest {
@Test
public void testUnassignedJobs() {
VehicleRoute vehicleRoute = mock(VehicleRoute.class);
Vehicle vehicle = mock(Vehicle.class);
Break aBreak = Break.Builder.newInstance(UUID.randomUUID().toString()).build();
when(vehicle.getBreak()).thenReturn(aBreak);
when(vehicleRoute.getVehicle()).thenReturn(vehicle);
TourActivities tourActivities = mock(TourActivities.class);
when(vehicleRoute.getTourActivities()).thenReturn(tourActivities);
Collection<VehicleRoute> routes = new ArrayList<>();
routes.add(vehicleRoute);
Break unRelatedBreak = Break.Builder.newInstance(UUID.randomUUID().toString()).build();
Set<Job> unassigned = new HashSet<>();
unassigned.add(unRelatedBreak);
when(tourActivities.servesJob(aBreak)).thenReturn(true);
assertEquals(0, getUnassignedJobs(new VehicleRoutingProblemSolution(routes, 212), unassigned).size());
when(tourActivities.servesJob(aBreak)).thenReturn(false);
assertEquals(1, getUnassignedJobs(new VehicleRoutingProblemSolution(routes, 212), unassigned).size());
}
}