Reputation: 312
I'm trying to solve the vehicle routing problem using Optaplanner. While the code works ok, I noticed something curious.
When I build the model I have a list of vehicles that have different characteristics. The vehicle fleet is heterogeneous and is composed of multiple vehicle types. Each vehicle type can have 1 or more vehicles. Vehicle category A could be a large truck while vehicle category B could be a small truck (and so on).
While I'm able to model correctly the vehicles in Optaplanner, when I actually run the solver I realised that the vehicles get allocated based on their position in the list. So if I place all vehicles A at the beginning, the solver will try to use them first, before using the vehicles B and vice-versa.
Here is the sample of the model that I use (very similar to what I found in Github):
@PlanningSolution
public class VehicleRoutingSolution {
@ProblemFactCollectionProperty
protected List<Location> locationList;
@ProblemFactCollectionProperty
protected List<Depot> depotList;
@PlanningEntityCollectionProperty
protected List<Vehicle> vehicleList;
@ProblemFactCollectionProperty
@ValueRangeProvider
protected List<Customer> customerList;
@PlanningScore
protected HardSoftLongScore score;
}
@PlanningEntity
public class Vehicle {
private int weight;
private int volume;
private float cost;
@PlanningListVariable
private List<Customer> customers = new ArrayList<>();
}
...
My goal is not only to allocate orders inside the trucks without breaking their constraint (weight, volume), but more importantly make the solver decide which truck is best to use given the specific conditions and minimize the overall score (cost, distance, time), primarily because the routing distance changes as well based on the vehicle type.
Is there a way to model the domain so that I can make Optaplanner pick the best truck and not use them based on their position in the PlanningEntityCollectionProperty
list?
Upvotes: 0
Views: 127
Reputation: 5702
OptaPlanner (and Timefold) doesn't have any preference for assigning one truck or the other. You do. You see the solver doing something and you don't like it. This tells me you are missing some constraints.
They way how you specify your preference is by writing a constraint to penalize the unwanted behavior, or reward the preferred behavior. Every time you see a solution you don't like, think whether or not you've given the solver enough information to avoid that solution.
Upvotes: 0