Let’s make a ‘python list & dictionary comprehension for loop’ example. It selects equal or greater than average value, then returns max 50 elements from dictionary.
Selection operator picks out individuals in the population for reproduction in genetic algorithms. Roulette wheel selection that an imaginary proportion of the wheel is assigned to each of the chromosomes based on their fitness value. The fitter chromosome has more chance to select than worse one. Roulette wheel selection is a kind of elitist selection that retaining the best individuals in a generation unchanged in the next generation.
Here an example of roulette wheel selection via pyhton. Imagine that you have population like
And you use a proper fitness function regarding to your problem, and get a python dictionary to wrap your individuals and their fitness values like
Firstly, we can create a probability list (imaginary proportion of the wheel)
After that, we can write the roulette wheel selection function. Parameters of the function are our population, probability list of the population and a number to select desired individuals for reproduction.
I have recently finished a master thesis about genetic algorithms for text segmentation via python, so I have a complicated relationship with python. Actually it always helps to me. This post consists of some python especially list and dictionary comprehensions.
First of all, splitting a list regarding with given another list. For example, you have a list which has sentences as elements. Another list is reference splitter and consists of 0s and 1s.
sentences = ["first sentence", "second sentence", "third sentence",
"fourth sentence", "fifth sentence"]
reference = [0,0,1,0]
This list indicates that cut the number of index of ‘1’ at the gap in the sentences list and result list will include two sublists
result = [["first sentence", "second sentence", "third sentence"],
["fourth sentence", "fifth sentence"]]