0

I am trying to generate initial population space for solving CVRP, using genetic algorithm. The demand list stores [[city,demand]] & VCAPACITY = 500. I am randomly generating a index number from the demand list, and I keeping on adding the corresponding city number to a particular vehicle till city's demand is met by the vehicles capacity. Then I am deleting the index from the list, so as to avoid duplicate cities.

Following is my code:

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()

ERROR: However It gives me cdemand = temp[index][DEMAND] IndexError: list index out of range error.

I am not able to fix this bug. Please help me!

6
  • 1. Show how you initialize your variables. As of now, temp, demand and DEMAND are not defined. 2. Remove everything which is not needed from your code. Leave a piece which someone can run on their own interpreter and see the error. Commented Nov 15, 2013 at 1:47
  • Hey Sashkello, here is how I initialize the variables: demand is a list storing city number & demand of each city at one index...I am reading from the demands from a file and store them in demand list CITY = 0 DEMAND = 1 demand=[[]] temp=demand... Commented Nov 15, 2013 at 1:50
  • edit your question (edit button on the bottom) Commented Nov 15, 2013 at 1:50
  • Hey @sashkello, thanks.. I edited it... did you get my initialization? I am reading city number and corresponding demand from the file & storing them in a list named demand Commented Nov 15, 2013 at 1:56
  • 1. Why is there an empty list in demand? It will throw an error because it doesn't have 2nd element (DEMAND = 1). 2. You never define route. Commented Nov 15, 2013 at 2:09

1 Answer 1

1

Error comes up when your index is equal to 0. Then for some reason you have an empty list there, so temp[0] = [] and obviously trying to get element 1 will result in this error.

I assume that you have it because you build demand variable by appending to demand = [[]]. So, now it has one element which is an empty list. You don't need it - declare demand = [] instead.

In addition, I don't know if you are aware of this, but when you write temp = demand, it doesn't create a copy of demand. You should use temp = demand[:] instead. See more info here: python list by value not by reference.

Sign up to request clarification or add additional context in comments.

3 Comments

This is another issue. Please don't use SO as a debugging tool. It is easy to see where this error comes from. When there are no more elements in temp, it tries to call randrange(0,0) which throws an error.
Welcome, mate. If this solves your problem, accept this answer and consider going to your previous questions and accepting and / or upvoting helpful answers there as well: stackoverflow.com/users/1185741/user1185741?tab=questions
And thanks.. I didnt know temp=demand wouldnt create its copy..I cant upvote because I dont have enough reputation.. Else I would have upvoted all the answers on all my questions

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.