如何控制流量?

时间:2019-09-26 13:35:28

标签: python

我正在对Clarke和Wright的节省算法进行编程,以设计从仓库到一组客户的车辆路线。现在,如果不满足任何条件,代码将卡在最终的if语句中。我想要的是,如果所有cust_pairs都已检查,并且没有剩余的cust_pair,那么在上一条路由的开头或结尾处就没有代码了。代码应该跳出第二个for循环。然后,返回第一个for循环以查找一对客户以开始新的路线。

我已经尝试过使用“ break”和“ continue”进行某些操作,但无法使其正常工作。

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

0 个答案:

没有答案