mirror of
https://github.com/graphhopper/jsprit.git
synced 2020-01-24 07:45:05 +01:00
add tests to reproduce bug #98
This commit is contained in:
parent
3771c8494d
commit
7914076810
2 changed files with 298 additions and 3 deletions
|
|
@ -0,0 +1,205 @@
|
||||||
|
/*******************************************************************************
|
||||||
|
* 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.algorithm;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
|
||||||
|
import java.io.BufferedReader;
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.FileNotFoundException;
|
||||||
|
import java.io.FileReader;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.Collection;
|
||||||
|
|
||||||
|
import jsprit.core.algorithm.box.GreedySchrimpfFactory;
|
||||||
|
import jsprit.core.algorithm.termination.IterationWithoutImprovementTermination;
|
||||||
|
import jsprit.core.problem.VehicleRoutingProblem;
|
||||||
|
import jsprit.core.problem.VehicleRoutingProblem.FleetSize;
|
||||||
|
import jsprit.core.problem.job.Service;
|
||||||
|
import jsprit.core.problem.solution.VehicleRoutingProblemSolution;
|
||||||
|
import jsprit.core.problem.vehicle.Vehicle;
|
||||||
|
import jsprit.core.problem.vehicle.VehicleImpl;
|
||||||
|
import jsprit.core.problem.vehicle.VehicleTypeImpl;
|
||||||
|
import jsprit.core.reporting.SolutionPrinter;
|
||||||
|
import jsprit.core.reporting.SolutionPrinter.Print;
|
||||||
|
import jsprit.core.util.Solutions;
|
||||||
|
import jsprit.core.util.VehicleRoutingTransportCostsMatrix;
|
||||||
|
import jsprit.core.util.VehicleRoutingTransportCostsMatrix.Builder;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
public class RefuseCollectionWithCostsHigherThanTimesAndFiniteFleet_withTimeAndDistanceCosts_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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testAlgo(){
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
* create vehicle-type and vehicle
|
||||||
|
*/
|
||||||
|
VehicleTypeImpl.Builder typeBuilder = VehicleTypeImpl.Builder.newInstance("vehicle-type").addCapacityDimension(0, 23);
|
||||||
|
typeBuilder.setCostPerDistance(1.0).setCostPerTime(1.);
|
||||||
|
VehicleTypeImpl bigType = typeBuilder.build();
|
||||||
|
|
||||||
|
VehicleImpl.Builder vehicleBuilder = VehicleImpl.Builder.newInstance("vehicle");
|
||||||
|
vehicleBuilder.setStartLocationId("1");
|
||||||
|
vehicleBuilder.setType(bigType);
|
||||||
|
vehicleBuilder.setLatestArrival(220);
|
||||||
|
Vehicle 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
|
||||||
|
*/
|
||||||
|
try {
|
||||||
|
readDemandQuantities(vrpBuilder);
|
||||||
|
readDistances(matrixBuilder);
|
||||||
|
} catch (FileNotFoundException e) {
|
||||||
|
// TODO Auto-generated catch block
|
||||||
|
e.printStackTrace();
|
||||||
|
} catch (IOException e) {
|
||||||
|
// TODO Auto-generated catch block
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
|
||||||
|
vrpBuilder.setRoutingCost(matrixBuilder.build());
|
||||||
|
VehicleRoutingProblem vrp = vrpBuilder.build();
|
||||||
|
VehicleRoutingAlgorithm vra = new GreedySchrimpfFactory().createAlgorithm(vrp);
|
||||||
|
vra.setPrematureAlgorithmTermination(new IterationWithoutImprovementTermination(100));
|
||||||
|
Collection<VehicleRoutingProblemSolution> solutions = vra.searchSolutions();
|
||||||
|
|
||||||
|
SolutionPrinter.print(vrp, Solutions.bestOf(solutions), Print.VERBOSE);
|
||||||
|
|
||||||
|
assertEquals(2.*397.+397.,Solutions.bestOf(solutions).getCost(),0.01);
|
||||||
|
assertEquals(2,Solutions.bestOf(solutions).getRoutes().size());
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private static void readDemandQuantities(VehicleRoutingProblem.Builder vrpBuilder) throws FileNotFoundException, IOException {
|
||||||
|
BufferedReader reader = new BufferedReader(new FileReader(new File("src/test/resources/refuseCollectionExample_Quantities")));
|
||||||
|
String line = null;
|
||||||
|
boolean firstLine = true;
|
||||||
|
while((line = reader.readLine()) != null){
|
||||||
|
if(firstLine) {
|
||||||
|
firstLine = false;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
String[] lineTokens = line.split(",");
|
||||||
|
/*
|
||||||
|
* build service
|
||||||
|
*/
|
||||||
|
Service service = Service.Builder.newInstance(lineTokens[0]).addSizeDimension(0, Integer.parseInt(lineTokens[1])).setLocationId(lineTokens[0]).build();
|
||||||
|
/*
|
||||||
|
* and add it to problem
|
||||||
|
*/
|
||||||
|
vrpBuilder.addJob(service);
|
||||||
|
}
|
||||||
|
reader.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private static void readDistances(Builder matrixBuilder) throws IOException {
|
||||||
|
BufferedReader reader = new BufferedReader(new FileReader(new File("src/test/resources/refuseCollectionExample_Distances")));
|
||||||
|
String line = null;
|
||||||
|
boolean firstLine = true;
|
||||||
|
while((line = reader.readLine()) != null){
|
||||||
|
if(firstLine) {
|
||||||
|
firstLine = false;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
String[] lineTokens = line.split(",");
|
||||||
|
matrixBuilder.addTransportDistance(lineTokens[0],lineTokens[1], 2.*Integer.parseInt(lineTokens[2]));
|
||||||
|
matrixBuilder.addTransportTime(lineTokens[0],lineTokens[1], Integer.parseInt(lineTokens[2]));
|
||||||
|
}
|
||||||
|
reader.close();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -3,7 +3,6 @@ package jsprit.core.util;
|
||||||
import static org.junit.Assert.assertEquals;
|
import static org.junit.Assert.assertEquals;
|
||||||
import static org.mockito.Mockito.mock;
|
import static org.mockito.Mockito.mock;
|
||||||
import static org.mockito.Mockito.when;
|
import static org.mockito.Mockito.when;
|
||||||
|
|
||||||
import jsprit.core.problem.vehicle.Vehicle;
|
import jsprit.core.problem.vehicle.Vehicle;
|
||||||
import jsprit.core.problem.vehicle.VehicleType;
|
import jsprit.core.problem.vehicle.VehicleType;
|
||||||
import jsprit.core.problem.vehicle.VehicleTypeImpl;
|
import jsprit.core.problem.vehicle.VehicleTypeImpl;
|
||||||
|
|
@ -124,7 +123,7 @@ public class VehicleRoutingTransportCostsMatrixTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void whenAddingTimeToAsymmetricMatrixUsingStringAsKey_itShouldReturnCorrectValues(){
|
public void whenAddingTimeToAsymmetricMatrixUsingStringAsKey_itShouldReturnCorrectValues(){
|
||||||
VehicleRoutingTransportCostsMatrix.Builder matrixBuilder = VehicleRoutingTransportCostsMatrix.Builder.newInstance(true);
|
VehicleRoutingTransportCostsMatrix.Builder matrixBuilder = VehicleRoutingTransportCostsMatrix.Builder.newInstance(false);
|
||||||
matrixBuilder.addTransportTime("from", "to", 2.);
|
matrixBuilder.addTransportTime("from", "to", 2.);
|
||||||
matrixBuilder.addTransportTime("to", "from", 4.);
|
matrixBuilder.addTransportTime("to", "from", 4.);
|
||||||
VehicleRoutingTransportCostsMatrix matrix = matrixBuilder.build();
|
VehicleRoutingTransportCostsMatrix matrix = matrixBuilder.build();
|
||||||
|
|
@ -137,7 +136,7 @@ public class VehicleRoutingTransportCostsMatrixTest {
|
||||||
VehicleType type = VehicleTypeImpl.Builder.newInstance("t").setCostPerDistance(0.).setCostPerTime(1.).build();
|
VehicleType type = VehicleTypeImpl.Builder.newInstance("t").setCostPerDistance(0.).setCostPerTime(1.).build();
|
||||||
Vehicle vehicle = mock(Vehicle.class);
|
Vehicle vehicle = mock(Vehicle.class);
|
||||||
when(vehicle.getType()).thenReturn(type);
|
when(vehicle.getType()).thenReturn(type);
|
||||||
VehicleRoutingTransportCostsMatrix.Builder matrixBuilder = VehicleRoutingTransportCostsMatrix.Builder.newInstance(true);
|
VehicleRoutingTransportCostsMatrix.Builder matrixBuilder = VehicleRoutingTransportCostsMatrix.Builder.newInstance(false);
|
||||||
matrixBuilder.addTransportTime("from", "to", 2.);
|
matrixBuilder.addTransportTime("from", "to", 2.);
|
||||||
matrixBuilder.addTransportTime("to", "from", 4.);
|
matrixBuilder.addTransportTime("to", "from", 4.);
|
||||||
VehicleRoutingTransportCostsMatrix matrix = matrixBuilder.build();
|
VehicleRoutingTransportCostsMatrix matrix = matrixBuilder.build();
|
||||||
|
|
@ -146,5 +145,96 @@ public class VehicleRoutingTransportCostsMatrixTest {
|
||||||
assertEquals(2.,matrix.getTransportCost("from", "to", 0.0, null, vehicle),0.1);
|
assertEquals(2.,matrix.getTransportCost("from", "to", 0.0, null, vehicle),0.1);
|
||||||
assertEquals(4.,matrix.getTransportCost("to", "from", 0.0, null, vehicle),0.1);
|
assertEquals(4.,matrix.getTransportCost("to", "from", 0.0, null, vehicle),0.1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenAddingTimeAndDistanceToSymmetricMatrix_itShouldReturnCorrectValues(){
|
||||||
|
VehicleRoutingTransportCostsMatrix.Builder matrixBuilder = VehicleRoutingTransportCostsMatrix.Builder.newInstance(true);
|
||||||
|
matrixBuilder.addTransportDistance("1", "2", 20.);
|
||||||
|
matrixBuilder.addTransportTime("1", "2", 2.);
|
||||||
|
VehicleRoutingTransportCostsMatrix matrix = matrixBuilder.build();
|
||||||
|
Vehicle vehicle = mock(Vehicle.class);
|
||||||
|
VehicleType type = VehicleTypeImpl.Builder.newInstance("t").setCostPerDistance(1.).setCostPerTime(2.).build();
|
||||||
|
when(vehicle.getType()).thenReturn(type);
|
||||||
|
assertEquals(24.,matrix.getTransportCost("1", "2", 0.0, null, vehicle),0.1);
|
||||||
|
assertEquals(24.,matrix.getTransportCost("2", "1", 0.0, null, vehicle),0.1);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenAddingTimeAndDistanceToSymmetricMatrixUsingStringAsKey_itShouldReturnCorrectValues(){
|
||||||
|
VehicleRoutingTransportCostsMatrix.Builder matrixBuilder = VehicleRoutingTransportCostsMatrix.Builder.newInstance(true);
|
||||||
|
matrixBuilder.addTransportTime("from", "to", 2.);
|
||||||
|
Vehicle vehicle = mock(Vehicle.class);
|
||||||
|
VehicleType type = VehicleTypeImpl.Builder.newInstance("t").setCostPerDistance(1.).setCostPerTime(2.).build();
|
||||||
|
when(vehicle.getType()).thenReturn(type);
|
||||||
|
VehicleRoutingTransportCostsMatrix matrix = matrixBuilder.build();
|
||||||
|
assertEquals(4.,matrix.getTransportCost("from", "to", 0.0, null, vehicle),0.1);
|
||||||
|
assertEquals(4.,matrix.getTransportCost("to", "from", 0.0, null, vehicle),0.1);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenAddingTimeAndDistanceToAsymmetricMatrix_itShouldReturnCorrectValues(){
|
||||||
|
VehicleRoutingTransportCostsMatrix.Builder matrixBuilder = VehicleRoutingTransportCostsMatrix.Builder.newInstance(false);
|
||||||
|
matrixBuilder.addTransportTime("1", "2", 2.);
|
||||||
|
matrixBuilder.addTransportTime("2", "1", 8.);
|
||||||
|
VehicleRoutingTransportCostsMatrix matrix = matrixBuilder.build();
|
||||||
|
Vehicle vehicle = mock(Vehicle.class);
|
||||||
|
VehicleType type = VehicleTypeImpl.Builder.newInstance("t").setCostPerDistance(1.).setCostPerTime(2.).build();
|
||||||
|
when(vehicle.getType()).thenReturn(type);
|
||||||
|
assertEquals(4.,matrix.getTransportCost("1", "2", 0.0, null, vehicle),0.1);
|
||||||
|
assertEquals(16.,matrix.getTransportCost("2", "1", 0.0, null, vehicle),0.1);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenAddingTimeAndDistanceToAsymmetricMatrixUsingStringAsKey_itShouldReturnCorrectValues(){
|
||||||
|
VehicleRoutingTransportCostsMatrix.Builder matrixBuilder = VehicleRoutingTransportCostsMatrix.Builder.newInstance(false);
|
||||||
|
matrixBuilder.addTransportTime("from", "to", 2.);
|
||||||
|
matrixBuilder.addTransportDistance("from", "to", 1.);
|
||||||
|
matrixBuilder.addTransportTime("to", "from", 4.);
|
||||||
|
matrixBuilder.addTransportDistance("to", "from", 3.);
|
||||||
|
VehicleRoutingTransportCostsMatrix matrix = matrixBuilder.build();
|
||||||
|
Vehicle vehicle = mock(Vehicle.class);
|
||||||
|
VehicleType type = VehicleTypeImpl.Builder.newInstance("t").setCostPerDistance(1.).setCostPerTime(2.).build();
|
||||||
|
when(vehicle.getType()).thenReturn(type);
|
||||||
|
assertEquals(5.,matrix.getTransportCost("from", "to", 0.0, null, vehicle),0.1);
|
||||||
|
assertEquals(11.,matrix.getTransportCost("to", "from", 0.0, null, vehicle),0.1);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenAddingTimeAndDistanceToAsymmetricMatrixUsingStringAsKey_itShouldReturnCorrectCostValues(){
|
||||||
|
VehicleType type = VehicleTypeImpl.Builder.newInstance("t").setCostPerDistance(2.).setCostPerTime(1.).build();
|
||||||
|
Vehicle vehicle = mock(Vehicle.class);
|
||||||
|
when(vehicle.getType()).thenReturn(type);
|
||||||
|
VehicleRoutingTransportCostsMatrix.Builder matrixBuilder = VehicleRoutingTransportCostsMatrix.Builder.newInstance(false);
|
||||||
|
matrixBuilder.addTransportTime("from", "to", 2.);
|
||||||
|
matrixBuilder.addTransportDistance("from", "to", 3.);
|
||||||
|
matrixBuilder.addTransportTime("to", "from", 4.);
|
||||||
|
matrixBuilder.addTransportDistance("to", "from", 5.);
|
||||||
|
VehicleRoutingTransportCostsMatrix matrix = matrixBuilder.build();
|
||||||
|
|
||||||
|
assertEquals(8.,matrix.getTransportCost("from", "to", 0.0, null, vehicle),0.1);
|
||||||
|
assertEquals(14.,matrix.getTransportCost("to", "from", 0.0, null, vehicle),0.1);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenAddingTimeAndDistanceToSymmetricMatrixUsingStringAsKey_and_overridesEntry_itShouldReturnCorrectValues(){
|
||||||
|
VehicleRoutingTransportCostsMatrix.Builder matrixBuilder = VehicleRoutingTransportCostsMatrix.Builder.newInstance(true);
|
||||||
|
|
||||||
|
matrixBuilder.addTransportDistance("from", "to", 1.);
|
||||||
|
matrixBuilder.addTransportTime("from", "to", 2.);
|
||||||
|
|
||||||
|
matrixBuilder.addTransportDistance("to", "from", 1.);
|
||||||
|
matrixBuilder.addTransportTime("to", "from", 2.);
|
||||||
|
|
||||||
|
VehicleRoutingTransportCostsMatrix matrix = matrixBuilder.build();
|
||||||
|
Vehicle vehicle = mock(Vehicle.class);
|
||||||
|
VehicleType type = VehicleTypeImpl.Builder.newInstance("t").setCostPerDistance(1.).setCostPerTime(0.).build();
|
||||||
|
when(vehicle.getType()).thenReturn(type);
|
||||||
|
assertEquals(1.,matrix.getDistance("from", "to"),0.1);
|
||||||
|
assertEquals(1.,matrix.getDistance("to", "from"),0.1);
|
||||||
|
assertEquals(1.,matrix.getTransportCost("from", "to", 0.0, null, vehicle),0.1);
|
||||||
|
assertEquals(1.,matrix.getTransportCost("to", "from", 0.0, null, vehicle),0.1);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue