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

IT tests with Jsprit algorithm

This commit is contained in:
oblonski 2015-03-12 09:59:12 +01:00
parent a7b572d815
commit e8a9363f83
7 changed files with 432 additions and 100 deletions

View file

@ -16,6 +16,7 @@
******************************************************************************/ ******************************************************************************/
package jsprit.core.algorithm; package jsprit.core.algorithm;
import jsprit.core.algorithm.box.Jsprit;
import jsprit.core.algorithm.io.VehicleRoutingAlgorithms; import jsprit.core.algorithm.io.VehicleRoutingAlgorithms;
import jsprit.core.problem.VehicleRoutingProblem; import jsprit.core.problem.VehicleRoutingProblem;
import jsprit.core.problem.io.VrpXMLReader; import jsprit.core.problem.io.VrpXMLReader;
@ -36,8 +37,19 @@ public class CVRPwithDeliveries_IT {
VehicleRoutingProblem vrp = vrpBuilder.build(); VehicleRoutingProblem vrp = vrpBuilder.build();
VehicleRoutingAlgorithm vra = VehicleRoutingAlgorithms.readAndCreateAlgorithm(vrp, "src/test/resources/algorithmConfig.xml"); VehicleRoutingAlgorithm vra = VehicleRoutingAlgorithms.readAndCreateAlgorithm(vrp, "src/test/resources/algorithmConfig.xml");
Collection<VehicleRoutingProblemSolution> solutions = vra.searchSolutions(); Collection<VehicleRoutingProblemSolution> solutions = vra.searchSolutions();
assertEquals(530.0, Solutions.bestOf(solutions).getCost(),50.0); assertEquals(530.0, Solutions.bestOf(solutions).getCost(),50.0);
assertEquals(5, Solutions.bestOf(solutions).getRoutes().size()); assertEquals(5, Solutions.bestOf(solutions).getRoutes().size());
} }
@Test
public void whenSolvingVRPNC1withDeliveriesWithJsprit_solutionsMustNoBeWorseThan5PercentOfBestKnownSolution(){
VehicleRoutingProblem.Builder vrpBuilder = VehicleRoutingProblem.Builder.newInstance();
new VrpXMLReader(vrpBuilder).read("src/test/resources/vrpnc1-jsprit-with-deliveries.xml");
VehicleRoutingProblem vrp = vrpBuilder.build();
VehicleRoutingAlgorithm vra = Jsprit.createAlgorithm(vrp);
Collection<VehicleRoutingProblemSolution> solutions = vra.searchSolutions();
assertEquals(530.0, Solutions.bestOf(solutions).getCost(), 50.0);
assertEquals(5, Solutions.bestOf(solutions).getRoutes().size());
}
} }

View file

@ -16,6 +16,7 @@
******************************************************************************/ ******************************************************************************/
package jsprit.core.algorithm; package jsprit.core.algorithm;
import jsprit.core.algorithm.box.Jsprit;
import jsprit.core.algorithm.io.VehicleRoutingAlgorithms; import jsprit.core.algorithm.io.VehicleRoutingAlgorithms;
import jsprit.core.problem.VehicleRoutingProblem; import jsprit.core.problem.VehicleRoutingProblem;
import jsprit.core.problem.io.VrpXMLReader; import jsprit.core.problem.io.VrpXMLReader;
@ -40,4 +41,16 @@ public class CVRPwithPickups_IT {
assertEquals(5, Solutions.bestOf(solutions).getRoutes().size()); assertEquals(5, Solutions.bestOf(solutions).getRoutes().size());
} }
@Test
public void whenSolvingVRPNC1WithPickupsWithJsprit_solutionsMustNoBeWorseThan5PercentOfBestKnownSolution(){
VehicleRoutingProblem.Builder vrpBuilder = VehicleRoutingProblem.Builder.newInstance();
new VrpXMLReader(vrpBuilder).read("src/test/resources/vrpnc1-jsprit-with-pickups.xml");
VehicleRoutingProblem vrp = vrpBuilder.build();
VehicleRoutingAlgorithm vra = Jsprit.createAlgorithm(vrp);
vra.setMaxIterations(1000);
Collection<VehicleRoutingProblemSolution> solutions = vra.searchSolutions();
assertEquals(530.0, Solutions.bestOf(solutions).getCost(),50.0);
assertEquals(5, Solutions.bestOf(solutions).getRoutes().size());
}
} }

