custom selection functions for genetic algorithm

I am brand new to genetic algorithms. I am trying to learn by reproducing the results I found in a paper. I am getting hung up on the selection method. The paper uses normalized geometric selection. It looks like this in not one of the canned methods available and that I need to write a custom function. I have an equation and most of the parameters that I need. The onlything I think I need is the rank of the individual. My big issue is that I am not clear on how to pass this to my function. I took a stab at it with the below function but this failed. I dont know the syntax for passing parameters to and from my function for this instance. I appreciate any help. I suspect the solution is very simple but I am really confused.
function [S] = Selection(r)
p = 60; % population size
q = 0.05; % probablity of selection
R = r; % rank of individual
q = p/(1-(1-q)^p);
S = q*(1-q)^(R-1); % normalized geometric equation

2 个评论

Too much text. Im not that good at english. Do you have a question?
Need help with syntax for custon selection function in genatic algorithm. I need to get parameter for rank into the function. I also need to output information in correct form.

请先登录,再进行评论。

 采纳的回答

The selection function for ga must have the following signature
function parents = myfun(expectation, nParents, options)
The explanation of the input and output variables are given here: https://www.mathworks.com/help/gads/genetic-algorithm-options.html#f6593. Note that the output variables 'parents' must have the dimension of 1xnParents. To see an example of selection function, check the code of one of the MATLAB's built-in selection function
edit selectionremainder

8 个评论

Hi Ameer,
Thanks for your reply. Out of the function parameters you mention which value is the rank? This is the part that I am unclear about. I think rank may also be refered to as score.
Justin
Yes, the expectation parameter is the score of each member of the current population. You can sort this vector to get the fitness rank of each member.
Justin's comment moved here to keep the discussion organized:
Ok, I see. Thank you so much.
I have only a little more confusion and this is because I don't fully understand the concept of genetic algorithms yet. Can you explain what selection is doing? My understanding was that it is determining the likelihood that someone will reproduce. I think I am not correct because I have the population size set to 60 and I am returning a column vector of nParents rows. In my case nParents = 103 but I have the population size set at 60. Can you help me to understand?
The vector parents have a size of 1xnParents. The 'parents' vector determines which member of the current population will have a higher chance of becoming a parent. As you mentioned, the current population only has 60 members, but MATLAB requires you to return 103 parents. This means that a population member that has more entries in 'parents' vectors will have a higher chance of becoming parents than other members. For example, you can return vector 'parents' such that it always selects one parent. In that case, as expected, the genetic algorithm will fail. Consider the following example,
opts = optimoptions('ga', 'SelectionFcn', @myfun);
[x,f] = ga(@(x) sum(x.^2), 10, [], [], [], [], [], [], [], opts); % a very simple optimization problem
function parents = myfun(expectation, nParents, options)
parents = ones(1,nParents); % at each iteration, I tell MATLAB that the population member
% at first location shoule be the parent. This is quite naieve
% approach, and you will see that the algorithm will fail
end
So by giving more entries in 'parents' to a particular population member, you can control that it will have a higher chance of becoming a parent,
Thank you. This answer is very clear. I had thougt that parents returned the probability that a member of the population should be the parrent, but instead this number needs to be an integer because we are telling MATLAB to select this person as the parent. This makes my approach very dificult because my function calculates probability. I am not sure how to move forward now. Would you be kind enough to offer a suggestion? I have attached a snip from a paper I found that contains an expression to find normalized geometric selection. The paper is cited below. Also, here is the code I have after incorporating your suggestions.
function parents = Selection(expectation, nParents, options)
popn = 60; % population size
p = 0.05; % probablity of selection (given in paper)
q = p/(1-(1-p)^popn);
best = max(expectation);
NormalizedRank = expectation ./ best;
for i=1 : length(NormalizedRank)
parents(i,1) = q*(1-p)^(NormalizedRank(i)-1); % normalized geometric equation
end
If I understand correctly, you have a vector of probability values of size 1*numel(expectation) [same as population size], and you want to generate vector 'parents', such that the population member gets entries in it according to your probability vector. If yes, then randsample from Statistics and Machine Learning Toolbox: https://www.mathworks.com/help/stats/randsample.html which is created exactly for this purpose. If you don't have the toolbox, then you can download this package from fileexchange: https://www.mathworks.com/matlabcentral/fileexchange/34101-random-numbers-from-a-discrete-distribution
p % your probability vector of size 1*numel(expectation)
parents = randsample(1:numel(expectation), nParents, true, p)
This is exactly what I want. And your suggestion seems to be working. The ga came up with values very close to what I was trying reproduce in the paper. Thank you so much for your help.

请先登录,再进行评论。

更多回答(0 个)

类别

产品

版本

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by