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

Merge branch 'master' into vrph

This commit is contained in:
oblonski 2014-01-14 17:59:05 -05:00
commit e4e556786b
2 changed files with 53 additions and 2 deletions

View file

@ -198,6 +198,8 @@ public class VehicleRoutingProblem {
private double penaltyFactor = 1.0; private double penaltyFactor = 1.0;
private Double penaltyFixedCosts = null;
/** /**
* @deprecated use static method .newInstance() instead * @deprecated use static method .newInstance() instead
*/ */
@ -378,10 +380,14 @@ public class VehicleRoutingProblem {
} }
} }
for(Vehicle v : uniqueVehicles){ for(Vehicle v : uniqueVehicles){
double fixed = v.getType().getVehicleCostParams().fix * penaltyFactor;
if(penaltyFixedCosts!=null){
fixed = penaltyFixedCosts;
}
VehicleTypeImpl t = VehicleTypeImpl.Builder.newInstance(v.getType().getTypeId(), v.getCapacity()) VehicleTypeImpl t = VehicleTypeImpl.Builder.newInstance(v.getType().getTypeId(), v.getCapacity())
.setCostPerDistance(penaltyFactor*v.getType().getVehicleCostParams().perDistanceUnit) .setCostPerDistance(penaltyFactor*v.getType().getVehicleCostParams().perDistanceUnit)
.setCostPerTime(penaltyFactor*v.getType().getVehicleCostParams().perTimeUnit) .setCostPerTime(penaltyFactor*v.getType().getVehicleCostParams().perTimeUnit)
.setFixedCost(penaltyFactor*v.getType().getVehicleCostParams().fix) .setFixedCost(fixed)
.build(); .build();
PenaltyVehicleType penType = new PenaltyVehicleType(t,penaltyFactor); PenaltyVehicleType penType = new PenaltyVehicleType(t,penaltyFactor);
String vehicleId = "penaltyVehicle_" + v.getLocationId() + "_" + t.getTypeId(); String vehicleId = "penaltyVehicle_" + v.getLocationId() + "_" + t.getTypeId();
@ -453,7 +459,8 @@ public class VehicleRoutingProblem {
} }
/** /**
* Adds penaltyVehicles, i.e. for every unique vehicle-location and type combination a penalty-vehicle is constructed having penaltyFactor times higher fixed and variable costs. * Adds penaltyVehicles, i.e. for every unique vehicle-location and type combination a penalty-vehicle is constructed having penaltyFactor times higher fixed and variable costs
* (see .addPenaltyVehicles(double penaltyFactor, double penaltyFixedCosts) if fixed costs = 0.0).
* *
* <p>This only makes sense for FleetSize.FINITE. Thus, penaltyVehicles are only added if is FleetSize.FINITE. * <p>This only makes sense for FleetSize.FINITE. Thus, penaltyVehicles are only added if is FleetSize.FINITE.
* <p>The id of penaltyVehicles is constructed as follows vehicleId = "penaltyVehicle" + "_" + {locationId} + "_" + {typeId}. * <p>The id of penaltyVehicles is constructed as follows vehicleId = "penaltyVehicle" + "_" + {locationId} + "_" + {typeId}.
@ -468,6 +475,24 @@ public class VehicleRoutingProblem {
return this; return this;
} }
/**
* Adds penaltyVehicles, i.e. for every unique vehicle-location and type combination a penalty-vehicle is constructed having penaltyFactor times higher fixed and variable costs.
* <p>This method takes penaltyFixedCosts as absolute value in contrary to the method without penaltyFixedCosts where fixedCosts is the product of penaltyFactor and typeFixedCosts.
* <p>This only makes sense for FleetSize.FINITE. Thus, penaltyVehicles are only added if is FleetSize.FINITE.
* <p>The id of penaltyVehicles is constructed as follows vehicleId = "penaltyVehicle" + "_" + {locationId} + "_" + {typeId}.
* <p>By default: no penalty-vehicles are added
*
* @param penaltyFactor
* @param penaltyFixedCosts which is an absolute penaltyValue (in contrary to penaltyFactor)
* @return this builder
*/
public Builder addPenaltyVehicles(double penaltyFactor, double penaltyFixedCosts){
this.addPenaltyVehicles = true;
this.penaltyFactor = penaltyFactor;
this.penaltyFixedCosts = penaltyFixedCosts;
return this;
}
/** /**
* Returns an unmodifiable collection of already added jobs. * Returns an unmodifiable collection of already added jobs.
* *

View file

@ -391,6 +391,32 @@ public class VehicleRoutingProblemTest {
} }
@Test
public void whenSettingAddPenaltyVehicleOptionsWithAbsoluteFixedCostsAndTwoVehiclesWithSameLocationAndType_onePenaltyVehicleIsAddedWithTheCorrectPenaltyFixedCosts(){
VehicleRoutingProblem.Builder builder = VehicleRoutingProblem.Builder.newInstance();
VehicleType type = VehicleTypeImpl.Builder.newInstance("type", 0).build();
Vehicle vehicle = VehicleImpl.Builder.newInstance("v").setLocationId("loc").setType(type).build();
Vehicle vehicle2 = VehicleImpl.Builder.newInstance("v2").setLocationId("loc").setType(type).build();
builder.addVehicle(vehicle);
builder.addVehicle(vehicle2);
builder.setFleetSize(FleetSize.FINITE);
builder.addPenaltyVehicles(3.0,10000);
VehicleRoutingProblem vrp = builder.build();
assertEquals(3,vrp.getVehicles().size());
double fix = 0.0;
for(Vehicle v : vrp.getVehicles()){
if(v.getId().equals("penaltyVehicle_loc_type")) {
fix = v.getType().getVehicleCostParams().fix;
}
}
assertEquals(10000,fix,0.01);
}
@Test @Test
public void whenSettingAddPenaltyVehicleOptionsAndTwoVehiclesWithDiffLocationAndType_twoPenaltyVehicleIsAdded(){ public void whenSettingAddPenaltyVehicleOptionsAndTwoVehiclesWithDiffLocationAndType_twoPenaltyVehicleIsAdded(){
VehicleRoutingProblem.Builder builder = VehicleRoutingProblem.Builder.newInstance(); VehicleRoutingProblem.Builder builder = VehicleRoutingProblem.Builder.newInstance();