From 0e42ac31b890b953d205b69fb97d13c4a1c74436 Mon Sep 17 00:00:00 2001 From: oblonski <4sschroeder@gmail.com> Date: Mon, 3 Mar 2014 20:12:27 +0100 Subject: [PATCH] adjusted algorithm.recreate.JobInsertionConsideringFixCostsCalculator to deal with multiple capacities --- ...nsertionConsideringFixCostsCalculator.java | 22 ++++++---- .../java/jsprit/core/problem/Capacity.java | 30 ++++++++++++++ ...tionConsideringFixCostsCalculatorTest.java | 8 ++++ .../jsprit/core/problem/CapacityTest.java | 41 +++++++++++++++++++ 4 files changed, 92 insertions(+), 9 deletions(-) create mode 100644 jsprit-core/src/test/java/jsprit/core/algorithm/recreate/JobInsertionConsideringFixCostsCalculatorTest.java diff --git a/jsprit-core/src/main/java/jsprit/core/algorithm/recreate/JobInsertionConsideringFixCostsCalculator.java b/jsprit-core/src/main/java/jsprit/core/algorithm/recreate/JobInsertionConsideringFixCostsCalculator.java index 5537649e..b01bd7a5 100644 --- a/jsprit-core/src/main/java/jsprit/core/algorithm/recreate/JobInsertionConsideringFixCostsCalculator.java +++ b/jsprit-core/src/main/java/jsprit/core/algorithm/recreate/JobInsertionConsideringFixCostsCalculator.java @@ -17,6 +17,7 @@ package jsprit.core.algorithm.recreate; import jsprit.core.algorithm.recreate.InsertionData.NoInsertionFound; +import jsprit.core.problem.Capacity; import jsprit.core.problem.driver.Driver; import jsprit.core.problem.job.Job; import jsprit.core.problem.solution.route.VehicleRoute; @@ -83,37 +84,40 @@ final class JobInsertionConsideringFixCostsCalculator implements JobInsertionCos } private double getDeltaAbsoluteFixCost(VehicleRoute route, Vehicle newVehicle, Job job) { - double load = getCurrentMaxLoadInRoute(route) + job.getCapacityDemand(); + Capacity load = Capacity.addup(getCurrentMaxLoadInRoute(route), job.getSize()); +// double load = getCurrentMaxLoadInRoute(route) + job.getCapacityDemand(); double currentFix = 0.0; if(route.getVehicle() != null){ if(!(route.getVehicle() instanceof NoVehicle)){ currentFix += route.getVehicle().getType().getVehicleCostParams().fix; } } - if(newVehicle.getCapacity() < load){ + if(!newVehicle.getType().getCapacityDimensions().isGreaterOrEqual(load)){ return Double.MAX_VALUE; } return newVehicle.getType().getVehicleCostParams().fix - currentFix; } private double getDeltaRelativeFixCost(VehicleRoute route, Vehicle newVehicle, Job job) { - int currentLoad = getCurrentMaxLoadInRoute(route); - double load = currentLoad + job.getCapacityDemand(); + Capacity currentLoad = getCurrentMaxLoadInRoute(route); +// int currentLoad = getCurrentMaxLoadInRoute(route); + Capacity load = Capacity.addup(currentLoad, job.getSize()); +// double load = currentLoad + job.getCapacityDemand(); double currentRelFix = 0.0; if(route.getVehicle() != null){ if(!(route.getVehicle() instanceof NoVehicle)){ - currentRelFix += route.getVehicle().getType().getVehicleCostParams().fix*currentLoad/route.getVehicle().getCapacity(); + currentRelFix += route.getVehicle().getType().getVehicleCostParams().fix * Capacity.divide(currentLoad, route.getVehicle().getType().getCapacityDimensions()); } } - if(newVehicle.getCapacity() < load){ + if(!newVehicle.getType().getCapacityDimensions().isGreaterOrEqual(load)){ return Double.MAX_VALUE; } - double relativeFixCost = newVehicle.getType().getVehicleCostParams().fix*(load/newVehicle.getCapacity()) - currentRelFix; + double relativeFixCost = newVehicle.getType().getVehicleCostParams().fix* (Capacity.divide(load, newVehicle.getType().getCapacityDimensions())) - currentRelFix; return relativeFixCost; } - private int getCurrentMaxLoadInRoute(VehicleRoute route) { - return (int) stateGetter.getRouteState(route, StateFactory.MAXLOAD).toDouble(); + private Capacity getCurrentMaxLoadInRoute(VehicleRoute route) { + return stateGetter.getRouteState(route, StateFactory.MAXLOAD, Capacity.class); } } diff --git a/jsprit-core/src/main/java/jsprit/core/problem/Capacity.java b/jsprit-core/src/main/java/jsprit/core/problem/Capacity.java index 3b07770c..6bdd2e35 100644 --- a/jsprit-core/src/main/java/jsprit/core/problem/Capacity.java +++ b/jsprit-core/src/main/java/jsprit/core/problem/Capacity.java @@ -67,6 +67,36 @@ public class Capacity { return capacityBuilder.build(); } + /** + * Divides every dimension of numerator capacity by the corresponding dimension of denominator capacity, + * , and averages each quotient. + * + *
If both nominator.get(i) and denominator.get(i) equal to 0, dimension i is ignored. + *
If both capacities are have only dimensions with dimensionVal=0, it returns 0.0
+ * @param numerator
+ * @param denominator
+ * @return
+ * @throws IllegalStateException if numerator.get(i) != 0 and denominator.get(i) == 0
+ */
+ public static double divide(Capacity numerator, Capacity denominator){
+ int nuOfDimensions = 0;
+ double sumQuotients = 0.0;
+ for(int index=0;index