2

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");
davidjf
  • 23
  • 1
  • 4

1 Answers1

1

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));
Mizux
  • 8,222
  • 7
  • 32
  • 48
  • Thanks, Mizux. Works great. Notes to others: [1] With this heavier constraint, I needed ALL_UNPERFORMED as a [FirstSolutionsStrategy.](https://developers.google.com/optimization/routing/routing_options#first_sol_options) [2] In implementing this, I broke the code at first by setting distanceDimension.setSpanCostCoefficientForAllVehicles as well on the same dimension. That changes the scaling of the distanceDimension.cumulVar(). – davidjf Aug 12 '21 at 22:01