1
0
Fork 0
mirror of https://github.com/graphhopper/jsprit.git synced 2020-01-24 07:45:05 +01:00
graphhopper-jsprit/docs/Bigger Multiple Depot VRP.md
2016-09-01 12:03:14 +02:00

4.2 KiB
Raw Blame History

This example covers:

  • defining and creating different 'depots', vehicles and their types
  • defining a problem with finite fleet-size
  • reading and creating an algorithm
  • plotting the solution

It is based on the problem instance P08 defined by

Cordeau, J.-F., Gendreau, M. and Laporte, G. (1997), A tabu search heuristic for periodic and multi-depot vehicle routing problems. Networks, 30: 105119.

Please visit for example this site to get more information on Multiple Depot VRP.

Before you start, add the latest release to your pom. Additionally, create an output folder in your project directory. Either do it manually or add the following lines to your code:

File dir = new File("output");
// if the directory does not exist, create it
if (!dir.exists()){
	System.out.println("creating directory ./output");
	boolean result = dir.mkdir();  
	if(result) System.out.println("./output created");  
}

All services of P08 are stored in an xml-file called vrp_cordeau_08.xml (you can find it here). You read them into your problemBuilder as follows

VehicleRoutingProblem.Builder vrpBuilder = VehicleRoutingProblem.Builder.newInstance();
/*
 * Read cordeau-instance p08, BUT only its services without any vehicles 
 */
new VrpXMLReader(vrpBuilder).read("input/vrp_cordeau_08.xml");

Define depots and vehicles:

/*
 * add vehicles with its depots
 * 2 depots with the following coordinates:
 * (-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);		
int depotCounter = 1;

for(Coordinate depotCoord : Arrays.asList(firstDepotCoord,second)){
    for(int i=0;i<nuOfVehicles;i++){
        String typeId = depotCounter + "_type";
        VehicleType vehicleType = VehicleTypeImpl.Builder.newInstance(typeId).addCapacityDimension(0,capacity).setCostPerDistance(1.0).build();
        String vehicleId = depotCounter + "_" + (i+1) + "_vehicle";
        VehicleImpl.VehicleBuilder vehicleBuilder = VehicleImpl.Builder.newInstance(vehicleId);
        vehicleBuilder.setStartLocation(depotCoord);  //defines the location of the vehicle and thus the depot
        vehicleBuilder.setType(vehicleType)
        vehicleBuilder.setLatestArrival(maxDuration);
        Vehicle vehicle = vehicleBuilder.build();
        vrpBuilder.addVehicle(vehicle);
	}
	depotCounter++;
}

Build the problem, and define and run an algorithm like this:


/*
 * define problem with finite fleet
 */
vrpBuilder.setFleetSize(FleetSize.FINITE);
		
/*
 * build the problem
 */
VehicleRoutingProblem vrp = vrpBuilder.build();
/*
 * solve the problem
 */
VehicleRoutingAlgorithm vra = VehicleRoutingAlgorithms.readAndCreateAlgorithm(vrp,"input/algorithmConfig.xml");
Collection solutions = vra.searchSolutions();

The problem will be looking like this:

p08

Running this algorithm yields to the following solution:

solution_p08

You can find the entire code here.