Pyevolve-染色体总和= 1

时间:2018-09-23 14:21:43

标签: pyevolve

# Genome instance, 1D List of 20 elements
genome = G1DList.G1DList(20)

Sets the range max and min of the 1D List
genome.setParams(rangemin=0, rangemax=1)

Change the initializator to Real values
genome.initializator.set(Initializators.G1DListInitializatorReal)

这给出0到1之间的20个元素。我需要染色体中所有元素的总和等于1。知道怎么做吗?

1 个答案:

答案 0 :(得分:0)

我找到了答案。我创建了自己的Initializator,它是G1DListInitializatorReal的修改后的副本:

def G1DListInitializatorRealSumEqualOne(genome, **args):
    """ Real initialization function of G1DList
    This initializator accepts the *rangemin* and *rangemax* genome parameters.
    """
    range_min = genome.getParam("rangemin", 0)
    range_max = genome.getParam("rangemax", 100)

    genome.genomeList = [random.uniform(range_min, range_max) for i in xrange(genome.getListSize())]
    genome.genomeList[:] = [x / sum(genome.genomeList) for x in genome.genomeList]

然后将我的问题中的代码更改为:

# Genome instance, 1D List of 20 elements
genome = G1DList.G1DList(20)

Sets the range max and min of the 1D List
genome.setParams(rangemin=0, rangemax=1)

Change the initializator to Real values
genome.initializator.set(G1DListInitializatorRealSumEqualOne)