View file

@ -18,6 +18,7 @@
******************************************************************************/ ******************************************************************************/
package jsprit.core.algorithm; package jsprit.core.algorithm;
import jsprit.core.algorithm.box.Jsprit;
import jsprit.core.algorithm.box.SchrimpfFactory; import jsprit.core.algorithm.box.SchrimpfFactory;
import jsprit.core.algorithm.recreate.NoSolutionFoundException; import jsprit.core.algorithm.recreate.NoSolutionFoundException;
import jsprit.core.problem.VehicleRoutingProblem; import jsprit.core.problem.VehicleRoutingProblem;
@ -55,4 +56,26 @@ public class FiniteVehicleFleetManagerIdentifiesDistinctVehicle_IT {
assertTrue(testFailed.isEmpty()); assertTrue(testFailed.isEmpty());
} }
@Test
public void whenEmployingVehicleWhereOnlyOneDistinctVehicleCanServeAParticularJobWith_jspritAlgorithmShouldFoundDistinctSolution(){
final List<Boolean> testFailed = new ArrayList<Boolean>();
for(int i=0;i<10;i++){
VehicleRoutingProblem.Builder vrpBuilder = VehicleRoutingProblem.Builder.newInstance();
new VrpXMLReader(vrpBuilder).read("src/test/resources/biggerProblem.xml");
VehicleRoutingProblem vrp = vrpBuilder.build();
VehicleRoutingAlgorithm vra = Jsprit.createAlgorithm(vrp);
vra.setMaxIterations(10);
try{
@SuppressWarnings("unused")
Collection<VehicleRoutingProblemSolution> solutions = vra.searchSolutions();
}
catch(NoSolutionFoundException e){
testFailed.add(true);
}
}
System.out.println("failed: " + testFailed.size());
assertTrue(testFailed.isEmpty());
}
} }

View file

