Python说数组尺寸不匹配,但我不明白为什么不

时间:2019-04-05 01:27:49

标签: python-3.x

我正在为通用算法编写代码,并希望对当前种群进行交叉运算,但是看不到为什么函数的数组维数不匹配。

我已经尝试了打印线调试,以查看数组尺寸是否正在更改,但是我不知道如何...

交叉代码:

def crossover(self,parent, pop):
    if np.random.rand() < self.CROSS_RATE: # Ensures crossover happens at crossover rate
        i_ = np.random.randint(0, self.POP_SIZE) # Select another individual from pop
        cross_points = np.random.randint(0, 2, size=self.DNA_SIZE).astype(np.bool) # Choose crossover points
        parent[cross_points] = pop[i_,cross_points] # Mating and produce one child
    return parent

调用该函数的代码:

pop = np.random.randint(2, size=(100,arr.shape[1] - 1)) # initialze initial population

ga = Genetic_Algorithm(arr, DNA_SIZE = arr.shape[1] - 1)

for i in range(0,ga.N_GENERATIONS):
    pop = ga.mutate(pop)
    pop_copy = pop.copy()
    for j in range(0,pop.shape[0]):
        np.vstack([pop,ga.crossover(pop[j,:],pop_copy)])
    calculated_results = ga.translateDNA(pop)
    fitness = ga.get_fitness_one(calculated_results,ga.output_data)
    print("Most fit model:",pop[np.argmax(fitness),:])

    pop = ga.select(pop,fitness)

0 个答案:

没有答案