'Tournament Selection on Python
How do you implement a Roulette wheel Selection on python so that it shows half of the total population after execution?
So first I generated a total of 100 random words and specified a certain letter to be counted at the beginning of each word. Afterwards, I am expected to halve the total population(100 words to 50 words) using roulette wheel selection. How do I implement roulette wheel selection using the following code?
import random
import numpy as np
def population (length, amount, letter, selection):
alfa='abcdefghijklmnñopqrstuvwxyz'
item=[]
fitness=0
fitness_it=0
individual=[]
individual2=[]
for i in range (amount):
words =''.join(random.choices(alfa, k=length))
aptitude = str(words.count(letter))
item.append(aptitude + words)
item = [x.upper() for x in item]
item.sort(reverse=True)
fitness = fitness + int(aptitude)
fitness = fitness/amount
maxi = item[0]
print(np.array(item))
print(maxi[0],round(fitness,2))
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
