import random
population=[‘01101′,’11000′,’01000′,’10011’]
def decode(chromosome):
decodingScheme=[2**i for i in range(4,-1,-1)]
phenoType=sum([int(i)*j for i,j in zip(chromosome,decodingScheme)])
return phenoType
def fitness(phenoType):
return phenoType*phenoType
def returnProbability(fitness):
probabilities= [individualFitness/sum(fitness) for individualFitness in fitness]
probabilities=[round(p,3) for p in probabilities]
probabilities=[round(p,2) for p in probabilities]
return probabilities
def createBin(probabilities):
# ADD YOUR CODE HERE
bins=[(0.00,0.14),(0.14,0.63),(0.63,0.69),(0.69,1.00)]
return bins
def generateRandomNumber():
rnadomFloatValues=[random.uniform(0,1) for _ in range(4)]
rnadomFloatValues=[round(r,3) for r in rnadomFloatValues]
rnadomFloatValues=[round(r,2) for r in rnadomFloatValues]
return rnadomFloatValues
def findBinIndex(rnadomFloatValues,bins):
# ADD YOUR CODE HERE
return indexList
def selection(indexList):
selectedParent=[population[i] for i in indexList]
return selectedParents
def crossOver(selectedParents):
lenthOfChromosome=len(selectedParents[0])
crossOverPointOne=random.choice(list(range(lenthOfChromosome)))
parent1=selectedParents[0]
parent2=selectedParents[1]
offspring1=parent1[:crossOverPointOne]+parent2[crossOverPointOne:]
offspring2=parent2[:crossOverPointOne]+parent1[crossOverPointOne:]
crossOverPointTwo=random.choice(list(range(lenthOfChromosome)))
parent3=selectedParents[2]
parent4=selectedParents[3]
offspring3=parent3[:crossOverPointTwo]+parent4[crossOverPointTwo:]
offspring4=parent4[:crossOverPointTwo]+parent3[crossOverPointTwo:]
offspring=[offspring1,offspring2,offspring3,offspring4]
return offspring
pheoTypes=[decode(chromosome) for chromosome in population]
fitness=[fitness(phenoType) for phenoType in pheoTypes]
probabilities=returnProbability(fitness)
bins=createBin(probabilities)
rnadomFloatValues=generateRandomNumber()
indexList=findBinIndex(rnadomFloatValues,bins)
selectedParents= selection(indexList)
offspring = crossOver(selectedParents)
——————————————-
——————————————-
1. This is a python code. This is incomplete code. Please go through it, understand and complete the code accordingly. You will find “#ADD YOUR CODE HERE”, please complete the code there.
Delivering a high-quality product at a reasonable price is not enough anymore.
That’s why we have developed 5 beneficial guarantees that will make your experience with our service enjoyable, easy, and safe.
You have to be 100% sure of the quality of your product to give a money-back guarantee. This describes us perfectly. Make sure that this guarantee is totally transparent.
Read moreEach paper is composed from scratch, according to your instructions. It is then checked by our plagiarism-detection software. There is no gap where plagiarism could squeeze in.
Read moreThanks to our free revisions, there is no way for you to be unsatisfied. We will work on your paper until you are completely happy with the result.
Read moreYour email is safe, as we store it according to international data protection rules. Your bank details are secure, as we use only reliable payment systems.
Read moreBy sending us your money, you buy the service we provide. Check out our terms and conditions if you prefer business talks to be laid out in official language.
Read more