Question: A Simple evolutionary alogorithm #!/usr/bin/python import random source = jiKnp4bqpmAbp target = Hello, World! def fitness(source, target): fitval = 0 for i in range(0, len(source)):

A Simple evolutionary alogorithm

#!/usr/bin/python

import random

source = "jiKnp4bqpmAbp"

target = "Hello, World!"

def fitness(source, target):

fitval = 0

for i in range(0, len(source)):

fitval += (ord(target[i]) - ord(source[i])) ** 2

return(fitval)

def mutate(source):

charpos = random.randint(0, len(source) - 1)

parts = list(source)

parts[charpos] = chr(ord(parts[charpos]) + random.randint(-1,1))

return(''.join(parts))

random.seed()

fitval = fitness(source, target)

i = 0

while True:

i += 1

m = mutate(source)

fitval_m = fitness(m, target)

if fitval_m < fitval:

fitval = fitval_m

source = m

print "%5i %5i %14s" % (i, fitval_m, m)

if fitval == 0:

break

(instruction 1) Simple evolutionary algorithm: Evolve_1

1. The user can enter any user defined string as the evolutionary target up to 30 characters max.

2. The program should randomly generate the first source from which to mutate.

3. The program should print-out every 100th iteration the iteration and the current mutated source

and its fitness in the command window.

The above python program was changed to matlab program according to the instruction like the following

clc clear all

t= input('Enter a string (max 30 chars in length): ', 's'); numRands=length(t); tlength=numRands; source=t(ceil(rand(1,tlength)*numRands)); fitval = fitness(source, t); i = 0;

disp(' ') % printing header fprintf('Iteration No.\t\tfitnes Value\t\tMutated Source ') disp('======================================================') while 1 m = mutate(source); fitval_m = fitness(m, t); if fitval_m < fitval fitval = fitval_m; source = m;

end if fitval == 0 % Printing the last Mutation detail fprintf('%-19i %-20i %s ', i, fitval_m, m); break end if mod(i, 100) == 0 % Printing each 100th mutation fprintf('%-19i %-20i %s ', i, fitval_m, m); end i = i + 1; end

function fitval = fitness(source, t) fitval = 0; for i = 1 : length(source) fitval = fitval + (double(t(i)) - double(source(i))) ^ 2; end

end

function parts = mutate(source) parts = source; % error was in this part, you were generating a random number in range 1 to length of % source and then subtracting 1 generated random integer hence % when random number generated was 1 then after subtracting 1, charpos was 0 and you know % matlab indexing starts from 1, not 0 charpos = randi(length(source)); parts(charpos) = char(double(parts(charpos)) + (randi(3)-2)); end

2. A better evolutionary algorithm

#!/usr/bin/python

import random

import string

target = "Hello, World!"

def calc_fitness(source, target):

fitval = 0

for i in range(0, len(source)):

fitval += (ord(target[i]) - ord(source[i])) ** 2

return(fitval)

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})

def random_parent(genepool):

wRndNr = random.random() * random.random() * (GENSIZE - 1)

wRndNr = int(wRndNr)

return(genepool[wRndNr])

def dump_genepool(generation, genepool):

for candidate in genepool:

print "%6i %6i %15s" % (

generation,

candidate['fitness'],

''.join(candidate['dna'])

)

print

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)

generation = 0

while True:

generation += 1

genepool.sort(key=lambda candidate: candidate['fitness'])

dump_genepool(generation, genepool)

if genepool[0]['fitness'] == 0:

# Target reached

break

parent1 = random_parent(genepool)

parent2 = random_parent(genepool)

child = mutate(parent1, parent2)

if child['fitness'] < genepool[-1]['fitness']:

genepool[-1] = child

(Instructon 2 ) A Better evolutionary algorithm : Evolve_2m

Your program must include at least 3 user- defined functions(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 user can enter the gene-pool size limited from 10-50 individuals.

The program should randomly generate the first generation of parents from which to breed

The program should print -out every 100th iteration the iteration and the best-fit individual along with its fitness in the command window

Please According to the instruction 2 change The above better evolutionary python program to matlab program like evolve_1 done and name it evolve_2m

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!