Skip to content

Commit

Permalink
fixed bug #112 and #114
Browse files Browse the repository at this point in the history
  • Loading branch information
oblonski committed Jul 30, 2014
1 parent bca9323 commit 58c9030
Show file tree
Hide file tree
Showing 3 changed files with 270 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@ public VehicleTypeDependentJobInsertionCalculator(final VehicleRoutingProblem vr
}

private void getInitialVehicleIds() {
for(VehicleRoute initialRoute : vrp.getInitialVehicleRoutes()){
Collection<VehicleRoute> initialVehicleRoutes = vrp.getInitialVehicleRoutes();
for(VehicleRoute initialRoute : initialVehicleRoutes){
initialVehicleIds.add(initialRoute.getVehicle().getId());
}
}
Expand Down
141 changes: 137 additions & 4 deletions jsprit-core/src/test/java/jsprit/core/algorithm/InitialRoutesTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@


import jsprit.core.algorithm.box.SchrimpfFactory;
import jsprit.core.problem.AbstractActivity;
import jsprit.core.problem.VehicleRoutingProblem;
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.route.VehicleRoute;
import jsprit.core.problem.solution.route.activity.TourActivity;
Expand All @@ -12,6 +16,7 @@
import org.junit.Test;

import java.util.Collection;
import java.util.List;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
Expand All @@ -25,22 +30,83 @@ public void whenReading_jobMapShouldOnlyContainJob2(){
new VrpXMLReader(vrpBuilder).read("src/test/resources/simpleProblem_iniRoutes.xml");
VehicleRoutingProblem vrp = vrpBuilder.build();

assertEquals(1,vrp.getJobs().size());
assertEquals(1,getNuServices(vrp));
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
public void whenReading_thereShouldBeOnlyOneActAssociatedToJob2(){

VehicleRoutingProblem.Builder vrpBuilder = VehicleRoutingProblem.Builder.newInstance();
new VrpXMLReader(vrpBuilder).read("src/test/resources/simpleProblem_iniRoutes.xml");
VehicleRoutingProblem vrp = vrpBuilder.build();

assertEquals(1,vrp.getActivities(vrp.getJobs().get("2")).size());
assertEquals(1, vrp.getActivities(vrp.getJobs().get("2")).size());
}

@Test
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_nuJobsShouldBe2(){
public void whenSolving_nuJobsInSolutionShouldBe2(){

VehicleRoutingProblem.Builder vrpBuilder = VehicleRoutingProblem.Builder.newInstance();
new VrpXMLReader(vrpBuilder).read("src/test/resources/simpleProblem_iniRoutes.xml");
Expand All @@ -55,6 +121,26 @@ public void whenSolving_nuJobsShouldBe2(){
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
public void whenSolving_nuActsShouldBe2(){

Expand All @@ -71,6 +157,24 @@ public void whenSolving_nuActsShouldBe2(){
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
public void whenSolving_deliverService1_shouldBeInRoute(){

Expand All @@ -85,6 +189,35 @@ public void whenSolving_deliverService1_shouldBeInRoute(){
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){
boolean isInRoute = false;
for(TourActivity act : route.getActivities()){
Expand All @@ -106,6 +239,6 @@ public void whenSolving_deliverService2_shouldBeInRoute(){
Collection<VehicleRoutingProblemSolution> solutions = vra.searchSolutions();
VehicleRoutingProblemSolution solution = Solutions.bestOf(solutions);

assertTrue(hasActivityIn(solution.getRoutes().iterator().next(),"2"));
assertTrue(hasActivityIn(solution.getRoutes().iterator().next(), "2"));
}
}
Original file line number Diff line number Diff line change
@@ -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>

0 comments on commit 58c9030

Please sign in to comment.