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:
commit
ba343fb555
2 changed files with 16 additions and 77 deletions
|
|
@ -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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -42,42 +42,32 @@ 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) {
|
||||||
return EuclideanDistanceCalculator.calculateDistance(from, to) * detourFactor;
|
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
|
@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
|
||||||
public double getDistance(Location from, Location to, double departureTime, Vehicle vehicle) {
|
public double getDistance(Location from, Location to, double departureTime, Vehicle vehicle) {
|
||||||
return calculateDistance(from, to);
|
return calculateDistance(from, to);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue