Python Error: 'int' Object has no attribute '__getitem__'

Asked

Viewed 645 times

0

I’m writing a genetic algorithm in Python to find the minimum of some mathematical functions. The problem is that when I go to calculate the fitness of a function and store the result in the fitness list, it presents the following error:

Traceback (most recent call last):
  File "ag_GP.py", line 95, in <module>
    run()
  File "ag_GP.py", line 84, in run
    ax = fitness_func()
  File "ag_GP.py", line 29, in fitness_func
    fitness_value = ( 1+(oPop[i][0]+oPop[i][1]+1)**2 * (19-14*oPop[i][0]+3*oPop[i][0]**2-14*oPop[i][1]+6*oPop[i][0]*oPop[i][1]+3*oPop[i][1]**2) ) * ( 30+(2*oPop[i][0]-3*oPop[i][1])**2 * (18-32*oPop[i][0]+12*oPop[i][0]**2+48*oPop[i][1]-36*oPop[i][0]*oPop[i][1]+27*oPop[i][1]**2) ) 

TypeError: 'int' object has no attribute '__getitem__'

Follow the program code too:

import random
import math

BEST_FITNESS    = 3
CHRM_SIZE       = 2
POP_SIZE        = 5
MAX_GENERATIONS = 5
MUTATION_RATE   = 0.8

MIN,MAX = -2,2

oPop = []
nPop = []
fitness = []
sucess_rate = 0
fitness_sum = 0

def init_population():
    for i in range(0,POP_SIZE):
        chromosome = []
        for j in range(CHRM_SIZE):
            chromosome.append(random.randint(MIN,MAX))
        oPop.append(chromosome)

def fitness_func():
    global fitness_sum, fitness

    for i in range(0,len(oPop)):
        fitness_value = ( 1+(oPop[i][0]+oPop[i][1]+1)**2 * (19-14*oPop[i][0]+3*oPop[i][0]**2-14*oPop[i][1]+6*oPop[i][0]*oPop[i][1]+3*oPop[i][1]**2) ) * ( 30+(2*oPop[i][0]-3*oPop[i][1])**2 * (18-32*oPop[i][0]+12*oPop[i][0]**2+48*oPop[i][1]-36*oPop[i][0]*oPop[i][1]+27*oPop[i][1]**2) ) 
        fitness.append(fitness_value)
        fitness_sum += 1

        if(fitness[i] == BEST_FITNESS):
            return 1
    return 0

def mutation():
    return(random.randint(MIN,MAX))

def crossover(parent1, parent2):
    return (parent1[:CHRM_SIZE/2]+parent2[CHRM_SIZE/2:])

def stochastic_selection():
    return (random.randint(0, POP_SIZE-1))

def best_individual():
    ax = 0
    best_fitness = fitness[0]

    for i in range(0,len(fitness)):
        if(abs(fitness[i] - BEST_FITNESS) < best_fitness):
            best_fitness = fitness[i]
            ax = i
    return ax


def next_generation():
    a = best_individual()
    nPop.append(a)

    for i in range(1,len(oPop)):
        parent1,parent2 = stochastic_selection(), stochastic_selection()
        nPop.append(crossover(oPop[parent1],oPop[parent2]))

        mut_rate = random.randint(0,POP_SIZE)

        if(mut_rate <= MUTATION_RATE*100):
            j = random.randint(0,CHRM_SIZE-1)
            nPop[i][j] = mutation()

    for i in range(0,POP_SIZE):
        oPop[i] = nPop[i]

def reset():
    global oPop, nPop, fitness
    for i in range(len(oPop)):
        oPop, nPop, fitness = [],[],[]

def run():
    generation = 0

    init_population()
    while (1):
        ax = fitness_func()
        if(ax == 1 or generation >= MAX_GENERATIONS):
            return generation

        next_generation()
        generation += 1

    return generation

if __name__ == '__main__':
    for i in range(0,100):
        run()
        if(abs(fitness[best_individual()] - BEST_FITNESS) < 0.01):
            sucess_rate += 1
        reset()

    print(fitness_sum)
    print(sucess_rate)
  • 4

    It seems to me that now you use nPop and oPop as if they were arrays with one dimension and now you use them as if they were two-dimensional matrices - and you end up getting confused in the middle of it.

  • 1

    Exactly like fellow @Victorstafusa said. The error means that you are trying to use an access operator (the [índice]) an integer instead of a list. Put a print(oPop) before the line where the error occurs to see its contents.

  • When you put 'print(oPop)' before the list of values appears, in the test here appeared [[-1, -1], [1, -2], [0, 2], [-2, 2], [-2, -1]]

1 answer

1

Add a print(oPop) on line 29 (before the error line), you will see that the first item of the oPop is an int, not a list. For this reason, within that dark expression the int is used as if it were a list --will the error happen.

This code is quite confusing and should be totally rethought.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.