Last active
August 21, 2017 22:19
-
-
Save NicolleLouis/1a3050bfe04d86280417a7f6103563b1 to your computer and use it in GitHub Desktop.
algorithme génétique
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import operator | |
import random | |
def computePerfPopulation(population, password): | |
populationPerf = {} | |
for individual in population: | |
populationPerf[individual] = fitness(password, individual) | |
return sorted(populationPerf.items(), key = operator.itemgetter(1), reverse=True) | |
def selectFromPopulation(populationSorted, best_sample, lucky_few): | |
nextGeneration = [] | |
for i in range(best_sample): | |
nextGeneration.append(populationSorted[i][0]) | |
for i in range(lucky_few): | |
nextGeneration.append(random.choice(populationSorted)[0]) | |
random.shuffle(nextGeneration) | |
return nextGeneration |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment