Google OR-Tools: Total Time Capacity over All Routes Combined

Viewed 155

In Google OR-Tools Java, how do you set a dimension capacity over all routes combined?

My use case is for the total duration of all routes—-there can only be X hours of travel for the entire fleet of vehicles. I see documentation online for how to set a span cost coefficient, soft upper bound, or capacity for all vehicles taken individually. How do I set a hard upper bound for all vehicles taken together?

The code below is not the right solution, since it sets capacities for all routes individually:

router.addDimension(transitCallbackIndex, 0, totalSecondsForAnIndividualRoute, true, "Time");
1 Answers

Basically you need to use the underlying solver and add a constraint

"sum of cumulVar of all vehicle end node is less than your hard limit"

Java sample based on https://github.com/google/or-tools/blob/stable/ortools/constraint_solver/samples/VrpGlobalSpan.java

import java.util.ArrayList; // import the ArrayList class
import com.google.ortools.constraintsolver.Solver;
import com.google.ortools.constraintsolver.IntVar;
...
  // Constraint sum of all distance to be <= 5500
  Solver solver = routing.solver();
  ArrayList<IntVar> ends = new ArrayList<>();
  for( int i=0; i < manager.getNumberOfVehicles(); ++i) {
    long index = routing.end(i);
    IntVar tmp = distanceDimension.cumulVar(index);
    ends.add(tmp);
  }
  IntVar[] vars = ends.toArray(new IntVar[0]);
  solver.addConstraint(solver.makeSumLessOrEqual(vars, 5500));
Related