help for roulette-wheel selection matlab code for minimization problem?
20 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
reshdev
am 26 Aug. 2014
Kommentiert: reshdev
am 27 Aug. 2014
Hello,
I am trying to implement genetic algorithm.(Already able to generate initial population (matlab code attached))
can any one please help to write roulette-wheel selection matlab code for minimization problem --
Just to explain, Suppose my input to matlab program is online2([9 1 3; 8 2 4], 5)
fitness function= Min. C(1,1)*X(1,1)+C(1,2)*X(1,2)+C(1,3)*X(1,3)+C(1,4)*X(1,4)+C(1,5)*X(1,5)+
C(2,1)*X(2,1)+..............+C(2,5)*X(2,5)+C(3,1)*X(3,1)+........C(3,5)*X(3,5)+C(4,1)*X(4,1)+
.....+C(4,5)*X(4,5)+
C(5,1)*X(5,1)+.....+C(5,5)*X(5,5)
input matrix for C(i,j)= [1 2 3 4 5;2 3 4 5 6; 1 2 3 4 5; 3 4 6 7 8; 9 6 5 3 2]
Matrix X = output from run u (:,1:5)+output from run u (:,6:10)..... note r=5 is given as input within program
X wiil be 5*5 matrix .
7 Kommentare
Geoff Hayes
am 26 Aug. 2014
reshdev - you will need to separate your fitness function from the population initialization. Something like
function [score] = myFitnessFunction(inputData)
X = inputData(:,1:5)+inputData(:,6:10);
C= [1 2 3 4 5;2 3 4 5 6; 1 2 3 4 5; 3 4 6 7 8; 9 6 5 3 2];
r = size(X,1);
Ff = zeros(r,r);
for row=1:r
for col =1:r
Ff(row,col)= C(row,col)*X(row,col);
end
end
%disp(Ff)
score= sum(sum(Ff));
%disp(Fitness)
end
Akzeptierte Antwort
Geoff Hayes
am 26 Aug. 2014
reshdev - I took your code to initialize the population and to score each member, and put both in the myInitPop.m and myFitnessFunc.m. See attached. Now when you execute
pop = myInitPop([9 1 3; 8 2 4], 5);
the output variable, pop, will be a 5x2 cell array where each element corresponds to a member of the population. The first column will have the 5x10 matrix for each member, and the second column will be the score/fitness of each member according to myFitnessFunc.
Now roulette wheel selection, or fitness proportionate selection, is relatively easy (there may be better methods for parent selection) but try using the following pseudo-code to get you going. I'll define the function as
function [parents] = mySelectionRoulette(pop,numParents)
So the inputs to the roulette selection method will be the population, pop, which was generated in the population initialization phase (and will be the updated population on subsequent iterations of the algorithm), and the number of parents to select, numParents. So from this population, we wish to choose a certain number of parents, which corresponds to the output parameter parents.
According to the above link, you need to calculate the probability of each member of the population being selected as a parent, with the idea being that the fitter members will more likely be selected as parents.
This probability will be defined as
invFitnessSum = sum of the inverse of the fitness (score) for each member of population
probForMember(k,1) = (1/pop{k,2})/invFitnessSum
The first is easy to calculate. Just sum the inverse of each score value from the second column of the input vector pop. You will need an array for the second value which will have the probabilities for each member of the population. The array size will correspond to the number of members of the population. Note that we do (1/pop{k,2})/invFitnessSum because (presumably) we are using the GA to minimize the fitness function, so we want those members with a smaller score to have a higher probability of being selected.
You then want to sort the probForMember in ascending order (keeping in mind which probability corresponds to which member). You may in fact want to store this data within probForMember. You could initialize this matrix as
probForMember = zeros(n,2); % where n is the number of members in population
probForMember(:,2) = [1:n]'; % assign the member id to each each element
Then, to do the sort, just try
probForMember = sortrows(probForMember,1)
which will sort the first column in ascending order AND maintain the ids of the members with that probability.
According to the link, we now need the cumulative probability distribution. So use cumsum on the first column of probForMember.
Finally, for each parent that you need to select, generate a random number (see rand) which will correspond to the ball in the roulette wheel analogy, and wherever that ball drops, that is your parent to choose.
Try writing the above code. I think that you have all that is needed to get you started on this.
3 Kommentare
Weitere Antworten (0)
Siehe auch
Kategorien
Find more on Genetic Algorithm in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!