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

CrowFlyCosts inherits from EuclideanCosts to remove duplicated code

This commit is contained in:
Michal Maciejewski 2017-11-06 11:18:26 +01:00
parent 78cf643384
commit 0a5c3e01c0
No known key found for this signature in database
GPG key ID: 015947E60A2AD77B
2 changed files with 16 additions and 77 deletions

View file

@ -21,80 +21,29 @@
package com.graphhopper.jsprit.core.util; package com.graphhopper.jsprit.core.util;
import com.graphhopper.jsprit.core.problem.Location; 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 * @author stefan schroeder
*/ */
public class CrowFlyCosts extends AbstractForwardVehicleRoutingTransportCosts { public class CrowFlyCosts extends EuclideanCosts {
public int speed = 1;
public double detourFactor = 1.0;
private Locations locations; private Locations locations;
public CrowFlyCosts(Locations locations) { public CrowFlyCosts(Locations locations) {
super();
this.locations = locations; this.locations = locations;
} }
@Override double calculateDistance(Location fromLocation, Location toLocation) {
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) {
Coordinate from = null; Coordinate from = null;
Coordinate to = null; Coordinate to = null;
if (fromLocation.getCoordinate() != null & toLocation.getCoordinate() != null) { if (fromLocation.getCoordinate() != null && toLocation.getCoordinate() != null) {
from = fromLocation.getCoordinate(); from = fromLocation.getCoordinate();
to = toLocation.getCoordinate(); to = toLocation.getCoordinate();
} else if (locations != null) { } else if (locations != null) {
from = locations.getCoord(fromLocation.getId()); from = locations.getCoord(fromLocation.getId());
to = locations.getCoord(toLocation.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); return calculateDistance(from, to);
} }
} }

View file

@ -42,38 +42,28 @@ public class EuclideanCosts extends AbstractForwardVehicleRoutingTransportCosts
@Override @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; double distance = calculateDistance(from, to);
try { if (vehicle != null && vehicle.getType() != null) {
distance = calculateDistance(from, to); return distance * vehicle.getType().getVehicleCostParams().perDistanceUnit;
} 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; return 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) {
return calculateDistance(fromLocation.getCoordinate(), toLocation.getCoordinate()); return calculateDistance(fromLocation.getCoordinate(), toLocation.getCoordinate());
} }
private double calculateDistance(Coordinate from, Coordinate to) { double calculateDistance(Coordinate from, Coordinate to) {
try {
return EuclideanDistanceCalculator.calculateDistance(from, to) * detourFactor; 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 @Override
public double getTransportTime(Location from, Location to, double time, Driver driver, Vehicle vehicle) { public double getTransportTime(Location from, Location to, double time, Driver driver, Vehicle vehicle) {
double distance; return calculateDistance(from, to) / speed;
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 @Override