Return value type for GA toolbox population function ??

2 views (last 30 days)
I am trying to use GA toolbox,in which I am creating a custom population function(in the options column in the toolbox) through which I want to return a set of values in an array for the subsequent operation in GA. For this,I am unable to return array of population through the custom function which I created for returning array of population for the GA process.
So, I want to know what kind of values does the custom population function can return so that I can change my fitness and population function accordingly.Can the population function return array of population.I know I can use binary and string bits but I want to use array,because it suits my fitness function and is also a personal preference.
Please have a look at the population function I wrote.It works seperately but not in the GA toolbox.I am stuck in channelizing the return of the population function through the toolbox.I hope it makes sense. Please help. Any suggestions are also welcomed.
The population function-
function Population = myfun()
pop=100;
boxes=30;
blocks=zeros(2,boxes);
n=1;
while n<=boxes
x=rand*0.5;
if x<5
blocks(1,n)=n;
blocks(2,n)=x;
n=n+1;
end
end
Population=zeros(2,30,100);
for k=1:100
p=randperm(30);
for i=1:30
for j=1:30
if blocks(1,j)== p(i)
Population(1,i,k)=p(i);
Population(2,i,k)=blocks(2,j);
end
end
end
end
end

Answers (1)

Alan Weiss
Alan Weiss on 7 May 2015
See the syntax for custom population function in the documentation. In particular:
"Your creation function must have the following calling syntax.
function Population = myfun(GenomeLength, FitnessFcn, options)
The input arguments to the function are:
˗ Genomelength — Number of independent variables for the fitness function
˗ FitnessFcn — Fitness function
˗ options — Options structure
The function returns Population, the initial population for the genetic algorithm."
ga passes GenomeLength, FitnessFcn, and options. You are free to ignore these inputs or to use them.
You should save your creation function as, say, myfun.m, and then tell ga that you want to use this creation fynction by setting options:
options = gaoptimset('CreationFcn',@myfun);
Make sure that you pass options to ga:
x = ga(fitnessfcn,nvar,A,b,Aeq,beq,lb,ub,nonlcon,options)
Alan Weiss
MATLAB mathematical toolbox documentation

Community Treasure Hunt

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

Start Hunting!