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

Merge branch 'bad-job-list'

This commit is contained in:
oblonski 2014-08-26 20:35:14 +02:00
commit 264dafd06a
31 changed files with 528 additions and 460 deletions

View file

@ -0,0 +1,57 @@
package jsprit.core.algorithm;
import jsprit.core.algorithm.box.GreedySchrimpfFactory;
import jsprit.core.problem.VehicleRoutingProblem;
import jsprit.core.problem.job.Service;
import jsprit.core.problem.solution.VehicleRoutingProblemSolution;
import jsprit.core.problem.solution.route.activity.TimeWindow;
import jsprit.core.problem.vehicle.VehicleImpl;
import jsprit.core.util.Coordinate;
import jsprit.core.util.Solutions;
import org.junit.Test;
import java.util.Collection;
import static org.junit.Assert.assertTrue;
public class UnassignedJobListTest {
@Test
public void job2ShouldBeInBadJobList_dueToTimeWindow(){
VehicleRoutingProblem.Builder builder = VehicleRoutingProblem.Builder.newInstance();
builder.addVehicle(VehicleImpl.Builder.newInstance("v1").setEarliestStart(0).setLatestArrival(12).setStartLocationCoordinate(Coordinate.newInstance(1, 1)).build());
Service job1 = Service.Builder.newInstance("job1").setCoord(Coordinate.newInstance(0, 0)).setTimeWindow(TimeWindow.newInstance(0, 12)).setServiceTime(1).build();
builder.addJob(job1);
Service job2 = Service.Builder.newInstance("job2").setCoord(Coordinate.newInstance(2, 2)).setTimeWindow(TimeWindow.newInstance(12, 24)).setServiceTime(1).build();
builder.addJob(job2);
VehicleRoutingProblem vrp = builder.build();
VehicleRoutingAlgorithm algorithm = new GreedySchrimpfFactory().createAlgorithm(vrp);
algorithm.setMaxIterations(10);
Collection<VehicleRoutingProblemSolution> solutions = algorithm.searchSolutions();
VehicleRoutingProblemSolution solution = Solutions.bestOf(solutions);
assertTrue(!solution.getUnassignedJobs().contains(job1));
assertTrue(solution.getUnassignedJobs().contains(job2));
}
@Test
public void job2ShouldBeInBadJobList_dueToSize(){
VehicleRoutingProblem.Builder builder = VehicleRoutingProblem.Builder.newInstance();
builder.addVehicle(VehicleImpl.Builder.newInstance("v1").setEarliestStart(0).setLatestArrival(12).setStartLocationCoordinate(Coordinate.newInstance(1, 1)).build());
Service job1 = Service.Builder.newInstance("job1").setCoord(Coordinate.newInstance(0, 0)).setTimeWindow(TimeWindow.newInstance(0, 12)).setServiceTime(1).build();
builder.addJob(job1);
Service job2 = Service.Builder.newInstance("job2").setCoord(Coordinate.newInstance(2, 2)).addSizeDimension(0,10).setTimeWindow(TimeWindow.newInstance(0, 12)).setServiceTime(1).build();
builder.addJob(job2);
VehicleRoutingProblem vrp = builder.build();
VehicleRoutingAlgorithm algorithm = new GreedySchrimpfFactory().createAlgorithm(vrp);
algorithm.setMaxIterations(10);
Collection<VehicleRoutingProblemSolution> solutions = algorithm.searchSolutions();
VehicleRoutingProblemSolution solution = Solutions.bestOf(solutions);
assertTrue(!solution.getUnassignedJobs().contains(job1));
assertTrue(solution.getUnassignedJobs().contains(job2));
}
}

View file

@ -16,12 +16,6 @@
******************************************************************************/
package jsprit.core.algorithm.io;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import java.util.ArrayList;
import java.util.Collection;
import jsprit.core.algorithm.SearchStrategy;
import jsprit.core.algorithm.SearchStrategyModule;
import jsprit.core.algorithm.VehicleRoutingAlgorithm;
@ -42,11 +36,16 @@ import jsprit.core.problem.io.VrpXMLReader;
import jsprit.core.problem.job.Job;
import jsprit.core.problem.solution.VehicleRoutingProblemSolution;
import jsprit.core.problem.solution.route.VehicleRoute;
import org.apache.commons.configuration.ConfigurationException;
import org.junit.Before;
import org.junit.Test;
import java.util.ArrayList;
import java.util.Collection;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
public class TestAlgorithmReader {
@ -242,5 +241,19 @@ public class TestAlgorithmReader {
}
assertEquals(3, nOfModules);
}
@Test
public void readerTest_whenReadingAlgoWithSchemaValidation_itReadsCorrectly(){
AlgorithmConfig algoConfig = new AlgorithmConfig();
new AlgorithmConfigXmlReader(algoConfig).read("src/test/resources/algorithmConfig.xml");
}
@Test
public void readerTest_whenReadingAlgoWithSchemaValidationWithoutIterations_itReadsCorrectly(){
AlgorithmConfig algoConfig = new AlgorithmConfig();
new AlgorithmConfigXmlReader(algoConfig).read("src/test/resources/algorithmConfig_withoutIterations.xml");
}
}

