Skip to content

Instantly share code, notes, and snippets.

@NicolleLouis
Last active August 21, 2017 22:19
Show Gist options
  • Save NicolleLouis/1a3050bfe04d86280417a7f6103563b1 to your computer and use it in GitHub Desktop.
Save NicolleLouis/1a3050bfe04d86280417a7f6103563b1 to your computer and use it in GitHub Desktop.
algorithme génétique
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