Writing a custom Selection Function for a Genetic Algorithm problem

6 ビュー (過去 30 日間)
Renovatio
Renovatio 2015 年 4 月 3 日
コメント済み: Renovatio 2017 年 12 月 29 日
Hey guys, I am a bit lost on this. Any help would be appreciated. Essentially my problem is this: I want to write a custom SelectionFcn for my GA to select the 25 best parents and then select 2 at random to create 25 children. And the children should be the arithmetic mean between the parents.
I have olny gotten as far as this:
function parents = parents(expectation, nParents, options, state)
nParents = 25
parents = zeros(1,nParents);
% get the scores of the current population
curPopScores = state.Score;
% get the 25 best parents
if length(curPopScores)>25
% created matrix with scores and indices for each parent
if iscolumn(curPopScores)
data = [curPopScores'; 1:length(curPopScores)];
else
data = [curPopScores'; 1:length(curPopScores)];
end
% now sort the data on the column that corresponds to the scores
% (-1 means sort the first column in descending order)
sortedData = sortrows(data',-1);
% get the sorted parent indices from the first 25 elements
parents = sortedData(1:25,2);
That should work except I am unable to access the scores with "state.Score"

採用された回答

Alan Weiss
Alan Weiss 2015 年 4 月 20 日
I am sorry that the documentation is not clear on this point. I will try to improve it.
As described here, a custom selection function needs to have the syntax
function parents = myfun(expectation, nParents, options)
The question is what does the expectation argument mean? In fact, it is the vector of scaled fitness functions associated with the members of the population. So if you want the best 25 members, just sort expectation and take the top 25.
Alan Weiss
MATLAB mathematical toolbox documentation
  2 件のコメント
Renovatio
Renovatio 2015 年 4 月 21 日
Oh ok! thank you very much. Now I am using this:
function parents = parents(expectation, nParents, options)
nParents = 25
expectation = sort(expectation(:,1))
parents = zeros(1,nParents)
parents = (expectation(1:25))'
end
But I now get this error message:
Index exceeds matrix dimensions.
Error in stepGA (line 34)
xoverKids = feval(options.CrossoverFcn, parents(1:(2 * nXoverKids)),options,GenomeLength,FitnessFcn,thisScore,thisPopulation,options.CrossoverFcnArgs{:});
Any idea how to solve this?
Renovatio
Renovatio 2015 年 4 月 21 日
Ok I think I solved my problem by using this as the selection function:
function parents = parents(expectation, nParents, options)
nParents = 25;
expectation = (expectation(:,1));
parents = zeros(1,nParents);
A = sort(expectation);
B = A(1:25);
for i = 1:25
parents(1,i) = find(expectation==B(i,1));
end
end

サインインしてコメントする。

その他の回答 (2 件)

Geoff Hayes
Geoff Hayes 2015 年 4 月 4 日
Renovatio - I don't have the Global Optimization Toolbox, but I suspect that you have to do something like the following (note that expectation and nParents are inputs to your function so you may not want to overwrite them)
function parents = parents(expectation, nParents, options)
nParents = 25
parents = zeros(1,nParents);
% get the current state of the GA
state = gaoptimget(options, 'state');
% get the scores of the current population
curPopScores = state.Score;
% get the 25 best parents
if length(curPopScores)>25
% created matrix with scores and indices for each parent
if iscolumn(curPopScores)
data = [curPopScores'; 1:length(curPopScores)];
else
data = [curPopScores'; 1:length(curPopScores)];
end
% now sort the data on the column that corresponds to the scores
% (-1 means sort the first column in descending order)
sortedData = sortrows(data',-1);
% get the sorted parent indices from the first 25 elements
parents = sortedData(1:25,2);
else
parents = 1:25;
end
The above should allow you to select the best 25 parents from the current generation. For more details on the state structure, see GA options.
  1 件のコメント
Renovatio
Renovatio 2015 年 4 月 17 日
Hey thank you very much for the help. I just had the time to look at your answer. The problem now is that "gaoptimget" only access the options for the GA not the actual output.
I added a custom output function so that I can see the output of each iteration but it seems now that my SelectionFcn doesn't recognize the variable "score".
This is my output function:
function [state, options,optchanged] = myoutputfcn(options,state,flag)
optchanged = false;
switch flag
case 'init'
disp('Starting the algorithm');
case {'iter','interrupt'}
disp('Iterating ...')
case 'done'
disp('Performing final task');
end
And the new selection function:
function parents = parents(expectation, nParents, options)
nParents = 25
parents = zeros(1,nParents);
% get the scores of the current population
curPopScores = score;
% get the 25 best parents
if length(curPopScores)>25
% created matrix with scores and indices for each parent
if iscolumn(curPopScores)
data = [curPopScores'; 1:length(curPopScores)];
else
data = [curPopScores'; 1:length(curPopScores)];
end
% now sort the data on the column that corresponds to the scores
% (-1 means sort the first column in descending order)
sortedData = sortrows(data',-1);
% get the sorted parent indices from the first 25 elements
parents = sortedData(1:25,2);
else
parents = 1:25;
end

サインインしてコメントする。


yuan feng
yuan feng 2017 年 12 月 10 日
hello,i want to write a custom mutation function, but i don't know how to write, i would appreciate it if you can help me.
  2 件のコメント
Geoff Hayes
Geoff Hayes 2017 年 12 月 11 日
yuan - please post this as a question rather than as an answer.
Renovatio
Renovatio 2017 年 12 月 29 日
Hi Yuan, you can find the code I wrote for this here https://bitbucket.org/SDelgadoE/bachelor-thesis/downloads

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeGenetic Algorithm についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by