The algorithm uses tournament selection to select which parents to use toreproduce the children. You are asked to use and implement a new selection method (moremethods are alsopossible if you want)...

1 answer below »
The algorithm uses tournament selection to select which parents to use toreproduce the children. You are asked to use and implement a new selection method (moremethods are alsopossible if you want) and to compare it to the one given in the algorithm. To do this, use the same reference solution, the same initial population and the same setting. Which method helps the algorithm gettingthe optimal solution inless iteration?Justify.https://pythonhealthcare.org/2018/10/01/94-genetic-algorithms-a-simple-genetic-algorithm-code-only/


Answered Same DayMar 11, 2021

Answer To: The algorithm uses tournament selection to select which parents to use toreproduce the children. You...

Ximi answered on Mar 13 2021
140 Votes
#!/usr/bin/env python
# coding: utf-8
# In[36]:
import random
import numpy as np
get_ipython().run_line_magic('matplotlib', 'inline')
def create_reference_solution(chromosome_length):
number_of_ones = int(chromosome_l
ength / 2)
# Build an array with an equal mix of zero and ones
reference = np.zeros(chromosome_length)
reference[0: number_of_ones] = 1
# Shuffle the array to mix the zeros and ones
np.random.shuffle(reference)

return reference
def create_starting_population(individuals, chromosome_length):
# Set up an initial array of all zeros
population = np.zeros((individuals, chromosome_length))
# Loop through each row (individual)
for i in range(individuals):
# Choose a random number of ones to create
ones = random.randint(0, chromosome_length)
# Change the required number of zeros to ones
population[i, 0:ones] = 1
# Sfuffle row
np.random.shuffle(population[i])

return population
def calculate_fitness(reference, population):
# Create an array of True/False compared to reference
identical_to_reference = population == reference
# Sum number of genes that are identical to the reference
fitness_scores = identical_to_reference.sum(axis=1)

return fitness_scores
def select_individual_by_tournament(population, scores):
# Get population size
population_size = len(scores)

# Pick individuals for tournament
fighter_1 = random.randint(0, population_size-1)
fighter_2 = random.randint(0, population_size-1)

#print ("Fighter 1", fighter_1)
#print ("Fighter 2", fighter_2)

# Get fitness score for each
fighter_1_fitness = scores[fighter_1]
fighter_2_fitness = scores[fighter_2]

# Identify undividual with highest fitness
# Fighter 1 will win if score are equal
if fighter_1_fitness >= fighter_2_fitness:
winner = fighter_1
else:
winner = fighter_2

#print ("Winner ", winner)
# Return the chromsome of the winner
return population[winner, :]
def select_individual_by_random_selection(population, scores):
# Get population size
population_size = len(scores)

...
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here