View file

@ -1,40 +0,0 @@
/*******************************************************************************
* Copyright (C) 2013 Stefan Schroeder
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3.0 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
******************************************************************************/
package jsprit.core.problem.io;
import jsprit.core.algorithm.io.AlgorithmConfig;
import jsprit.core.algorithm.io.AlgorithmConfigXmlReader;
import org.junit.Test;
public class AlgorithmReaderTest {
@Test
public void readerTest_whenReadingAlgoWithSchemaValidation_itReadsCorrectly(){
AlgorithmConfig algoConfig = new AlgorithmConfig();
new AlgorithmConfigXmlReader(algoConfig).read("src/test/resources/algorithmConfig.xml");
}
@Test
public void readerTest_whenReadingAlgoWithSchemaValidationWithoutIterations_itReadsCorrectly(){
AlgorithmConfig algoConfig = new AlgorithmConfig();
new AlgorithmConfigXmlReader(algoConfig).read("src/test/resources/algorithmConfig_withoutIterations.xml");
}
}

View file

@ -1,63 +0,0 @@
/*******************************************************************************
* Copyright (C) 2013 Stefan Schroeder
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3.0 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
******************************************************************************/
package jsprit.core.problem.io;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import java.util.ArrayList;
import java.util.List;
import jsprit.core.problem.VehicleRoutingProblem;
import jsprit.core.problem.VehicleRoutingProblem.Builder;
import jsprit.core.problem.io.VrpXMLReader;
import jsprit.core.problem.solution.VehicleRoutingProblemSolution;
import jsprit.core.problem.solution.route.activity.DeliverShipment;
import jsprit.core.problem.solution.route.activity.PickupService;
import jsprit.core.problem.solution.route.activity.PickupShipment;
import jsprit.core.problem.solution.route.activity.TourActivity;
import org.junit.Test;
public class ReaderTest {
@Test
public void testRead_ifReaderIsCalled_itReadsSuccessfully(){
new VrpXMLReader(VehicleRoutingProblem.Builder.newInstance(), new ArrayList<VehicleRoutingProblemSolution>()).read("src/test/resources/lui-shen-solution.xml");
}
@Test
public void testRead_ifReaderIsCalled_itReadsSuccessfullyV2(){
Builder vrpBuilder = VehicleRoutingProblem.Builder.newInstance();
ArrayList<VehicleRoutingProblemSolution> solutions = new ArrayList<VehicleRoutingProblemSolution>();
new VrpXMLReader(vrpBuilder, solutions).read("src/test/resources/finiteVrpWithShipmentsAndSolution.xml");
VehicleRoutingProblem vrp = vrpBuilder.build();
assertEquals(3,vrp.getJobs().size());
assertEquals(1,solutions.size());
assertEquals(1,solutions.get(0).getRoutes().size());
List<TourActivity> activities = solutions.get(0).getRoutes().iterator().next().getTourActivities().getActivities();
assertEquals(4,activities.size());
assertTrue(activities.get(0) instanceof PickupService);
assertTrue(activities.get(1) instanceof PickupService);
assertTrue(activities.get(2) instanceof PickupShipment);
assertTrue(activities.get(3) instanceof DeliverShipment);
}
}

View file

