Created
March 14, 2021 10:26
-
-
Save rocreguant/e9f2481f4e9842dd76e9c61f653eb7c0 to your computer and use it in GitHub Desktop.
Roulette wheel selection example in python for minimisation problems
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] | |
# Making the probabilities for a minimization problem | |
chromosome_probabilities = 1 - np.array(chromosome_probabilities) | |
# Selects one chromosome based on the computed probabilities | |
return np.random.choice(population p=chromosome_probabilities) |
This function is throwing errors for me. chromosome.fitness.values[0] in place of chromosome.fitness is working.
Also, sum of probabilities is not one. Hence, this is not working.
Maybe you would be interested in checking the full code and adapt it to yours.
https://github.com/rocreguant/personal_blog/tree/main/Genetic_Algorithm_Python_Example
If you have more questions I'm glad to answer them ;)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This function is throwing errors for me. chromosome.fitness.values[0] in place of chromosome.fitness is working.
Also, sum of probabilities is not one. Hence, this is not working.