Skip to content

Commit

Permalink
reproduced bug #112
Browse files Browse the repository at this point in the history
  • Loading branch information
oblonski committed Jul 30, 2014
1 parent 63cd55e commit c325073
Show file tree
Hide file tree
Showing 3 changed files with 221 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -288,25 +288,43 @@ else if(job instanceof Shipment){
}


@SuppressWarnings("deprecation")

public Builder addInitialVehicleRoute(VehicleRoute route){
addVehicle(route.getVehicle());
for(Job job : route.getTourActivities().getJobs()){
jobsInInitialRoutes.add(job.getId());
if(job instanceof Service) {
tentative_coordinates.put(((Service)job).getLocationId(), ((Service)job).getCoord());
}
if(job instanceof Shipment){
Shipment shipment = (Shipment)job;
tentative_coordinates.put(shipment.getPickupLocation(), shipment.getPickupCoord());
tentative_coordinates.put(shipment.getDeliveryLocation(), shipment.getDeliveryCoord());
}
}
addVehicle((AbstractVehicle)route.getVehicle());
for(TourActivity act : route.getActivities()){
AbstractActivity abstractAct = (AbstractActivity) act;
abstractAct.setIndex(activityIndexCounter);
incActivityIndexCounter();
if(act instanceof TourActivity.JobActivity) {
Job job = ((TourActivity.JobActivity) act).getJob();
jobsInInitialRoutes.add(job.getId());
registerLocation(job);
registerJobAndActivity(abstractAct, job);
}
}
initialRoutes.add(route);
return this;
}

public Builder addInitialVehicleRoutes(Collection<VehicleRoute> routes){

private void registerLocation(Job job) {
if (job instanceof Service) tentative_coordinates.put(((Service) job).getLocationId(), ((Service) job).getCoord());
if (job instanceof Shipment) {
Shipment shipment = (Shipment) job;
tentative_coordinates.put(shipment.getPickupLocation(), shipment.getPickupCoord());
tentative_coordinates.put(shipment.getDeliveryLocation(), shipment.getDeliveryCoord());
}
}

private void registerJobAndActivity(AbstractActivity abstractAct, Job job) {
if(activityMap.containsKey(job)) activityMap.get(job).add(abstractAct);
else{
List<AbstractActivity> actList = new ArrayList<AbstractActivity>();
actList.add(abstractAct);
activityMap.put(job,actList);
}
}

public Builder addInitialVehicleRoutes(Collection<VehicleRoute> routes){
for(VehicleRoute r : routes){
addInitialVehicleRoute(r);
}
Expand Down
101 changes: 101 additions & 0 deletions jsprit-core/src/test/java/jsprit/core/algorithm/InitialRoutesTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
package jsprit.core.algorithm;


import jsprit.core.algorithm.box.SchrimpfFactory;
import jsprit.core.problem.VehicleRoutingProblem;
import jsprit.core.problem.io.VrpXMLReader;
import jsprit.core.problem.solution.VehicleRoutingProblemSolution;
import jsprit.core.problem.solution.route.VehicleRoute;
import jsprit.core.problem.solution.route.activity.TourActivity;
import jsprit.core.reporting.SolutionPrinter;
import jsprit.core.util.Solutions;
import org.junit.Test;

import java.util.Collection;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

public class InitialRoutesTest {

@Test
public void whenReading_thereShouldBeNoDuplicates(){

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

assertEquals(1,vrp.getJobs().size());

}

@Test
public void whenSolving_nuJobsShouldBe2(){

VehicleRoutingProblem.Builder vrpBuilder = VehicleRoutingProblem.Builder.newInstance();
new VrpXMLReader(vrpBuilder).read("src/test/resources/simpleProblem_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);

assertEquals(2,solution.getRoutes().iterator().next().getTourActivities().getJobs().size());
}

@Test
public void whenSolving_nuActsShouldBe2(){

VehicleRoutingProblem.Builder vrpBuilder = VehicleRoutingProblem.Builder.newInstance();
new VrpXMLReader(vrpBuilder).read("src/test/resources/simpleProblem_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);

assertEquals(2, solution.getRoutes().iterator().next().getActivities().size());
}

@Test
public void whenSolving_deliverService1_shouldBeInRoute(){

VehicleRoutingProblem.Builder vrpBuilder = VehicleRoutingProblem.Builder.newInstance();
new VrpXMLReader(vrpBuilder).read("src/test/resources/simpleProblem_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().iterator().next(),"1"));
}

private boolean hasActivityIn(VehicleRoute route, String jobId){
boolean isInRoute = false;
for(TourActivity act : route.getActivities()){
if(act instanceof TourActivity.JobActivity){
if(((TourActivity.JobActivity) act).getJob().getId().equals(jobId)) isInRoute = true;
}
}
return isInRoute;
}

@Test
public void whenSolving_deliverService2_shouldBeInRoute(){

VehicleRoutingProblem.Builder vrpBuilder = VehicleRoutingProblem.Builder.newInstance();
new VrpXMLReader(vrpBuilder).read("src/test/resources/simpleProblem_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().iterator().next(),"2"));
}
}
87 changes: 87 additions & 0 deletions jsprit-core/src/test/resources/simpleProblem_iniRoutes.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
<?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>2</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="1000.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_s3</locationId>
<coord x="1000.0" y="1000.0"/>
<capacity-dimensions>
<dimension index="0">0</dimension>
</capacity-dimensions>
<duration>0.0</duration>
</service>
</services>
<initialRoutes>
<route>
<driverId>noDriver</driverId>
<vehicleId>veh1</vehicleId>
<start>0.</start>
<act type="deliverService">
<serviceId>1</serviceId>
</act>
<end/>
</route>

</initialRoutes>
</problem>

0 comments on commit c325073

Please sign in to comment.