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

Merge pull request #387 from michalmac/master

CrowFlyCosts inherits from EuclideanCosts to remove duplicated code
This commit is contained in:
Stefan Schröder 2017-11-06 19:49:41 +01:00 committed by GitHub
commit ba343fb555
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 16 additions and 77 deletions

View file

@ -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);
}
}

View file

@ -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);
}
}