Skip to content

Instantly share code, notes, and snippets.

@ifle
Last active March 22, 2018 12:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ifle/887a336809314d513bcf393ff36cf5ef to your computer and use it in GitHub Desktop.
Save ifle/887a336809314d513bcf393ff36cf5ef to your computer and use it in GitHub Desktop.
jsprit pickup\delivery problem
package com.graphhopper.jsprit.examples;
import com.graphhopper.jsprit.analysis.toolbox.GraphStreamViewer;
import com.graphhopper.jsprit.analysis.toolbox.GraphStreamViewer.Label;
import com.graphhopper.jsprit.analysis.toolbox.Plotter;
import com.graphhopper.jsprit.core.algorithm.VehicleRoutingAlgorithm;
import com.graphhopper.jsprit.core.algorithm.box.Jsprit;
import com.graphhopper.jsprit.core.problem.Location;
import com.graphhopper.jsprit.core.problem.VehicleRoutingProblem;
import com.graphhopper.jsprit.core.problem.VehicleRoutingProblem.FleetSize;
import com.graphhopper.jsprit.core.problem.cost.VehicleRoutingTransportCosts;
import com.graphhopper.jsprit.core.problem.job.Shipment;
import com.graphhopper.jsprit.core.problem.solution.VehicleRoutingProblemSolution;
import com.graphhopper.jsprit.core.problem.vehicle.VehicleImpl;
import com.graphhopper.jsprit.core.problem.vehicle.VehicleImpl.Builder;
import com.graphhopper.jsprit.core.problem.vehicle.VehicleType;
import com.graphhopper.jsprit.core.problem.vehicle.VehicleTypeImpl;
import com.graphhopper.jsprit.core.reporting.SolutionPrinter;
import com.graphhopper.jsprit.core.util.Coordinate;
import com.graphhopper.jsprit.core.util.Solutions;
import com.graphhopper.jsprit.core.util.VehicleRoutingTransportCostsMatrix;
import com.graphhopper.jsprit.io.problem.VrpXMLWriter;
//import com.graphhopper.jsprit.util.ConsolePrinter;
import com.graphhopper.jsprit.util.Examples;
import java.util.Arrays;
import java.util.Collection;
public class Sample {
public static void main(String[] args) {
/*
* some preparation - create output folder
*/
Examples.createOutputFolder();
VehicleTypeImpl.Builder vehicleTypeBuilder = VehicleTypeImpl.Builder.newInstance("vehicleType");
VehicleType vehicleType = vehicleTypeBuilder.build();
Builder vehicleBuilder1 = Builder.newInstance("0");
vehicleBuilder1.setStartLocation(loc("0", Coordinate.newInstance(1, 1)))
.setReturnToDepot(false);
vehicleBuilder1.setType(vehicleType);
VehicleImpl vehicle1 = vehicleBuilder1.build();
VehicleRoutingProblem.Builder vrpBuilder = VehicleRoutingProblem.Builder.newInstance();
double matrix[][]= {{0,8.9,18.9,95.6,107.4},{53.1,0,10,86.7,98.5},{107.9,116.8,0,76.7,88.5},{31.2,40.1,50.1,0,11.8},{19.4,28.3,38.3,115,0}};
VehicleRoutingTransportCostsMatrix.Builder costMatrixBuilder = VehicleRoutingTransportCostsMatrix.Builder
.newInstance(false);
for(int i=0; i<matrix.length; i++)
{
for(int j=0; j<matrix.length; j++)
costMatrixBuilder.addTransportTime(String.valueOf (i), String.valueOf(j), matrix[i][j]);
}
VehicleRoutingTransportCosts costMatrix = costMatrixBuilder.build();
vrpBuilder.setRoutingCost(costMatrix);
vrpBuilder.addJob(Shipment.Builder.newInstance("1")
.setPickupLocation(loc("1", Coordinate.newInstance(2, 1)))
.setDeliveryLocation(loc("2", Coordinate.newInstance(3, 1)))
.build());
vrpBuilder.addJob(Shipment.Builder.newInstance("2")
.setPickupLocation(loc("3", Coordinate.newInstance(3, 2)))
.setDeliveryLocation(loc("4", Coordinate.newInstance(1, 2)))
.build());
vrpBuilder.addVehicle(vehicle1);
vrpBuilder.setFleetSize(FleetSize.FINITE);
VehicleRoutingProblem problem = vrpBuilder.build();
VehicleRoutingAlgorithm algorithm = Jsprit.createAlgorithm(problem);
Collection<VehicleRoutingProblemSolution> solutions = algorithm.searchSolutions();
VehicleRoutingProblemSolution bestSolution = Solutions.bestOf(solutions);
new VrpXMLWriter(problem, solutions).write("output/example1.xml");
//ConsolePrinter.printMatrix("Cost Matrix", matrix);
SolutionPrinter.print(problem, bestSolution, SolutionPrinter.Print.VERBOSE);
Plotter problemPlotter = new Plotter(problem);
problemPlotter.plotShipments(true);
problemPlotter.plot("output/example1_problem.png", "example1");
Plotter solutionPlotter = new Plotter(problem, Arrays.asList(Solutions.bestOf(solutions).getRoutes().iterator().next()));
solutionPlotter.plotShipments(true);
solutionPlotter.plot("output/example1_solution.png", "example1");
new GraphStreamViewer(problem, Solutions.bestOf(solutions)).labelWith(Label.ACTIVITY).setRenderDelay(100)
.setRenderShipments(true).display();
}
private static Location loc(String id, Coordinate coordinate) {
return Location.Builder.newInstance().setId(id).setCoordinate(coordinate).build();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment