Pyevolve - sum of chromosome = 1

60 views Asked by At
# 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)

This give 20 elements between 0 and 1. I need that the sum of all elements in the chromosome to be equal to 1 . Any idea how to do this?

1

There are 1 answers

0
Jonas On BEST ANSWER

I found the answer. I created my own Initializator which is a modified copy of 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]

And then changed the code in my question to:

# 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)