diff --git a/jsprit-core/src/main/java/jsprit/core/analysis/SolutionAnalyser.java b/jsprit-core/src/main/java/jsprit/core/analysis/SolutionAnalyser.java index 4754e340..7fe18e59 100644 --- a/jsprit-core/src/main/java/jsprit/core/analysis/SolutionAnalyser.java +++ b/jsprit-core/src/main/java/jsprit/core/analysis/SolutionAnalyser.java @@ -22,6 +22,7 @@ import jsprit.core.algorithm.state.*; import jsprit.core.problem.Capacity; import jsprit.core.problem.Location; import jsprit.core.problem.VehicleRoutingProblem; +import jsprit.core.problem.cost.TransportDistance; import jsprit.core.problem.cost.VehicleRoutingTransportCosts; import jsprit.core.problem.solution.SolutionCostCalculator; import jsprit.core.problem.solution.VehicleRoutingProblemSolution; @@ -52,6 +53,10 @@ public class SolutionAnalyser { private final static String LOAD_DELIVERED = "load-delivered"; + /** + * @deprecated use TransportDistance instead + */ + @Deprecated public static interface DistanceCalculator { public double getDistance(Location from, Location to); @@ -503,6 +508,14 @@ public class SolutionAnalyser { private VehicleRoutingProblemSolution solution; + /** + * + * @param vrp + * @param solution + * @param distanceCalculator + * @deprecated use SolutionAnalyser(VehicleRoutingProblem vrp, VehicleRoutingProblemSolution solution, final TransportDistance distanceCalculator) instead + */ + @Deprecated public SolutionAnalyser(VehicleRoutingProblem vrp, VehicleRoutingProblemSolution solution, DistanceCalculator distanceCalculator) { this.vrp = vrp; this.solution = solution; @@ -512,6 +525,20 @@ public class SolutionAnalyser { refreshStates(); } + public SolutionAnalyser(VehicleRoutingProblem vrp, VehicleRoutingProblemSolution solution, final TransportDistance distanceCalculator) { + this.vrp = vrp; + this.solution = solution; + this.distanceCalculator = new DistanceCalculator() { + @Override + public double getDistance(Location from, Location to) { + return distanceCalculator.getDistance(from,to); + } + }; + initialise(); + this.solutionCostCalculator = new VariablePlusFixedSolutionCostCalculatorFactory(stateManager).createCalculator(); + refreshStates(); + } + public SolutionAnalyser(VehicleRoutingProblem vrp, VehicleRoutingProblemSolution solution, SolutionCostCalculator solutionCostCalculator, DistanceCalculator distanceCalculator) { this.vrp = vrp; this.solution = solution; diff --git a/jsprit-core/src/main/java/jsprit/core/problem/cost/TransportDistance.java b/jsprit-core/src/main/java/jsprit/core/problem/cost/TransportDistance.java new file mode 100644 index 00000000..e33c3044 --- /dev/null +++ b/jsprit-core/src/main/java/jsprit/core/problem/cost/TransportDistance.java @@ -0,0 +1,12 @@ +package jsprit.core.problem.cost; + +import jsprit.core.problem.Location; + +/** + * Created by schroeder on 23/12/14. + */ +public interface TransportDistance { + + public double getDistance(Location from, Location to); + +} diff --git a/jsprit-core/src/main/java/jsprit/core/util/CrowFlyCosts.java b/jsprit-core/src/main/java/jsprit/core/util/CrowFlyCosts.java index 6f3dfaac..f41f516b 100644 --- a/jsprit-core/src/main/java/jsprit/core/util/CrowFlyCosts.java +++ b/jsprit-core/src/main/java/jsprit/core/util/CrowFlyCosts.java @@ -21,6 +21,7 @@ package jsprit.core.util; import jsprit.core.problem.Location; import jsprit.core.problem.cost.AbstractForwardVehicleRoutingTransportCosts; +import jsprit.core.problem.cost.TransportDistance; import jsprit.core.problem.driver.Driver; import jsprit.core.problem.vehicle.Vehicle; @@ -30,7 +31,7 @@ import jsprit.core.problem.vehicle.Vehicle; * @author stefan schroeder * */ -public class CrowFlyCosts extends AbstractForwardVehicleRoutingTransportCosts { +public class CrowFlyCosts extends AbstractForwardVehicleRoutingTransportCosts implements TransportDistance{ public int speed = 1; @@ -52,7 +53,7 @@ public class CrowFlyCosts extends AbstractForwardVehicleRoutingTransportCosts { public double getTransportCost(Location from, Location to, double time, Driver driver, Vehicle vehicle) { double distance; try { - distance = EuclideanDistanceCalculator.calculateDistance(locations.getCoord(from.getId()), locations.getCoord(to.getId())) * detourFactor; + distance = calculateDistance(from, to); } catch (NullPointerException e) { throw new NullPointerException("cannot calculate euclidean distance. coordinates are missing. either add coordinates or use another transport-cost-calculator."); } @@ -65,15 +66,38 @@ public class CrowFlyCosts extends AbstractForwardVehicleRoutingTransportCosts { return costs; } + private double calculateDistance(Location fromLocation, Location toLocation) { + Coordinate from = null; + Coordinate to = null; + if(fromLocation.getCoordinate() != null & toLocation.getCoordinate() != null){ + from = fromLocation.getCoordinate(); + to = toLocation.getCoordinate(); + } + else if(locations != null){ + from = locations.getCoord(fromLocation.getId()); + to = locations.getCoord(toLocation.getId()); + } + if(from == null || to == null) throw new NullPointerException(); + return calculateDistance(from,to); + } + + private double calculateDistance(Coordinate from, Coordinate to) { + return EuclideanDistanceCalculator.calculateDistance(from,to) * detourFactor; + } + @Override public double getTransportTime(Location from, Location to, double time, Driver driver, Vehicle vehicle) { double distance; try { - distance = EuclideanDistanceCalculator.calculateDistance(locations.getCoord(from.getId()), locations.getCoord(to.getId())) * detourFactor; + distance = calculateDistance(from, to); } catch (NullPointerException e) { throw new NullPointerException("cannot calculate euclidean distance. coordinates are missing. either add coordinates or use another transport-cost-calculator."); } return distance / speed; } + @Override + public double getDistance(Location from, Location to) { + return calculateDistance(from,to); + } } diff --git a/jsprit-core/src/main/java/jsprit/core/util/FastVehicleRoutingTransportCostsMatrix.java b/jsprit-core/src/main/java/jsprit/core/util/FastVehicleRoutingTransportCostsMatrix.java index de22c06b..84d58046 100644 --- a/jsprit-core/src/main/java/jsprit/core/util/FastVehicleRoutingTransportCostsMatrix.java +++ b/jsprit-core/src/main/java/jsprit/core/util/FastVehicleRoutingTransportCostsMatrix.java @@ -18,6 +18,7 @@ package jsprit.core.util; import jsprit.core.problem.Location; import jsprit.core.problem.cost.AbstractForwardVehicleRoutingTransportCosts; +import jsprit.core.problem.cost.TransportDistance; import jsprit.core.problem.driver.Driver; import jsprit.core.problem.vehicle.Vehicle; import jsprit.core.problem.vehicle.VehicleTypeImpl.VehicleCostParams; @@ -30,7 +31,7 @@ import jsprit.core.problem.vehicle.VehicleTypeImpl.VehicleCostParams; * @author schroeder * */ -public class FastVehicleRoutingTransportCostsMatrix extends AbstractForwardVehicleRoutingTransportCosts { +public class FastVehicleRoutingTransportCostsMatrix extends AbstractForwardVehicleRoutingTransportCosts implements TransportDistance { /** * Builder that builds the matrix. @@ -143,6 +144,11 @@ public class FastVehicleRoutingTransportCostsMatrix extends AbstractForwardVehic return get(fromIndex, toIndex, distanceIndex); } + @Override + public double getDistance(Location from, Location to) { + return getDistance(from.getIndex(),to.getIndex()); + } + @Override public double getTransportCost(Location from, Location to, double departureTime, Driver driver, Vehicle vehicle) { if(from.getIndex() < 0 || to.getIndex() < 0) throw new IllegalArgumentException("index of from " + from + " to " + to + " < 0 "); diff --git a/jsprit-core/src/main/java/jsprit/core/util/GreatCircleCosts.java b/jsprit-core/src/main/java/jsprit/core/util/GreatCircleCosts.java index 63f01291..85df4310 100644 --- a/jsprit-core/src/main/java/jsprit/core/util/GreatCircleCosts.java +++ b/jsprit-core/src/main/java/jsprit/core/util/GreatCircleCosts.java @@ -20,6 +20,7 @@ package jsprit.core.util; import jsprit.core.problem.Location; import jsprit.core.problem.cost.AbstractForwardVehicleRoutingTransportCosts; +import jsprit.core.problem.cost.TransportDistance; import jsprit.core.problem.driver.Driver; import jsprit.core.problem.vehicle.Vehicle; @@ -29,7 +30,7 @@ import jsprit.core.problem.vehicle.Vehicle; * */ -public class GreatCircleCosts extends AbstractForwardVehicleRoutingTransportCosts { +public class GreatCircleCosts extends AbstractForwardVehicleRoutingTransportCosts implements TransportDistance{ private double speed = 1.; @@ -94,4 +95,8 @@ public class GreatCircleCosts extends AbstractForwardVehicleRoutingTransportCost return GreatCircleDistanceCalculator.calculateDistance(fromCoordinate, toCoordinate, distanceUnit) * detour; } + @Override + public double getDistance(Location from, Location to) { + return getDistance(from.getId(),to.getId()); + } } diff --git a/jsprit-core/src/main/java/jsprit/core/util/ManhattanCosts.java b/jsprit-core/src/main/java/jsprit/core/util/ManhattanCosts.java index 1482416c..db8a841d 100644 --- a/jsprit-core/src/main/java/jsprit/core/util/ManhattanCosts.java +++ b/jsprit-core/src/main/java/jsprit/core/util/ManhattanCosts.java @@ -20,6 +20,7 @@ package jsprit.core.util; import jsprit.core.problem.Location; import jsprit.core.problem.cost.AbstractForwardVehicleRoutingTransportCosts; +import jsprit.core.problem.cost.TransportDistance; import jsprit.core.problem.driver.Driver; import jsprit.core.problem.vehicle.Vehicle; @@ -29,7 +30,7 @@ import jsprit.core.problem.vehicle.Vehicle; * */ -public class ManhattanCosts extends AbstractForwardVehicleRoutingTransportCosts { +public class ManhattanCosts extends AbstractForwardVehicleRoutingTransportCosts implements TransportDistance { public double speed = 1; @@ -40,12 +41,16 @@ public class ManhattanCosts extends AbstractForwardVehicleRoutingTransportCosts this.locations = locations; } + public ManhattanCosts(){ + + } + @Override - public double getTransportCost(Location from, Location to, double time,Driver driver, Vehicle vehicle) { + public double getTransportCost(Location from, Location to, double time, Driver driver, Vehicle vehicle) { double distance; try { - distance = calculateDistance(from.getId(), to.getId()); - } catch (NullPointerException e) { + distance = calculateDistance(from, to); + } catch (NullPointerException e) { throw new NullPointerException("cannot calculate euclidean distance. coordinates are missing. either add coordinates or use another transport-cost-calculator."); } double costs = distance; @@ -59,12 +64,30 @@ public class ManhattanCosts extends AbstractForwardVehicleRoutingTransportCosts @Override public double getTransportTime(Location from, Location to, double time,Driver driver, Vehicle vehicle) { - return calculateDistance(from.getId(), to.getId()) / speed; + return calculateDistance(from, to) / speed; } - private double calculateDistance(String fromId, String toId) { - return Math.abs(locations.getCoord(fromId).getX() - locations.getCoord(toId).getX()) - + Math.abs(locations.getCoord(fromId).getY() - locations.getCoord(toId).getY()); + private double calculateDistance(Location fromLocation, Location toLocation) { + Coordinate from = null; + Coordinate to = null; + if(fromLocation.getCoordinate() != null & toLocation.getCoordinate() != null){ + from = fromLocation.getCoordinate(); + to = toLocation.getCoordinate(); + } + else if(locations != null){ + from = locations.getCoord(fromLocation.getId()); + to = locations.getCoord(toLocation.getId()); + } + if(from == null || to == null) throw new NullPointerException(); + return calculateDistance(from,to); } + private double calculateDistance(Coordinate from, Coordinate to) { + return Math.abs(from.getX() - to.getX()) + Math.abs(from.getY() - to.getY()); + } + + @Override + public double getDistance(Location from, Location to) { + return calculateDistance(from,to); + } }