mirror of
https://github.com/graphhopper/jsprit.git
synced 2020-01-24 07:45:05 +01:00
add interface TransportDistance to simplify distance calc
This commit is contained in:
parent
bc57ac6174
commit
e8175b11f9
6 changed files with 110 additions and 13 deletions
|
|
@ -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;
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
|
||||
}
|
||||
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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 ");
|
||||
|
|
|
|||
|
|
@ -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());
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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,11 +41,15 @@ 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());
|
||||
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.");
|
||||
}
|
||||
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue