0

我正在尝试使用遗传算法生成用于解决 CVRP 的初始种群空间。需求列表存储 [[city,demand]] & VCAPACITY = 500。我从需求列表中随机生成一个索引号,并继续将相应的城市编号添加到特定车辆,直到车辆容量满足城市的需求. 然后我从列表中删除索引,以避免重复城市。

以下是我的代码:

import random

VCAPACITY=500
CITY=0
DEMAND=1
demand=[[], [1, 0], [2, 6], [3, 72], [4, 93], [5, 28], [6, 5], [7, 43], [8, 1], [9, 36], [10, 53], [11, 63], [12, 25], [13, 50], [14, 57], [15, 1], [16, 66], [17, 37], [18, 51], [19, 47], [20, 88], [21, 75], [22, 48], [23, 40], [24, 8], [25, 69], [26, 93], [27, 29], [28, 5], [29, 53], [30, 8], [31, 24], [32, 53], [33, 13], [34, 47], [35, 57], [36, 9], [37, 74], [38, 83], [39, 96], [40, 42], [41, 80], [42, 22], [43, 56], [44, 43], [45, 12], [46, 73], [47, 32], [48, 8], [49, 79], [50, 79], [51, 4], [52, 14], [53, 17], [54, 19], [55, 44], [56, 5], [57, 37], [58, 100], [59, 62], [60, 90], [61, 57], [62, 44], [63, 37], [64, 80], [65, 60], [66, 95], [67, 56], [68, 56], [69, 9], [70, 39], [71, 15], [72, 4], [73, 58], [74, 73], [75, 5], [76, 12], [77, 3], [78, 8], [79, 31], [80, 48], [81, 3], [82, 52], [83, 99], [84, 29], [85, 12], [86, 50], [87, 98], [88, 4], [89, 56], [90, 24], [91, 33], [92, 45], [93, 98], [94, 4], [95, 36], [96, 72], [97, 26], [98, 71], [99, 84], [100, 21], [101, 99], [102, 33], [103, 84], [104, 74], [105, 93], [106, 25], [107, 39], [108, 42], [109, 77], [110, 68], [111, 50], [112, 42], [113, 71], [114, 85], [115, 78], [116, 64], [117, 5], [118, 93], [119, 18], [120, 38], [121, 29], [122, 81], [123, 4], [124, 23], [125, 11], [126, 86], [127, 2], [128, 31], [129, 54], [130, 87], [131, 17], [132, 81], [133, 72], [134, 10], [135, 50], [136, 25], [137, 71], [138, 85], [139, 51], [140, 29], [141, 55], [142, 45], [143, 100], [144, 38], [145, 11], [146, 82], [147, 50], [148, 39], [149, 6], [150, 87], [151, 83], [152, 22], [153, 24], [154, 69], [155, 97], [156, 65], [157, 97], [158, 79], [159, 79], [160, 46], [161, 52], [162, 39], [163, 94], [164, 97], [165, 18], [166, 3], [167, 23], [168, 19], [169, 40], [170, 49], [171, 96], [172, 58], [173, 15], [174, 21], [175, 56], [176, 67], [177, 10], [178, 36], [179, 84], [180, 59], [181, 85], [182, 60], [183, 33], [184, 62], [185, 70], [186, 79], [187, 98], [188, 99], [189, 18], [190, 55], [191, 75], [192, 94], [193, 89], [194, 13], [195, 19], [196, 19], [197, 90], [198, 35], [199, 76], [200, 3], [201, 11], [202, 98], [203, 92], [204, 1], [205, 2], [206, 63], [207, 57], [208, 50], [209, 19], [210, 24], [211, 14], [212, 18], [213, 77], [214, 28], [215, 72], [216, 49], [217, 58], [218, 84], [219, 58], [220, 41], [221, 98], [222, 77], [223, 57], [224, 39], [225, 99], [226, 83], [227, 54], [228, 86], [229, 2], [230, 14], [231, 42], [232, 14], [233, 55], [234, 2], [235, 18], [236, 17], [237, 22], [238, 28], [239, 3], [240, 96], [241, 53], [242, 15], [243, 36], [244, 98], [245, 78], [246, 92], [247, 65], [248, 64], [249, 43], [250, 50]]


#read city number & value from a file & store it in demand array
#for e.g print demand gives [[1,20],[2,30],....], there are 250 cities each having
#its own demand

def solution():
    route=[]    
    temp=demand
    while(len(temp)!=0):
        index=random.randrange(0,len(temp))
        cdemand = temp[index][DEMAND]
        capacity=VCAPACITY
        while(capacity>cdemand):
            capacity = capacity-cdemand
            route.append(temp[index][CITY])
            del temp[index]
            index=random.randrange(0,len(temp))
            cdemand = temp[index][DEMAND]

solution()

错误:但是它给了我 cdemand = temp[index][DEMAND] IndexError: list index out of range 错误。

我无法修复此错误。请帮我!

4

1 回答 1

1

当您index等于 0 时出现错误。然后由于某种原因,您在那里有一个空列表,因此temp[0] = []显然尝试获取元素 1 将导致此错误。

我假设您拥有它,因为您demand通过附加到demand = [[]]. 所以,现在它有一个元素是一个空列表。您不需要它 -demand = []改为声明。

另外,我不知道您是否意识到这一点,但是当您编写. 时temp = demand,它不会创建demand. 你应该temp = demand[:]改用。在此处查看更多信息:python list by value not by reference

于 2013-11-15T02:13:00.460 回答