node-or-tools icon indicating copy to clipboard operation
node-or-tools copied to clipboard

Fairness - balance number of locations serviced per vehicle

Open daniel-j-h opened this issue 8 years ago • 3 comments

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 Distribute constraint
  • Add a Deviation constraint for vehicle counts targeting the deviation var
  • Minimizing the deviation var

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);

daniel-j-h avatar Apr 18 '17 23:04 daniel-j-h

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?

albertsgrc avatar Oct 23 '17 00:10 albertsgrc

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?

emakarov avatar Oct 23 '17 03:10 emakarov

@albertsgrc could you check whether following approach will work:

  1. You create a solution with your existing model, add deviationVar to the assignment (if its possible) and check its' value when solution is found
  2. 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

emakarov avatar Oct 23 '17 03:10 emakarov