I'm programming Clarke and Wright's savings algorithm to design vehicle routes from a depot to a set of customers. Right now, the code gets stuck in the final if statement if none of the conditions is met. What I want is that if ALL the cust_pairs have been checked and there is NO unserved cust_pair left which is either at the start or end of the previous route The code should break out of the second for loop. And then, go back to the first for loop to find a customer pair for starting a new route.
I've tried some things with 'break' and 'continue' but can not get it working.
def inPrevious(new,existing):
start = existing[0]
end = existing[len(existing)-1]
if new == start:
return 1
elif new == end:
return 0
else:
return -1
idx = -1
while not(allCustomersServed):
for c in cust_pairs:
# find initial cust_pair with high savings
idx += 1
routes[idx] = ([c[0],c[1]])
break
#finding a cust that is either at the start or end of previous route
for c in cust_pairs:
# find feasible cust_pair that is either at the start or end of previous route
res = inPrevious(c[0], routes[idx])
if res == 0:
# append c[1] to route
elif res == 1:
# insert c[1] at position 0 in route
elif res == -1:
res = inPrevious(c[1], routes[idx])
if res == 0:
# append c[0] to route
elif res == 1:
# insert c[0] at position 0 in route