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

modified .addPenaltyVehicles() in core.problem.VehicleRoutingProblem

such that penalty-vehicles can also shadow vehicles that only
distinguish themselves by their working shift
This commit is contained in:
oblonski 2014-05-15 13:52:09 +02:00
parent c3514dbf23
commit af52639b10
2 changed files with 20 additions and 64 deletions

View file

@ -38,6 +38,7 @@ import jsprit.core.problem.vehicle.Vehicle;
import jsprit.core.problem.vehicle.VehicleImpl; import jsprit.core.problem.vehicle.VehicleImpl;
import jsprit.core.problem.vehicle.VehicleType; import jsprit.core.problem.vehicle.VehicleType;
import jsprit.core.problem.vehicle.VehicleTypeImpl; import jsprit.core.problem.vehicle.VehicleTypeImpl;
import jsprit.core.problem.vehicle.VehicleTypeKey;
import jsprit.core.util.Coordinate; import jsprit.core.util.Coordinate;
import jsprit.core.util.CrowFlyCosts; import jsprit.core.util.CrowFlyCosts;
import jsprit.core.util.Locations; import jsprit.core.util.Locations;
@ -85,57 +86,6 @@ public class VehicleRoutingProblem {
*/ */
public static class Builder { public static class Builder {
/**
* Two locTypeKeys are equal if they have the same locationId and typeId
*
* @author schroeder
*
*/
private static class LocTypeKey {
String locationId;
String typeId;
public LocTypeKey(String locationId, String typeId) {
super();
this.locationId = locationId;
this.typeId = typeId;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result
+ ((locationId == null) ? 0 : locationId.hashCode());
result = prime * result
+ ((typeId == null) ? 0 : typeId.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
LocTypeKey other = (LocTypeKey) obj;
if (locationId == null) {
if (other.locationId != null)
return false;
} else if (!locationId.equals(other.locationId))
return false;
if (typeId == null) {
if (other.typeId != null)
return false;
} else if (!typeId.equals(other.typeId))
return false;
return true;
}
}
/** /**
* Returns a new instance of this builder. * Returns a new instance of this builder.
* *
@ -433,13 +383,13 @@ public class VehicleRoutingProblem {
} }
private void addPenaltyVehicles() { private void addPenaltyVehicles() {
Set<LocTypeKey> locTypeKeys = new HashSet<LocTypeKey>(); Set<VehicleTypeKey> vehicleTypeKeys = new HashSet<VehicleTypeKey>();
List<Vehicle> uniqueVehicles = new ArrayList<Vehicle>(); List<Vehicle> uniqueVehicles = new ArrayList<Vehicle>();
for(Vehicle v : this.uniqueVehicles){ for(Vehicle v : this.uniqueVehicles){
LocTypeKey key = new LocTypeKey(v.getStartLocationId(),v.getType().getTypeId()); VehicleTypeKey key = new VehicleTypeKey(v.getType().getTypeId(),v.getStartLocationId(),v.getEndLocationId(),v.getEarliestDeparture(),v.getLatestArrival());
if(!locTypeKeys.contains(key)){ if(!vehicleTypeKeys.contains(key)){
uniqueVehicles.add(v); uniqueVehicles.add(v);
locTypeKeys.add(key); vehicleTypeKeys.add(key);
} }
} }
for(Vehicle v : uniqueVehicles){ for(Vehicle v : uniqueVehicles){
@ -454,7 +404,7 @@ public class VehicleRoutingProblem {
.setCapacityDimensions(v.getType().getCapacityDimensions()) .setCapacityDimensions(v.getType().getCapacityDimensions())
.build(); .build();
PenaltyVehicleType penType = new PenaltyVehicleType(t,penaltyFactor); PenaltyVehicleType penType = new PenaltyVehicleType(t,penaltyFactor);
String vehicleId = "penaltyVehicle_" + v.getStartLocationId() + "_" + t.getTypeId(); String vehicleId = "penaltyVehicle_" + new VehicleTypeKey(v.getType().getTypeId(),v.getStartLocationId(),v.getEndLocationId(),v.getEarliestDeparture(),v.getLatestArrival()).toString();
Vehicle penVehicle = VehicleImpl.Builder.newInstance(vehicleId).setEarliestStart(v.getEarliestDeparture()) Vehicle penVehicle = VehicleImpl.Builder.newInstance(vehicleId).setEarliestStart(v.getEarliestDeparture())
.setLatestArrival(v.getLatestArrival()).setStartLocationCoordinate(v.getStartLocationCoordinate()).setStartLocationId(v.getStartLocationId()) .setLatestArrival(v.getLatestArrival()).setStartLocationCoordinate(v.getStartLocationCoordinate()).setStartLocationId(v.getStartLocationId())
.setEndLocationId(v.getEndLocationId()).setEndLocationCoordinate(v.getEndLocationCoordinate()) .setEndLocationId(v.getEndLocationId()).setEndLocationCoordinate(v.getEndLocationCoordinate())

View file

@ -42,6 +42,7 @@ import jsprit.core.problem.vehicle.Vehicle;
import jsprit.core.problem.vehicle.VehicleImpl; import jsprit.core.problem.vehicle.VehicleImpl;
import jsprit.core.problem.vehicle.VehicleType; import jsprit.core.problem.vehicle.VehicleType;
import jsprit.core.problem.vehicle.VehicleTypeImpl; import jsprit.core.problem.vehicle.VehicleTypeImpl;
import jsprit.core.problem.vehicle.VehicleTypeKey;
import jsprit.core.util.Coordinate; import jsprit.core.util.Coordinate;
import org.junit.Test; import org.junit.Test;
@ -343,7 +344,7 @@ public class VehicleRoutingProblemTest {
boolean penaltyVehicleInCollection = false; boolean penaltyVehicleInCollection = false;
for(Vehicle v : vrp.getVehicles()){ for(Vehicle v : vrp.getVehicles()){
if(v.getId().equals("penaltyVehicle_loc_type")) penaltyVehicleInCollection = true; if(v.getId().equals(getPenaltyVehicleId(v))) penaltyVehicleInCollection = true;
} }
assertTrue(penaltyVehicleInCollection); assertTrue(penaltyVehicleInCollection);
@ -364,11 +365,16 @@ public class VehicleRoutingProblemTest {
boolean penaltyVehicleInCollection = false; boolean penaltyVehicleInCollection = false;
for(Vehicle v : vrp.getVehicles()){ for(Vehicle v : vrp.getVehicles()){
if(v.getId().equals("penaltyVehicle_loc_type")) penaltyVehicleInCollection = true; if(v.getId().equals(getPenaltyVehicleId(v))) penaltyVehicleInCollection = true;
} }
assertFalse(penaltyVehicleInCollection); assertFalse(penaltyVehicleInCollection);
} }
private String getPenaltyVehicleId(Vehicle v) {
return "penaltyVehicle_" + new VehicleTypeKey(v.getType().getTypeId(), v.getStartLocationId(), v.getEndLocationId(),
v.getEarliestDeparture(), v.getLatestArrival()).toString();
}
@Test @Test
public void whenSettingAddPenaltyVehicleOptionsAndTwoVehiclesWithSameLocationAndType_onlyOnePenaltyVehicleIsAdded(){ public void whenSettingAddPenaltyVehicleOptionsAndTwoVehiclesWithSameLocationAndType_onlyOnePenaltyVehicleIsAdded(){
@ -388,7 +394,7 @@ public class VehicleRoutingProblemTest {
boolean penaltyVehicleInCollection = false; boolean penaltyVehicleInCollection = false;
for(Vehicle v : vrp.getVehicles()){ for(Vehicle v : vrp.getVehicles()){
if(v.getId().equals("penaltyVehicle_loc_type")) penaltyVehicleInCollection = true; if(v.getId().equals(getPenaltyVehicleId(v))) penaltyVehicleInCollection = true;
} }
assertTrue(penaltyVehicleInCollection); assertTrue(penaltyVehicleInCollection);
@ -412,7 +418,7 @@ public class VehicleRoutingProblemTest {
double fix = 0.0; double fix = 0.0;
for(Vehicle v : vrp.getVehicles()){ for(Vehicle v : vrp.getVehicles()){
if(v.getId().equals("penaltyVehicle_loc_type")) { if(v.getId().equals(getPenaltyVehicleId(v))) {
fix = v.getType().getVehicleCostParams().fix; fix = v.getType().getVehicleCostParams().fix;
} }
} }
@ -439,8 +445,8 @@ public class VehicleRoutingProblemTest {
boolean penaltyVehicleInCollection = false; boolean penaltyVehicleInCollection = false;
boolean anotherPenVehInCollection = false; boolean anotherPenVehInCollection = false;
for(Vehicle v : vrp.getVehicles()){ for(Vehicle v : vrp.getVehicles()){
if(v.getId().equals("penaltyVehicle_loc_type")) penaltyVehicleInCollection = true; if(v.getId().equals(getPenaltyVehicleId(vehicle))) penaltyVehicleInCollection = true;
if(v.getId().equals("penaltyVehicle_loc2_type")) anotherPenVehInCollection = true; if(v.getId().equals(getPenaltyVehicleId(vehicle2))) anotherPenVehInCollection = true;
} }
assertTrue(penaltyVehicleInCollection); assertTrue(penaltyVehicleInCollection);
assertTrue(anotherPenVehInCollection); assertTrue(anotherPenVehInCollection);
@ -467,8 +473,8 @@ public class VehicleRoutingProblemTest {
boolean penaltyVehicleInCollection = false; boolean penaltyVehicleInCollection = false;
boolean anotherPenVehInCollection = false; boolean anotherPenVehInCollection = false;
for(Vehicle v : vrp.getVehicles()){ for(Vehicle v : vrp.getVehicles()){
if(v.getId().equals("penaltyVehicle_loc_type")) penaltyVehicleInCollection = true; if(v.getId().equals(getPenaltyVehicleId(vehicle))) penaltyVehicleInCollection = true;
if(v.getId().equals("penaltyVehicle_loc_type2")) anotherPenVehInCollection = true; if(v.getId().equals(getPenaltyVehicleId(vehicle2))) anotherPenVehInCollection = true;
} }
assertTrue(penaltyVehicleInCollection); assertTrue(penaltyVehicleInCollection);
assertTrue(anotherPenVehInCollection); assertTrue(anotherPenVehInCollection);