mirror of
https://github.com/graphhopper/jsprit.git
synced 2020-01-24 07:45:05 +01:00
reproduced bug #112
This commit is contained in:
parent
63cd55ebb7
commit
c32507329d
3 changed files with 221 additions and 15 deletions
|
|
@ -288,25 +288,43 @@ public class VehicleRoutingProblem {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@SuppressWarnings("deprecation")
|
|
||||||
public Builder addInitialVehicleRoute(VehicleRoute route){
|
public Builder addInitialVehicleRoute(VehicleRoute route){
|
||||||
addVehicle(route.getVehicle());
|
addVehicle((AbstractVehicle)route.getVehicle());
|
||||||
for(Job job : route.getTourActivities().getJobs()){
|
for(TourActivity act : route.getActivities()){
|
||||||
jobsInInitialRoutes.add(job.getId());
|
AbstractActivity abstractAct = (AbstractActivity) act;
|
||||||
if(job instanceof Service) {
|
abstractAct.setIndex(activityIndexCounter);
|
||||||
tentative_coordinates.put(((Service)job).getLocationId(), ((Service)job).getCoord());
|
incActivityIndexCounter();
|
||||||
}
|
if(act instanceof TourActivity.JobActivity) {
|
||||||
if(job instanceof Shipment){
|
Job job = ((TourActivity.JobActivity) act).getJob();
|
||||||
Shipment shipment = (Shipment)job;
|
jobsInInitialRoutes.add(job.getId());
|
||||||
tentative_coordinates.put(shipment.getPickupLocation(), shipment.getPickupCoord());
|
registerLocation(job);
|
||||||
tentative_coordinates.put(shipment.getDeliveryLocation(), shipment.getDeliveryCoord());
|
registerJobAndActivity(abstractAct, job);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
initialRoutes.add(route);
|
initialRoutes.add(route);
|
||||||
return this;
|
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){
|
for(VehicleRoute r : routes){
|
||||||
addInitialVehicleRoute(r);
|
addInitialVehicleRoute(r);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -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
jsprit-core/src/test/resources/simpleProblem_iniRoutes.xml
Normal file
87
jsprit-core/src/test/resources/simpleProblem_iniRoutes.xml
Normal file
|
|
@ -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>
|
||||||
Loading…
Add table
Add a link
Reference in a new issue