mirror of
https://github.com/graphhopper/jsprit.git
synced 2020-01-24 07:45:05 +01:00
add VehicleRoutingTransportCostMatrix to easily consider time and
distance matrices, and add an example that illustrates the matrix
This commit is contained in:
parent
13a9122df1
commit
8950d49e5a
6 changed files with 370 additions and 9 deletions
|
|
@ -0,0 +1,93 @@
|
|||
package examples;
|
||||
|
||||
import java.util.Collection;
|
||||
|
||||
import util.Solutions;
|
||||
import util.VehicleRoutingTransportCostsMatrix;
|
||||
import algorithms.GreedySchrimpfFactory;
|
||||
import algorithms.VehicleRoutingAlgorithms;
|
||||
import analysis.SolutionPlotter;
|
||||
import analysis.SolutionPrinter;
|
||||
import analysis.SolutionPrinter.Print;
|
||||
import basics.Service;
|
||||
import basics.VehicleRoutingAlgorithm;
|
||||
import basics.VehicleRoutingProblem;
|
||||
import basics.VehicleRoutingProblem.FleetSize;
|
||||
import basics.VehicleRoutingProblemSolution;
|
||||
import basics.costs.VehicleRoutingTransportCosts;
|
||||
import basics.route.Vehicle;
|
||||
import basics.route.VehicleImpl;
|
||||
import basics.route.VehicleType;
|
||||
import basics.route.VehicleTypeImpl;
|
||||
|
||||
/**
|
||||
* Illustrates how you can use jsprit with an already compiled distance and time matrix.
|
||||
*
|
||||
* @author schroeder
|
||||
*
|
||||
*/
|
||||
public class CostMatrixExample {
|
||||
|
||||
/**
|
||||
* @param args
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
|
||||
VehicleType type = VehicleTypeImpl.Builder.newInstance("type", 2).setCostPerDistance(1).setCostPerTime(2).build();
|
||||
Vehicle vehicle = VehicleImpl.Builder.newInstance("vehicle").setLocationId("0").setType(type).build();
|
||||
|
||||
Service s1 = Service.Builder.newInstance("1", 1).setLocationId("1").build();
|
||||
Service s2 = Service.Builder.newInstance("2", 1).setLocationId("2").build();
|
||||
Service s3 = Service.Builder.newInstance("3", 1).setLocationId("3").build();
|
||||
|
||||
|
||||
/*
|
||||
* Assume the following symmetric distance-matrix
|
||||
* from,to,distance
|
||||
* 0,1,10.0
|
||||
* 0,2,20.0
|
||||
* 0,3,5.0
|
||||
* 1,2,4.0
|
||||
* 1,3,1.0
|
||||
* 2,3,2.0
|
||||
*
|
||||
* and this time-matrix
|
||||
* 0,1,5.0
|
||||
* 0,2,10.0
|
||||
* 0,3,2.5
|
||||
* 1,2,2.0
|
||||
* 1,3,0.5
|
||||
* 2,3,1.0
|
||||
*/
|
||||
//define a matrix-builder building a symmetric matrix
|
||||
VehicleRoutingTransportCostsMatrix.Builder costMatrixBuilder = VehicleRoutingTransportCostsMatrix.Builder.newInstance(true);
|
||||
costMatrixBuilder.addTransportDistance("0", "1", 10.0);
|
||||
costMatrixBuilder.addTransportDistance("0", "2", 20.0);
|
||||
costMatrixBuilder.addTransportDistance("0", "3", 5.0);
|
||||
costMatrixBuilder.addTransportDistance("1", "2", 4.0);
|
||||
costMatrixBuilder.addTransportDistance("1", "3", 1.0);
|
||||
costMatrixBuilder.addTransportDistance("2", "3", 2.0);
|
||||
|
||||
costMatrixBuilder.addTransportTime("0", "1", 10.0);
|
||||
costMatrixBuilder.addTransportTime("0", "2", 20.0);
|
||||
costMatrixBuilder.addTransportTime("0", "3", 5.0);
|
||||
costMatrixBuilder.addTransportTime("1", "2", 4.0);
|
||||
costMatrixBuilder.addTransportTime("1", "3", 1.0);
|
||||
costMatrixBuilder.addTransportTime("2", "3", 2.0);
|
||||
|
||||
VehicleRoutingTransportCosts costMatrix = costMatrixBuilder.build();
|
||||
|
||||
VehicleRoutingProblem vrp = VehicleRoutingProblem.Builder.newInstance().setFleetSize(FleetSize.INFINITE).setRoutingCost(costMatrix)
|
||||
.addVehicle(vehicle).addService(s1).addService(s2).addService(s3).build();
|
||||
|
||||
VehicleRoutingAlgorithm vra = VehicleRoutingAlgorithms.readAndCreateAlgorithm(vrp, "input/fastAlgo.xml");
|
||||
|
||||
Collection<VehicleRoutingProblemSolution> solutions = vra.searchSolutions();
|
||||
|
||||
SolutionPrinter.print(Solutions.getBest(solutions), Print.VERBOSE);
|
||||
|
||||
SolutionPlotter.plotSolutionAsPNG(vrp, Solutions.getBest(solutions), "output/yo.png", "po");
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -30,6 +30,8 @@ import java.util.HashMap;
|
|||
import java.util.Map;
|
||||
|
||||
import util.Solutions;
|
||||
import util.VehicleRoutingTransportCostsMatrix;
|
||||
import util.VehicleRoutingTransportCostsMatrix.Builder;
|
||||
import algorithms.GreedySchrimpfFactory;
|
||||
import analysis.SolutionPrinter;
|
||||
import analysis.SolutionPrinter.Print;
|
||||
|
|
@ -179,13 +181,12 @@ public class RefuseCollectionExample {
|
|||
readDemandQuantities(vrpBuilder);
|
||||
|
||||
/*
|
||||
* read distances
|
||||
* create cost-matrix
|
||||
*/
|
||||
Map<RelationKey,Integer> distances = new HashMap<RelationKey, Integer>();
|
||||
readDistances(distances);
|
||||
|
||||
VehicleRoutingTransportCosts routingCosts = new RoutingCosts(distances);
|
||||
vrpBuilder.setRoutingCost(routingCosts);
|
||||
VehicleRoutingTransportCostsMatrix.Builder matrixBuilder = VehicleRoutingTransportCostsMatrix.Builder.newInstance(true);
|
||||
readDistances(matrixBuilder);
|
||||
|
||||
vrpBuilder.setRoutingCost(matrixBuilder.build());
|
||||
|
||||
VehicleRoutingProblem vrp = vrpBuilder.build();
|
||||
|
||||
|
|
@ -199,6 +200,7 @@ public class RefuseCollectionExample {
|
|||
|
||||
}
|
||||
|
||||
|
||||
private static void readDemandQuantities(VehicleRoutingProblem.Builder vrpBuilder) throws FileNotFoundException, IOException {
|
||||
BufferedReader reader = new BufferedReader(new FileReader(new File("input/RefuseCollectionExample_Quantities")));
|
||||
String line = null;
|
||||
|
|
@ -221,7 +223,8 @@ public class RefuseCollectionExample {
|
|||
reader.close();
|
||||
}
|
||||
|
||||
private static void readDistances(Map<RelationKey, Integer> distances) throws IOException {
|
||||
|
||||
private static void readDistances(Builder matrixBuilder) throws IOException {
|
||||
BufferedReader reader = new BufferedReader(new FileReader(new File("input/RefuseCollectionExample_Distances")));
|
||||
String line = null;
|
||||
boolean firstLine = true;
|
||||
|
|
@ -231,8 +234,7 @@ public class RefuseCollectionExample {
|
|||
continue;
|
||||
}
|
||||
String[] lineTokens = line.split(",");
|
||||
RelationKey key = RelationKey.newKey(lineTokens[0],lineTokens[1]);
|
||||
distances.put(key, Integer.parseInt(lineTokens[2]));
|
||||
matrixBuilder.addTransportDistance(lineTokens[0],lineTokens[1], Integer.parseInt(lineTokens[2]));
|
||||
}
|
||||
reader.close();
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue