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

remove Double.MAX_VALUE - related to #286

This commit is contained in:
oblonski 2016-11-08 11:20:51 +01:00
parent f8ea447cb9
commit f924293520
No known key found for this signature in database
GPG key ID: 179DE487285680D1
2 changed files with 170 additions and 84 deletions

View file

@ -35,32 +35,32 @@ final class JobInsertionConsideringFixCostsCalculator implements JobInsertionCos
private static final Logger logger = LoggerFactory.getLogger(JobInsertionConsideringFixCostsCalculator.class);
private final JobInsertionCostsCalculator standardServiceInsertion;
private final JobInsertionCostsCalculator standardInsertion;
private double weight_deltaFixCost = 0.5;
private double weightDeltaFixCost = 0.5;
private double solution_completeness_ratio = 0.5;
private double solutionCompletenessRatio = 0.5;
private RouteAndActivityStateGetter stateGetter;
public JobInsertionConsideringFixCostsCalculator(final JobInsertionCostsCalculator standardInsertionCalculator, RouteAndActivityStateGetter stateGetter) {
public JobInsertionConsideringFixCostsCalculator(final JobInsertionCostsCalculator standardCalculator, RouteAndActivityStateGetter stateGetter) {
super();
this.standardServiceInsertion = standardInsertionCalculator;
this.standardInsertion = standardCalculator;
this.stateGetter = stateGetter;
logger.debug("inialise {}", this);
logger.debug("initialise {}", this);
}
@Override
public InsertionData getInsertionData(final VehicleRoute currentRoute, final Job jobToInsert, final Vehicle newVehicle, double newVehicleDepartureTime, final Driver newDriver, final double bestKnownPrice) {
double fixcost_contribution = getFixCostContribution(currentRoute, jobToInsert, newVehicle);
if (fixcost_contribution > bestKnownPrice) {
double fixedCostContribution = getFixCostContribution(currentRoute, jobToInsert, newVehicle);
if (fixedCostContribution > bestKnownPrice) {
return InsertionData.createEmptyInsertionData();
}
InsertionData iData = standardServiceInsertion.getInsertionData(currentRoute, jobToInsert, newVehicle, newVehicleDepartureTime, newDriver, bestKnownPrice);
InsertionData iData = standardInsertion.getInsertionData(currentRoute, jobToInsert, newVehicle, newVehicleDepartureTime, newDriver, bestKnownPrice);
if (iData instanceof InsertionData.NoInsertionFound) {
return iData;
}
double totalInsertionCost = iData.getInsertionCost() + fixcost_contribution;
double totalInsertionCost = iData.getInsertionCost() + fixedCostContribution;
InsertionData insertionData = new InsertionData(totalInsertionCost, iData.getPickupInsertionIndex(), iData.getDeliveryInsertionIndex(), newVehicle, newDriver);
insertionData.setVehicleDepartureTime(newVehicleDepartureTime);
insertionData.getEvents().addAll(iData.getEvents());
@ -70,55 +70,42 @@ final class JobInsertionConsideringFixCostsCalculator implements JobInsertionCos
private double getFixCostContribution(final VehicleRoute currentRoute, final Job jobToInsert, final Vehicle newVehicle) {
Capacity currentMaxLoadInRoute = getCurrentMaxLoadInRoute(currentRoute);
double relFixCost = getDeltaRelativeFixCost(currentRoute, newVehicle, jobToInsert,currentMaxLoadInRoute);
double absFixCost = getDeltaAbsoluteFixCost(currentRoute, newVehicle, jobToInsert,currentMaxLoadInRoute);
double deltaFixCost = (1 - solution_completeness_ratio) * relFixCost + solution_completeness_ratio * absFixCost;
double fixcost_contribution = weight_deltaFixCost * solution_completeness_ratio * deltaFixCost;
return fixcost_contribution;
double absFixCost = getDeltaAbsoluteFixCost(currentRoute, newVehicle);
double deltaFixCost = (1 - solutionCompletenessRatio) * relFixCost + solutionCompletenessRatio * absFixCost;
return weightDeltaFixCost * solutionCompletenessRatio * deltaFixCost;
}
public void setWeightOfFixCost(double weight) {
weight_deltaFixCost = weight;
weightDeltaFixCost = weight;
logger.debug("set weightOfFixCostSaving to {}", weight);
}
@Override
public String toString() {
return "[name=calculatesServiceInsertionConsideringFixCost][weightOfFixedCostSavings=" + weight_deltaFixCost + "]";
return "[name=calculatesServiceInsertionConsideringFixCost][weightOfFixedCostSavings=" + weightDeltaFixCost + "]";
}
public void setSolutionCompletenessRatio(double ratio) {
solution_completeness_ratio = ratio;
solutionCompletenessRatio = ratio;
}
public double getSolutionCompletenessRatio() { return solution_completeness_ratio; }
public double getSolutionCompletenessRatio() { return solutionCompletenessRatio; }
private double getDeltaAbsoluteFixCost(VehicleRoute route, Vehicle newVehicle, Job job, Capacity currentMaxLoadInRoute) {
Capacity load = Capacity.addup(currentMaxLoadInRoute, job.getSize());
double currentFix = 0.0;
if (route.getVehicle() != null) {
if (!(route.getVehicle() instanceof VehicleImpl.NoVehicle)) {
currentFix += route.getVehicle().getType().getVehicleCostParams().fix;
}
}
if (!newVehicle.getType().getCapacityDimensions().isGreaterOrEqual(load)) {
return Double.MAX_VALUE;
private double getDeltaAbsoluteFixCost(VehicleRoute route, Vehicle newVehicle) {
double currentFix = 0d;
if (route.getVehicle() != null && !(route.getVehicle() instanceof VehicleImpl.NoVehicle)) {
currentFix = route.getVehicle().getType().getVehicleCostParams().fix;
}
return newVehicle.getType().getVehicleCostParams().fix - currentFix;
}
private double getDeltaRelativeFixCost(VehicleRoute route, Vehicle newVehicle, Job job, Capacity currentLoad) {
Capacity load = Capacity.addup(currentLoad, job.getSize());
double currentRelFix = 0.0;
if (route.getVehicle() != null) {
if (!(route.getVehicle() instanceof VehicleImpl.NoVehicle)) {
currentRelFix += route.getVehicle().getType().getVehicleCostParams().fix * Capacity.divide(currentLoad, route.getVehicle().getType().getCapacityDimensions());
double currentRelFix = 0d;
if (route.getVehicle() != null && !(route.getVehicle() instanceof VehicleImpl.NoVehicle)) {
currentRelFix = route.getVehicle().getType().getVehicleCostParams().fix * Capacity.divide(currentLoad, route.getVehicle().getType().getCapacityDimensions());
}
}
if (!newVehicle.getType().getCapacityDimensions().isGreaterOrEqual(load)) {
return Double.MAX_VALUE;
}
double relativeFixCost = newVehicle.getType().getVehicleCostParams().fix * (Capacity.divide(load, newVehicle.getType().getCapacityDimensions())) - currentRelFix;
return relativeFixCost;
return newVehicle.getType().getVehicleCostParams().fix * (Capacity.divide(load, newVehicle.getType().getCapacityDimensions())) - currentRelFix;
}
private Capacity getCurrentMaxLoadInRoute(VehicleRoute route) {

View file

@ -36,9 +36,11 @@ public class JobInsertionConsideringFixCostsCalculatorTest {
private JobInsertionConsideringFixCostsCalculator calc;
private Vehicle oVehicle;
private Vehicle small;
private Vehicle nVehicle;
private Vehicle medium;
private Vehicle large;
private Job job;
@ -52,18 +54,23 @@ public class JobInsertionConsideringFixCostsCalculatorTest {
job = mock(Job.class);
when(job.getSize()).thenReturn(Capacity.Builder.newInstance().addDimension(0, 50).build());
oVehicle = mock(Vehicle.class);
VehicleType oType = VehicleTypeImpl.Builder.newInstance("otype").addCapacityDimension(0, 50).setFixedCost(50.0).build();
when(oVehicle.getType()).thenReturn(oType);
small = mock(Vehicle.class);
VehicleType smallType = VehicleTypeImpl.Builder.newInstance("smallType").addCapacityDimension(0, 50).setFixedCost(50.0).build();
when(small.getType()).thenReturn(smallType);
nVehicle = mock(Vehicle.class);
VehicleType type = VehicleTypeImpl.Builder.newInstance("type").addCapacityDimension(0, 100).setFixedCost(100.0).build();
when(nVehicle.getType()).thenReturn(type);
medium = mock(Vehicle.class);
VehicleType mediumType = VehicleTypeImpl.Builder.newInstance("mediumType").addCapacityDimension(0, 100).setFixedCost(100.0).build();
when(medium.getType()).thenReturn(mediumType);
InsertionData iData = new InsertionData(0.0, 1, 1, nVehicle, null);
large = mock(Vehicle.class);
VehicleType largeType = VehicleTypeImpl.Builder.newInstance("largeType").addCapacityDimension(0, 400).setFixedCost(200.0).build();
when(large.getType()).thenReturn(largeType);
InsertionData iData = new InsertionData(0.0, 1, 1, medium, null);
route = mock(VehicleRoute.class);
when(jobInsertionCosts.getInsertionData(route, job, nVehicle, 0.0, null, Double.MAX_VALUE)).thenReturn(iData);
when(jobInsertionCosts.getInsertionData(route, job, medium, 0.0, null, Double.MAX_VALUE)).thenReturn(iData);
when(jobInsertionCosts.getInsertionData(route, job, large, 0.0, null, Double.MAX_VALUE)).thenReturn(new InsertionData(0.0, 1, 1, large, null));
stateGetter = mock(RouteAndActivityStateGetter.class);
when(stateGetter.getRouteState(route, InternalStates.MAXLOAD, Capacity.class)).thenReturn(Capacity.Builder.newInstance().build());
@ -76,7 +83,7 @@ public class JobInsertionConsideringFixCostsCalculatorTest {
calc.setSolutionCompletenessRatio(1.0);
calc.setWeightOfFixCost(1.0);
//(1.*absFix + 0.*relFix) * completeness * weight = (1.*100. + 0.*50.) * 1. * 1. = 100.
assertEquals(100., calc.getInsertionData(route, job, nVehicle, 0.0, null, Double.MAX_VALUE).getInsertionCost(), 0.01);
assertEquals(100., calc.getInsertionData(route, job, medium, 0.0, null, Double.MAX_VALUE).getInsertionCost(), 0.01);
}
@Test
@ -84,7 +91,7 @@ public class JobInsertionConsideringFixCostsCalculatorTest {
calc.setSolutionCompletenessRatio(0.0);
calc.setWeightOfFixCost(1.0);
//(0.*absFix + 1.*relFix) * completeness * weight = 0.
assertEquals(0., calc.getInsertionData(route, job, nVehicle, 0.0, null, Double.MAX_VALUE).getInsertionCost(), 0.01);
assertEquals(0., calc.getInsertionData(route, job, medium, 0.0, null, Double.MAX_VALUE).getInsertionCost(), 0.01);
}
@Test
@ -92,7 +99,7 @@ public class JobInsertionConsideringFixCostsCalculatorTest {
calc.setSolutionCompletenessRatio(0.5);
calc.setWeightOfFixCost(1.0);
//(0.5*absFix + 0.5*relFix) * 0.5 * 1. = (0.5*100+0.5*50)*0.5*1. = 37.5
assertEquals(37.5, calc.getInsertionData(route, job, nVehicle, 0.0, null, Double.MAX_VALUE).getInsertionCost(), 0.01);
assertEquals(37.5, calc.getInsertionData(route, job, medium, 0.0, null, Double.MAX_VALUE).getInsertionCost(), 0.01);
}
@Test
@ -100,7 +107,7 @@ public class JobInsertionConsideringFixCostsCalculatorTest {
calc.setSolutionCompletenessRatio(0.75);
calc.setWeightOfFixCost(1.0);
//(0.75*absFix + 0.25*relFix) * 0.75 * 1.= (0.75*100.+0.25*50.)*0.75*1. = 65.625
assertEquals(65.625, calc.getInsertionData(route, job, nVehicle, 0.0, null, Double.MAX_VALUE).getInsertionCost(), 0.01);
assertEquals(65.625, calc.getInsertionData(route, job, medium, 0.0, null, Double.MAX_VALUE).getInsertionCost(), 0.01);
}
@Test
@ -108,7 +115,7 @@ public class JobInsertionConsideringFixCostsCalculatorTest {
calc.setSolutionCompletenessRatio(1.0);
calc.setWeightOfFixCost(.5);
//(1.*absFix + 0.*relFix) * 1. * 0.5 = (1.*100. + 0.*50.) * 1. * 0.5 = 5.
assertEquals(50., calc.getInsertionData(route, job, nVehicle, 0.0, null, Double.MAX_VALUE).getInsertionCost(), 0.01);
assertEquals(50., calc.getInsertionData(route, job, medium, 0.0, null, Double.MAX_VALUE).getInsertionCost(), 0.01);
}
@Test
@ -116,7 +123,7 @@ public class JobInsertionConsideringFixCostsCalculatorTest {
calc.setSolutionCompletenessRatio(0.0);
calc.setWeightOfFixCost(.5);
//(0.*absFix + 1.*relFix) * 0. * .5 = 0.
assertEquals(0., calc.getInsertionData(route, job, nVehicle, 0.0, null, Double.MAX_VALUE).getInsertionCost(), 0.01);
assertEquals(0., calc.getInsertionData(route, job, medium, 0.0, null, Double.MAX_VALUE).getInsertionCost(), 0.01);
}
@Test
@ -124,7 +131,7 @@ public class JobInsertionConsideringFixCostsCalculatorTest {
calc.setSolutionCompletenessRatio(0.5);
calc.setWeightOfFixCost(.5);
//(0.5*absFix + 0.5*relFix) * 0.5 * 0.= (0.5*100+0.5*50)*0.5*0.5 = 18.75
assertEquals(18.75, calc.getInsertionData(route, job, nVehicle, 0.0, null, Double.MAX_VALUE).getInsertionCost(), 0.01);
assertEquals(18.75, calc.getInsertionData(route, job, medium, 0.0, null, Double.MAX_VALUE).getInsertionCost(), 0.01);
}
@Test
@ -132,98 +139,98 @@ public class JobInsertionConsideringFixCostsCalculatorTest {
calc.setSolutionCompletenessRatio(0.75);
calc.setWeightOfFixCost(0.5);
//(0.75*absFix + 0.25*relFix) * 0.75 * 0.5 = (0.75*100.+0.25*50.)*0.75*0.5 = 32.8125
assertEquals(32.8125, calc.getInsertionData(route, job, nVehicle, 0.0, null, Double.MAX_VALUE).getInsertionCost(), 0.01);
assertEquals(32.8125, calc.getInsertionData(route, job, medium, 0.0, null, Double.MAX_VALUE).getInsertionCost(), 0.01);
}
@Test
public void whenOldVehicleIsNotNullAndSolutionComplete_itShouldReturnHalfOfFixedCostsOfNewVehicle() {
calc.setSolutionCompletenessRatio(1.0);
calc.setWeightOfFixCost(1.0);
when(route.getVehicle()).thenReturn(oVehicle);
when(route.getVehicle()).thenReturn(small);
//(1.*absFix + 0.*relFix) * completeness * weight = (1.*(100.-50.) + 0.*(50.-0.)) * 1. * 1. = 50.
assertEquals(50., calc.getInsertionData(route, job, nVehicle, 0.0, null, Double.MAX_VALUE).getInsertionCost(), 0.01);
assertEquals(50., calc.getInsertionData(route, job, medium, 0.0, null, Double.MAX_VALUE).getInsertionCost(), 0.01);
}
@Test
public void whenOldVehicleIsNotNullAndSolutionIs0PercentComplete_itShouldReturnNoFixedCosts() {
calc.setSolutionCompletenessRatio(0.0);
calc.setWeightOfFixCost(1.0);
when(route.getVehicle()).thenReturn(oVehicle);
when(route.getVehicle()).thenReturn(small);
//(0.*absFix + 1.*relFix) * completeness * weight = 0.
assertEquals(0., calc.getInsertionData(route, job, nVehicle, 0.0, null, Double.MAX_VALUE).getInsertionCost(), 0.01);
assertEquals(0., calc.getInsertionData(route, job, medium, 0.0, null, Double.MAX_VALUE).getInsertionCost(), 0.01);
}
@Test
public void whenOldVehicleIsNotNullAndSolutionIs50PercentComplete_itShouldCorrectVal() {
calc.setSolutionCompletenessRatio(0.5);
calc.setWeightOfFixCost(1.0);
when(route.getVehicle()).thenReturn(oVehicle);
when(route.getVehicle()).thenReturn(small);
//(0.5*absFix + 0.5*relFix) * 0.5 * 1. = (0.5*(100-50)+0.5*(50-0))*0.5*1. = 25.
assertEquals(25., calc.getInsertionData(route, job, nVehicle, 0.0, null, Double.MAX_VALUE).getInsertionCost(), 0.01);
assertEquals(25., calc.getInsertionData(route, job, medium, 0.0, null, Double.MAX_VALUE).getInsertionCost(), 0.01);
}
@Test
public void whenOldVehicleIsNotNullAndSolutionIs75PercentComplete_itShouldReturnCorrectVal() {
calc.setSolutionCompletenessRatio(0.75);
calc.setWeightOfFixCost(1.0);
when(route.getVehicle()).thenReturn(oVehicle);
when(route.getVehicle()).thenReturn(small);
//(0.75*absFix + 0.25*relFix) * 0.75 * 1.= (0.75*(100.-50.)+0.25*(50.-0.))*0.75*1. = 37.5
assertEquals(37.5, calc.getInsertionData(route, job, nVehicle, 0.0, null, Double.MAX_VALUE).getInsertionCost(), 0.01);
assertEquals(37.5, calc.getInsertionData(route, job, medium, 0.0, null, Double.MAX_VALUE).getInsertionCost(), 0.01);
}
@Test
public void whenOldVehicleIsNotNullAndSolutionCompleteAndWeightIs05_itShouldReturnCorrectVal() {
calc.setSolutionCompletenessRatio(1.0);
calc.setWeightOfFixCost(.5);
when(route.getVehicle()).thenReturn(oVehicle);
when(route.getVehicle()).thenReturn(small);
//(1.*absFix + 0.*relFix) * 1. * 0.5 = (1.*(100.-50.) + 0.*(50.-0.)) * 1. * 0.5 = 25.
assertEquals(25., calc.getInsertionData(route, job, nVehicle, 0.0, null, Double.MAX_VALUE).getInsertionCost(), 0.01);
assertEquals(25., calc.getInsertionData(route, job, medium, 0.0, null, Double.MAX_VALUE).getInsertionCost(), 0.01);
}
@Test
public void whenOldVehicleIsNotNullAndSolutionIs0PercentCompleteAndWeightIs05_itShouldReturnCorrectVal() {
calc.setSolutionCompletenessRatio(0.0);
calc.setWeightOfFixCost(.5);
when(route.getVehicle()).thenReturn(oVehicle);
when(route.getVehicle()).thenReturn(small);
//(0.*absFix + 1.*relFix) * 0. * .5 = 0.
assertEquals(0., calc.getInsertionData(route, job, nVehicle, 0.0, null, Double.MAX_VALUE).getInsertionCost(), 0.01);
assertEquals(0., calc.getInsertionData(route, job, medium, 0.0, null, Double.MAX_VALUE).getInsertionCost(), 0.01);
}
@Test
public void whenOldVehicleIsNotNullAndSolutionIs50PercentCompleteAndWeightIs05_itShouldReturnCorrectVal() {
calc.setSolutionCompletenessRatio(0.5);
calc.setWeightOfFixCost(.5);
when(route.getVehicle()).thenReturn(oVehicle);
when(route.getVehicle()).thenReturn(small);
//(0.5*absFix + 0.5*relFix) * 0.5 * 0.= (0.5*(100-50)+0.5*(50-0))*0.5*0.5 = 12.5
assertEquals(12.5, calc.getInsertionData(route, job, nVehicle, 0.0, null, Double.MAX_VALUE).getInsertionCost(), 0.01);
assertEquals(12.5, calc.getInsertionData(route, job, medium, 0.0, null, Double.MAX_VALUE).getInsertionCost(), 0.01);
}
@Test
public void whenOldVehicleIsNotNullAndSolutionIs75PercentCompleteAndWeightIs05_itShouldReturnCorrectVal() {
calc.setSolutionCompletenessRatio(0.75);
calc.setWeightOfFixCost(0.5);
when(route.getVehicle()).thenReturn(oVehicle);
when(route.getVehicle()).thenReturn(small);
//(0.75*absFix + 0.25*relFix) * 0.75 * 0.5 = (0.75*(100.-50.)+0.25*(50.-0.))*0.75*0.5 = 18.75
assertEquals(18.75, calc.getInsertionData(route, job, nVehicle, 0.0, null, Double.MAX_VALUE).getInsertionCost(), 0.01);
assertEquals(18.75, calc.getInsertionData(route, job, medium, 0.0, null, Double.MAX_VALUE).getInsertionCost(), 0.01);
}
@Test
public void whenOldVehicleIsNotNullAndCurrentLoadIs25AndSolutionIs50PercentCompleteAndWeightIs05_itShouldReturnCorrectVal() {
calc.setSolutionCompletenessRatio(0.5);
calc.setWeightOfFixCost(.5);
when(route.getVehicle()).thenReturn(oVehicle);
when(route.getVehicle()).thenReturn(small);
when(stateGetter.getRouteState(route, InternalStates.MAXLOAD, Capacity.class)).thenReturn(Capacity.Builder.newInstance().addDimension(0, 25).build());
//(0.5*absFix + 0.5*relFix) * 0.5 * 0.= (0.5*(100-50)+0.5*(75-25))*0.5*0.5 = 12.5
assertEquals(12.5, calc.getInsertionData(route, job, nVehicle, 0.0, null, Double.MAX_VALUE).getInsertionCost(), 0.01);
assertEquals(12.5, calc.getInsertionData(route, job, medium, 0.0, null, Double.MAX_VALUE).getInsertionCost(), 0.01);
}
@Test
public void whenOldVehicleIsNotNullAndCurrentLoadIs25AndSolutionIs75PercentCompleteAndWeightIs05_itShouldReturnCorrectVal() {
calc.setSolutionCompletenessRatio(0.75);
calc.setWeightOfFixCost(0.5);
when(route.getVehicle()).thenReturn(oVehicle);
when(route.getVehicle()).thenReturn(small);
//(0.75*absFix + 0.25*relFix) * 0.75 * 0.5 = (0.75*(100.-50.)+0.25*(75.-25.))*0.75*0.5 = 18.75
assertEquals(18.75, calc.getInsertionData(route, job, nVehicle, 0.0, null, Double.MAX_VALUE).getInsertionCost(), 0.01);
assertEquals(18.75, calc.getInsertionData(route, job, medium, 0.0, null, Double.MAX_VALUE).getInsertionCost(), 0.01);
}
@Test
@ -234,12 +241,12 @@ public class JobInsertionConsideringFixCostsCalculatorTest {
when(job.getSize()).thenReturn(Capacity.Builder.newInstance().addDimension(0, 50).addDimension(1, 0).build());
VehicleType oType = VehicleTypeImpl.Builder.newInstance("otype").addCapacityDimension(0, 50).addCapacityDimension(1, 100).setFixedCost(50.0).build();
when(oVehicle.getType()).thenReturn(oType);
when(small.getType()).thenReturn(oType);
VehicleType type = VehicleTypeImpl.Builder.newInstance("type").addCapacityDimension(0, 100).addCapacityDimension(1, 400).setFixedCost(100.0).build();
when(nVehicle.getType()).thenReturn(type);
when(medium.getType()).thenReturn(type);
when(route.getVehicle()).thenReturn(oVehicle);
when(route.getVehicle()).thenReturn(small);
when(stateGetter.getRouteState(route, InternalStates.MAXLOAD, Capacity.class)).thenReturn(Capacity.Builder.newInstance().addDimension(0, 25).addDimension(1, 100).build());
//(0.5*absFix + 0.5*relFix) * 0.5 * 0.= (0.5*(100-50)+0.5*(75-25))*0.5*0.5 = 12.5
/*
@ -249,7 +256,99 @@ public class JobInsertionConsideringFixCostsCalculatorTest {
* = (0.5*(100-50)+0.5*((75/100+100/400)/2.*100 - ((25/50+100/100)/2.*50.)))*0.5*0.5
* = (0.5*(100-50)+0.5*12.5)*0.5*0.5 = 7.8125
*/
assertEquals(7.8125, calc.getInsertionData(route, job, nVehicle, 0.0, null, Double.MAX_VALUE).getInsertionCost(), 0.01);
assertEquals(7.8125, calc.getInsertionData(route, job, medium, 0.0, null, Double.MAX_VALUE).getInsertionCost(), 0.01);
}
@Test
public void whenOldVehicleIsMoreExpensive() {
calc.setSolutionCompletenessRatio(1);
calc.setWeightOfFixCost(1);
when(job.getSize()).thenReturn(Capacity.Builder.newInstance().addDimension(0, 50).addDimension(1, 0).build());
VehicleType oType = VehicleTypeImpl.Builder.newInstance("otype").addCapacityDimension(0, 50).addCapacityDimension(1, 100).setFixedCost(50.0).build();
when(medium.getType()).thenReturn(oType);
VehicleType type = VehicleTypeImpl.Builder.newInstance("type").addCapacityDimension(0, 100).addCapacityDimension(1, 400).setFixedCost(100.0).build();
when(small.getType()).thenReturn(type);
when(route.getVehicle()).thenReturn(small);
when(stateGetter.getRouteState(route, InternalStates.MAXLOAD, Capacity.class)).thenReturn(Capacity.Builder.newInstance().addDimension(0, 25).addDimension(1, 100).build());
//(0.5*absFix + 0.5*relFix) * 0.5 * 0.= (0.5*(100-50)+0.5*(75-25))*0.5*0.5 = 12.5
/*
* (0.5*(100-50)+0.5*(
* relFixNew - relFixOld = (75/100+100/400)/2.*100 - ((25/50+100/100)/2.*50.) =
* )*0.5*0.5
* = (0.5*(100-50)+0.5*((75/100+100/400)/2.*100 - ((25/50+100/100)/2.*50.)))*0.5*0.5
* = (0.5*(100-50)+0.5*12.5)*0.5*0.5 = 7.8125
*/
double insertionCost = calc.getInsertionData(route, job, medium, 0.0, null, Double.MAX_VALUE).getInsertionCost();
assertEquals(-50d, insertionCost, 0.01);
}
@Test
public void smallVSMediumAbsCosts() {
calc.setSolutionCompletenessRatio(1);
calc.setWeightOfFixCost(1);
when(route.getVehicle()).thenReturn(small);
double insertionCost = calc.getInsertionData(route, job, medium, 0.0, null, Double.MAX_VALUE).getInsertionCost();
assertEquals(50d, insertionCost, 0.01);
}
@Test
public void smallVSLargeAbsCosts() {
calc.setSolutionCompletenessRatio(1);
calc.setWeightOfFixCost(1);
when(route.getVehicle()).thenReturn(small);
double insertionCost = calc.getInsertionData(route, job, large, 0.0, null, Double.MAX_VALUE).getInsertionCost();
assertEquals(150d, insertionCost, 0.01);
}
@Test
public void largeVSMediumAbsCosts() {
calc.setSolutionCompletenessRatio(1);
calc.setWeightOfFixCost(1);
when(route.getVehicle()).thenReturn(large);
double insertionCost = calc.getInsertionData(route, job, medium, 0.0, null, Double.MAX_VALUE).getInsertionCost();
assertEquals(-100d, insertionCost, 0.01);
}
@Test
public void mediumVSLargeAbsCosts() {
calc.setSolutionCompletenessRatio(1);
calc.setWeightOfFixCost(1);
when(route.getVehicle()).thenReturn(medium);
double insertionCost = calc.getInsertionData(route, job, large, 0.0, null, Double.MAX_VALUE).getInsertionCost();
assertEquals(100d, insertionCost, 0.01);
}
@Test
public void whenOldVehicleIsMoreExpensive2() {
calc.setSolutionCompletenessRatio(0.1);
calc.setWeightOfFixCost(1);
when(job.getSize()).thenReturn(Capacity.Builder.newInstance().addDimension(0, 50).addDimension(1, 0).build());
VehicleType oType = VehicleTypeImpl.Builder.newInstance("otype").addCapacityDimension(0, 50).addCapacityDimension(1, 100).setFixedCost(50.0).build();
when(medium.getType()).thenReturn(oType);
VehicleType type = VehicleTypeImpl.Builder.newInstance("type").addCapacityDimension(0, 100).addCapacityDimension(1, 400).setFixedCost(100.0).build();
when(small.getType()).thenReturn(type);
when(route.getVehicle()).thenReturn(small);
when(stateGetter.getRouteState(route, InternalStates.MAXLOAD, Capacity.class)).thenReturn(Capacity.Builder.newInstance().addDimension(0, 25).addDimension(1, 100).build());
//(0.5*absFix + 0.5*relFix) * 0.5 * 0.= (0.5*(100-50)+0.5*(75-25))*0.5*0.5 = 12.5
/*
* (0.5*(100-50)+0.5*(
* relFixNew - relFixOld = (75/100+100/400)/2.*100 - ((25/50+100/100)/2.*50.) =
* )*0.5*0.5
* = (0.5*(100-50)+0.5*((75/100+100/400)/2.*100 - ((25/50+100/100)/2.*50.)))*0.5*0.5
* = (0.5*(100-50)+0.5*12.5)*0.5*0.5 = 7.8125
*/
double insertionCost = calc.getInsertionData(route, job, medium, 0.0, null, Double.MAX_VALUE).getInsertionCost();
assertEquals(-50d, insertionCost, 0.01);
}
@Test
@ -259,16 +358,16 @@ public class JobInsertionConsideringFixCostsCalculatorTest {
when(job.getSize()).thenReturn(Capacity.Builder.newInstance().addDimension(0, 50).addDimension(1, 0).build());
VehicleType oType = VehicleTypeImpl.Builder.newInstance("otype").addCapacityDimension(0, 50).addCapacityDimension(1, 100).setFixedCost(50.0).build();
when(oVehicle.getType()).thenReturn(oType);
when(small.getType()).thenReturn(oType);
VehicleType type = VehicleTypeImpl.Builder.newInstance("type").addCapacityDimension(0, 100).addCapacityDimension(1, 400).setFixedCost(100.0).build();
when(nVehicle.getType()).thenReturn(type);
when(medium.getType()).thenReturn(type);
when(route.getVehicle()).thenReturn(oVehicle);
when(route.getVehicle()).thenReturn(small);
when(stateGetter.getRouteState(route, InternalStates.MAXLOAD, Capacity.class)).thenReturn(Capacity.Builder.newInstance().addDimension(0, 25).addDimension(1, 100).build());
//(0.75*absFix + 0.25*relFix) * 0.75 * 0.5 = (0.75*(100.-50.)+0.25*12.5)*0.75*0.5 = 15.234375
assertEquals(15.234375, calc.getInsertionData(route, job, nVehicle, 0.0, null, Double.MAX_VALUE).getInsertionCost(), 0.01);
assertEquals(15.234375, calc.getInsertionData(route, job, medium, 0.0, null, Double.MAX_VALUE).getInsertionCost(), 0.01);
}