-2

I'm using genetic algorithm for knapsack problem and when I wanted to debug it says : int' object is not subscriptable

it just pointed this 3 spots in the code and in the end it just said this is the error, also it referenced the first error in the fitness def twice

 import random
value = [20, 30, 40, 50, 90, 5, 250, 100]
weight = [5, 10, 50,  25, 36, 71, 61, 48,]
inventory = 100
popSize = 10
gens = 100
mutationRate = 0.1
numberOfItems = len(value)

def fitness(individual):
   ---> totalval = sum(value[i] for i in range(numberOfItems)if individual[i] == 1) 
    totalW = sum(weight[i] for i in range(numberOfItems)if individual[i] == 1) 
    if totalW > inventory :
        return 0
    else :
        return totalval
    
def createItem() :
    return[random.randint(0,1) for i in range(numberOfItems)]

def createPop() :
    return[random.randint(0,1) for i in range(popSize)]

def selection(pop) :
    players = random.sample(pop, 3)
  --->  players.sort(key = fitness, reverse=True)
    return players[0]

def crossover(parent1, parent2) :
    point = random.randint(0, numberOfItems-1)
    child = parent1[:point] + parent2[point:]
    return child

def mutation(individual) :
    for i in range(numberOfItems) :
        if random.random() < mutationRate:
            individual[i] = 1 - individual[i]




pop = createPop()

for i in range(gens) :
    newPop = []
    for gens in range(popSize) :
      --->  parent1 = selection(pop)
        parent2 = selection(pop)
        child = crossover(parent1, parent2) 
        mutation(child)
        newPop.append(child)
    pop = newPop

bestInd = max(pop, key=fitness)
bestVal = fitness(bestInd)
bestWei = sum(weight[i] for i in range(numberOfItems) if bestInd[i] == 1 )

print("Best solution is ", bestInd)
print("total value is ", bestVal)
print("total weight is ", bestWei)

what is the problem ?

1
  • 2
    The value passed to fitness() is an integer. Your code implies that it should be some subscriptable type (e.g., list, tuple) Commented Nov 9 at 14:35

1 Answer 1

2

You have:

bestInd = max(pop, key=fitness)

pop is a list of int. So when fitness is called for each element of pop, its argument, individual, will be instantiated with an int element of pop. Then in function fitness, you have the test if individual[i] == 1. But since individual is an int rather than a type that is indexable (subscriptable), you get an exception.

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

2 Comments

and how should I fix it ? i just need to turn individual into a list ?
I am not familiar with the genetic algorithm for the 0-1 knapsack problem so I cannot know whether the rest of your code is correct. Clearly, individual needs to be a list. How it should be initialized in another question. You can look at the suggestion of @Omkardev.py. But if you run your code with that change, you get a different result on every invocation. Does that seem right to you? You could equally initialize individual with all 0's or all 1's and again, each run produces a different result but occasionally what I think is the correct value i.e. 340.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.