Write a Python code that implements the following: 1. Generate 20 chromosomes, each of which has a length of 5 binary digits (either 0 or 1). 2. Evaluate the fitness of each individual by counting number of ones in each chromosome (as demonstrated in the below table) and compare it to a target chromosomes of all ones (i.e. 11111) Table-1: Fitness of a sample of chromosomes Chromosome Fitness 01110 3 01100 2 00000 0 10111 4 3. Write a function that selects the best fitted individuals for the next population using “Roulette Wheel selection”, by taking the fitness of each individual and generates the probabilities of it. The probability of being selected is proportional to the relative fitness of the individual, that is calculated using the following formula: Hence, the expected output of the individuals in Table-1 is [0.333, 0.222, 0.444] 4.
def print_check():
sum = 0
print("Roulette Wheel selection")
example_array = [1, 0, 1,1,0,1,1,1,0,1,1,0,1,1,0,0,0,]
for i in range(len(example_array)):
print(example_array[i])
print(print_check())
Comments
Leave a comment