Control extra slack time with breaks #3269
-
hello community, Our requirement is, we want to place a break of 20 mins for our driver in between 4hrs - 6hrs. Here we added routing.AddDimension(
transit_callback_index,
20, # allow waiting time ( in without breaks flow we were using 0 for this wait time)
1500,
False,
"Time",
) and added breaks per vehicle time_dimension = routing.GetDimensionOrDie(time
node_visit_transit = [0] * routing.Size()
for v in range(data["num_vehicles"]):
start_var = time_dimension.CumulVar(routing.Start(v))
break_start = routing.solver().Sum(
[
routing.solver().IntVar(
int(break_time_range["BreakStartTimeInMinutes"]),
int(break_time_range["BreakEndTimeInMinutes"]),
),
start_var,
]
)
breaks = [
routing.solver().FixedDurationIntervalVar(
break_start,
20, # break duration (20m)
f"Break for vehicle {v} at #{break_time} min",
),
]
time_dimension.SetBreakIntervalsOfVehicle(
breaks, v, node_visit_transit # breaks # vehicle index
) Printing the vehicle breaks intervals = solution.IntervalVarContainer()
vehicle_breaks = {}
for i in range(intervals.Size()):
brk = intervals.Element(i)
if brk.PerformedValue():
logger.info(
f"{brk.Var().Name()}: "
+ f"Start({brk.StartValue()}) End({brk.EndValue()}) Duration({brk.DurationValue()})"
) It gave us the following output
And routes as follows
Questionaccording to
But if we see the output for route 1, we are getting the some extra slack times as well. Which we don't want. see We want only 1 break per route in the given time range and no extra wait time in the route. routing.AddDimension(
transit_callback_index,
wait_time, # allow waiting time ( in without breaks flow we were using 0 for this wait time)
1500,
False,
"Time",
) But if we use wait_time as 0 then it is not placing any breaks. Is there any workaround this? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 3 replies
-
|
Beta Was this translation helpful? Give feedback.
-
I got a similar problem that i need to place a break of 5 hours every 7 hours. So I have to assign a high value to the maximum slack in routing.AddDimension(). It results in some very large slack value in other nodes, which are not desired. I tried to SetSpanCostCoefficientForVehicle. It has some effect, but leads to worse solution in terms of travelling distance. |
Beta Was this translation helpful? Give feedback.
basically break extent slack to fit in it so forcing slack to zero mean there is no way for the solver to integrate the breaks in the route...
if at routing dimension construction (i.e.
routing.AddDimension()
) you force slack to be zero then internally all slacks will be built using a ConstantIntVar object (initialised to0
) so any subsequent call tosetRange()
/setValues()
will be silently ignored...on a TW model, slack if often use by the solver when the vehicle will visit too early the location otherwise.
if you want soft TW instead (i.e. visiting a location too early is OK but you may pay a penalty proportional to your earliness) take a look at
or-tools/ortools/constraint_sol…