1

Is it possible to define a seperate cost matrix for every vehicle type in Jsprit? I have a number of very different vehicle types (trucks, bikes, cars, electrical pickups etc.) and every type has its own cost matrix. The matrices are not linearly dependent, therefore working with different cost factors for distance and time is not an option. The VRP has infinite fleet size.

I use JSprit 1.6.2 and implemented the AbstractForwardVehicleRoutingTransportCosts-Interface. Both of its methods have a vehicle parameter that I use to select the correct matrix, but the passed values are always null and subsequently a NullPointerException is thrown. Any ideas why this approach is not working and how I can get it to work?

Thanks in advance!

Jan
  • 13
  • 3
  • I'm curious to know why the cost matrix is not linearly dependent? Is this because your vehicles have some kind of differing access restrictions e.g. bikes getting through bollards, or something like different maximum speeds? – roganjosh May 01 '16 at 16:25
  • That is absolutly correct. Different modes of transportation may have completey different routes between the same two locations. Furthermore, there are many cases where e.g. trucks are not even able to serve a certain relations due to access restructions (e.g. max weight or height exceed some threshold value). – Jan May 02 '16 at 14:00

1 Answers1

5

The problem seems similar to a post in the mailing list: Vehicle dependent velocities in Jsprit. Below is Stefan's answer in that post:

You need to implement your own VehicleRoutingTransportCosts. Here you need to differentiate between vehicle types. For example, if you have two travel time matrices motorbikeMatrix and truckMatrix, then you specify in your implementation that motorbikeMatrix should be used if vehicle is of type motorbike.

I suppose you already have those vehicle-type-dependent cost matrices, and your problem would be to call corresponding cost matrix in the VehicleRoutingTransportCosts class.

Something like:

vrpBuilder.setRoutingCost(new MultiVehTypeCosts(vrpBuilder.getLocations(), motorbikeMatrix, truckMatrix, ...));

Then in the MultiVehTypeCosts class, in

getTransportCost(Location from, Location to, double time, Driver driver, Vehicle vehicle) {}

and

getTransportTime(Location from, Location to, double time, Driver driver, Vehicle vehicle) {}

you have something like:

    if (vehicle.getType().getTypeId().equals("motorbike")) {
        double time = motorbikeMatrix[from.getIndex()][to.getIndex()][1];
        double distance = motorbikeMatrix[from.getIndex()][to.getIndex()][0];
        VehicleTypeImpl.VehicleCostParams costParams = vehicle.getType().getVehicleCostParams();
        double cost = costParams.perDistanceUnit * distance + costParams.perTimeUnit * time;
        ....
    }
He Huang
  • 136
  • 3
  • That is exacty what my implementation is doing. The problem is, that this line _vehicle.getType().getTypeId().equals("motorbike")_ throws a NullPointerException, because the vehicle parameter is _null_. Can you think of a reason why this might be the case? – Jan May 04 '16 at 13:14
  • 1
    @Jan: Yes you need to handle the case vehicle == null in your getTransportCost() method. You can check any transport cost calculator in package com.graphhopper.jsprit.core.util, e.g., CrowFlyCosts, FastVehicleRoutingTransportCostsMatrix, etc. They all have handled the case vehicle == null, and they just make cost = distance in that case. Note that distance does not depend on vehicle type in their cases but it does in yours. I guess what you can do is that you use some default vehicle type in that case. As for why vehicle can be null, see the next comment. – He Huang May 05 '16 at 04:08
  • 1
    @Jan: vehicle can be null because the method is called as vehicle = null in the algorithm. For example, https://github.com/graphhopper/jsprit/blob/master/jsprit-core/src/main/java/com/graphhopper/jsprit/core/algorithm/ruin/distance/AvgServiceAndShipmentDistance.java#L86, https://github.com/graphhopper/jsprit/blob/master/jsprit-core/src/main/java/com/graphhopper/jsprit/core/algorithm/recreate/DefaultScorer.java#L76 and https://github.com/graphhopper/jsprit/blob/master/jsprit-core/src/main/java/com/graphhopper/jsprit/core/algorithm/ruin/DBSCANClusterer.java#L86. – He Huang May 05 '16 at 04:09
  • Thank you He Huang – Jan May 05 '16 at 05:47