Sam Barnum
Sam Barnum

Reputation: 10714

JSprit not using closer vehicle, due to time issues

Single-job schedule with two vehicles. One vehicle starts close to the job, the other starts far from the job. Seems it should prefer to use the closer vehicle, as there's a cost-per-distance. But it uses the farther one, if there's a non-zero setCostPerWaitingTime(). Why?

public void testUseCloserVehicleWhenCostsAreSet() throws Exception {
    VehicleType type = VehicleTypeImpl.Builder.newInstance("generic")
            .setCostPerDistance(0.017753)
            //.setCostPerTransportTime(1.0)
            .setCostPerWaitingTime(1.0)
            .build();
    double serviceTime = 420.0;
    Location pointA = Location.newInstance(100.0, 100.0);
    Location pointB = Location.newInstance(100.0, 200.0);
    Location closeToPointA = Location.newInstance(110.0, 110.0);
    Location farFromPointA = Location.newInstance(500.0, 110.0);
    VehicleRoutingProblem vrp = VehicleRoutingProblem.Builder
            .newInstance()
            .setFleetSize(VehicleRoutingProblem.FleetSize.FINITE)
            .addVehicle(VehicleImpl.Builder.newInstance("CloseBy")
                    .setType(type)
                    .setStartLocation(closeToPointA)
                    .build())
            .addVehicle(VehicleImpl.Builder.newInstance("FarAway")
                    .setType(type)
                    .setStartLocation(farFromPointA)
                    .build())
            .addJob(Shipment.Builder.newInstance("123")
                    .setPickupLocation(pointA)
                    .setPickupServiceTime(serviceTime)
                    .setDeliveryLocation(pointB)
                    .setDeliveryServiceTime(serviceTime)
                    .setPickupTimeWindow(new TimeWindow(36000.0, 36360.0))
                    .setDeliveryTimeWindow(new TimeWindow(36360.0, 36720.0))
                    .setMaxTimeInVehicle(720.0)
                    .build())
            .build();

    VehicleRoutingAlgorithm algorithm = Jsprit.Builder.newInstance(vrp)
            .buildAlgorithm();

    VehicleRoutingProblemSolution bestSolution = Solutions.bestOf(algorithm.searchSolutions());
    SolutionPrinterWithTimes.print(vrp, bestSolution, SolutionPrinterWithTimes.Print.VERBOSE);
    System.out.flush();
    assertEquals("CloseBy", bestSolution.getRoutes().iterator().next().getVehicle().getId());
}

Result:

+----------------------------------------------------------+
| solution                                                 |
+---------------+------------------------------------------+
| indicator     | value                                    |
+---------------+------------------------------------------+
| costs         | 35616.03246830352                        | 
| noVehicles    | 1                                        | 
| unassgndJobs  | 0                                        | 
+----------------------------------------------------------+
+--------------------------------------------------------------------------------------------------------------------------------+
| detailed solution                                                                                                              |
+---------+----------------------+-----------------------+-----------------+-----------------+-----------------+-----------------+
| route   | vehicle              | activity              | job             | arrTime         | endTime         | costs           |
+---------+----------------------+-----------------------+-----------------+-----------------+-----------------+-----------------+
| 1       | FarAway              | start                 | -               | undef           | 0               | 0               |
| 1       | FarAway              | pickupShipment        | 123             | 400             | 36420           | 35607           |
| 1       | FarAway              | deliverShipment       | 123             | 36520           | 36940           | 35609           |
| 1       | FarAway              | end                   | -               | 37350           | undef           | 35616           |
+--------------------------------------------------------------------------------------------------------------------------------+
junit.framework.ComparisonFailure: 
Expected :CloseBy
Actual   :FarAway

I suspect it has something to do with the vehicle arriving at 400 for a job that can't start until 36000. Is there a way to prevent that, so the vehicle starts only as early as needed to reach the first job? Does setCostPerWaitingTime do something other than what I think?

Upvotes: 0

Views: 198

Answers (1)

Sam Barnum
Sam Barnum

Reputation: 10714

Here's a comparison of the job with only the CloseBy vehicle.

+--------------------------------------------------------------------------------------------------------------------------------+
| detailed solution                                                                                                              |
+---------+----------------------+-----------------------+-----------------+-----------------+-----------------+-----------------+
| route   | vehicle              | activity              | job             | arrTime         | endTime         | costs           |
+---------+----------------------+-----------------------+-----------------+-----------------+-----------------+-----------------+
| 1       | FarAway              | start                 | -               | undef           | 0               | 0               |
| 1       | FarAway              | pickupShipment        | 123             | 400             | 36420           | 35607           |
| 1       | FarAway              | deliverShipment       | 123             | 36520           | 36940           | 35609           |
| 1       | FarAway              | end                   | -               | 37350           | undef           | 35616           |
+--------------------------------------------------------------------------------------------------------------------------------+

+--------------------------------------------------------------------------------------------------------------------------------+
| detailed solution                                                                                                              |
+---------+----------------------+-----------------------+-----------------+-----------------+-----------------+-----------------+
| route   | vehicle              | activity              | job             | arrTime         | endTime         | costs           |
+---------+----------------------+-----------------------+-----------------+-----------------+-----------------+-----------------+
| 1       | CloseBy              | start                 | -               | undef           | 0               | 0               |
| 1       | CloseBy              | pickupShipment        | 123             | 14              | 36420           | 35986           |
| 1       | CloseBy              | deliverShipment       | 123             | 36520           | 36940           | 35988           |
| 1       | CloseBy              | end                   | -               | 37031           | undef           | 35989           |
+--------------------------------------------------------------------------------------------------------------------------------+

I think the problem is that the CloseBy vehicle arrives sooner, so it pays higher wait costs, while the other vehicle is driving during that time so pays less in wait costs. This would be mitigated if the vehicle didn't start until it needed to, but I'm unsure how to set that up.

Upvotes: 0

Related Questions