@ -18,6 +18,7 @@
******************************************************************************/ ******************************************************************************/
package jsprit.core.algorithm; package jsprit.core.algorithm;
import jsprit.core.algorithm.box.Jsprit;
import jsprit.core.algorithm.box.SchrimpfFactory; import jsprit.core.algorithm.box.SchrimpfFactory;
import jsprit.core.algorithm.io.VehicleRoutingAlgorithms; import jsprit.core.algorithm.io.VehicleRoutingAlgorithms;
import jsprit.core.algorithm.recreate.listener.JobInsertedListener; import jsprit.core.algorithm.recreate.listener.JobInsertedListener;
@ -293,6 +294,273 @@ public class MeetTimeWindowConstraint_IT {
assertTrue(containsJob(vrp.getJobs().get("2"),getRoute("19",Solutions.bestOf(solutions)))); assertTrue(containsJob(vrp.getJobs().get("2"),getRoute("19",Solutions.bestOf(solutions))));
} }
@Test
public void whenEmployingVehicleWithDifferentWorkingShifts_jsprit_nRoutesShouldBeCorrect(){
VehicleRoutingProblem.Builder vrpBuilder = VehicleRoutingProblem.Builder.newInstance();
new VrpXMLReader(vrpBuilder).read("src/test/resources/simpleProblem.xml");
VehicleRoutingProblem vrp = vrpBuilder.build();
VehicleRoutingAlgorithm vra = Jsprit.createAlgorithm(vrp);
vra.setMaxIterations(100);
Collection<VehicleRoutingProblemSolution> solutions = vra.searchSolutions();
assertEquals(2,Solutions.bestOf(solutions).getRoutes().size());
}
@Test
public void whenEmployingVehicleWithDifferentWorkingShifts_jsprit_certainJobsCanNeverBeAssignedToCertainVehicles(){
VehicleRoutingProblem.Builder vrpBuilder = VehicleRoutingProblem.Builder.newInstance();
new VrpXMLReader(vrpBuilder).read("src/test/resources/simpleProblem.xml");
VehicleRoutingProblem vrp = vrpBuilder.build();
VehicleRoutingAlgorithm vra = Jsprit.createAlgorithm(vrp);
vra.setMaxIterations(100);
final List<Boolean> testFailed = new ArrayList<Boolean>();
vra.addListener(new JobInsertedListener() {
@Override
public void informJobInserted(Job job2insert, VehicleRoute inRoute, double additionalCosts, double additionalTime) {
if(job2insert.getId().equals("1")){
if(inRoute.getVehicle().getId().equals("19")){
testFailed.add(true);
}
}
if(job2insert.getId().equals("2")){
if(inRoute.getVehicle().getId().equals("21")){
testFailed.add(true);
}
}
}
});
@SuppressWarnings("unused")
Collection<VehicleRoutingProblemSolution> solutions = vra.searchSolutions();
assertTrue(testFailed.isEmpty());
}
@Test
public void whenEmployingVehicleWithDifferentWorkingShifts_jsprit_certainVehiclesCanNeverBeAssignedToCertainRoutes(){
VehicleRoutingProblem.Builder vrpBuilder = VehicleRoutingProblem.Builder.newInstance();
new VrpXMLReader(vrpBuilder).read("src/test/resources/simpleProblem.xml");
VehicleRoutingProblem vrp = vrpBuilder.build();
VehicleRoutingAlgorithm vra = Jsprit.createAlgorithm(vrp);
vra.setMaxIterations(100);
final List<Boolean> testFailed = new ArrayList<Boolean>();
vra.addListener(new VehicleSwitchedListener() {
@Override
public void vehicleSwitched(VehicleRoute vehicleRoute, Vehicle oldVehicle, Vehicle newVehicle) {
if(oldVehicle==null) return;
if(oldVehicle.getId().equals("21") && newVehicle.getId().equals("19")){
for(Job j : vehicleRoute.getTourActivities().getJobs()){
if(j.getId().equals("1")){
testFailed.add(true);
}
}
}
if(oldVehicle.getId().equals("19") && newVehicle.getId().equals("21")){
for(Job j : vehicleRoute.getTourActivities().getJobs()){
if(j.getId().equals("2")){
testFailed.add(true);
}
}
}
}
});
@SuppressWarnings("unused")
Collection<VehicleRoutingProblemSolution> solutions = vra.searchSolutions();
System.out.println("failed " + testFailed.size());
assertTrue(testFailed.isEmpty());
}
@Test
public void whenEmployingVehicleWithDifferentWorkingShifts_jsprit_job2CanNeverBeInVehicle21(){
VehicleRoutingProblem.Builder vrpBuilder = VehicleRoutingProblem.Builder.newInstance();
new VrpXMLReader(vrpBuilder).read("src/test/resources/simpleProblem.xml");
VehicleRoutingProblem vrp = vrpBuilder.build();
VehicleRoutingAlgorithm vra = Jsprit.createAlgorithm(vrp);
vra.setMaxIterations(100);
Collection<VehicleRoutingProblemSolution> solutions = vra.searchSolutions();
assertEquals(2,Solutions.bestOf(solutions).getRoutes().size());
}
@Test
public void whenEmployingVehicleWithDifferentWorkingShifts_jsprit_job1ShouldBeAssignedCorrectly(){
VehicleRoutingProblem.Builder vrpBuilder = VehicleRoutingProblem.Builder.newInstance();
new VrpXMLReader(vrpBuilder).read("src/test/resources/simpleProblem.xml");
VehicleRoutingProblem vrp = vrpBuilder.build();
VehicleRoutingAlgorithm vra = Jsprit.createAlgorithm(vrp);
vra.setMaxIterations(100);
Collection<VehicleRoutingProblemSolution> solutions = vra.searchSolutions();
// assertEquals(2,Solutions.bestOf(solutions).getRoutes().size());
assertTrue(containsJob(vrp.getJobs().get("1"),getRoute("21",Solutions.bestOf(solutions))));
}
@Test
public void whenEmployingVehicleWithDifferentWorkingShifts_jsprit_job2ShouldBeAssignedCorrectly(){
VehicleRoutingProblem.Builder vrpBuilder = VehicleRoutingProblem.Builder.newInstance();
new VrpXMLReader(vrpBuilder).read("src/test/resources/simpleProblem.xml");
VehicleRoutingProblem vrp = vrpBuilder.build();
VehicleRoutingAlgorithm vra = Jsprit.createAlgorithm(vrp);
vra.setMaxIterations(100);
Collection<VehicleRoutingProblemSolution> solutions = vra.searchSolutions();
// assertEquals(2,Solutions.bestOf(solutions).getRoutes().size());
assertTrue(containsJob(vrp.getJobs().get("2"),getRoute("19",Solutions.bestOf(solutions))));
}
@Test
public void whenEmployingVehicleWithDifferentWorkingShifts_jsprit_and_vehicleSwitchIsNotAllowed_nRoutesShouldBeCorrect(){
VehicleRoutingProblem.Builder vrpBuilder = VehicleRoutingProblem.Builder.newInstance();
new VrpXMLReader(vrpBuilder).read("src/test/resources/simpleProblem.xml");
VehicleRoutingProblem vrp = vrpBuilder.build();
VehicleRoutingAlgorithm vra = Jsprit.Builder.newInstance(vrp).setProperty(Jsprit.Parameter.VEHICLE_SWITCH, "false").buildAlgorithm();
vra.setMaxIterations(100);
Collection<VehicleRoutingProblemSolution> solutions = vra.searchSolutions();
assertEquals(2,Solutions.bestOf(solutions).getRoutes().size());
}
@Test
public void whenEmployingVehicleWithDifferentWorkingShifts_and_vehicleSwitchIsNotAllowed_jsprit_certainJobsCanNeverBeAssignedToCertainVehicles(){
VehicleRoutingProblem.Builder vrpBuilder = VehicleRoutingProblem.Builder.newInstance();
new VrpXMLReader(vrpBuilder).read("src/test/resources/simpleProblem.xml");
VehicleRoutingProblem vrp = vrpBuilder.build();
VehicleRoutingAlgorithm vra = Jsprit.Builder.newInstance(vrp).setProperty(Jsprit.Parameter.VEHICLE_SWITCH,"false").buildAlgorithm();
vra.setMaxIterations(100);
final List<Boolean> testFailed = new ArrayList<Boolean>();
vra.addListener(new JobInsertedListener() {
@Override
public void informJobInserted(Job job2insert, VehicleRoute inRoute, double additionalCosts, double additionalTime) {
if(job2insert.getId().equals("1")){
if(inRoute.getVehicle().getId().equals("19")){
testFailed.add(true);
}
}
if(job2insert.getId().equals("2")){
if(inRoute.getVehicle().getId().equals("21")){
testFailed.add(true);
}
}
}
});
@SuppressWarnings("unused")
Collection<VehicleRoutingProblemSolution> solutions = vra.searchSolutions();
assertTrue(testFailed.isEmpty());
}
@Test
public void whenEmployingVehicleWithDifferentWorkingShifts_and_vehicleSwitchIsNotAllowed_jsprit_certainVehiclesCanNeverBeAssignedToCertainRoutes(){
VehicleRoutingProblem.Builder vrpBuilder = VehicleRoutingProblem.Builder.newInstance();
new VrpXMLReader(vrpBuilder).read("src/test/resources/simpleProblem.xml");
VehicleRoutingProblem vrp = vrpBuilder.build();
VehicleRoutingAlgorithm vra = Jsprit.Builder.newInstance(vrp).setProperty(Jsprit.Parameter.VEHICLE_SWITCH,"false").buildAlgorithm();
vra.setMaxIterations(100);
final List<Boolean> testFailed = new ArrayList<Boolean>();
vra.addListener(new VehicleSwitchedListener() {
@Override
public void vehicleSwitched(VehicleRoute vehicleRoute, Vehicle oldVehicle, Vehicle newVehicle) {
if(oldVehicle==null) return;
if(oldVehicle.getId().equals("21") && newVehicle.getId().equals("19")){
for(Job j : vehicleRoute.getTourActivities().getJobs()){
if(j.getId().equals("1")){
testFailed.add(true);
}
}
}
if(oldVehicle.getId().equals("19") && newVehicle.getId().equals("21")){
for(Job j : vehicleRoute.getTourActivities().getJobs()){
if(j.getId().equals("2")){
testFailed.add(true);
}
}
}
}
});
@SuppressWarnings("unused")
Collection<VehicleRoutingProblemSolution> solutions = vra.searchSolutions();
System.out.println("failed " + testFailed.size());
assertTrue(testFailed.isEmpty());
}
@Test
public void whenEmployingVehicleWithDifferentWorkingShifts_and_vehicleSwitchIsNotAllowed_jsprit_job2CanNeverBeInVehicle21(){
VehicleRoutingProblem.Builder vrpBuilder = VehicleRoutingProblem.Builder.newInstance();
new VrpXMLReader(vrpBuilder).read("src/test/resources/simpleProblem.xml");
VehicleRoutingProblem vrp = vrpBuilder.build();
VehicleRoutingAlgorithm vra = Jsprit.Builder.newInstance(vrp).setProperty(Jsprit.Parameter.VEHICLE_SWITCH,"false").buildAlgorithm();
vra.setMaxIterations(100);
Collection<VehicleRoutingProblemSolution> solutions = vra.searchSolutions();
assertEquals(2,Solutions.bestOf(solutions).getRoutes().size());
}
@Test
public void whenEmployingVehicleWithDifferentWorkingShifts_and_vehicleSwitchIsNotAllowed_jsprit_job1ShouldBeAssignedCorrectly(){
VehicleRoutingProblem.Builder vrpBuilder = VehicleRoutingProblem.Builder.newInstance();
new VrpXMLReader(vrpBuilder).read("src/test/resources/simpleProblem.xml");
VehicleRoutingProblem vrp = vrpBuilder.build();
VehicleRoutingAlgorithm vra = Jsprit.Builder.newInstance(vrp).setProperty(Jsprit.Parameter.VEHICLE_SWITCH,"false").buildAlgorithm();
vra.setMaxIterations(100);
Collection<VehicleRoutingProblemSolution> solutions = vra.searchSolutions();
assertEquals(2,Solutions.bestOf(solutions).getRoutes().size());
assertTrue(containsJob(vrp.getJobs().get("1"),getRoute("21",Solutions.bestOf(solutions))));
}
@Test
public void whenEmployingVehicleWithDifferentWorkingShifts_and_vehicleSwitchIsNotAllowed_jsprit_job2ShouldBeAssignedCorrectly(){
VehicleRoutingProblem.Builder vrpBuilder = VehicleRoutingProblem.Builder.newInstance();
new VrpXMLReader(vrpBuilder).read("src/test/resources/simpleProblem.xml");
VehicleRoutingProblem vrp = vrpBuilder.build();
VehicleRoutingAlgorithm vra = Jsprit.Builder.newInstance(vrp).setProperty(Jsprit.Parameter.VEHICLE_SWITCH,"false").buildAlgorithm();
vra.setMaxIterations(100);
Collection<VehicleRoutingProblemSolution> solutions = vra.searchSolutions();
assertEquals(2,Solutions.bestOf(solutions).getRoutes().size());
assertTrue(containsJob(vrp.getJobs().get("2"),getRoute("19",Solutions.bestOf(solutions))));
}
private boolean containsJob(Job job, VehicleRoute route) { private boolean containsJob(Job job, VehicleRoute route) {
if(route == null) return false; if(route == null) return false;
for(Job j : route.getTourActivities().getJobs()){ for(Job j : route.getTourActivities().getJobs()){

View file

@ -1,6 +1,7 @@
package jsprit.core.algorithm; package jsprit.core.algorithm;
import jsprit.core.algorithm.box.Jsprit;
import jsprit.core.algorithm.box.SchrimpfFactory; import jsprit.core.algorithm.box.SchrimpfFactory;
import jsprit.core.problem.AbstractJob; import jsprit.core.problem.AbstractJob;
import jsprit.core.problem.Location; import jsprit.core.problem.Location;
@ -87,6 +88,59 @@ public class PDTW_IT {
} }
} }
@Test
public void whenDealingWithShipments_usingJsprit_timeWindowsShouldNOTbeBroken() {
VehicleRoutingProblem.Builder vrpBuilder = VehicleRoutingProblem.Builder.newInstance();
for(int i =0 ; i < nVehicles ; i++){
vrpBuilder.addVehicle(createVehicle());
}
for(int i =0 ; i < nJobs;i++){
vrpBuilder.addJob(createShipment());
}
vrpBuilder.setFleetSize(FleetSize.FINITE);
VehicleRoutingProblem problem = vrpBuilder.build();
VehicleRoutingAlgorithm algorithm = Jsprit.createAlgorithm(problem);
algorithm.setMaxIterations(0);
Collection<VehicleRoutingProblemSolution> solutions = algorithm.searchSolutions();
VehicleRoutingProblemSolution bestSolution = Solutions.bestOf(solutions);
for(VehicleRoute route : bestSolution.getRoutes()){
Vehicle v = route.getVehicle();
for(TourActivity ta : route.getActivities()){
if(ta.getArrTime() > v.getLatestArrival() * 1.00001){
assertFalse(true);
}
}
}
}
@Test
public void whenDealingWithServices_usingJsprit_timeWindowsShouldNOTbeBroken() {
VehicleRoutingProblem.Builder vrpBuilder = VehicleRoutingProblem.Builder.newInstance();
for(int i =0 ; i < nVehicles ; i++){
vrpBuilder.addVehicle(createVehicle());
}
for(int i =0 ; i < nJobs;i++){
vrpBuilder.addJob(createService());
}
vrpBuilder.setFleetSize(FleetSize.FINITE);
VehicleRoutingProblem problem = vrpBuilder.build();
VehicleRoutingAlgorithm algorithm = Jsprit.createAlgorithm(problem);
algorithm.setMaxIterations(100);
Collection<VehicleRoutingProblemSolution> solutions = algorithm.searchSolutions();
VehicleRoutingProblemSolution bestSolution = Solutions.bestOf(solutions);
for(VehicleRoute route : bestSolution.getRoutes()){
Vehicle v = route.getVehicle();
for(TourActivity ta : route.getActivities()){
if(ta.getArrTime() * 1.000001 > v.getLatestArrival()){
assertFalse(true);
}
}
}
}
private AbstractJob createService() { private AbstractJob createService() {
Service.Builder b = Service.Builder.newInstance(Integer.toString(nextShipmentId++)); Service.Builder b = Service.Builder.newInstance(Integer.toString(nextShipmentId++));
b.addSizeDimension(0, 1); b.addSizeDimension(0, 1);

View file

@ -18,6 +18,7 @@
******************************************************************************/ ******************************************************************************/
package jsprit.core.algorithm; package jsprit.core.algorithm;
import jsprit.core.algorithm.box.Jsprit;
import jsprit.core.algorithm.io.VehicleRoutingAlgorithms; import jsprit.core.algorithm.io.VehicleRoutingAlgorithms;
import jsprit.core.problem.VehicleRoutingProblem; import jsprit.core.problem.VehicleRoutingProblem;
import jsprit.core.problem.io.VrpXMLReader; import jsprit.core.problem.io.VrpXMLReader;
@ -42,4 +43,16 @@ public class PickupsAndDeliveries_IT {
assertEquals(19,Solutions.bestOf(solutions).getRoutes().size(),1); assertEquals(19,Solutions.bestOf(solutions).getRoutes().size(),1);
} }
@Test
public void whenSolvingLR101InstanceOfLiLim_withJsprit_solutionsMustNoBeWorseThan5PercentOfBestKnownSolution(){
VehicleRoutingProblem.Builder vrpBuilder = VehicleRoutingProblem.Builder.newInstance();
new VrpXMLReader(vrpBuilder).read("src/test/resources/lilim_lr101.xml");
VehicleRoutingProblem vrp = vrpBuilder.build();
VehicleRoutingAlgorithm vra = Jsprit.createAlgorithm(vrp);
vra.setMaxIterations(1000);
Collection<VehicleRoutingProblemSolution> solutions = vra.searchSolutions();
assertEquals(1650.8,Solutions.bestOf(solutions).getCost(),80.);
assertEquals(19,Solutions.bestOf(solutions).getRoutes().size(),1);
}
} }

