1
0
Fork 0
mirror of https://github.com/graphhopper/jsprit.git synced 2020-01-24 07:45:05 +01:00
graphhopper-jsprit/docs/before-1.7/Read-classical-VRP-instance---with-time-windows.md
2016-09-01 12:25:57 +02:00

3.1 KiB

This example covers

  • reading a classical VRP instance (here the Solomon instance C101),
  • plotting the problem,
  • reading and running a predefined algorithm,
  • plotting the solution.

Make sure, your pom is prepared (see Add the latest snapshot to your pom). Additionally, create an output folder in your project directory. Either do it manually or add the following lines to your code (even this obfuscates the code-example a bit):

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");
}

Download the solomon problem instance (for instance here) or download C101_solomon.txt. It is assumed you put the instance file into a folder called 'input'.

Read and build the problem:

/*
 * define problem-builder first
 */
VehicleRoutingProblem.Builder vrpBuilder = VehicleRoutingProblem.Builder.newInstance();

/*
 * Read solomon instance with SolomonReader
 * Note that the reader assigns fixed costs of 100 to each vehicle used (even the original problem does not
 * exhibit any fixed cost components). Total costs should indicate then
 * nuOfVehicles * 100 + variable costs
 */
new SolomonReader(vrpBuilder).read("input/C101_solomon.txt");

/*
 * Build the problem. By default, transport costs are calculated as Euclidean distances.
 */
VehicleRoutingProblem vrp = vrpBuilder.build();

Plot the problem to see how it looks like:

SolutionPlotter.plotVrpAsPNG(vrp, "output/solomon_C101.png", "C101");

It looks like this.

To solve it, define an algorithm. Here, it comes out-of-the-box. The SchrimpfFactory creates an algo which is an implemenation of Schrimpf et al.. In this configuration, it is best suited to solve the VRP with time windows.

/*
* get the algorithm out-of-the-box.
*/
VehicleRoutingAlgorithm algorithm = new SchrimpfFactory().createAlgorithm(problem);

/*
* and search a solution which returns a collection of solution (here only one solution is in the collection)
*/
Collection solutions = algorithm.searchSolutions();

/*
 * use helper to get the best
 */
VehicleRoutingProblemSolution bestSolution = Solutions.bestOf(solutions);

Plot the solution now to analyse how it looks like:

SolutionPlotter.plotSolutionAsPNG(vrp, "output/solomon_C101_solution.png", "C101");

It looks like this.

Get the entire code of this example here.