import random import string Target=”Hello,world!” def mutate(parent1, parent2):
ID: 3740833 • Letter: I
Question
import random
import string
Target=”Hello,world!”
def mutate(parent1, parent2):
child_dna = parent1['dna'][:]
# Mix both DNAs
start = random.randint(0, len(parent2['dna']) - 1)
stop = random.randint(0, len(parent2['dna']) - 1)
if start > stop:
stop, start = start, stop
child_dna[start:stop] = parent2['dna'][start:stop]
# Mutate one position
charpos = random.randint(0, len(child_dna) - 1)
child_dna[charpos] = chr(ord(child_dna[charpos]) + random.randint(-1,1))
child_fitness = calc_fitness(child_dna, target)
return({'dna': child_dna, 'fitness': child_fitness})
GENSIZE = 20
genepool = []
for i in range(0, GENSIZE):
dna = [random.choice(string.printable[:-5]) for j in range(0, len(target))]
fitness = calc_fitness(dna, target)
candidate = {'dna': dna, 'fitness': fitness }
genepool.append(candidate)
def mutate(parent1, parent2):
child_dna = parent1['dna'][:]
# Mix both DNAs
start = random.randint(0, len(parent2['dna']) - 1)
stop = random.randint(0, len(parent2['dna']) - 1)
if start > stop:
stop, start = start, stop
child_dna[start:stop] = parent2['dna'][start:stop]
def dump_genepool(generation,genepool);
for candidate in genepool:
print “%6i%6i %15s”%( generation,candidate[‘fitness’],
“.join(candidate[‘dna’])
)
this is a pyton program I want to some one to change it in matlab program and the quation says
Your program must include at least 3 user-defined function (fitness.m, get_parent.m and mutate.m).
The user can enter any user defined string as the evolutionary target up to 30 characters max.
The program should randomly generate the first generation of parents from which to breed.
The program should print out every 100th iteration and the best-fit individual along with its fitness in the command window.
Explanation / Answer
#!/usr/bin/env python # this is a genetic algorithms tutorial by kristaRelated Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.