MATLAB: Hello everyone! this is the first time I am using GA and I encountered with this message:Failure in initial user-supplied fitness function evaluation. GA cannot continue.

failuregenetic algorithm

here is my codes.
k1=0.6;
k2=0.25;
k3=0.15;
[x,fval] = ga(@(f)myfcn(f(1),f(2),f(3)),3)
function t=myfcn(f)
t=(k1*f(1)+k2*f(2)+k3*f(3));
end
%%all f values have been attained in another code
can anyone help me with this?

Best Answer

amir - you don't need to supply the f, those f values that have been attained in another code. The genetic algorithm will provide those from the initial population and from the subsequent generations (via crossover and mutation). All you need to do is pass in the function handle like
[x,fval] = ga(@myfcn, 3)
Since your myfcn depends upon the k1, k2, and k3, then ensure that your fitness function is nested within the main function
function main
k1=0.6;
k2=0.25;
k3=0.15;
[x,fval] = ga(@myfcn,3)
function t=myfcn(f)
t=(k1*f(1)+k2*f(2)+k3*f(3));
end
end
and saved (in this case) to a file named main.m. (Perhaps you have already done this.)
Related Question