How can i use the 'feval' function in Binary Genetic Algorithm for evaluating a fitness function?
Show older comments
Respected Scholars,
I have used "cost=feval(fitnessfunction,argument)", where fitnessfunction is a function call and argument is a flexible Matrix. But,Optimizer GA have shown errors saying that "Argument must be a string or a function handle" and "Index exceeds matrix dimensions"
Code:
ff=@funcname; %ff is the function handle (handles the function 'funcname')
npar=1; %no.of optimization variables
maxit=100; %Maximum No.of Iterations
mincost=-9999999; %Minimum Cost
%GA Parameters
selection=0.5; %Fraction of Population Kept
popsize=input('enter pop size depending on the no.of possible 'a' values='); %Set Population Size
mutrate=0.15;
nbits=6;
Nt=nbits*npar;
keep=floor(selection*popsize); %Population Members that Survive
iga=0; %Generation Counter Initialized
pop=round(rand(popsize,Nt)); %Random population of 1s and 0s
par=gadecode(pop,0,10,nbits); %convert binary to continuous values (par handles the function 'gadecode')
cost=feval(ff,par); %Calculates population cost using ff
Example fitness function:
function f=funcname(a)
for a=1:10
f=(a*a)+a;
disp(f);
end
end
Thank You... In Advance
Accepted Answer
More Answers (0)
Categories
Find more on Entering Commands 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!