Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I almost done this MATLAB program but please I need some assistance to finish up

ID: 3712186 • Letter: I

Question

I almost done this MATLAB program but please I need some assistance to finish up according to the instruction

1. Your matlab program must include at least 3 user-defined functions (fitness.m, get_parent.m and

mutate.m).

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

3. The user can enter the gene-pool size limited from 10 – 50 individuals.

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

5. The program should print-out every 100th iteration the iteration and the best-fit individual along

   with its fitness in the command window.

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=0;

disp(' ');

for i=1:GENSIZE

    

    dna = pop(i,:);

    fitness = calc_fitness(dna, target);

    genepool(i,:)=[ dna fitness];

  

  

end

child_fitness = 100;

genepool=sortrows(genepool,length(target)+1,'ascend')

while child_fitness~=0

i=i+1;   

  

    parent1 = get_parent(genepool,GENSIZE);

    parent2 = get_parent(genepool,GENSIZE);

    child = mutate(parent1,parent2,target);

   child_fitness = calc_fitness(child,target);

      if child_fitness< genepool(GENSIZE,length(target))

       

        genepool(GENSIZE,:) =[child, child_fitness];

        genepool=sortrows(genepool,length(target)+1,'ascend');

     end

end

function [fitval]= calc_fitness( source , target)%def calc_fitness;

     fitval = 0;

   for i = 1 : length(source)-1

      fitval= fitval + (double(target(i)) - double(source(i))) ^ 2;

   end

end

function [child_dna]=mutate(parent1,parent2,target)

    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

Explanation / Answer

Try this. It should work.

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=0;

disp(' ');

for i=1:GENSIZE

    

    dna = pop(i,:);

    fitness = calc_fitness(dna, target);

    genepool(i,:)=[ dna fitness];

  

  

end

child_fitness = 100;

genepool=sortrows(genepool,length(target)+1,'ascend')

while child_fitness~=0

i=i+1;   

  

    parent1 = get_parent(genepool,GENSIZE);

    parent2 = get_parent(genepool,GENSIZE);

    child = mutate(parent1,parent2,target);

   child_fitness = calc_fitness(child,target);

      if child_fitness< genepool(GENSIZE,length(target))

       

        genepool(GENSIZE,:) =[child, child_fitness];

        genepool=sortrows(genepool,length(target)+1,'ascend');

     end

end

function [fitval]= calc_fitness( source , target)%def calc_fitness;

     fitval = 0;

   for i = 1 : length(source)-1

      fitval= fitval + (double(target(i)) - double(source(i))) ^ 2;

   end

end

function [child_dna]=mutate(parent1,parent2,target)

    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