How can i use the 'feval' function in Binary Genetic Algorithm for evaluating a fitness function?
Afficher commentaires plus anciens
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
Réponse acceptée
Plus de réponses (0)
Catégories
En savoir plus sur Entering Commands dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!