Last active
July 25, 2021 11:39
-
-
Save rocreguant/b14ab2c2ecb58f98ee44b4d75785b8af to your computer and use it in GitHub Desktop.
roulette wheel selection example in python
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 numpy as np | |
def roulette_wheel_selection(population): | |
# Computes the totallity of the population fitness | |
population_fitness = sum([chromosome.fitness for chromosome in population]) | |
# Computes for each chromosome the probability | |
chromosome_probabilities = [chromosome.fitness/population_fitness for chromosome in population] | |
# Selects one chromosome based on the computed probabilities | |
return np.random.choice(population p=chromosome_probabilities) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment