一些结果后索引超出范围

时间:2018-05-17 21:17:16

标签: python

我正在遗传算法中执行优化操作,并面临索引出界问题。

我的代码是:

import numpy as np
import itertools
import random

population_size=5
chromosome_size=2
geneset1=[7,9,19,23,25,27,31]
geneset2=[2,78,46,68,34,24,12]
mutation_rate=0.1
crossover_rate=0.95
generation=24


def summation(a,b):
    return a+b

#Creating fitness function

def fitness_array(population):
    fitness_array=[]
    for i in range(population_size):
        fitness_array.append(summation(population[i,0],population[i,1]))

    fitness_array=np.array(fitness_array)
    return fitness_array


#Creating random population

def return_random_population(geneset1,geneset2,population_size):
    population=random.sample(set(itertools.product(geneset1,geneset2)),population_size)
    population=np.array(population)
    return population


#Arranging the population from best to worst fitness

def return_best_worst_population(population):
    fitness_array1=fitness_array(population)
    new_population=np.zeros(population.shape)
    new_fitness_array=np.zeros(fitness_array1.shape)
    worst_best=np.argsort(fitness_array1)
    best_worst=worst_best[::-1]
    row_counter=0
    for i in best_worst:
        new_population[row_counter,:]=population[i,:]
        new_fitness_array[row_counter]=fitness_array1[i]
        row_counter +=1
    return new_population[0],new_fitness_array[0]


#Linear ranked_selection method

def return_ranked_selected_population(population):
    ranked_population=[]
    fitness_of_given_population=fitness_array(population)

    sort=np.argsort(fitness_of_given_population)   

    rank_population=np.zeros(fitness_of_given_population.shape)
    x=1
    for i in sort:
        rank_population[i]=x
        x=x+1
    fitness_score=[(x/sum(rank_population)) for x in rank_population]     


    for i in range(len(fitness_score)):
        n=int(fitness_score[i]*100)
        for j in range(n):
            ranked_population.append(population[i])
    return ranked_population


#crossovered population

def return_crossovered_child(ranked_selected_population):
    if  np.random.random()<crossover_rate:
        a=np.random.randint(0,len(ranked_selected_population))
        b=np.random.randint(0,len(ranked_selected_population))
        parent1=ranked_selected_population[a]
        #print("x",parent1)
        parent2=ranked_selected_population[b]
        #print("y",parent2)
        slicing_point=np.random.randint(0,chromosome_size)
       # print("a",parent1[:slicing_point])
        #print("b",parent2[slicing_point:])
        child=list(parent1[:slicing_point])+list(parent2[slicing_point:])

        #print("c",child)
        return child
    return 0

#Mutated population

def return_mutated_child(crossovered_child):
    for i in range(chromosome_size):
        if np.random.random()<mutation_rate:
            if i==0:
                crossovered_child[i]=np.random.choice(geneset1)
            else:
                crossovered_child[i]=np.random.choice(geneset2)


    return crossovered_child

#creating initial population

new_population =return_random_population(geneset1,geneset2,population_size)

a,b=return_best_worst_population(new_population)
print("best",a,"fitness",b)
#print(new_population)
for i in range(generation):
    mate_pool=return_ranked_selected_population(new_population)
    last_population=[]
    for j in range(population_size):

        crossed_child=return_crossovered_child(mate_pool)
        #print("c",crossed_child)
        if crossed_child is 0:
            continue
        else:
            mutated_child=return_mutated_child(crossed_child)
            #print("m",mutated_child)
        last_population.append(mutated_child)
    new_population=np.array(last_population)

    a,b=return_best_worst_population(new_population)
    print('generation',i,"best",a,"fitness",b)

我的代码有时会给出一些结果,然后显示出问题。 显示的问题是:

fitness_array.append(求和(人口[I,0],人口[I,1))

IndexError:索引4超出了轴0的大小为4

如何解决此问题?

1 个答案:

答案 0 :(得分:1)

根据您当前的代码,每一代实际人口规模都会减少enumerate次。如果实际填充大小小于object,则代码将返回错误。我不清楚为什么你希望你的人口规模减少,但如果你这样做,那么你需要在它减少时更新(1-crossover_rate)**population。如果您不希望人口规模减少,那么您需要重写代码。