mirror of
https://github.com/graphhopper/jsprit.git
synced 2020-01-24 07:45:05 +01:00
rename vehicleType to vehicleTypeImpl, extract interface vehicleType;
refactor vehicleType to new file; add penaltyVehicleType; improve vehicleFleetManagerImpl
This commit is contained in:
parent
d223475cb4
commit
c1849a9d5a
39 changed files with 319 additions and 572 deletions
|
|
@ -1,107 +0,0 @@
|
|||
package examples;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
|
||||
import util.Coordinate;
|
||||
import util.Solutions;
|
||||
import algorithms.VehicleRoutingAlgorithms;
|
||||
import analysis.AlgorithmSearchProgressChartListener;
|
||||
import analysis.SolutionPlotter;
|
||||
import analysis.SolutionPrinter;
|
||||
import analysis.StopWatch;
|
||||
import basics.VehicleRoutingAlgorithm;
|
||||
import basics.VehicleRoutingProblem;
|
||||
import basics.VehicleRoutingProblem.FleetSize;
|
||||
import basics.VehicleRoutingProblemSolution;
|
||||
import basics.algo.VehicleRoutingAlgorithmListeners.Priority;
|
||||
import basics.io.VrpXMLReader;
|
||||
import basics.route.Vehicle;
|
||||
import basics.route.VehicleImpl;
|
||||
import basics.route.VehicleImpl.VehicleType;
|
||||
|
||||
public class ConcurrentMultipleDepotExample {
|
||||
|
||||
/**
|
||||
* @param args
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
|
||||
VehicleRoutingProblem.Builder vrpBuilder = VehicleRoutingProblem.Builder.newInstance();
|
||||
/*
|
||||
* Read cordeau-instance p01, BUT only its services without any vehicles
|
||||
*/
|
||||
new VrpXMLReader(vrpBuilder).read("input/vrp_cordeau_01.xml");
|
||||
|
||||
/*
|
||||
* add vehicles with its depots
|
||||
* 4 depots:
|
||||
* (20,20)
|
||||
* (30,40)
|
||||
* (50,30)
|
||||
* (60,50)
|
||||
*
|
||||
* each with 4 vehicles each with a capacity of 80
|
||||
*/
|
||||
int nuOfVehicles = 4;
|
||||
int capacity = 80;
|
||||
Coordinate firstDepotCoord = Coordinate.newInstance(20, 20);
|
||||
Coordinate second = Coordinate.newInstance(30, 40);
|
||||
Coordinate third = Coordinate.newInstance(50, 30);
|
||||
Coordinate fourth = Coordinate.newInstance(60, 50);
|
||||
|
||||
int depotCounter = 1;
|
||||
for(Coordinate depotCoord : Arrays.asList(firstDepotCoord,second,third,fourth)){
|
||||
for(int i=0;i<nuOfVehicles;i++){
|
||||
VehicleType vehicleType = VehicleType.Builder.newInstance(depotCounter + "_" + (i+1) + "_type", capacity).setCostPerDistance(1.0).build();
|
||||
Vehicle vehicle = VehicleImpl.VehicleBuilder.newInstance(depotCounter + "_" + (i+1) + "_vehicle").setLocationCoord(depotCoord).setType(vehicleType).build();
|
||||
vrpBuilder.addVehicle(vehicle);
|
||||
}
|
||||
depotCounter++;
|
||||
}
|
||||
|
||||
/*
|
||||
* define problem with finite fleet
|
||||
*/
|
||||
vrpBuilder.setFleetSize(FleetSize.FINITE);
|
||||
|
||||
/*
|
||||
* build the problem
|
||||
*/
|
||||
VehicleRoutingProblem vrp = vrpBuilder.build();
|
||||
|
||||
/*
|
||||
* plot to see how the problem looks like
|
||||
*/
|
||||
// SolutionPlotter.plotVrpAsPNG(vrp, "output/problem01.png", "p01");
|
||||
|
||||
/*
|
||||
* Def. executorService and nuOfThreads
|
||||
*/
|
||||
int nuOfThreads = Runtime.getRuntime().availableProcessors() + 2;
|
||||
ExecutorService executor = Executors.newFixedThreadPool(nuOfThreads);
|
||||
|
||||
/*
|
||||
* solve the problem
|
||||
*/
|
||||
VehicleRoutingAlgorithm vra = VehicleRoutingAlgorithms.readAndCreateConcurrentAlgorithm(vrp, "input/algorithmConfig.xml", executor, nuOfThreads);
|
||||
vra.setNuOfIterations(20000);
|
||||
vra.getAlgorithmListeners().addListener(new StopWatch(),Priority.HIGH);
|
||||
vra.getAlgorithmListeners().addListener(new AlgorithmSearchProgressChartListener("output/progress.png"));
|
||||
Collection<VehicleRoutingProblemSolution> solutions = vra.searchSolutions();
|
||||
|
||||
executor.shutdown();
|
||||
// Wait until all threads are finish
|
||||
while (!executor.isTerminated()) {
|
||||
|
||||
}
|
||||
System.out.println("Finished all threads");
|
||||
|
||||
SolutionPrinter.print(Solutions.getBest(solutions));
|
||||
SolutionPlotter.plotSolutionAsPNG(vrp, Solutions.getBest(solutions), "output/p01_solution.png", "p01");
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -1,126 +0,0 @@
|
|||
package examples;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
|
||||
import util.Coordinate;
|
||||
import util.Solutions;
|
||||
import algorithms.VehicleRoutingAlgorithms;
|
||||
import analysis.AlgorithmSearchProgressChartListener;
|
||||
import analysis.SolutionPlotter;
|
||||
import analysis.SolutionPrinter;
|
||||
import analysis.StopWatch;
|
||||
import basics.VehicleRoutingAlgorithm;
|
||||
import basics.VehicleRoutingProblem;
|
||||
import basics.VehicleRoutingProblem.FleetSize;
|
||||
import basics.VehicleRoutingProblemSolution;
|
||||
import basics.algo.VehicleRoutingAlgorithmListeners.Priority;
|
||||
import basics.io.VrpXMLReader;
|
||||
import basics.route.Vehicle;
|
||||
import basics.route.VehicleImpl;
|
||||
import basics.route.VehicleImpl.VehicleType;
|
||||
|
||||
public class ConcurrentMultipleDepotExampleWithPenaltyVehicles {
|
||||
|
||||
/**
|
||||
* @param args
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
|
||||
VehicleRoutingProblem.Builder vrpBuilder = VehicleRoutingProblem.Builder.newInstance();
|
||||
/*
|
||||
* Read cordeau-instance p01, BUT only its services without any vehicles
|
||||
*/
|
||||
new VrpXMLReader(vrpBuilder).read("input/vrp_cordeau_08.xml");
|
||||
|
||||
/*
|
||||
* add vehicles with its depots
|
||||
* 2 depots:
|
||||
* (-33,33)
|
||||
* (33,-33)
|
||||
*
|
||||
* each with 14 vehicles each with a capacity of 500 and a maximum duration of 310
|
||||
*/
|
||||
int nuOfVehicles = 14;
|
||||
int capacity = 500;
|
||||
double maxDuration = 310;
|
||||
Coordinate firstDepotCoord = Coordinate.newInstance(-33, 33);
|
||||
Coordinate second = Coordinate.newInstance(33, -33);
|
||||
|
||||
/*
|
||||
* number of penalty vehicles
|
||||
*/
|
||||
int nuOfPenaltyVehicles = 4;
|
||||
int depotCounter = 1;
|
||||
for(Coordinate depotCoord : Arrays.asList(firstDepotCoord,second)){
|
||||
for(int i=0;i<nuOfVehicles;i++){
|
||||
VehicleType vehicleType = VehicleType.Builder.newInstance(depotCounter + "_type", capacity).setCostPerDistance(1.0).build();
|
||||
String vehicleId = depotCounter + "_" + (i+1) + "_vehicle";
|
||||
VehicleImpl.VehicleBuilder vehicleBuilder = VehicleImpl.VehicleBuilder.newInstance(vehicleId);
|
||||
vehicleBuilder.setLocationCoord(depotCoord);
|
||||
vehicleBuilder.setType(vehicleType);
|
||||
vehicleBuilder.setLatestArrival(maxDuration);
|
||||
Vehicle vehicle = vehicleBuilder.build();
|
||||
vrpBuilder.addVehicle(vehicle);
|
||||
}
|
||||
// for(int i=0;i<nuOfPenaltyVehicles;i++){
|
||||
VehicleType penaltyType = VehicleType.Builder.newInstance(depotCounter + "_type#penalty", capacity).setFixedCost(50).setCostPerDistance(3.0).build();
|
||||
String vehicleId = depotCounter + "_vehicle#penalty";
|
||||
VehicleImpl.VehicleBuilder vehicleBuilder = VehicleImpl.VehicleBuilder.newInstance(vehicleId);
|
||||
vehicleBuilder.setLocationCoord(depotCoord);
|
||||
vehicleBuilder.setType(penaltyType);
|
||||
vehicleBuilder.setLatestArrival(maxDuration);
|
||||
Vehicle penaltyVehicle = vehicleBuilder.build();
|
||||
vrpBuilder.addVehicle(penaltyVehicle);
|
||||
// }
|
||||
depotCounter++;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*
|
||||
* define problem with finite fleet
|
||||
*/
|
||||
vrpBuilder.setFleetSize(FleetSize.FINITE);
|
||||
|
||||
/*
|
||||
* build the problem
|
||||
*/
|
||||
VehicleRoutingProblem vrp = vrpBuilder.build();
|
||||
|
||||
/*
|
||||
* plot to see how the problem looks like
|
||||
*/
|
||||
// SolutionPlotter.plotVrpAsPNG(vrp, "output/problem08.png", "p08");
|
||||
|
||||
/*
|
||||
* Def. executorService and nuOfThreads
|
||||
*/
|
||||
int nuOfThreads = Runtime.getRuntime().availableProcessors() + 2;
|
||||
ExecutorService executor = Executors.newFixedThreadPool(nuOfThreads);
|
||||
|
||||
|
||||
/*
|
||||
* solve the problem
|
||||
*/
|
||||
VehicleRoutingAlgorithm vra = VehicleRoutingAlgorithms.readAndCreateConcurrentAlgorithm(vrp, "input/algorithmConfig.xml",executor,nuOfThreads);
|
||||
vra.setNuOfIterations(5000);
|
||||
vra.getAlgorithmListeners().addListener(new StopWatch(),Priority.HIGH);
|
||||
vra.getAlgorithmListeners().addListener(new AlgorithmSearchProgressChartListener("output/progress.png"));
|
||||
Collection<VehicleRoutingProblemSolution> solutions = vra.searchSolutions();
|
||||
|
||||
executor.shutdown();
|
||||
// Wait until all threads are finish
|
||||
while (!executor.isTerminated()) {
|
||||
|
||||
}
|
||||
System.out.println("Finished all threads");
|
||||
|
||||
SolutionPrinter.print(Solutions.getBest(solutions));
|
||||
SolutionPlotter.plotSolutionAsPNG(vrp, Solutions.getBest(solutions), "output/p08_solution.png", "p08");
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -18,7 +18,8 @@ import basics.algo.VehicleRoutingAlgorithmListeners.Priority;
|
|||
import basics.io.VrpXMLReader;
|
||||
import basics.route.Vehicle;
|
||||
import basics.route.VehicleImpl;
|
||||
import basics.route.VehicleImpl.VehicleType;
|
||||
import basics.route.VehicleType;
|
||||
import basics.route.VehicleTypeImpl;
|
||||
|
||||
public class MultipleDepotExample {
|
||||
|
||||
|
|
@ -53,7 +54,7 @@ public class MultipleDepotExample {
|
|||
int depotCounter = 1;
|
||||
for(Coordinate depotCoord : Arrays.asList(firstDepotCoord,second,third,fourth)){
|
||||
for(int i=0;i<nuOfVehicles;i++){
|
||||
VehicleType vehicleType = VehicleType.Builder.newInstance(depotCounter + "_type", capacity).setCostPerDistance(1.0).build();
|
||||
VehicleTypeImpl vehicleType = VehicleTypeImpl.Builder.newInstance(depotCounter + "_type", capacity).setCostPerDistance(1.0).build();
|
||||
Vehicle vehicle = VehicleImpl.VehicleBuilder.newInstance(depotCounter + "_" + (i+1) + "_vehicle").setLocationCoord(depotCoord).setType(vehicleType).build();
|
||||
vrpBuilder.addVehicle(vehicle);
|
||||
}
|
||||
|
|
@ -73,15 +74,16 @@ public class MultipleDepotExample {
|
|||
/*
|
||||
* plot to see how the problem looks like
|
||||
*/
|
||||
SolutionPlotter.plotVrpAsPNG(vrp, "output/problem01.png", "p01");
|
||||
// SolutionPlotter.plotVrpAsPNG(vrp, "output/problem01.png", "p01");
|
||||
|
||||
/*
|
||||
* solve the problem
|
||||
*/
|
||||
VehicleRoutingAlgorithm vra = VehicleRoutingAlgorithms.readAndCreateAlgorithm(vrp, "input/algorithmConfig.xml");
|
||||
vra.setNuOfIterations(10000);
|
||||
vra.setPrematureBreak(100);
|
||||
vra.getAlgorithmListeners().addListener(new StopWatch(),Priority.HIGH);
|
||||
// vra.getAlgorithmListeners().addListener(new AlgorithmSearchProgressChartListener("output/progress.png"));
|
||||
vra.getAlgorithmListeners().addListener(new AlgorithmSearchProgressChartListener("output/progress.png"));
|
||||
Collection<VehicleRoutingProblemSolution> solutions = vra.searchSolutions();
|
||||
|
||||
SolutionPrinter.print(Solutions.getBest(solutions));
|
||||
|
|
|
|||
|
|
@ -9,6 +9,7 @@ import algorithms.VehicleRoutingAlgorithms;
|
|||
import analysis.AlgorithmSearchProgressChartListener;
|
||||
import analysis.SolutionPlotter;
|
||||
import analysis.SolutionPrinter;
|
||||
import analysis.SolutionPrinter.Print;
|
||||
import analysis.StopWatch;
|
||||
import basics.VehicleRoutingAlgorithm;
|
||||
import basics.VehicleRoutingProblem;
|
||||
|
|
@ -16,9 +17,10 @@ import basics.VehicleRoutingProblem.FleetSize;
|
|||
import basics.VehicleRoutingProblemSolution;
|
||||
import basics.algo.VehicleRoutingAlgorithmListeners.Priority;
|
||||
import basics.io.VrpXMLReader;
|
||||
import basics.route.PenaltyVehicleType;
|
||||
import basics.route.Vehicle;
|
||||
import basics.route.VehicleImpl;
|
||||
import basics.route.VehicleImpl.VehicleType;
|
||||
import basics.route.VehicleTypeImpl;
|
||||
|
||||
public class MultipleDepotExampleWithPenaltyVehicles {
|
||||
|
||||
|
|
@ -50,7 +52,7 @@ public class MultipleDepotExampleWithPenaltyVehicles {
|
|||
int depotCounter = 1;
|
||||
for(Coordinate depotCoord : Arrays.asList(firstDepotCoord,second)){
|
||||
for(int i=0;i<nuOfVehicles;i++){
|
||||
VehicleType vehicleType = VehicleType.Builder.newInstance(depotCounter + "_type", capacity).setCostPerDistance(1.0).build();
|
||||
VehicleTypeImpl vehicleType = VehicleTypeImpl.Builder.newInstance(depotCounter + "_type", capacity).setCostPerDistance(1.0).build();
|
||||
String vehicleId = depotCounter + "_" + (i+1) + "_vehicle";
|
||||
VehicleImpl.VehicleBuilder vehicleBuilder = VehicleImpl.VehicleBuilder.newInstance(vehicleId);
|
||||
vehicleBuilder.setLocationCoord(depotCoord);
|
||||
|
|
@ -59,11 +61,12 @@ public class MultipleDepotExampleWithPenaltyVehicles {
|
|||
Vehicle vehicle = vehicleBuilder.build();
|
||||
vrpBuilder.addVehicle(vehicle);
|
||||
}
|
||||
VehicleType penaltyType = VehicleType.Builder.newInstance(depotCounter + "_type#penalty", capacity).setFixedCost(50).setCostPerDistance(3.0).build();
|
||||
VehicleTypeImpl penaltyType = VehicleTypeImpl.Builder.newInstance(depotCounter + "_type", capacity).setFixedCost(50).setCostPerDistance(3.0).build();
|
||||
PenaltyVehicleType penaltyVehicleType = new PenaltyVehicleType(penaltyType);
|
||||
String vehicleId = depotCounter + "_vehicle#penalty";
|
||||
VehicleImpl.VehicleBuilder vehicleBuilder = VehicleImpl.VehicleBuilder.newInstance(vehicleId);
|
||||
vehicleBuilder.setLocationCoord(depotCoord);
|
||||
vehicleBuilder.setType(penaltyType);
|
||||
vehicleBuilder.setType(penaltyVehicleType);
|
||||
vehicleBuilder.setLatestArrival(maxDuration);
|
||||
Vehicle penaltyVehicle = vehicleBuilder.build();
|
||||
vrpBuilder.addVehicle(penaltyVehicle);
|
||||
|
|
@ -92,12 +95,13 @@ public class MultipleDepotExampleWithPenaltyVehicles {
|
|||
* solve the problem
|
||||
*/
|
||||
VehicleRoutingAlgorithm vra = VehicleRoutingAlgorithms.readAndCreateAlgorithm(vrp, "input/algorithmConfig.xml");
|
||||
vra.setNuOfIterations(5000);
|
||||
vra.setNuOfIterations(80000);
|
||||
vra.setPrematureBreak(1000);
|
||||
vra.getAlgorithmListeners().addListener(new StopWatch(),Priority.HIGH);
|
||||
vra.getAlgorithmListeners().addListener(new AlgorithmSearchProgressChartListener("output/progress.png"));
|
||||
Collection<VehicleRoutingProblemSolution> solutions = vra.searchSolutions();
|
||||
|
||||
SolutionPrinter.print(Solutions.getBest(solutions));
|
||||
SolutionPrinter.print(Solutions.getBest(solutions),Print.VERBOSE);
|
||||
SolutionPlotter.plotSolutionAsPNG(vrp, Solutions.getBest(solutions), "output/p08_solution.png", "p08");
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -43,7 +43,7 @@ import basics.io.VrpXMLWriter;
|
|||
import basics.route.Driver;
|
||||
import basics.route.Vehicle;
|
||||
import basics.route.VehicleImpl;
|
||||
import basics.route.VehicleImpl.VehicleType;
|
||||
import basics.route.VehicleTypeImpl;
|
||||
|
||||
|
||||
/**
|
||||
|
|
@ -157,9 +157,9 @@ public class RefuseCollectionExample {
|
|||
/*
|
||||
* create vehicle-type and vehicle
|
||||
*/
|
||||
VehicleType.Builder typeBuilder = VehicleType.Builder.newInstance("vehicle-type", 23);
|
||||
VehicleTypeImpl.Builder typeBuilder = VehicleTypeImpl.Builder.newInstance("vehicle-type", 23);
|
||||
typeBuilder.setCostPerDistance(1.0);
|
||||
VehicleType bigType = typeBuilder.build();
|
||||
VehicleTypeImpl bigType = typeBuilder.build();
|
||||
|
||||
VehicleImpl.VehicleBuilder vehicleBuilder = VehicleImpl.VehicleBuilder.newInstance("vehicle");
|
||||
vehicleBuilder.setLocationId("1");
|
||||
|
|
|
|||
|
|
@ -36,7 +36,8 @@ import basics.io.VrpXMLWriter;
|
|||
import basics.route.Vehicle;
|
||||
import basics.route.VehicleImpl;
|
||||
import basics.route.VehicleImpl.VehicleBuilder;
|
||||
import basics.route.VehicleImpl.VehicleType;
|
||||
import basics.route.VehicleType;
|
||||
import basics.route.VehicleTypeImpl;
|
||||
|
||||
public class SimpleExample {
|
||||
|
||||
|
|
@ -45,8 +46,8 @@ public class SimpleExample {
|
|||
/*
|
||||
* get a vehicle type-builder and build a type with the typeId "vehicleType" and a capacity of 2
|
||||
*/
|
||||
VehicleType.Builder vehicleTypeBuilder = VehicleImpl.VehicleType.Builder.newInstance("vehicleType", 2);
|
||||
VehicleType vehicleType = vehicleTypeBuilder.build();
|
||||
VehicleTypeImpl.Builder vehicleTypeBuilder = VehicleTypeImpl.Builder.newInstance("vehicleType", 2);
|
||||
VehicleTypeImpl vehicleType = vehicleTypeBuilder.build();
|
||||
|
||||
/*
|
||||
* get a vehicle-builder and build a vehicle located at (10,10) with type "vehicleType"
|
||||
|
|
|
|||
|
|
@ -66,7 +66,7 @@ public class SolomonExample {
|
|||
* The algorithm can be defined and configured in an xml-file.
|
||||
*/
|
||||
VehicleRoutingAlgorithm vra = new SchrimpfFactory().createAlgorithm(vrp);
|
||||
// vra.getAlgorithmListeners().addListener(new AlgorithmSearchProgressChartListener(pngFileName));
|
||||
vra.getAlgorithmListeners().addListener(new AlgorithmSearchProgressChartListener("output/sol_progress.png"));
|
||||
/*
|
||||
* Solve the problem.
|
||||
*
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue