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

fixed issue #411

This commit is contained in:
oblonski 2018-02-23 09:56:11 +01:00
parent a0009918cc
commit 994b97b48b
No known key found for this signature in database
GPG key ID: 179DE487285680D1
4 changed files with 102 additions and 28 deletions

View file

@ -87,7 +87,7 @@ public class VehicleRoutingProblem {
private FleetSize fleetSize = FleetSize.INFINITE; private FleetSize fleetSize = FleetSize.INFINITE;
private Collection<VehicleType> vehicleTypes = new ArrayList<VehicleType>(); private Map<String, VehicleType> vehicleTypes = new HashMap<>();
private Collection<VehicleRoute> initialRoutes = new ArrayList<VehicleRoute>(); private Collection<VehicleRoute> initialRoutes = new ArrayList<VehicleRoute>();
@ -395,8 +395,13 @@ public class VehicleRoutingProblem {
incVehicleTypeIdIndexCounter(); incVehicleTypeIdIndexCounter();
} }
uniqueVehicles.add(vehicle); uniqueVehicles.add(vehicle);
if (!vehicleTypes.contains(vehicle.getType())) { if (!vehicleTypes.containsKey(vehicle.getType().getTypeId())) {
vehicleTypes.add(vehicle.getType()); vehicleTypes.put(vehicle.getType().getTypeId(), vehicle.getType());
} else {
VehicleType existingType = vehicleTypes.get(vehicle.getType().getTypeId());
if (!vehicle.getType().equals(existingType)) {
throw new IllegalArgumentException("A type with type id " + vehicle.getType().getTypeId() + " already exists. However, types are different. Please use unique vehicle types only.");
}
} }
String startLocationId = vehicle.getStartLocation().getId(); String startLocationId = vehicle.getStartLocation().getId();
addLocationToTentativeLocations(vehicle.getStartLocation()); addLocationToTentativeLocations(vehicle.getStartLocation());
@ -495,7 +500,7 @@ public class VehicleRoutingProblem {
* @return collection of vehicle-types * @return collection of vehicle-types
*/ */
public Collection<VehicleType> getAddedVehicleTypes() { public Collection<VehicleType> getAddedVehicleTypes() {
return Collections.unmodifiableCollection(vehicleTypes); return Collections.unmodifiableCollection(vehicleTypes.values());
} }
/** /**
@ -587,7 +592,7 @@ public class VehicleRoutingProblem {
this.jobs = builder.jobs; this.jobs = builder.jobs;
this.fleetSize = builder.fleetSize; this.fleetSize = builder.fleetSize;
this.vehicles = builder.uniqueVehicles; this.vehicles = builder.uniqueVehicles;
this.vehicleTypes = builder.vehicleTypes; this.vehicleTypes = builder.vehicleTypes.values();
this.initialVehicleRoutes = builder.initialRoutes; this.initialVehicleRoutes = builder.initialRoutes;
this.transportCosts = builder.transportCosts; this.transportCosts = builder.transportCosts;
this.activityCosts = builder.activityCosts; this.activityCosts = builder.activityCosts;

View file

@ -81,6 +81,37 @@ public class VehicleTypeImpl implements VehicleType {
public String toString() { public String toString() {
return "[fixed=" + fix + "][perTime=" + perTransportTimeUnit + "][perDistance=" + perDistanceUnit + "][perWaitingTimeUnit=" + perWaitingTimeUnit + "]"; return "[fixed=" + fix + "][perTime=" + perTransportTimeUnit + "][perDistance=" + perDistanceUnit + "][perWaitingTimeUnit=" + perWaitingTimeUnit + "]";
} }
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof VehicleCostParams)) return false;
VehicleCostParams that = (VehicleCostParams) o;
if (Double.compare(that.fix, fix) != 0) return false;
if (Double.compare(that.perTransportTimeUnit, perTransportTimeUnit) != 0) return false;
if (Double.compare(that.perDistanceUnit, perDistanceUnit) != 0) return false;
if (Double.compare(that.perWaitingTimeUnit, perWaitingTimeUnit) != 0) return false;
return Double.compare(that.perServiceTimeUnit, perServiceTimeUnit) == 0;
}
@Override
public int hashCode() {
int result;
long temp;
temp = Double.doubleToLongBits(fix);
result = (int) (temp ^ (temp >>> 32));
temp = Double.doubleToLongBits(perTransportTimeUnit);
result = 31 * result + (int) (temp ^ (temp >>> 32));
temp = Double.doubleToLongBits(perDistanceUnit);
result = 31 * result + (int) (temp ^ (temp >>> 32));
temp = Double.doubleToLongBits(perWaitingTimeUnit);
result = 31 * result + (int) (temp ^ (temp >>> 32));
temp = Double.doubleToLongBits(perServiceTimeUnit);
result = 31 * result + (int) (temp ^ (temp >>> 32));
return result;
}
} }
/** /**
@ -299,32 +330,30 @@ public class VehicleTypeImpl implements VehicleType {
} }
@Override @Override
public int hashCode() { public boolean equals(Object o) {
final int prime = 31; if (this == o) return true;
int result = 1; if (!(o instanceof VehicleTypeImpl)) return false;
result = prime * result
+ ((typeId == null) ? 0 : typeId.hashCode()); VehicleTypeImpl that = (VehicleTypeImpl) o;
return result;
if (Double.compare(that.maxVelocity, maxVelocity) != 0) return false;
if (!typeId.equals(that.typeId)) return false;
if (profile != null ? !profile.equals(that.profile) : that.profile != null) return false;
if (!vehicleCostParams.equals(that.vehicleCostParams)) return false;
return capacityDimensions.equals(that.capacityDimensions);
} }
/**
* Two vehicle-types are equal if they have the same vehicleId.
*/
@Override @Override
public boolean equals(Object obj) { public int hashCode() {
if (this == obj) int result;
return true; long temp;
if (obj == null) result = typeId.hashCode();
return false; result = 31 * result + (profile != null ? profile.hashCode() : 0);
if (getClass() != obj.getClass()) result = 31 * result + vehicleCostParams.hashCode();
return false; result = 31 * result + capacityDimensions.hashCode();
VehicleTypeImpl other = (VehicleTypeImpl) obj; temp = Double.doubleToLongBits(maxVelocity);
if (typeId == null) { result = 31 * result + (int) (temp ^ (temp >>> 32));
if (other.typeId != null) return result;
return false;
} else if (!typeId.equals(other.typeId))
return false;
return true;
} }
private final String typeId; private final String typeId;

View file

@ -316,6 +316,17 @@ public class VehicleRoutingProblemTest {
builder.addVehicle(vehicle2); builder.addVehicle(vehicle2);
} }
@Test(expected = IllegalArgumentException.class)
public void whenAddingVehicleTypesWithSameIdButDifferentCosts_itShouldThrowException() {
VehicleRoutingProblem.Builder builder = VehicleRoutingProblem.Builder.newInstance();
VehicleType type1 = VehicleTypeImpl.Builder.newInstance("type").build();
VehicleType type2 = VehicleTypeImpl.Builder.newInstance("type").setCostPerServiceTime(2d).build();
VehicleImpl vehicle1 = VehicleImpl.Builder.newInstance("v1").setStartLocation(Location.newInstance("loc")).setType(type1).build();
VehicleImpl vehicle2 = VehicleImpl.Builder.newInstance("v2").setStartLocation(Location.newInstance("loc")).setType(type2).build();
builder.addVehicle(vehicle1);
builder.addVehicle(vehicle2);
}
@Test(expected = IllegalArgumentException.class) @Test(expected = IllegalArgumentException.class)
public void whenBuildingProblemWithSameBreakId_itShouldThrowException(){ public void whenBuildingProblemWithSameBreakId_itShouldThrowException(){
VehicleRoutingProblem.Builder builder = VehicleRoutingProblem.Builder.newInstance(); VehicleRoutingProblem.Builder builder = VehicleRoutingProblem.Builder.newInstance();

View file

@ -124,6 +124,7 @@ public class VehicleTypeImplTest {
VehicleTypeImpl type = VehicleTypeImpl.Builder.newInstance("type").setCostPerDistance(-10).build(); VehicleTypeImpl type = VehicleTypeImpl.Builder.newInstance("type").setCostPerDistance(-10).build();
} }
@Test
public void whenSettingPerDistanceCosts_itShouldBeSetCorrectly() { public void whenSettingPerDistanceCosts_itShouldBeSetCorrectly() {
VehicleTypeImpl type = VehicleTypeImpl.Builder.newInstance("type").setCostPerDistance(10).build(); VehicleTypeImpl type = VehicleTypeImpl.Builder.newInstance("type").setCostPerDistance(10).build();
assertEquals(10.0, type.getVehicleCostParams().perDistanceUnit, 0.0); assertEquals(10.0, type.getVehicleCostParams().perDistanceUnit, 0.0);
@ -166,4 +167,32 @@ public class VehicleTypeImplTest {
assertEquals(42, two.getUserData()); assertEquals(42, two.getUserData());
assertNull(three.getUserData()); assertNull(three.getUserData());
} }
@Test
public void typesShouldBeEqual() {
VehicleType one = VehicleTypeImpl.Builder.newInstance("type").setFixedCost(100).build();
VehicleType two = VehicleTypeImpl.Builder.newInstance("type").setFixedCost(100).build();
assertTrue(one.equals(two));
}
@Test
public void typesShouldBeNotEqual() {
VehicleType one = VehicleTypeImpl.Builder.newInstance("type").build();
VehicleType two = VehicleTypeImpl.Builder.newInstance("type").setFixedCost(100).build();
assertFalse(one.equals(two));
}
@Test
public void typesShouldBeNotEqual2() {
VehicleType one = VehicleTypeImpl.Builder.newInstance("type").addCapacityDimension(0, 10).build();
VehicleType two = VehicleTypeImpl.Builder.newInstance("type").addCapacityDimension(0, 20).build();
assertFalse(one.equals(two));
}
@Test
public void typesShouldBeEqual2() {
VehicleType one = VehicleTypeImpl.Builder.newInstance("type").addCapacityDimension(0, 10).build();
VehicleType two = VehicleTypeImpl.Builder.newInstance("type").addCapacityDimension(0, 10).build();
assertTrue(one.equals(two));
}
} }