Demo entry 6850741

GA

   

Submitted by LPZ on Jul 11, 2019 at 04:54
Language: Python. Code size: 815 Bytes.

def selectChild(population,fitnessResult):
    childPopulation = []
    Sumfitness = []
    indiviSumP = []
    for i in range(len(fitnessResult)):
        if i == 0:
            Sumfitness.append(fitnessResult[i])
        else:
            Sumfitness.append(Sumfitness[i-1] + fitnessResult[i])   
    for j in range(len(Sumfitness)):
        indiviSumP.append(Sumfitness[j]/sum(fitnessResult))
    for childNum in range(len(population)):
        x = np.random.uniform(0,1)
        if x <= indiviSumP[0]:
            childPopulation.append(population[0])
        else:
            for m in range(1,len(population)):
                if indiviSumP[m-1] <= x and x <= indiviSumP[m]:
                    childPopulation.append(population[m])
                    break     
    return childPopulation

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).