From 1d999c4a28e85492252ea7290d0072d58e1466c4 Mon Sep 17 00:00:00 2001 From: oblonski Date: Thu, 26 Feb 2015 19:32:21 +0100 Subject: [PATCH] add EuclideanCosts --- .../java/jsprit/core/util/EuclideanCosts.java | 91 +++++++++++++++++++ 1 file changed, 91 insertions(+) create mode 100644 jsprit-core/src/main/java/jsprit/core/util/EuclideanCosts.java diff --git a/jsprit-core/src/main/java/jsprit/core/util/EuclideanCosts.java b/jsprit-core/src/main/java/jsprit/core/util/EuclideanCosts.java new file mode 100644 index 00000000..7b67e4a1 --- /dev/null +++ b/jsprit-core/src/main/java/jsprit/core/util/EuclideanCosts.java @@ -0,0 +1,91 @@ +/******************************************************************************* + * Copyright (C) 2014 Stefan Schroeder + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3.0 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library. If not, see . + ******************************************************************************/ +/** + * + */ +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; + + +/** + * @author stefan schroeder + * + */ +public class EuclideanCosts extends AbstractForwardVehicleRoutingTransportCosts implements TransportDistance{ + + public int speed = 1; + + public double detourFactor = 1.0; + + @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) { + Coordinate from = null; + Coordinate to = null; + if(fromLocation.getCoordinate() != null & toLocation.getCoordinate() != null){ + from = fromLocation.getCoordinate(); + to = toLocation.getCoordinate(); + } + 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) { + return calculateDistance(from,to); + } +}