Question: Both matlab program (Evolve1.m and Evolve2.m are need to be incoprated in to the same GUI please i need help if you want the instruction
Both matlab program (Evolve1.m and Evolve2.m are need to be incoprated in to the same GUI please i need help if you want the instruction as bellow
For Evolve1.m the instruction says that The Gui should display the final iteration number,source and fitness(should be the target with a fitness 0f 0).
For Evolve2.m the instruction says that The Gui should display the gene-pool and the fitness values beside that the GUI should display the final sorted population and fitness values,along with the final iteration number
%%Evolve1.m
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;
charpos = randi(length(source));
parts(charpos) = char(double(parts(charpos)) + (randi(3)-2));
end
%%Evolve2.m
clear;close all;clc;
GENSIZE=20;
target =input('Enter a string (max 30 chars in length): ', 's');
genepool=[];
pop= char(randi(127,GENSIZE,length(target)));
i=1;
disp(' ')
% printing header
fprintf('Iteration No.\t\tfitnes Value\t\tMutated Source ')
disp('======================================================')
for i=1:GENSIZE
dna = pop(i,:);
fitness = calc_fitness(dna, target);
genepool(i,:)=[dna fitness];
end
child_fitness = 1500;
parent1 = get_parent(genepool);
parent2 = get_parent(genepool);
child = mutate(parent1,parent2);
fprintf('%-19i %-20i %s ',i, child_fitness, char(child));
while child_fitness~=0
i=i+1;
parent1 = get_parent(genepool);
parent2 = get_parent(genepool);
child = mutate(parent1,parent2);
child_fitness = calc_fitness(child,target);
genepool(GENSIZE,:) =[child, child_fitness];
genepool=(sortrows(genepool,length(target)+1,'ascend'));
genepool=(sortrows(genepool,length(target)+1,'ascend'));
if mod(i, 100) == 0 % Printing each 100th mutation
disp('');
fprintf('%-19i %-20i %s ', i, child_fitness, char(child));
end
end
genepool=(sortrows(genepool,length(target)+1,'ascend'));
fprintf('%-19i %-20i %s ',i, child_fitness, char(child));
disp(sortrows(genepool,length(target),'ascend'));
function[parent1,parent2] = get_parent( genepool)
parent1 = genepool(:,1:end-1);
parent2 = genepool(:,2:end-1);
end
function [child_dna]=mutate(parent1,parent2)
child_dna =parent1(1,:);
start=randi(size(parent2(1,:)),1,1);
stop=randi(size(parent2(1,:)),1,1);
if(start>stop)
tmp=start;
start=stop;
stop=start;
end
child_dna(start:stop)=parent2(1,start:stop);
charpos=randi(size(child_dna),1,1);
child_dna(charpos)=char(uint8(child_dna(charpos))+randi(3,1,1)-2);
end
function [fitval]= calc_fitness(source, target)%def calc_fitness;
fitval=0;
for i = 1 : length(source)
fitval= fitval + (double(target(i)) - double(source(i))) ^ 2;
end
end
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
