Last active
September 19, 2018 17:51
-
-
Save Dhrumilcse/2ad9ced135f411b5ebd8c51e5692ddbb to your computer and use it in GitHub Desktop.
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