Prefered driver penalty #4631
-
I'm working on a VRPTW for the home care services. penalty_pref = 36000
penalty_vars = []
for node, preferred_vehicles in data["pref_driver"].items():
if not preferred_vehicles:
continue
index = manager.NodeToIndex(node)
vehicle_var = routing.VehicleVar(index)
is_preferred_bools = [
routing.solver().IsEqualCstVar(vehicle_var, v)
for v in preferred_vehicles
]
is_preferred = routing.solver().Max(is_preferred_bools)
is_not_preferred = routing.solver().IntVar(0, 1, f'is_not_preferred_{node}')
routing.solver().Add(is_not_preferred + is_preferred == 1)
penalty_var = routing.solver().IntVar(0, penalty_pref, f'penalty_{node}')
routing.solver().Add(penalty_var == is_not_preferred * (penalty_pref-1))
penalty_vars.append(penalty_var)
penalty_sum = routing.solver().Sum(penalty_vars)
penalty_total = routing.solver().IntVar(0, 3600000000, "penalty_total")
routing.solver().Add(penalty_total == penalty_sum)
routing.AddVariableMinimizedByFinalizer(penalty_total) This was my only finalizer before adding the prefered drivers, which produces good results. It looks like this; for i in range(data["num_vehicles"]):
routing.AddVariableMinimizedByFinalizer(time_dimension.CumulVar(routing.Start(i)))
routing.AddVariableMinimizedByFinalizer(time_dimension.CumulVar(routing.End(i))) The model is not affected no matter what penalty i give in the prefered driver section. It seems like the model doesn't take this variable into account. What am I doing wrong? Thanks in advance |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 11 replies
-
Few questions: penalty_sum = routing.solver().Sum(penalty_vars)
penalty_total = routing.solver().IntVar(0, 3600000000, "penalty_total")
routing.solver().Add(penalty_total == penalty_sum)
routing.AddVariableMinimizedByFinalizer(penalty_total)| You should be able to use: penalty_sum = routing.solver().Sum(penalty_vars).Var()
routing.AddVariableMinimizedByFinalizer(penalty_sum) ref: After Finalizer is kind of post processing mostyl use when there is a SlackVar e.g. waiting time to force the solver to minimize/maximize the value of the cumulvar instead of returning a range (kind of meta solution of all possibilities entangled) In your case you want to have the prefered vehicle cost embeded in the objective function not in post processing
Now the objective function should have an extra cost equal to |
Beta Was this translation helpful? Give feedback.
(based on
vrp_capacity.py
....)vrp_capacity_with_prefered_vehicle.py
: