Fixed Fleet Size Constraint Implementation

I have to solve a VRPTW problem with the constraint that the solution must have a fixed number of vehicles.

I have tried to search inside the library on how to implement this constraint. Through the SolomonReader I tried to insert manually a certain nr of vehicles but this helped only in defining the max number of possible vehicles (but still it was showing solutions with a lower nr of vehicles).

How can I implement a fixed fleet size constraint?

Use this to fix the fleet size and set it to FleetSize.FINITE. This specifies the maximum number of vehicles that can be used.
If you want to specify the max number of vehicle and to use all vehicle, you need to find appropriate constraints, e.g. reduce capacity, reduce the operation time of your drivers (earliestStart and latestArrival) etc… The logic to find such constraints is up to you and your problem. jsprit cannot determine this.


When i set FleetSize to FINITE, how can I specify the maximum number of vehicles? For example in problem rc101 i would like to fix the size to 16 routes.

As I wrote, you need to define appropriate constraints. For example,
reduce vehicle’s operation time (earliestStart, latestArrival) so that
you can just serve all your customers with 16 vehicles. This is what you
need to do yourself. If you can make it manually, you should also be
able to automate it.

Am 18/12/15 um 09:47 schrieb Martino Mensio:

You will need to add 16 vehicles and set FleetSize Finite.
A piece of sample codes can be found below:

    VehicleRoutingProblem.Builder vrpBuilder = VehicleRoutingProblem.Builder.newInstance();
    VehicleTypeImpl vehicleType = ...
    vehicleCount = 16;
    for (int i = 0; i < vehicleCount; i++) {
        VehicleImpl vehicle = VehicleImpl.Builder.newInstance("vehicle" + i)
                . ...

Thank you for your reply. Very useful sample code.
Now i only need to find a good way of forcing to use all the vehicles, without excluding some good solutions

Just this code or need to add more … because it is not working.

Hi @freddyfli,

Take a look at this example and see how Stefan added multiple vehicles to vrpBuilder and defined the problem with finite fleet.

Best regards,

I implemented this solution and it works! Thank you a lot @stefan
Now i want that if i have to create, for example, 10 vehicles; and i want that all of these cover all the demands so that there aren’t unassigned Jobs. How can i perform it? I have to add more constrains? Or maybe i have to shape better the capacity of each vehicles?

Hi @stefan …I have a fixed number of vehicles and there aren’t unassigned jobs but the cost is bed …why???

@Hendrix: Whether or not jobs end up being unassigned can depend on many constraints such as capacity, time-window, operation time of drivers, etc… For example, if the physical constraints of your vehicle do not allow to serve all your customers, you either need to add more vehicles, reduce the size of service (which should actually be a physical constraint as well) or increase the capacity of your vehicles.

1 Like

@freddyfli I do not know. Please be more precise and give examples. The best way would be to reduce your case to a very simple one so that we can analyse the reason for your “bad” solution.

1 Like

Hey! Have you implemented by forcing all vehicle to utilize?

Last year we “solved” the problem by means of some tricks:

  • limit on number of vehicles: vrpBuilder.setFleetSize(FleetSize.FINITE) and instantiating the number of desired vehicles as an upper bound
  • soft constraint: we added penalties to solutions that didn’t use all the desired vehicles. This can converge to wanted solution but could be too slow (because working on finished solutions: selection at the end)
  • hard constraint: in the isertionContext, we don’t allow some solutions to be generated. The criteria that we followed was to put a limit of jobs per tour. The hard constraint could also have been set on the capacity of the vehicles or by playing with EarliestStart and LatestArrival

If you want to see our “dirty tricks” you can have a look at this repository that contains our school project. The relevant classes are that contains the soft and hard constraint, while contains the change on the fleet size.
You can find more details also in the presentation PDF

Hope this can be helpful for a better designed approach. Good luck!


Hey! @MartinoMensio Thanks for giving responded.
I read your presentation and you and your team done nice work on a project.
Is it work for finite fleet size to forcefully utilize the vehicle? Till then I have to check the code.

Is anybody gets any approach to deal with it?

Thank you very much!
Yes it worked on all the instances selected in this file, but in order to work it needs a lot of iterations (because the soft constraint is applied on completely built solutions and the hard constraint is not so restricting)

1 Like