From 0a196f0abde20ed9b6ba616fe2ac9ba9155b15cd Mon Sep 17 00:00:00 2001
From: oblonski <4sschroeder@gmail.com>
Date: Wed, 17 Dec 2014 21:29:22 +0100
Subject: [PATCH] add and test FastTransportCostMatrix based array
---
...astVehicleRoutingTransportCostsMatrix.java | 150 ++++++++++++++++++
...ehicleRoutingTransportCostsMatrixTest.java | 99 ++++++++++++
2 files changed, 249 insertions(+)
create mode 100644 jsprit-core/src/main/java/jsprit/core/util/FastVehicleRoutingTransportCostsMatrix.java
create mode 100644 jsprit-core/src/test/java/jsprit/core/util/FastVehicleRoutingTransportCostsMatrixTest.java
diff --git a/jsprit-core/src/main/java/jsprit/core/util/FastVehicleRoutingTransportCostsMatrix.java b/jsprit-core/src/main/java/jsprit/core/util/FastVehicleRoutingTransportCostsMatrix.java
new file mode 100644
index 00000000..73bf69ff
--- /dev/null
+++ b/jsprit-core/src/main/java/jsprit/core/util/FastVehicleRoutingTransportCostsMatrix.java
@@ -0,0 +1,150 @@
+/*******************************************************************************
+ * 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
Note that you can also use it with distance matrix only (or time matrix). + * @author schroeder + * + */ +public class FastVehicleRoutingTransportCostsMatrix extends AbstractForwardVehicleRoutingTransportCosts { + + /** + * Builder that builds the matrix. + * + * @author schroeder + * + */ + public static class Builder { + + private boolean isSymmetric; + + private double[][][] matrix; + + /** + * Creates a new builder returning the matrix-builder. + *
If you want to consider symmetric matrices, set isSymmetric to true.
+ * @param isSymmetric true if matrix is symmetric, false otherwise
+ * @return builder
+ */
+ public static Builder newInstance(int noLocations, boolean isSymmetric){
+ return new Builder(noLocations, isSymmetric);
+ }
+
+ private Builder(int noLocations, boolean isSymmetric){
+ this.isSymmetric = isSymmetric;
+ matrix = new double[noLocations][noLocations][2];
+ }
+
+ /**
+ * Adds a transport-distance for a particular relation.
+ * @param fromIndex from location index
+ * @param toIndex to location index
+ * @param distance the distance to be added
+ * @return builder
+ */
+ public Builder addTransportDistance(int fromIndex, int toIndex, double distance){
+ add(fromIndex,toIndex,0,distance);
+ return this;
+ }
+
+ private void add(int fromIndex, int toIndex, int indicatorIndex, double distance){
+ if(isSymmetric){
+ if(fromIndex < toIndex) matrix[fromIndex][toIndex][indicatorIndex] = distance;
+ else matrix[toIndex][fromIndex][indicatorIndex] = distance;
+ }
+ else matrix[fromIndex][toIndex][indicatorIndex] = distance;
+ }
+
+ /**
+ * Adds transport-time for a particular relation.
+ * @param fromIndex from location index
+ * @param toIndex to location index
+ * @param time the time to be added
+ * @return builder
+ */
+ public Builder addTransportTime(int fromIndex, int toIndex, double time){
+ add(fromIndex,toIndex,1,time);
+ return this;
+ }
+
+ /**
+ * Builds the matrix.
+ * @return matrix
+ */
+ public FastVehicleRoutingTransportCostsMatrix build(){
+ return new FastVehicleRoutingTransportCostsMatrix(this);
+ }
+
+
+ }
+
+ private final boolean isSymmetric;
+
+ private final double[][][] matrix;
+
+ private FastVehicleRoutingTransportCostsMatrix(Builder builder){
+ this.isSymmetric = builder.isSymmetric;
+ matrix = builder.matrix;
+ }
+
+
+ @Override
+ public double getTransportTime(Location from, Location to, double departureTime, Driver driver, Vehicle vehicle) {
+ return get(from.getIndex(),to.getIndex(),1);
+ }
+
+ private double get(int from, int to, int indicatorIndex){
+ double value;
+ if(isSymmetric){
+ if(from < to) value = matrix[from][to][indicatorIndex];
+ else value = matrix[to][from][indicatorIndex];
+ }
+ else{
+ value = matrix[from][to][indicatorIndex];
+ }
+ return value;
+ }
+
+ /**
+ * Returns the distance from to to.
+ *
+ * @param fromIndex from location index
+ * @param toIndex to location index
+ * @return the distance
+ */
+ public double getDistance(int fromIndex, int toIndex) {
+ return get(fromIndex,toIndex,0);
+ }
+
+ @Override
+ public double getTransportCost(Location from, Location to, double departureTime, Driver driver, Vehicle vehicle) {
+ if(vehicle == null) return getDistance(from.getIndex(), to.getIndex());
+ VehicleCostParams costParams = vehicle.getType().getVehicleCostParams();
+ return costParams.perDistanceUnit * getDistance(from.getIndex(), to.getIndex()) + costParams.perTimeUnit * getTransportTime(from, to, departureTime, driver, vehicle);
+ }
+
+}
diff --git a/jsprit-core/src/test/java/jsprit/core/util/FastVehicleRoutingTransportCostsMatrixTest.java b/jsprit-core/src/test/java/jsprit/core/util/FastVehicleRoutingTransportCostsMatrixTest.java
new file mode 100644
index 00000000..2b3b1e9d
--- /dev/null
+++ b/jsprit-core/src/test/java/jsprit/core/util/FastVehicleRoutingTransportCostsMatrixTest.java
@@ -0,0 +1,99 @@
+/*******************************************************************************
+ * 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