How to force a vehicle to visit a node depending on a condition. #2966
Replies: 2 comments
-
Your constraint doesn't seem right to me, but maybe I'm just misinterpreting. I would do something like: idx0 = routing.NodeToIndex(cloned_index[0])
idx1 = routing.NodeToIndex(cloned_index[1])
# make sure idx0 and idx1 are served by same vehicle
routing.solver().Add(routing.VehicleVar(idx0) ==
routing.VehicleVar(idx1))
# make sure the day of the week is the same. assuming time is in
# minutes, and that it has been forced to start such that time=0
# aligns with midnight, day 0, then one day is 1440 minutes long
# dunno if this will work, but it seems floordiv is defined
# https://github.com/google/or-tools/blob/v7.1/ortools/constraint_solver/python/constraint_solver.i#L408
time_dimension = routing.GetDimensionOrDie('time') # assuming you call it that
time_0 = time_dimension.CumulVar(idx0)
time_1 = time_dimension.CumulVar(idx1)
day_0 = time_0 // 1440 # floor div gives integer day of week
day_1 = time_1 // 1440 # ditto, ideally 7 days later
# days of the week should be exactly 7 days apart. For example, 0 and 7, 1 and 8, etc
routing.solver().Add(day_0 + 7 == day_1) |
Beta Was this translation helpful? Give feedback.
-
Many thanks for the answers. I had put five days into what I consider from Monday to Friday, and I think each vehicle one day. However, I realize that in your solution, you consider that a vehicle runs the entire two weeks. For me it makes sense to force the vehicle to go through the original and cloned node did not understand how to do the modeling for days. In my case, the time dimension contains the distance from one node to the other in seconds, but I did not understand how to relate this to the fact that each day reaches 1440 minutes. Ps: The restriction I put on it is indeed wrong, it was just an attempt to help find some response. |
Beta Was this translation helpful? Give feedback.
-
I have already checked the similar problems in the forum on this issue, but I believe my demand is a bit different so:
I modeled my problem so that there are two weeks. Nodes can be visited once (fortnightly), or twice (weekly).
For nodes that are visited twice, I duplicated the node in question. The weekly nodes have an additional restriction; they have to be visited on the same day of the week. So if each week has five days, we will have ten vehicles representing each day of the week.
Week A Week B
Day 1 1 Monday
Day 2 2
.....
Day 5 5 Friday
My problem is: how to ensure that when a node is visited on vehicle 1 of week A, it will be visited on vehicle 1 of week B, remembering that it can be visited on any day (Monday, Tuesday ... Friday)
Update:
Does the above code make sense? I try to force node 1 visite for vehicle 6 if node 0 was visited by vehicle 1 and so on... node 1 visited by vehicle 7 if node 0 was visited by node 2 ...
idx0 = routing.NodeToIndex(cloned_index[0])
idx1 = routing.NodeToIndex(cloned_index[1])
routing.solver().Add(routing.VehicleVar(routing.Start(idx0)) == routing.VehicleVar(routing.Start(idx1))+5))
Beta Was this translation helpful? Give feedback.
All reactions