Reputation: 35
I have a problem for calculating times on a routing. It starts with a dictionary which represents a routing:
Routing = [('Car1', 'C3', 'C2'),('Car1', 'IT', 'C3'),('Car1', 'C2', 'C4'),('Car1', 'C6', 'IT'),('Car1', 'C4', 'C5'),('Car1', 'C5', 'C6')]
Each movement from i[1] to i[2] in Routing has its own time and there is a starting time for IT:
Time = {('C3', 'C2'): 0,('IT', 'C3'): 14,('C2', 'C4'): 1,('C6', 'IT'): 14,('C4', 'C5'): 0,('C5', 'C6'): 0}
Start_time_IT: 128
As you notice, the routing for car1 = IT, C3, C2, C4, C5, C6, IT. where IT is begin and end point. What I need is to calculate the arrival time for each step, and end up with a dictionary like this:
Arrival_time = {'C3': 142, 'C2': 142, 'C4': 143, 'C5': 143, 'C6':, 143, 'IT': 157}
For example, C3 is calculated on the starting time of IT which is 128 and travel time to C3 which is 14 so I end up with 142, etc for the rest.
Right now I am really struggling with building Arrival_time. I have tried for and while loops, but i do not get the correct values of the error that Arrival_time changes during the loop.
Above is an example, in reality the route is with multiple cars and more stops. Does anyone have an idea how I can tackle the above scenario?
Tnx in advance!
Upvotes: 0
Views: 103
Reputation: 121
Using the information you have provided, this might help you get on the right path. I don't know the full context of what you're doing so it's probably not optimal.
def make_route(car, routing):
# multiple cars in routing so only get connections for the one we want
connections = [(r[1],r[2]) for r in routing if r[0] == car]
# get start
route = [c for c in connections if c[0] == 'IT']
# join connections
while route[-1][1] != 'IT':
route += [c for c in connections if c[0] == route[-1][1]]
return route
def time_route(route, Time, start_time=0):
times = []
for r in route:
# look up time for the connection and add to running total
start_time += Time[r]
# add times to list - dictionaries don't preserve order
times.append((r[1], start_time))
return times
r = make_route('Car1', Routing)
Arrival_time = time_route(r, Time, 128)
This should return [('C3', 142), ('C2', 142), ('C4', 143), ('C5', 143), ('C6', 143), ('IT', 157)]
. It's a list as a dictionary won't preserve the order.
Upvotes: 1