diff --git a/jsprit-core/src/main/java/com/graphhopper/jsprit/core/util/CrowFlyCosts.java b/jsprit-core/src/main/java/com/graphhopper/jsprit/core/util/CrowFlyCosts.java index 5ed4b217..709f1f4c 100644 --- a/jsprit-core/src/main/java/com/graphhopper/jsprit/core/util/CrowFlyCosts.java +++ b/jsprit-core/src/main/java/com/graphhopper/jsprit/core/util/CrowFlyCosts.java @@ -21,80 +21,29 @@ package com.graphhopper.jsprit.core.util; import com.graphhopper.jsprit.core.problem.Location; -import com.graphhopper.jsprit.core.problem.cost.AbstractForwardVehicleRoutingTransportCosts; -import com.graphhopper.jsprit.core.problem.driver.Driver; -import com.graphhopper.jsprit.core.problem.vehicle.Vehicle; /** * @author stefan schroeder */ -public class CrowFlyCosts extends AbstractForwardVehicleRoutingTransportCosts { - - public int speed = 1; - - public double detourFactor = 1.0; +public class CrowFlyCosts extends EuclideanCosts { private Locations locations; public CrowFlyCosts(Locations locations) { - super(); this.locations = locations; } - @Override - public String toString() { - return "[name=crowFlyCosts]"; - } - - @Override - public double getTransportCost(Location from, Location to, double time, Driver driver, Vehicle vehicle) { - double distance; - try { - 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; - if (vehicle != null) { - if (vehicle.getType() != null) { - costs = distance * vehicle.getType().getVehicleCostParams().perDistanceUnit; - } - } - return costs; - } - - private double calculateDistance(Location fromLocation, Location toLocation) { + double calculateDistance(Location fromLocation, Location toLocation) { Coordinate from = null; Coordinate to = null; - if (fromLocation.getCoordinate() != null & toLocation.getCoordinate() != 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 = 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, double departureTime, Vehicle vehicle) { return calculateDistance(from, to); } } diff --git a/jsprit-core/src/main/java/com/graphhopper/jsprit/core/util/EuclideanCosts.java b/jsprit-core/src/main/java/com/graphhopper/jsprit/core/util/EuclideanCosts.java index 265ac70e..c17e6357 100644 --- a/jsprit-core/src/main/java/com/graphhopper/jsprit/core/util/EuclideanCosts.java +++ b/jsprit-core/src/main/java/com/graphhopper/jsprit/core/util/EuclideanCosts.java @@ -42,42 +42,32 @@ public class EuclideanCosts extends AbstractForwardVehicleRoutingTransportCosts @Override public double getTransportCost(Location from, Location to, double time, Driver driver, Vehicle vehicle) { - double distance; - try { - 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 distance = calculateDistance(from, to); + if (vehicle != null && vehicle.getType() != null) { + return distance * vehicle.getType().getVehicleCostParams().perDistanceUnit; } - double costs = distance; - if (vehicle != null) { - if (vehicle.getType() != null) { - costs = distance * vehicle.getType().getVehicleCostParams().perDistanceUnit; - } - } - return costs; + return distance; } - private double calculateDistance(Location fromLocation, Location toLocation) { + double calculateDistance(Location fromLocation, Location toLocation) { return calculateDistance(fromLocation.getCoordinate(), toLocation.getCoordinate()); } - private double calculateDistance(Coordinate from, Coordinate to) { - return EuclideanDistanceCalculator.calculateDistance(from, to) * detourFactor; + double calculateDistance(Coordinate from, Coordinate to) { + try { + return EuclideanDistanceCalculator.calculateDistance(from, to) * detourFactor; + } catch (NullPointerException e) { + throw new NullPointerException("cannot calculate euclidean distance. coordinates are missing. either add coordinates or use another transport-cost-calculator."); + } } @Override public double getTransportTime(Location from, Location to, double time, Driver driver, Vehicle vehicle) { - double distance; - try { - 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; + return calculateDistance(from, to) / speed; } @Override public double getDistance(Location from, Location to, double departureTime, Vehicle vehicle) { - return calculateDistance(from, to); + return calculateDistance(from, to); } }