遗传算法索引误差

问题描述

    from random import *
def start(pp_length):#generate random population
    population=[]
    for i in range(pp_length):
        temp=[]
        for j in range(0,6):#random func
            temp.append(uniform(-10,10))
        population.append(temp)
    return population#context
def fitness(population,parameters):#fitness calculator using (1/mse)*10000
    lst=[]
    for i in range(len(population)):
        mse=0#context
        for j in range(len(population[0])):
            mse+=(population[i][j]-parameters[j])**2
        lst.append((1/mse)*10000)
    return lst#context
def selectparents(lst,pp_size):#selecting parents based on fitness scores. select 1 percent randomly
    largest=lst[0]
    sel = []#context
    for i in range(0,int(pp_size*0.1)):
        for j in range(0,len(lst)-1):
            if lst[j] >= largest and j not in sel:
                largest = lst[j]
                sel.append(j)
    for i in range(0,int(pp_size*0.01)):
        sel.append(randint(0,len(lst)))
    return sel#context
def thekid(pp_size,pp,parents):
    child = []#context
    for i in range(0,pp_size):
        what1 = randint(0,len(parents))
        what2 = randint(0,len(parents))
        child.append(pp[parents[what1-1]][:3] + pp[parents[what2-1]][3:])
    return child#context
def mutate(pp,mutation_rate):#mutate according to mutation rate
    for i in range(0,int(len(pp)*mutation_rate)):
        what1=randint(0,len(pp)-1)
        what2=randint(0,len(pp[0])-1)
        what3=randint(-100,100)
        pp[what1][what2]=what3#context
    return pp#context
pp_size=int(input('size : '))#input population size
mutation_rate=input('mutation rate(0~100) : ')#input mutation rate
mutation_rate=float(mutation_rate)/100#fix mutation rate
iteration=int(input('iteration : '))#input iteration
pp=start(pp_size)
parameters=[]
lst2=[]
for i in range(0,6):
    tmp=int(input('input integer (-10~10): '))
    parameters.append(tmp)
lst=[100,200,300,400,500,600,700]
changed=False
cnt=0
new=mutation_rate*1.02
mutation=mutation_rate

while cnt<iteration:
    cnt+=1
    lst = fitness(pp,parameters)
    print(sum(lst)/len(lst)/len(pp[0]))
    lst2.append(sum(lst)/len(lst)/len(pp[0]))
    parents=selectparents(lst,pp_size)
    child=thekid(pp_size,parents)
    pp=child
    pp=mutate(pp,mutation_rate)
    try:
        if (lst2[-2]-lst2[-1])<=1:
            mutation_rate=new
            changed=True
        elif changed is True:
            changed = False
            mutation_rate=mutation
    except IndexError:#context
        pass
    
    genetic algorithm: choose fittest 10% of population,1% of randomly selected individuals
    error that occured: indexerror in thechild function
    error code:
      File "C:\Users\user\Downloads\genetic.py",line 65,in 
      File "C:\Users\user\Downloads\genetic.py",line 34,in thekid
        child.append(pp[parents[what1-1]][:3] + pp[parents[what2-1]][3:])
    IndexError: list index out of range
    apparently,the function got wrong lists. Otherwise,there wouldn't be an indexerror
    or is it possible that the population numbers changed when doing the reproduction sequence?
    the website tells me to insert more context but I don't have any,so ignore what I wrote 
    below:
    Just kidding. Please read the error message I pasted below.
    It just says list index out of range at thekid function.
    The method I am currently using first generates random population,and then evaluates them 
    using the fitness function. Instead of the roulette wheel method,It selects top 10% and another 1%,this time randomly. Then,it creates children and mutates a certain 
    Traceback (most recent call last):
  File "C:\Users\user\Downloads\genetic.py",in <module>
    child=thekid(pp_size,parents)
  File "C:\Users\user\Downloads\genetic.py",in thekid
    child.append(pp[parents[what1-1]][:3] + pp[parents[what2-1]][3:])
   IndexError: list index out of range

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)