@ -1,16 +1,16 @@
/*******************************************************************************
* Copyright (C) 2013 Stefan Schroeder
*
* Copyright (C) 2014 Stefan Schroeder
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* License as published by the Free Software Foundation; either
* version 3.0 of the License, or (at your option) any later version.
*
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
******************************************************************************/
@ -21,7 +21,13 @@ import jsprit.core.problem.VehicleRoutingProblem.FleetSize;
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.activity.DeliverShipment;
import jsprit.core.problem.solution.route.activity.PickupService;
import jsprit.core.problem.solution.route.activity.PickupShipment;
import jsprit.core.problem.solution.route.activity.TourActivity;
import jsprit.core.problem.vehicle.Vehicle;
import jsprit.core.util.Solutions;
import org.junit.Before;
import org.junit.Test;
@ -570,5 +576,47 @@ public class VrpXMLReaderTest {
assertEquals(2,vrp.getInitialVehicleRoutes().iterator().next().getActivities().size());
}
@Test
public void testRead_ifReaderIsCalled_itReadsSuccessfullyV2(){
VehicleRoutingProblem.Builder vrpBuilder = VehicleRoutingProblem.Builder.newInstance();
ArrayList<VehicleRoutingProblemSolution> solutions = new ArrayList<VehicleRoutingProblemSolution>();
new VrpXMLReader(vrpBuilder, solutions).read("src/test/resources/finiteVrpWithShipmentsAndSolution.xml");
VehicleRoutingProblem vrp = vrpBuilder.build();
assertEquals(4,vrp.getJobs().size());
assertEquals(1,solutions.size());
assertEquals(1,solutions.get(0).getRoutes().size());
List<TourActivity> activities = solutions.get(0).getRoutes().iterator().next().getTourActivities().getActivities();
assertEquals(4,activities.size());
assertTrue(activities.get(0) instanceof PickupService);
assertTrue(activities.get(1) instanceof PickupService);
assertTrue(activities.get(2) instanceof PickupShipment);
assertTrue(activities.get(3) instanceof DeliverShipment);
}
@Test
public void testRead_ifReaderIsCalled_itReadsSuccessfully(){
new VrpXMLReader(VehicleRoutingProblem.Builder.newInstance(), new ArrayList<VehicleRoutingProblemSolution>()).read("src/test/resources/lui-shen-solution.xml");
assertTrue(true);
}
@Test
public void unassignedJobShouldBeRead(){
VehicleRoutingProblem.Builder vrpBuilder = VehicleRoutingProblem.Builder.newInstance();
ArrayList<VehicleRoutingProblemSolution> solutions = new ArrayList<VehicleRoutingProblemSolution>();
new VrpXMLReader(vrpBuilder, solutions).read("src/test/resources/finiteVrpWithShipmentsAndSolution.xml");
VehicleRoutingProblemSolution solution = Solutions.bestOf(solutions);
assertEquals(1,solution.getUnassignedJobs().size());
assertEquals("4",solution.getUnassignedJobs().iterator().next().getId());
}
// @Test
// public void solutionListShouldBeEmpty(){
// VehicleRoutingProblem.Builder vrpBuilder = VehicleRoutingProblem.Builder.newInstance();
// ArrayList<VehicleRoutingProblemSolution> solutions = new ArrayList<VehicleRoutingProblemSolution>();
// new VrpXMLReader(vrpBuilder, solutions).read("src/test/resources/finiteVrpforReaderTest.xml");
// assertTrue(solutions.isEmpty());
// }
}

View file

