Fairness - balance number of locations serviced per vehicle
We want to be able to balance the number of locations serviced by each vehicle to be as even as possible. The use-case is for example drivers who are getting paid (at least partially) by number of deliveries.
This constraint should be behind a flag and optional.
Implementation: use the underlying solver to
- Establish a vehicle vars <-> vehicle counts relationship via the
Distributeconstraint - Add a
Deviationconstraint for vehicle counts targeting thedeviationvar - Minimizing the
deviationvar
Roughly as follows:
std::vector<IntVar*> vehicleCounts;
for (auto vehicle = 0; vehicle < numVehicles; ++vehicle)
vehicleCounts.push_back(solver->MakeIntVar(0, numNodes));
solver->AddConstraint(solver->MakeDistribute(vehicleVars, vehicleCounts));
auto* deviationVar = solver->MakeIntVar(0, numNodes * numNodes);
solver->AddConstraint(solver->MakeDeviation(vehicleCounts, deviationVar, numNodes));
model.AddVariableMinimizedByFinalizer(deviationVar);
Hi @daniel-j-h,
I have tried implementing your solution and came up with the following code, very similar to yours:
// vehicleCounts[i] == # of nodes being serviced by vehicle i
std::vector<operations_research::IntVar*> vehicleCounts;
// vehicleVars[i] == vehicle attending node i
std::vector<operations_research::IntVar*> vehicleVars;
for (auto vehicle = 0; vehicle < numVehicles; ++vehicle) {
vehicleCounts.push_back(solver->MakeIntVar(0, numNodes));
}
for (auto node = 0; node < numNodes; ++node) {
vehicleVars.push_back(model.VehicleVar(model.NodeToIndex((NodeIndex)(node))));
}
// Definition of vehicleCounts
solver->AddConstraint(solver->MakeDistribute(vehicleVars, vehicleCounts));
// Holds deviation from ideally balanced route assignment
auto* deviationVar = solver->MakeIntVar(0, numNodes * numNodes);
// Definition of the deviation from the ideally balanced route assignment
solver->AddConstraint(solver->MakeDeviation(vehicleCounts, deviationVar, numNodes));
// Minimize the deviation
model.AddVariableMinimizedByFinalizer(deviationVar);
model.CloseModel();
I more or less understand the meaning of the constraints and how they work together with the minimisation rule to implement the balancing but I am clearly missing something, as the solutions are still unbalanced, with some vehicles being unused. Do you have any idea on how to get it working?
hi all. I'm also interested in such model. But I'm not totally sure that proposed approach will work. Because, AddVariableMinimizedByFinalizer is working "after" solution is found, not during the search process. In this case I assume it cannot change the solution dramatically. What were the assumptions that such approach will lead to required distribution?
@albertsgrc could you check whether following approach will work:
- You create a solution with your existing model, add deviationVar to the assignment (if its possible) and check its' value when solution is found
- By some technic, for example incrementally decreasing the Max value (horizon) for deviationVar domain, try to find another solution. And with several iterations compare solutions