Skip to content

Instantly share code, notes, and snippets.

@rocreguant
Last active July 25, 2021 11:39
Show Gist options
  • Save rocreguant/b14ab2c2ecb58f98ee44b4d75785b8af to your computer and use it in GitHub Desktop.
Save rocreguant/b14ab2c2ecb58f98ee44b4d75785b8af to your computer and use it in GitHub Desktop.
roulette wheel selection example in python
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