@ -21,15 +21,20 @@ import jsprit.core.problem.VehicleRoutingProblem.Builder;
import jsprit.core.problem.VehicleRoutingProblem.FleetSize;
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.TimeWindow;
import jsprit.core.problem.vehicle.Vehicle;
import jsprit.core.problem.vehicle.VehicleImpl;
import jsprit.core.problem.vehicle.VehicleTypeImpl;
import jsprit.core.util.Coordinate;
import jsprit.core.util.Solutions;
import org.junit.Before;
import org.junit.Test;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import static org.junit.Assert.*;
@ -865,4 +870,68 @@ public class VrpXMLWriterTest {
assertEquals(2,newVrp.getInitialVehicleRoutes().iterator().next().getActivities().size());
}
@Test
public void solutionWithoutUnassignedJobsShouldBeWrittenCorrectly(){
Builder builder = VehicleRoutingProblem.Builder.newInstance();
VehicleTypeImpl type1 = VehicleTypeImpl.Builder.newInstance("vehType").addCapacityDimension(0, 20).build();
VehicleImpl v1 = VehicleImpl.Builder.newInstance("v1").setStartLocationId("loc").setType(type1).build();
builder.addVehicle(v1);
Service s1 = Service.Builder.newInstance("1").addSizeDimension(0, 1).setLocationId("loc").setServiceTime(2.0).build();
Service s2 = Service.Builder.newInstance("2").addSizeDimension(0, 1).setLocationId("loc2").setServiceTime(4.0).build();
VehicleRoutingProblem vrp = builder.addJob(s1).addJob(s2).build();
VehicleRoute route = VehicleRoute.Builder.newInstance(v1).addService(s1).addService(s2).build();
List<VehicleRoute> routes = new ArrayList<VehicleRoute>();
routes.add(route);
VehicleRoutingProblemSolution solution = new VehicleRoutingProblemSolution(routes,10.);
List<VehicleRoutingProblemSolution> solutions = new ArrayList<VehicleRoutingProblemSolution>();
solutions.add(solution);
new VrpXMLWriter(vrp, solutions).write(infileName);
VehicleRoutingProblem.Builder vrpToReadBuilder = VehicleRoutingProblem.Builder.newInstance();
List<VehicleRoutingProblemSolution> solutionsToRead = new ArrayList<VehicleRoutingProblemSolution>();
new VrpXMLReader(vrpToReadBuilder, solutionsToRead).read(infileName);
assertEquals(1, solutionsToRead.size());
assertEquals(10., Solutions.bestOf(solutionsToRead).getCost(),0.01);
assertTrue(Solutions.bestOf(solutionsToRead).getUnassignedJobs().isEmpty());
}
@Test
public void solutionWithUnassignedJobsShouldBeWrittenCorrectly(){
Builder builder = VehicleRoutingProblem.Builder.newInstance();
VehicleTypeImpl type1 = VehicleTypeImpl.Builder.newInstance("vehType").addCapacityDimension(0, 20).build();
VehicleImpl v1 = VehicleImpl.Builder.newInstance("v1").setStartLocationId("loc").setType(type1).build();
builder.addVehicle(v1);
Service s1 = Service.Builder.newInstance("1").addSizeDimension(0, 1).setLocationId("loc").setServiceTime(2.0).build();
Service s2 = Service.Builder.newInstance("2").addSizeDimension(0, 1).setLocationId("loc2").setServiceTime(4.0).build();
VehicleRoutingProblem vrp = builder.addJob(s1).addJob(s2).build();
VehicleRoute route = VehicleRoute.Builder.newInstance(v1).addService(s1).build();
List<VehicleRoute> routes = new ArrayList<VehicleRoute>();
routes.add(route);
VehicleRoutingProblemSolution solution = new VehicleRoutingProblemSolution(routes,10.);
solution.getUnassignedJobs().add(s2);
List<VehicleRoutingProblemSolution> solutions = new ArrayList<VehicleRoutingProblemSolution>();
solutions.add(solution);
new VrpXMLWriter(vrp, solutions).write(infileName);
VehicleRoutingProblem.Builder vrpToReadBuilder = VehicleRoutingProblem.Builder.newInstance();
List<VehicleRoutingProblemSolution> solutionsToRead = new ArrayList<VehicleRoutingProblemSolution>();
new VrpXMLReader(vrpToReadBuilder, solutionsToRead).read(infileName);
assertEquals(1, solutionsToRead.size());
assertEquals(10., Solutions.bestOf(solutionsToRead).getCost(),0.01);
assertEquals(1, Solutions.bestOf(solutionsToRead).getUnassignedJobs().size());
assertEquals("2", Solutions.bestOf(solutionsToRead).getUnassignedJobs().iterator().next().getId());
}
}

View file

@ -18,14 +18,17 @@
******************************************************************************/
package jsprit.core.problem.solution;
import jsprit.core.problem.job.Job;
import jsprit.core.problem.solution.route.VehicleRoute;
import org.junit.Test;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import jsprit.core.problem.solution.route.VehicleRoute;
import static org.junit.Assert.*;
import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.mock;
import org.junit.Test;
public class VehicleRoutingProblemSolutionTest {
@ -51,4 +54,42 @@ public class VehicleRoutingProblemSolutionTest {
assertEquals(20.0,sol.getCost(),0.01);
}
@Test
public void sizeOfBadJobsShouldBeCorrect(){
Job badJob = mock(Job.class);
List<Job> badJobs = new ArrayList<Job>();
badJobs.add(badJob);
VehicleRoutingProblemSolution sol = new VehicleRoutingProblemSolution(Collections.<VehicleRoute>emptyList(), badJobs, 10.0);
assertEquals(1,sol.getUnassignedJobs().size());
}
@Test
public void sizeOfBadJobsShouldBeCorrect_2(){
Job badJob = mock(Job.class);
List<Job> badJobs = new ArrayList<Job>();
badJobs.add(badJob);
VehicleRoutingProblemSolution sol = new VehicleRoutingProblemSolution(Collections.<VehicleRoute>emptyList(), 10.0);
sol.getUnassignedJobs().addAll(badJobs);
assertEquals(1, sol.getUnassignedJobs().size());
}
@Test
public void badJobsShouldBeCorrect(){
Job badJob = mock(Job.class);
List<Job> badJobs = new ArrayList<Job>();
badJobs.add(badJob);
VehicleRoutingProblemSolution sol = new VehicleRoutingProblemSolution(Collections.<VehicleRoute>emptyList(), badJobs, 10.0);
assertEquals(badJob,sol.getUnassignedJobs().iterator().next());
}
@Test
public void badJobsShouldBeCorrect_2() {
Job badJob = mock(Job.class);
List<Job> badJobs = new ArrayList<Job>();
badJobs.add(badJob);
VehicleRoutingProblemSolution sol = new VehicleRoutingProblemSolution(Collections.<VehicleRoute>emptyList(), 10.0);
sol.getUnassignedJobs().addAll(badJobs);
assertEquals(badJob, sol.getUnassignedJobs().iterator().next());
}
}

View file

@ -81,6 +81,19 @@
</timeWindow>
</timeWindows>
</service>
<service id="4" type="service">
<locationId>i(3,9)</locationId>
<coord x="10.0" y="10.0"/>
<capacity-demand>1</capacity-demand>
<duration>0.0</duration>
<timeWindows>
<timeWindow>
<start>0.0</start>
<end>4000.0</end>
</timeWindow>
</timeWindows>
</service>
</services>
<shipments>
@ -143,6 +156,9 @@
<end>100.0</end>
</route>
</routes>
<unassignedJobs>
<job id="4"/>
</unassignedJobs>
</solution>
</solutions>

View file

@ -6,15 +6,13 @@
</problemType>
<vehicles>
<vehicle>
<id>v</id>
<typeId>type</typeId>
<id>v1</id>
<typeId>vehType</typeId>
<startLocation>
<id>startLoc</id>
<coord x="1.0" y="2.0"/>
<id>loc</id>
</startLocation>
<endLocation>
<id>endLoc</id>
<coord x="4.0" y="5.0"/>
<id>loc</id>
</endLocation>
<timeSchedule>
<start>0.0</start>
@ -25,19 +23,9 @@
</vehicles>
<vehicleTypes>
<type>
<id>type</id>
<id>vehType</id>
<capacity-dimensions>
<dimension index="0">100</dimension>
<dimension index="1">1000</dimension>
<dimension index="2">0</dimension>
<dimension index="3">0</dimension>
<dimension index="4">0</dimension>
<dimension index="5">0</dimension>
<dimension index="6">0</dimension>
<dimension index="7">0</dimension>
<dimension index="8">0</dimension>
<dimension index="9">0</dimension>
<dimension index="10">10000</dimension>
<dimension index="0">20</dimension>
</capacity-dimensions>
<costs>
<fixed>0.0</fixed>
@ -46,4 +34,53 @@
</costs>
</type>
</vehicleTypes>
<services>
<service id="2" type="service">
<locationId>loc2</locationId>
<capacity-dimensions>
<dimension index="0">1</dimension>
</capacity-dimensions>
<duration>4.0</duration>
<timeWindows>
<timeWindow>
<start>0.0</start>
<end>1.7976931348623157E308</end>
</timeWindow>
</timeWindows>
</service>
<service id="1" type="service">
<locationId>loc</locationId>
<capacity-dimensions>
<dimension index="0">1</dimension>
</capacity-dimensions>
<duration>2.0</duration>
<timeWindows>
<timeWindow>
<start>0.0</start>
<end>1.7976931348623157E308</end>
</timeWindow>
</timeWindows>
</service>
</services>
<solutions>
<solution>
<cost>10.0</cost>
<routes>
<route>
<driverId>noDriver</driverId>
<vehicleId>v1</vehicleId>
<start>0.0</start>
<act type="service">
<serviceId>1</serviceId>
<arrTime>0.0</arrTime>
<endTime>0.0</endTime>
</act>
<end>0.0</end>
</route>
</routes>
<unassignedJobs>
<job id="2"/>
</unassignedJobs>
</solution>
</solutions>
</problem>