Ejemplo n.º 1
0
def main():

    sims = [('max', MyChromoPos), ('min', MyChromoPos), ('max', MyChromoNeg),
            ('min', MyChromoNeg)]

    for s in sims:
        print(s)
        ga = GenAlg(
            size=10,
            elitism=0.10,
            crossover=0.60,
            pureMutation=0.30,
            parentsPct=0.80,
            chromoClass=s[1],
            #selectionFcn = GenAlgOps.tournamentSelection,
            #crossoverFcn = GenAlgOps.crossover22,
            #mutationFcn  = GenAlgOps.mutateFew,
            #pureMutationSelectionFcn = GenAlgOps.simpleSelection,
            #pureMutationFcn = GenAlgOps.mutateFew,
            #feasibleSolnFcn = GenAlgOps.disallowDupes,
            minOrMax=s[0],
            showBest=0,
        )

        ga.initPopulation()
        for i in range(10):
            if (s[1] == MyChromoNeg):
                ga.population[i].data[0] = -i - 1
            else:
                ga.population[i].data[0] = i + 1
        ga.calcFitness()
        ga.sortPopulation()
        printGaPopulation(ga)

        counts = [0 for i in range(10)]

        for i in range(10000):
            idx1, idx2 = GenAlgOps.rouletteWheelSelection(ga)
            counts[idx1] = counts[idx1] + 1
            counts[idx2] = counts[idx2] + 1
        print('     roulette counts = ' + str(counts))

        counts = [0 for i in range(10)]

        for i in range(10000):
            idx1, idx2 = GenAlgOps.rankSelection(ga)
            counts[idx1] = counts[idx1] + 1
            counts[idx2] = counts[idx2] + 1
        print('     rank counts = ' + str(counts))
Ejemplo n.º 2
0
def main():

    ga = GenAlg(
        size=10,
        elitism=0.10,
        crossover=0.60,
        pureMutation=0.30,
        parentsPct=0.80,
        chromoClass=MyChromo,
        #selectionFcn = GenAlgOps.tournamentSelection,
        #crossoverFcn = GenAlgOps.crossover22,
        #mutationFcn  = GenAlgOps.mutateFew,
        #pureMutationSelectionFcn = GenAlgOps.simpleSelection,
        #pureMutationFcn = GenAlgOps.mutateFew,
        feasibleSolnFcn=GenAlgOps.disallowDupes,
        minOrMax='max',
        showBest=0,
    )

    ga.initPopulation()
    ga.calcFitness()

    child = MyChromo()
    child.data[0] = ga.population[4].data[0]
    child.fitness = ga.population[4].fitness
    feas = GenAlgOps.disallowDupes(ga, child)
    print('unsorted population; data present; feas=', feas)
    child.data[0] = ga.population[4].data[0] + 9
    child.fitness = ga.population[4].fitness + 9
    feas = GenAlgOps.disallowDupes(ga, child)
    print('unsorted population; data not present; feas=', feas)

    ga.sortPopulation()
    child.data[0] = ga.population[4].data[0]
    child.fitness = ga.population[4].fitness
    feas = GenAlgOps.disallowDupes(ga, child)
    print('sorted population; data at pos=4; feas=', feas)
    child.data[0] = ga.population[4].data[0] + 9
    child.fitness = ga.population[4].fitness + 9
    feas = GenAlgOps.disallowDupes(ga, child)
    print('sorted population; data not present; feas=', feas)