graphhopper / jsprit

jsprit is a java based, open source toolkit for solving rich vehicle routing problems
https://www.graphhopper.com/open-source/
Apache License 2.0
1.64k stars 605 forks source link

How to use cost matrix to solve multi-vehicle routing problem? #534

Closed touchmii closed 2 years ago

touchmii commented 3 years ago

I changed the CostMatriEample.java and added a car, but the result was a route for only one car. Whether the routing result of multiple vehicles can be calculated by cost matrix. I want to embed Jsprit into my scheduling software. The calculation of the distance between each position is complicated (there are many points between them), which has been calculated by the algorithm. Can Jsprit calculate the routing of multiple vehicles through the cost matrix

`public class CostMatrixExample {

public static void main(String[] args) {
    /*
     * some preparation - create output folder
     */
    Examples.createOutputFolder();

    VehicleType type = VehicleTypeImpl.Builder.newInstance("type").addCapacityDimension(0, 1).setCostPerDistance(1).setCostPerTime(2).build();
    VehicleImpl vehicle = VehicleImpl.Builder.newInstance("vehicle1")
        .setStartLocation(Location.newInstance("0")).setType(type).build();
    VehicleImpl vehicle2 = VehicleImpl.Builder.newInstance("vehicle2")
        .setStartLocation(Location.newInstance("3")).setType(type).build();

    Service s1 = Service.Builder.newInstance("1").addSizeDimension(0, 1).setLocation(Location.newInstance("1")).build();
    Service s2 = Service.Builder.newInstance("2").addSizeDimension(0, 1).setLocation(Location.newInstance("2")).build();
    Service s3 = Service.Builder.newInstance("3").addSizeDimension(0, 1).setLocation(Location.newInstance("3")).build();
    Service s4 = Service.Builder.newInstance("4").addSizeDimension(0, 1).setLocation(Location.newInstance("1")).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).addVehicle(vehicle2).addJob(s1).addJob(s2).addJob(s3).addJob(s4).build();

    VehicleRoutingAlgorithm vra = Jsprit.createAlgorithm(vrp);

    Collection<VehicleRoutingProblemSolution> solutions = vra.searchSolutions();
    VehicleRoutingProblemSolution best = Solutions.bestOf(solutions);
    SolutionPrinter.print(Solutions.bestOf(solutions));

// vra.

    new Plotter(vrp, Solutions.bestOf(solutions)).plot("output/yo.png", "po");

// new GraphStreamViewer(vrp, Solutions.bestOf(solutions)).setRenderDelay(100).display();

}

}`

Screen Shot 2021-11-07 at 9 20 44 PM ?