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

fixed bug #112 and #114

This commit is contained in:
oblonski 2014-07-30 12:18:56 +02:00
parent bca9323c67
commit 58c9030b8a
3 changed files with 270 additions and 5 deletions

View file

@ -64,7 +64,8 @@ final class VehicleTypeDependentJobInsertionCalculator implements JobInsertionCo
} }
private void getInitialVehicleIds() { private void getInitialVehicleIds() {
for(VehicleRoute initialRoute : vrp.getInitialVehicleRoutes()){ Collection<VehicleRoute> initialVehicleRoutes = vrp.getInitialVehicleRoutes();
for(VehicleRoute initialRoute : initialVehicleRoutes){
initialVehicleIds.add(initialRoute.getVehicle().getId()); initialVehicleIds.add(initialRoute.getVehicle().getId());
} }
} }

View file

@ -2,8 +2,12 @@ package jsprit.core.algorithm;
import jsprit.core.algorithm.box.SchrimpfFactory; import jsprit.core.algorithm.box.SchrimpfFactory;
import jsprit.core.problem.AbstractActivity;
import jsprit.core.problem.VehicleRoutingProblem; import jsprit.core.problem.VehicleRoutingProblem;
import jsprit.core.problem.io.VrpXMLReader; import jsprit.core.problem.io.VrpXMLReader;
import jsprit.core.problem.job.Job;
import jsprit.core.problem.job.Service;
import jsprit.core.problem.job.Shipment;
import jsprit.core.problem.solution.VehicleRoutingProblemSolution; import jsprit.core.problem.solution.VehicleRoutingProblemSolution;
import jsprit.core.problem.solution.route.VehicleRoute; import jsprit.core.problem.solution.route.VehicleRoute;
import jsprit.core.problem.solution.route.activity.TourActivity; import jsprit.core.problem.solution.route.activity.TourActivity;
@ -12,6 +16,7 @@ import jsprit.core.util.Solutions;
import org.junit.Test; import org.junit.Test;
import java.util.Collection; import java.util.Collection;
import java.util.List;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
@ -25,10 +30,48 @@ public class InitialRoutesTest {
new VrpXMLReader(vrpBuilder).read("src/test/resources/simpleProblem_iniRoutes.xml"); new VrpXMLReader(vrpBuilder).read("src/test/resources/simpleProblem_iniRoutes.xml");
VehicleRoutingProblem vrp = vrpBuilder.build(); VehicleRoutingProblem vrp = vrpBuilder.build();
assertEquals(1,vrp.getJobs().size()); assertEquals(1,getNuServices(vrp));
assertTrue(vrp.getJobs().containsKey("2")); assertTrue(vrp.getJobs().containsKey("2"));
} }
@Test
public void whenReadingProblem2_jobMapShouldContain_service2(){
VehicleRoutingProblem.Builder vrpBuilder = VehicleRoutingProblem.Builder.newInstance();
new VrpXMLReader(vrpBuilder).read("src/test/resources/simpleProblem_inclShipments_iniRoutes.xml");
VehicleRoutingProblem vrp = vrpBuilder.build();
assertEquals(1,getNuServices(vrp));
assertTrue(vrp.getJobs().containsKey("2"));
}
@Test
public void whenReading_jobMapShouldContain_shipment4(){
VehicleRoutingProblem.Builder vrpBuilder = VehicleRoutingProblem.Builder.newInstance();
new VrpXMLReader(vrpBuilder).read("src/test/resources/simpleProblem_inclShipments_iniRoutes.xml");
VehicleRoutingProblem vrp = vrpBuilder.build();
assertEquals(1,getNuShipments(vrp));
assertTrue(vrp.getJobs().containsKey("4"));
}
private int getNuShipments(VehicleRoutingProblem vrp) {
int nuShipments = 0;
for(Job job : vrp.getJobs().values()){
if(job instanceof Shipment) nuShipments++;
}
return nuShipments;
}
private int getNuServices(VehicleRoutingProblem vrp) {
int nuServices = 0;
for(Job job : vrp.getJobs().values()){
if(job instanceof Service) nuServices++;
}
return nuServices;
}
@Test @Test
public void whenReading_thereShouldBeOnlyOneActAssociatedToJob2(){ public void whenReading_thereShouldBeOnlyOneActAssociatedToJob2(){
@ -36,11 +79,34 @@ public class InitialRoutesTest {
new VrpXMLReader(vrpBuilder).read("src/test/resources/simpleProblem_iniRoutes.xml"); new VrpXMLReader(vrpBuilder).read("src/test/resources/simpleProblem_iniRoutes.xml");
VehicleRoutingProblem vrp = vrpBuilder.build(); VehicleRoutingProblem vrp = vrpBuilder.build();
assertEquals(1,vrp.getActivities(vrp.getJobs().get("2")).size()); assertEquals(1, vrp.getActivities(vrp.getJobs().get("2")).size());
} }
@Test @Test
public void whenSolving_nuJobsShouldBe2(){ public void whenReading_thereShouldBeOnlyOneActAssociatedToJob2_v2(){
VehicleRoutingProblem.Builder vrpBuilder = VehicleRoutingProblem.Builder.newInstance();
new VrpXMLReader(vrpBuilder).read("src/test/resources/simpleProblem_inclShipments_iniRoutes.xml");
VehicleRoutingProblem vrp = vrpBuilder.build();
assertEquals(1, vrp.getActivities(vrp.getJobs().get("2")).size());
}
@Test
public void whenReading_thereShouldBeTwoActsAssociatedToShipment4(){
VehicleRoutingProblem.Builder vrpBuilder = VehicleRoutingProblem.Builder.newInstance();
new VrpXMLReader(vrpBuilder).read("src/test/resources/simpleProblem_inclShipments_iniRoutes.xml");
VehicleRoutingProblem vrp = vrpBuilder.build();
Job job = vrp.getJobs().get("4");
List<AbstractActivity> activities = vrp.getActivities(job);
assertEquals(2, activities.size());
}
@Test
public void whenSolving_nuJobsInSolutionShouldBe2(){
VehicleRoutingProblem.Builder vrpBuilder = VehicleRoutingProblem.Builder.newInstance(); VehicleRoutingProblem.Builder vrpBuilder = VehicleRoutingProblem.Builder.newInstance();
new VrpXMLReader(vrpBuilder).read("src/test/resources/simpleProblem_iniRoutes.xml"); new VrpXMLReader(vrpBuilder).read("src/test/resources/simpleProblem_iniRoutes.xml");
@ -55,6 +121,26 @@ public class InitialRoutesTest {
assertEquals(2,solution.getRoutes().iterator().next().getTourActivities().getJobs().size()); assertEquals(2,solution.getRoutes().iterator().next().getTourActivities().getJobs().size());
} }
@Test
public void whenSolvingProblem2_nuJobsInSolutionShouldBe4(){
VehicleRoutingProblem.Builder vrpBuilder = VehicleRoutingProblem.Builder.newInstance();
new VrpXMLReader(vrpBuilder).read("src/test/resources/simpleProblem_inclShipments_iniRoutes.xml");
VehicleRoutingProblem vrp = vrpBuilder.build();
VehicleRoutingAlgorithm vra = new SchrimpfFactory().createAlgorithm(vrp);
Collection<VehicleRoutingProblemSolution> solutions = vra.searchSolutions();
VehicleRoutingProblemSolution solution = Solutions.bestOf(solutions);
SolutionPrinter.print(vrp,solution, SolutionPrinter.Print.VERBOSE);
int jobsInSolution = 0;
for(VehicleRoute r : solution.getRoutes()){
jobsInSolution += r.getTourActivities().jobSize();
}
assertEquals(4,jobsInSolution);
}
@Test @Test
public void whenSolving_nuActsShouldBe2(){ public void whenSolving_nuActsShouldBe2(){
@ -71,6 +157,24 @@ public class InitialRoutesTest {
assertEquals(2, solution.getRoutes().iterator().next().getActivities().size()); assertEquals(2, solution.getRoutes().iterator().next().getActivities().size());
} }
@Test
public void whenSolvingProblem2_nuActsShouldBe6(){
VehicleRoutingProblem.Builder vrpBuilder = VehicleRoutingProblem.Builder.newInstance();
new VrpXMLReader(vrpBuilder).read("src/test/resources/simpleProblem_inclShipments_iniRoutes.xml");
VehicleRoutingProblem vrp = vrpBuilder.build();
VehicleRoutingAlgorithm vra = new SchrimpfFactory().createAlgorithm(vrp);
Collection<VehicleRoutingProblemSolution> solutions = vra.searchSolutions();
VehicleRoutingProblemSolution solution = Solutions.bestOf(solutions);
int nuActs = 0;
for(VehicleRoute r : solution.getRoutes()){
nuActs += r.getActivities().size();
}
assertEquals(6, nuActs);
}
@Test @Test
public void whenSolving_deliverService1_shouldBeInRoute(){ public void whenSolving_deliverService1_shouldBeInRoute(){
@ -85,6 +189,35 @@ public class InitialRoutesTest {
assertTrue(hasActivityIn(solution.getRoutes().iterator().next(),"1")); assertTrue(hasActivityIn(solution.getRoutes().iterator().next(),"1"));
} }
@Test
public void whenSolvingProblem2_deliverServices_and_allShipmentActs_shouldBeInRoute(){
VehicleRoutingProblem.Builder vrpBuilder = VehicleRoutingProblem.Builder.newInstance();
new VrpXMLReader(vrpBuilder).read("src/test/resources/simpleProblem_inclShipments_iniRoutes.xml");
VehicleRoutingProblem vrp = vrpBuilder.build();
VehicleRoutingAlgorithm vra = new SchrimpfFactory().createAlgorithm(vrp);
Collection<VehicleRoutingProblemSolution> solutions = vra.searchSolutions();
VehicleRoutingProblemSolution solution = Solutions.bestOf(solutions);
assertTrue(hasActivityIn(solution.getRoutes(),"1"));
assertTrue(hasActivityIn(solution.getRoutes(),"2"));
assertTrue(hasActivityIn(solution.getRoutes(),"3"));
assertTrue(hasActivityIn(solution.getRoutes(),"4"));
}
private boolean hasActivityIn(Collection<VehicleRoute> routes, String jobId) {
boolean isInRoute = false;
for(VehicleRoute route : routes) {
for (TourActivity act : route.getActivities()) {
if (act instanceof TourActivity.JobActivity) {
if (((TourActivity.JobActivity) act).getJob().getId().equals(jobId)) isInRoute = true;
}
}
}
return isInRoute;
}
private boolean hasActivityIn(VehicleRoute route, String jobId){ private boolean hasActivityIn(VehicleRoute route, String jobId){
boolean isInRoute = false; boolean isInRoute = false;
for(TourActivity act : route.getActivities()){ for(TourActivity act : route.getActivities()){
@ -106,6 +239,6 @@ public class InitialRoutesTest {
Collection<VehicleRoutingProblemSolution> solutions = vra.searchSolutions(); Collection<VehicleRoutingProblemSolution> solutions = vra.searchSolutions();
VehicleRoutingProblemSolution solution = Solutions.bestOf(solutions); VehicleRoutingProblemSolution solution = Solutions.bestOf(solutions);
assertTrue(hasActivityIn(solution.getRoutes().iterator().next(),"2")); assertTrue(hasActivityIn(solution.getRoutes().iterator().next(), "2"));
} }
} }

View file

@ -0,0 +1,131 @@
<?xml version="1.0" encoding="UTF-8"?>
<problem xmlns="http://www.w3schools.com"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.w3schools.com vrp_xml_schema.xsd">
<problemType>
<fleetSize>FINITE</fleetSize>
<fleetComposition>HOMOGENEOUS</fleetComposition>
</problemType>
<vehicles>
<vehicle>
<id>veh1</id>
<typeId>type1</typeId>
<startLocation>
<id>[x=0.0][y=0.0]</id>
<coord x="0.0" y="0.0"/>
</startLocation>
<endLocation>
<id>[x=0.0][y=0.0]</id>
<coord x="0.0" y="0.0"/>
</endLocation>
<timeSchedule>
<start>0.0</start>
<end>46800.0</end>
</timeSchedule>
<returnToDepot>true</returnToDepot>
</vehicle>
<vehicle>
<id>veh2</id>
<typeId>type1</typeId>
<startLocation>
<id>[x=0.0][y=0.0]</id>
<coord x="0.0" y="0.0"/>
</startLocation>
<endLocation>
<id>[x=0.0][y=0.0]</id>
<coord x="0.0" y="0.0"/>
</endLocation>
<timeSchedule>
<start>0.0</start>
<end>64800.0</end>
</timeSchedule>
<returnToDepot>true</returnToDepot>
</vehicle>
</vehicles>
<vehicleTypes>
<type>
<id>type1</id>
<capacity-dimensions>
<dimension index="0">0</dimension>
</capacity-dimensions>
<costs>
<fixed>0.0</fixed>
<distance>1.0</distance>
<time>0.0</time>
</costs>
</type>
</vehicleTypes>
<services>
<service id="2" type="service">
<locationId>loc_s2</locationId>
<coord x="10.0" y="0.0"/>
<capacity-dimensions>
<dimension index="0">0</dimension>
</capacity-dimensions>
<duration>0.0</duration>
</service>
<service id="1" type="service">
<locationId>loc_s1</locationId>
<coord x="20.0" y="0.0"/>
<capacity-dimensions>
<dimension index="0">0</dimension>
</capacity-dimensions>
<duration>0.0</duration>
</service>
</services>
<shipments>
<shipment id="3">
<pickup>
<locationId>loc_pickup_shipment_3</locationId>
<coord x="0." y="10.0"/>
</pickup>
<delivery>
<locationId>loc_deliver_shipment_3</locationId>
<coord x="0." y="20.0"/>
</delivery>
<capacity-dimensions>
<dimension index="0">0</dimension>
</capacity-dimensions>
</shipment>
<shipment id="4">
<pickup>
<locationId>loc_pickup_shipment_4</locationId>
<coord x="0." y="12.0"/>
</pickup>
<delivery>
<locationId>loc_deliver_shipment_4</locationId>
<coord x="0." y="18.0"/>
</delivery>
<capacity-dimensions>
<dimension index="0">0</dimension>
</capacity-dimensions>
</shipment>
</shipments>
<initialRoutes>
<route>
<driverId>noDriver</driverId>
<vehicleId>veh1</vehicleId>
<start>0.</start>
<act type="deliverService">
<serviceId>1</serviceId>
</act>
<end/>
</route>
<route>
<driverId>noDriver</driverId>
<vehicleId>veh2</vehicleId>
<start>0.</start>
<act type="pickupShipment">
<shipmentId>3</shipmentId>
</act>
<act type="deliverShipment">
<shipmentId>3</shipmentId>
</act>
<end/>
</route>
</initialRoutes>
</problem>