View file

@ -16,18 +16,16 @@
******************************************************************************/ ******************************************************************************/
package jsprit.core.algorithm; package jsprit.core.algorithm;
import jsprit.core.algorithm.box.Jsprit;
import jsprit.core.algorithm.box.SchrimpfFactory; import jsprit.core.algorithm.box.SchrimpfFactory;
import jsprit.core.algorithm.termination.IterationWithoutImprovementTermination; import jsprit.core.algorithm.termination.IterationWithoutImprovementTermination;
import jsprit.core.problem.Location; import jsprit.core.problem.Location;
import jsprit.core.problem.VehicleRoutingProblem; import jsprit.core.problem.VehicleRoutingProblem;
import jsprit.core.problem.VehicleRoutingProblem.FleetSize; import jsprit.core.problem.VehicleRoutingProblem.FleetSize;
import jsprit.core.problem.cost.VehicleRoutingTransportCosts;
import jsprit.core.problem.driver.Driver;
import jsprit.core.problem.job.Delivery; import jsprit.core.problem.job.Delivery;
import jsprit.core.problem.job.Pickup; import jsprit.core.problem.job.Pickup;
import jsprit.core.problem.job.Service; import jsprit.core.problem.job.Service;
import jsprit.core.problem.solution.VehicleRoutingProblemSolution; import jsprit.core.problem.solution.VehicleRoutingProblemSolution;
import jsprit.core.problem.vehicle.Vehicle;
import jsprit.core.problem.vehicle.VehicleImpl; import jsprit.core.problem.vehicle.VehicleImpl;
import jsprit.core.problem.vehicle.VehicleTypeImpl; import jsprit.core.problem.vehicle.VehicleTypeImpl;
import jsprit.core.reporting.SolutionPrinter; import jsprit.core.reporting.SolutionPrinter;
@ -39,7 +37,6 @@ import org.junit.Test;
import java.io.*; import java.io.*;
import java.util.Collection; import java.util.Collection;
import java.util.Map;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
@ -47,98 +44,6 @@ import static org.junit.Assert.assertEquals;
public class RefuseCollection_IT { public class RefuseCollection_IT {
static class RelationKey {
static RelationKey newKey(String from, String to){
int fromInt = Integer.parseInt(from);
int toInt = Integer.parseInt(to);
if(fromInt < toInt){
return new RelationKey(from, to);
}
else {
return new RelationKey(to, from);
}
}
final String from;
final String to;
public RelationKey(String from, String to) {
super();
this.from = from;
this.to = to;
}
/* (non-Javadoc)
* @see java.lang.Object#hashCode()
*/
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((from == null) ? 0 : from.hashCode());
result = prime * result + ((to == null) ? 0 : to.hashCode());
return result;
}
/* (non-Javadoc)
* @see java.lang.Object#equals(java.lang.Object)
*/
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
RelationKey other = (RelationKey) obj;
if (from == null) {
if (other.from != null)
return false;
} else if (!from.equals(other.from))
return false;
if (to == null) {
if (other.to != null)
return false;
} else if (!to.equals(other.to))
return false;
return true;
}
}
static class RoutingCosts implements VehicleRoutingTransportCosts {
private Map<RelationKey,Integer> distances;
public RoutingCosts(Map<RelationKey, Integer> distances) {
super();
this.distances = distances;
}
@Override
public double getTransportTime(Location from, Location to, double departureTime, Driver driver, Vehicle vehicle) {
return getTransportCost(from, to, departureTime, driver, vehicle);
}
@Override
public double getBackwardTransportTime(Location from, Location to, double arrivalTime, Driver driver, Vehicle vehicle) {
return getTransportCost(from, to, arrivalTime, driver, vehicle);
}
@Override
public double getTransportCost(Location from, Location to,double departureTime, Driver driver, Vehicle vehicle) {
if(from.equals(to)) return 0.0;
RelationKey key = RelationKey.newKey(from.getId(), to.getId());
return distances.get(key);
}
@Override
public double getBackwardTransportCost(Location from, Location to,double arrivalTime, Driver driver, Vehicle vehicle) {
return getTransportCost(from, to, arrivalTime, driver, vehicle);
}
}
@Test @Test
@ -152,7 +57,7 @@ public class RefuseCollection_IT {
VehicleTypeImpl bigType = typeBuilder.build(); VehicleTypeImpl bigType = typeBuilder.build();
VehicleImpl.Builder vehicleBuilder = VehicleImpl.Builder.newInstance("vehicle"); VehicleImpl.Builder vehicleBuilder = VehicleImpl.Builder.newInstance("vehicle");
vehicleBuilder.setStartLocationId("1"); vehicleBuilder.setStartLocation(Location.newInstance("1"));
vehicleBuilder.setType(bigType); vehicleBuilder.setType(bigType);
VehicleImpl bigVehicle = vehicleBuilder.build(); VehicleImpl bigVehicle = vehicleBuilder.build();
@ -185,6 +90,50 @@ public class RefuseCollection_IT {
assertEquals(2,Solutions.bestOf(solutions).getRoutes().size()); assertEquals(2,Solutions.bestOf(solutions).getRoutes().size());
} }
@Test
public void whenReadingServices_usingJsprit_itShouldCalculateCorrectly(){
/*
* create vehicle-type and vehicle
*/
VehicleTypeImpl.Builder typeBuilder = VehicleTypeImpl.Builder.newInstance("vehicle-type").addCapacityDimension(0, 23);
typeBuilder.setCostPerDistance(1.0);
VehicleTypeImpl bigType = typeBuilder.build();
VehicleImpl.Builder vehicleBuilder = VehicleImpl.Builder.newInstance("vehicle");
vehicleBuilder.setStartLocation(Location.newInstance("1"));
vehicleBuilder.setType(bigType);
VehicleImpl bigVehicle = vehicleBuilder.build();
/*
* start building the problem
*/
VehicleRoutingProblem.Builder vrpBuilder = VehicleRoutingProblem.Builder.newInstance();
vrpBuilder.setFleetSize(FleetSize.INFINITE);
vrpBuilder.addVehicle(bigVehicle);
/*
* create cost-matrix
*/
VehicleRoutingTransportCostsMatrix.Builder matrixBuilder = VehicleRoutingTransportCostsMatrix.Builder.newInstance(true);
/*
* read demand quantities
*/
readDemandQuantitiesAsServices(vrpBuilder);
readDistances(matrixBuilder);
vrpBuilder.setRoutingCost(matrixBuilder.build());
VehicleRoutingProblem vrp = vrpBuilder.build();
VehicleRoutingAlgorithm vra = Jsprit.createAlgorithm(vrp);
vra.setPrematureAlgorithmTermination(new IterationWithoutImprovementTermination(100));
Collection<VehicleRoutingProblemSolution> solutions = vra.searchSolutions();
SolutionPrinter.print(vrp, Solutions.bestOf(solutions), Print.VERBOSE);
assertEquals(397.0,Solutions.bestOf(solutions).getCost(),40.);
assertEquals(2,Solutions.bestOf(solutions).getRoutes().size());
}
@Test @Test
public void whenReadingPickups_itShouldCalculateCorrectly(){ public void whenReadingPickups_itShouldCalculateCorrectly(){
@ -196,7 +145,7 @@ public class RefuseCollection_IT {
VehicleTypeImpl bigType = typeBuilder.build(); VehicleTypeImpl bigType = typeBuilder.build();
VehicleImpl.Builder vehicleBuilder = VehicleImpl.Builder.newInstance("vehicle"); VehicleImpl.Builder vehicleBuilder = VehicleImpl.Builder.newInstance("vehicle");
vehicleBuilder.setStartLocationId("1"); vehicleBuilder.setStartLocation(Location.newInstance("1"));
vehicleBuilder.setType(bigType); vehicleBuilder.setType(bigType);
VehicleImpl bigVehicle = vehicleBuilder.build(); VehicleImpl bigVehicle = vehicleBuilder.build();
@ -240,7 +189,7 @@ public class RefuseCollection_IT {
VehicleTypeImpl bigType = typeBuilder.build(); VehicleTypeImpl bigType = typeBuilder.build();
VehicleImpl.Builder vehicleBuilder = VehicleImpl.Builder.newInstance("vehicle"); VehicleImpl.Builder vehicleBuilder = VehicleImpl.Builder.newInstance("vehicle");
vehicleBuilder.setStartLocationId("1"); vehicleBuilder.setStartLocation(Location.newInstance("1"));
vehicleBuilder.setType(bigType); vehicleBuilder.setType(bigType);
VehicleImpl bigVehicle = vehicleBuilder.build(); VehicleImpl bigVehicle = vehicleBuilder.build();