I'm writing a genetic algorithm in Python to find the least of some mathematical functions. The problem is that when I calculate the fitness of a function and save 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 as well:
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)