matlab program for comparison and loop

1 view (last 30 days)
Chromosome[1] = [12;05;23;08] Chromosome[2] = [02;21;18;03] Chromosome[3] = [10;04;13;14] Chromosome[4] = [20;01;10;06] Chromosome[5] = [01;04;13;19] Chromosome[6] = [20;05;17;01]
R[1] = 0.201 R[2] = 0.284 R[3] = 0.099 R[4] = 0.822 R[5] = 0.398 R[6] = 0.501
C[1] = 0.1254 C[2] = 0.2710 C[3] = 0.4118 C[4] = 0.6639 C[5] = 0.7882 C[6] = 1.0
i want to write a simple matlab program in which i have to compare the r(k) with value of c(k) if r(k) lies within the range of c(k) then it will return chromosome of that particular k. actually it is part of genetic algorithm i have write 30% program but i am confused at roulette stage how to write it.
the response of the above program must return the following output.
Chromosome[1] = [02;21;18;03] Chromosome[2] = [10;04;13;14] Chromosome[3] = [12;05;23;08] Chromosome[4] = [20;05;17;01] Chromosome[5] = [10;04;13;14] Chromosome[6] = [20;01;10;06]
manual calculation are as follow.
as r1=0.201 doesnot lie in the range of c1=0.125 (c1<r1), so we move forward and compare it with next, means with c2. as r1=0.201<c2 = 0.2710 it means it must replace chromosome of 1 with 2.
following is the table of replacement.
NewChromosome[1] = Chromosome[2] NewChromosome[2] = Chromosome[3] NewChromosome[3] = Chromosome[1] NewChromosome[4] = Chromosome[6] NewChromosome[5] = Chromosome[3] NewChromosome[6] = Chromosome[4]

Accepted Answer

Geoff Hayes
Geoff Hayes on 3 Oct 2014
Mudasir - from your roulette selection scheme to select parents, where the vector R seems to be the randomly assigned values (between 0 and 1) for each parent, and C is the cumulative probability distribution vector, then you can implement the following to get the parent selection
R(1) = 0.201;
R(2) = 0.284;
R(3) = 0.099;
R(4) = 0.822;
R(5) = 0.398;
R(6) = 0.501;
C(1) = 0.1254;
C(2) = 0.2710;
C(3) = 0.4118;
C(4) = 0.6639;
C(5) = 0.7882;
C(6) = 1.0;
for k=1:length&reg;
% use find to find the first (1) element of
% C that is greater than R(k)
idx = find(C>R(k),1);
if isempty(idx)
idx = length(C);
end
% select chromosome idx as new parent
end
The above determines the idx according to R and C; you just need to map the population chromosomes to the new parent set.
  3 Comments
Geoff Hayes
Geoff Hayes on 6 Oct 2014
Mudasir - looking at his paper (in the link you provided), the statement which you highlighted in bold does not make all that much sense. I think that you can ignore it. He may be alluding to how other mutation processes work where you mutate the gene only if the random number generated for that gene is less than the mutation rate. But when he states that Mutation process is done by generating a random integer between 1 and total_gen (1 to 24). If generated random number is smaller than mutation_rate(ρm) variable then marked the position of gen in chromosomes, it is unclear how an randomly generated integer between 1 and 24 can ever be less than the mutation rate, which will always be less than one!
I think if you follow his next steps and just realize that if the mutation rate is 10%, then 10% of the genes in the whole population should be mutated. So this is just
numGenes = 4; % number of genes per chromosome (number of variables)
numChrom = 6; % number of chromosomes (population size)
mutRate = 0.1; % mutation rate
totalNumGenes = numGenes*numChrom; % total number of genes
% across all chromosomes
numGenesToMut = floor(0.1* totalNumGenes); % number of genes to mutate
So there are two genes to mutate, from numGenesToMut. And so we must randomly generate two integers from 1 to 24. In his example, the random numbers are 12 and 18. If we consider 12, since there are 4 genes per chromosome, then the mutation occurs in the 4th gene of the 3rd chromosome as he has shown above (since 12=4+4+4). With 18, again since there are 4 genes per chromosome (imagine all 6 chromosomes lined up, side by side), then the mutation occurs at the second gene of the fifth chromosome (since 18=4+4+4+4+2).
Mudasir Ahmed
Mudasir Ahmed on 20 Oct 2014
Edited: Mudasir Ahmed on 20 Oct 2014
sir i successfully make a program for Genetic algorithm (without using elite feature) and achieve goal which was desired. i have tried for various number of chromosomes like.5,6,10 and 20. but didn't notice any significant change. sir as i logically believe, there must me some impact on output response by increasing the number of chromosome. and sir my second question is i want to know, at which stage elite strategy is used and how. i am following the below link: http://www.mathworks.com/help/gads/how-the-genetic-algorithm-works.html in which a line is written "Some of the individuals in the current population that have lower fitness are chosen as elite. These elite individuals are passed to the next population." i am confused at stage , means if a individual have low fitness then how it is processed to a next level?.
kindly share information if you have any, thanks in advance sir

Sign in to comment.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!