Google/OR-Tools Get Duration And Distance

Viewed 181

I'm trying to understand the solution call in the MVRP examples

I have two matrixes, duration and distance that have been returned via calls to google

My solution is based on distance but given that i have the data already returned i want to find the index associated with the duration. unfortunately I'm not sure completely what is going on under the hood of the the Routing Calls so hoping for a simple fast answer for look up and what index to use

for simplicity sake I will show the google example rather than my code and highlight what im looking for:

    public string PrintSolution()
        {
            // Inspect solution.
            string ret = "";
            long maxRouteDistance = 0;
            for (int i = 0; i < _data.Drivers; ++i)
            {
                ret += $"Route for Vehicle {i}:";
                ret += Environment.NewLine;
                long routeDistance = 0;
                var index = _routing.Start(i);
                while (_routing.IsEnd(index) == false)
                {
                    ret += $"{_manager.IndexToNode((int) index)} -> ";

                    var previousIndex = index;
                    index = _solution.Value(_routing.NextVar(index));
                    long legDistance = _routing.GetArcCostForVehicle(previousIndex, index, i);
                    //LOOKING FOR
                    //long legDuration = ??? what index am is using here to find in my duration matrix which is built the same as indexes as distance
                    ret += " leg distance: " + legDistance;
                    routeDistance += legDistance;
                }

                ret += $"{_manager.IndexToNode((int) index)}";
                ret += Environment.NewLine;
                ret += $"Distance of the route: {routeDistance}m";
                ret += Environment.NewLine;
                ret += Environment.NewLine;
                maxRouteDistance = Math.Max(routeDistance, maxRouteDistance);
            }

            ret += $"Maximum distance of the routes: {maxRouteDistance}m";
            ret += Environment.NewLine;
            return ret;
        }

@Mizux

1 Answers

disclaimer: This is a simplification but should help you to understand.

In OR-Tools Routing there is a primal "hidden" dimension without name but you can retrieve the cost using RoutingModel::GetArcCostForVehicle()

For any "regular" dimension you can get inspect the CumulVar at each node. e.g. supposing you have created two dimensions using RoutingModel::AddDimension() whose name were "Distance" and "Duration". note: CumulVar is an accumulator so if you want the "arc cost" you'll need something like this dim.CumulVar(next_index) - dim.CumulVar(index)

Then in you PrintFunction you can use:

public string PrintSolution()
{
  ...
  RoutingDimension distanceDimension = routing.GetMutableDimension("Distance");
  RoutingDimension durationDimension = routing.GetMutableDimension("Duration");
  for (int i = 0; i < _manager.getNumberOfVehicles(); ++i)
  {
    while (_routing.IsEnd(index) == false)
    {
      ...
      IntVar distanceVar = distanceDimension.CumulVar(index);
      IntVar durationVar = durationDimension.CumulVar(index);
      long distance = _solution.Value(distanceVar);
      long duration = _solution.Value(durationVar);
      ...
    }
  }
}
Related