遗传算法-轮盘赌选择缩放(用于选择和删除)-python

时间:2020-05-21 13:28:26

标签: python python-3.x genetic-algorithm

我对自己申请的遗传算法轮盘选择方法有疑问。我有以下逻辑:

适应度函数值介于2到8之间。我想将其最大化以选择最佳染色体,因此我在适当位置进行了加权随机选择,将适应度平方值作为权重(由于数字之间的距离较近,在这种情况下,较好的健身得分机会更高-这是正确的吗?)。代码看起来像这样:

import numpy as np
import random as rd

    #list structure [[chromosome][fitness][index]]

    population = [[[0, 1, 1, 0, 0, 0, 1, 1, 1, 0, 1, 1, 0, 0, 0, 1], [6.0], [0]],
    [[0, 1, 1, 0, 0, 0, 1, 1, 0, 1, 1, 1, 0, 0, 1, 0], [2.0], [1]],
    [[0, 1, 1, 0, 0, 0, 1, 1, 0, 0, 1, 1, 0, 1, 1, 0], [2.0], [2]],
    [[0, 1, 1, 0, 0, 0, 1, 1, 0, 0, 1, 1, 1, 0, 0, 1], [5.0], [3]],
    [[0, 1, 1, 0, 0, 0, 1, 1, 0, 1, 1, 1, 1, 0, 0, 0], [8.0], [4]],
    [[0, 1, 1, 0, 0, 0, 1, 1, 1, 0, 1, 1, 0, 1, 0, 0], [3.0], [5]],
    [[1, 0, 0, 1, 0, 0, 1, 1, 1, 0, 1, 1, 0, 0, 0, 1], [6.0], [6]],
    [[1, 0, 0, 1, 0, 0, 1, 1, 0, 1, 1, 1, 0, 0, 1, 0], [7.0], [7]]]

    #%%
    def WeightsSelection(population):
        weights_s = []
        for chromosome in range(len(population)):
            ind_fit_s = np.square(population[chromosome][1][0])
            weights_s.append(ind_fit_s)
        total_weight_s = sum(weights_s)    
        weights_selection = [ind_fit_s/total_weight_s for ind_fit_s in weights_s]    
        return(weights_selection, weights_s, total_weight_s) 

然后,我有与删除类似的逻辑。我的想法是也要根据适应度值使它尽可能地被删除或选择,因此我想到了反转值的想法,

我可以有2,3,4,5,6,7,8;然后我替换列表8,7,6,5,4,3,2中的相反数字。在这种情况下,2变成8,3变成7,依此类推。然后,我也将它们平方并使用该值进行加权随机选择:

def WeightsDeletion(population):
    weights_d = []
    for chromosome in range(len(population)):
        ind_fit_d = population[chromosome][1][0]
        weights_d.append((10 - ind_fit_d)**2)
    total_weight_d = sum(weights_d)    
    weights_deletion= [ind_fit_d/total_weight_d for ind_fit_d in weights_d]    
    return(weights_deletion, weights_d, total_weight_d) 

两个函数都会生成权重,然后在两种情况下都使用random.choices()

进行选择
selected1 = rd.choices(population_clone, weights = weights_selection, k = 1)

整个代码似乎都可以正常工作,但是我觉得这不是一个非常聪明的方法。是否有人可以提出任何建议(方法或文献)以取得更好的结果?

0 个答案:

没有答案
相关问题