Dear Geoff how can I find out at which iteration I got the result?
You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
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.
2 views (last 30 days)
Show older comments
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?
1 Comment
Accepted Answer
Geoff Hayes
on 25 Mar 2020
Edited: Geoff Hayes
on 25 Mar 2020
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.)
12 Comments
Amir Hosseini
on 25 Mar 2020
thanks for youre attention Geoff!
actually I did it but still getting the same error. what should I do?
Amir Hosseini
on 25 Mar 2020
Edited: Geoff Hayes
on 25 Mar 2020
yes-
function myfun %%which is main m.file
k1=0.6;
k2=0.25;
k3=0.15;
[x,fval] = ga(@(f)myfcn ,3)
function t=myfcn(f)
t=(k1*f(1)+k2*f(2)+k3*f(3));
end
end
and the error is:
>> myfun
Not enough input arguments.
Error in myfun>myfcn (line 15)
t=(k1*f(1)+k2*f(2)+k3*f(3));
Error in myfun>@(f)myfcn(f)
Error in createAnonymousFcn>@(x)fcn(x,FcnArgs{:}) (line 11)
fcn_handle = @(x) fcn(x,FcnArgs{:});
Error in makeState (line 47)
firstMemberScore = FitnessFcn(state.Population(initScoreProvided+1,:));
Error in gaunc (line 40)
state = makeState(GenomeLength,FitnessFcn,Iterate,output.problemtype,options);
Error in ga (line 398)
[x,fval,exitFlag,output,population,scores] = gaunc(FitnessFcn,nvars, ...
Error in myfun (line 11)
[x,fval] = ga(fft,3)
Caused by:
Failure in initial user-supplied fitness function evaluation. GA cannot continue.
>> myfun
Index exceeds the number of array elements (1).
Error in myfun>myfcn (line 15)
t=(k1*f(1)+k2*f(2)+k3*f(3));
Error in myfun>@(f)myfcn(f(1),f(2),f(3))
Error in createAnonymousFcn>@(x)fcn(x,FcnArgs{:}) (line 11)
fcn_handle = @(x) fcn(x,FcnArgs{:});
Error in makeState (line 47)
firstMemberScore = FitnessFcn(state.Population(initScoreProvided+1,:));
Error in gaunc (line 40)
state = makeState(GenomeLength,FitnessFcn,Iterate,output.problemtype,options);
Error in ga (line 398)
[x,fval,exitFlag,output,population,scores] = gaunc(FitnessFcn,nvars, ...
Error in myfun (line 11)
[x,fval] = ga(@(f)myfcn(f(1),f(2),f(3)),3)
Caused by:
Failure in initial user-supplied fitness function evaluation. GA cannot continue.
>> myfun
Error using myfun>myfcn
Too many input arguments.
Error in myfun>@(f)myfcn(f(1),f(2),f(3))
Error in createAnonymousFcn>@(x)fcn(x,FcnArgs{:}) (line 11)
fcn_handle = @(x) fcn(x,FcnArgs{:});
Error in makeState (line 47)
firstMemberScore = FitnessFcn(state.Population(initScoreProvided+1,:));
Error in gaunc (line 40)
state = makeState(GenomeLength,FitnessFcn,Iterate,output.problemtype,options);
Error in ga (line 398)
[x,fval,exitFlag,output,population,scores] = gaunc(FitnessFcn,nvars, ...
Error in myfun (line 11)
[x,fval] = ga(@(f)myfcn(f(1),f(2),f(3)),3)
Caused by:
Failure in initial user-supplied fitness function evaluation. GA cannot continue.
>> myfun
Error using myfun>myfcn
Too many input arguments.
Error in myfun>@(f)myfcn(f(1),f(2),f(3))
Error in createAnonymousFcn>@(x)fcn(x,FcnArgs{:}) (line 11)
fcn_handle = @(x) fcn(x,FcnArgs{:});
Error in makeState (line 47)
firstMemberScore = FitnessFcn(state.Population(initScoreProvided+1,:));
Error in gaunc (line 40)
state = makeState(GenomeLength,FitnessFcn,Iterate,output.problemtype,options);
Error in ga (line 398)
[x,fval,exitFlag,output,population,scores] = gaunc(FitnessFcn,nvars, ...
Error in myfun (line 11)
[x] = ga(@(f)myfcn(f(1),f(2),f(3)),3)
Caused by:
Failure in initial user-supplied fitness function evaluation. GA cannot continue.
>> myfun
Error using myfun>myfcn
Too many input arguments.
Error in myfun>@(f)myfcn(f(1),f(2),f(3))
Error in createAnonymousFcn>@(x)fcn(x,FcnArgs{:}) (line 11)
fcn_handle = @(x) fcn(x,FcnArgs{:});
Error in makeState (line 47)
firstMemberScore = FitnessFcn(state.Population(initScoreProvided+1,:));
Error in gaunc (line 40)
state = makeState(GenomeLength,FitnessFcn,Iterate,output.problemtype,options);
Error in ga (line 398)
[x,fval,exitFlag,output,population,scores] = gaunc(FitnessFcn,nvars, ...
Error in myfun (line 11)
x = ga(@(f)myfcn(f(1),f(2),f(3)),3)
Caused by:
Failure in initial user-supplied fitness function evaluation. GA cannot continue.
>> myfun
Error using functionHandleOrCell (line 12)
The constraint function must be a function handle.
Error in validate (line 228)
[nonlcon,NonconFcnArgs] = functionHandleOrCell('NonconFcn',nonlcon);
Error in gacommon (line 65)
[options,nvars,FitnessFcn,NonconFcn] = validate(options,type,nvars,fun,nonlcon,user_options);
Error in ga (line 363)
NonconFcn,options,Iterate,type] = gacommon(nvars,fun,Aineq,bineq,Aeq,beq,lb,ub, ...
Error in myfun (line 11)
[x,fval] = ga(@(f)myfcn(f(1),f(2),f(3)),3,[],[],[],[],[],[],100)
>> myfun
Error using functionHandleOrCell (line 12)
The constraint function must be a function handle.
Error in validate (line 228)
[nonlcon,NonconFcnArgs] = functionHandleOrCell('NonconFcn',nonlcon);
Error in gacommon (line 65)
[options,nvars,FitnessFcn,NonconFcn] = validate(options,type,nvars,fun,nonlcon,user_options);
Error in ga (line 363)
NonconFcn,options,Iterate,type] = gacommon(nvars,fun,Aineq,bineq,Aeq,beq,lb,ub, ...
Error in myfun (line 11)
[x,fval,exitFlag,output,population,scores] = ga(@(f)myfcn(f(1),f(2),f(3)),3,[],[],[],[],[],[],100)
>> myfun
Error using preProcessLinearConstr (line 69)
The number of rows in Aeq must be the same as the length of beq.
Error in gacommon (line 97)
[Iterate.x,Aineq,bineq,Aeq,beq,lb,ub,msg,exitFlag] = preProcessLinearConstr( ...
Error in ga (line 363)
NonconFcn,options,Iterate,type] = gacommon(nvars,fun,Aineq,bineq,Aeq,beq,lb,ub, ...
Error in myfun (line 11)
[x,fval,exitFlag,output,population,scores] = ga(@(f)myfcn(f(1),f(2),f(3)),3,[],[],100,[])
>> myfun
Error using preProcessLinearConstr (line 69)
The number of rows in Aeq must be the same as the length of beq.
Error in gacommon (line 97)
[Iterate.x,Aineq,bineq,Aeq,beq,lb,ub,msg,exitFlag] = preProcessLinearConstr( ...
Error in ga (line 363)
NonconFcn,options,Iterate,type] = gacommon(nvars,fun,Aineq,bineq,Aeq,beq,lb,ub, ...
Error in myfun (line 11)
[x,fval,exitFlag,output,population,scores] = ga(@(f)myfcn(f(1),f(2),f(3)),3,[],[],100,[])
>> myfun
Error using preProcessLinearConstr (line 69)
The number of rows in Aeq must be the same as the length of beq.
Error in gacommon (line 97)
[Iterate.x,Aineq,bineq,Aeq,beq,lb,ub,msg,exitFlag] = preProcessLinearConstr( ...
Error in ga (line 363)
NonconFcn,options,Iterate,type] = gacommon(nvars,fun,Aineq,bineq,Aeq,beq,lb,ub, ...
Error in myfun (line 11)
[x,fval,exitFlag,output,population,scores] = ga(@(f)myfcn(f(1),f(2),f(3)),3,[],[],100,[])
>> myfun
Error using preProcessLinearConstr (line 69)
The number of rows in Aeq must be the same as the length of beq.
Error in gacommon (line 97)
[Iterate.x,Aineq,bineq,Aeq,beq,lb,ub,msg,exitFlag] = preProcessLinearConstr( ...
Error in ga (line 363)
NonconFcn,options,Iterate,type] = gacommon(nvars,fun,Aineq,bineq,Aeq,beq,lb,ub, ...
Error in myfun (line 11)
[x,fval,exitFlag,output,population,scores] = ga(@(f)myfcn(f(1),f(2),f(3)),3,[],[],100,[])
>> myfun
Error using myfun>myfcn
Too many input arguments.
Error in myfun>@(f)myfcn(f(1),f(2),f(3))
Error in createAnonymousFcn>@(x)fcn(x,FcnArgs{:}) (line 11)
fcn_handle = @(x) fcn(x,FcnArgs{:});
Error in makeState (line 47)
firstMemberScore = FitnessFcn(state.Population(initScoreProvided+1,:));
Error in gaunc (line 40)
state = makeState(GenomeLength,FitnessFcn,Iterate,output.problemtype,options);
Error in ga (line 398)
[x,fval,exitFlag,output,population,scores] = gaunc(FitnessFcn,nvars, ...
Error in myfun (line 8)
[x,fval] = ga(@(f)myfcn(f(1),f(2),f(3)),3)
Caused by:
Failure in initial user-supplied fitness function evaluation. GA cannot continue.
>> myfun
Error using myfun/myfcn
Too many input arguments.
Error in myfun>@(f)myfcn(f(1),f(2),f(3)) (line 8)
[~,~] = ga(@(f)myfcn(f(1),f(2),f(3)),3)
Error in createAnonymousFcn>@(x)fcn(x,FcnArgs{:}) (line 11)
fcn_handle = @(x) fcn(x,FcnArgs{:});
Error in makeState (line 47)
firstMemberScore = FitnessFcn(state.Population(initScoreProvided+1,:));
Error in gaunc (line 40)
state = makeState(GenomeLength,FitnessFcn,Iterate,output.problemtype,options);
Error in ga (line 398)
[x,fval,exitFlag,output,population,scores] = gaunc(FitnessFcn,nvars, ...
Error in myfun (line 8)
[~,~] = ga(@(f)myfcn(f(1),f(2),f(3)),3)
Caused by:
Failure in initial user-supplied fitness function evaluation. GA cannot continue.
>> myfun
Error using myfun/myfcn
Too many input arguments.
Error in myfun>@(f)myfcn(f(1),f(2),f(3)) (line 8)
[x,fval] = ga(@(f)myfcn(f(1),f(2),f(3)),3)
Error in createAnonymousFcn>@(x)fcn(x,FcnArgs{:}) (line 11)
fcn_handle = @(x) fcn(x,FcnArgs{:});
Error in makeState (line 47)
firstMemberScore = FitnessFcn(state.Population(initScoreProvided+1,:));
Error in gaunc (line 40)
state = makeState(GenomeLength,FitnessFcn,Iterate,output.problemtype,options);
Error in ga (line 398)
[x,fval,exitFlag,output,population,scores] = gaunc(FitnessFcn,nvars, ...
Error in myfun (line 8)
[x,fval] = ga(@(f)myfcn(f(1),f(2),f(3)),3)
Caused by:
Failure in initial user-supplied fitness function evaluation. GA cannot continue.
>> myfun
Not enough input arguments.
Error in myfun/myfcn (line 12)
t=(k1*f(1)+k2*f(2)+k3*f(3));
Error in myfun>@(f)myfcn (line 8)
[x,fval] = ga(@(f)myfcn ,3)
Error in createAnonymousFcn>@(x)fcn(x,FcnArgs{:}) (line 11)
fcn_handle = @(x) fcn(x,FcnArgs{:});
Error in makeState (line 47)
firstMemberScore = FitnessFcn(state.Population(initScoreProvided+1,:));
Error in gaunc (line 40)
state = makeState(GenomeLength,FitnessFcn,Iterate,output.problemtype,options);
Error in ga (line 398)
[x,fval,exitFlag,output,population,scores] = gaunc(FitnessFcn,nvars, ...
Error in myfun (line 8)
[x,fval] = ga(@(f)myfcn ,3)
Caused by:
Failure in initial user-supplied fitness function evaluation. GA cannot continue.
>> myfun
Not enough input arguments.
Error in myfun/myfcn (line 12)
t=(k1*f(1)+k2*f(2)+k3*f(3));
Error in myfun>@(f)myfcn (line 8)
[x,fval] = ga(@(f)myfcn ,3)
Error in createAnonymousFcn>@(x)fcn(x,FcnArgs{:}) (line 11)
fcn_handle = @(x) fcn(x,FcnArgs{:});
Error in makeState (line 47)
firstMemberScore = FitnessFcn(state.Population(initScoreProvided+1,:));
Error in gaunc (line 40)
state = makeState(GenomeLength,FitnessFcn,Iterate,output.problemtype,options);
Error in ga (line 398)
[x,fval,exitFlag,output,population,scores] = gaunc(FitnessFcn,nvars, ...
Error in myfun (line 8)
[x,fval] = ga(@(f)myfcn ,3)
Caused by:
Failure in initial user-supplied fitness function evaluation. GA cannot continue.
>> myfun
Not enough input arguments.
Error in myfun/myfcn (line 12)
t=(k1*f(1)+k2*f(2)+k3*f(3));
Error in myfun>@(f)myfcn (line 8)
[x,fval] = ga(@(f)myfcn ,3)
Error in createAnonymousFcn>@(x)fcn(x,FcnArgs{:}) (line 11)
fcn_handle = @(x) fcn(x,FcnArgs{:});
Error in makeState (line 47)
firstMemberScore = FitnessFcn(state.Population(initScoreProvided+1,:));
Error in gaunc (line 40)
state = makeState(GenomeLength,FitnessFcn,Iterate,output.problemtype,options);
Error in ga (line 398)
[x,fval,exitFlag,output,population,scores] = gaunc(FitnessFcn,nvars, ...
Error in myfun (line 8)
[x,fval] = ga(@(f)myfcn ,3)
Caused by:
Failure in initial user-supplied fitness function evaluation. GA cannot continue.
>> myfun
Not enough input arguments.
Error in myfun/myfcn (line 12)
t=(k1*f(1)+k2*f(2)+k3*f(3));
Error in myfun>@(f)myfcn (line 16)
[x,fval] = ga(@(f)myfcn ,3)
Error in createAnonymousFcn>@(x)fcn(x,FcnArgs{:}) (line 11)
fcn_handle = @(x) fcn(x,FcnArgs{:});
Error in makeState (line 47)
firstMemberScore = FitnessFcn(state.Population(initScoreProvided+1,:));
Error in gaunc (line 40)
state = makeState(GenomeLength,FitnessFcn,Iterate,output.problemtype,options);
Error in ga (line 398)
[x,fval,exitFlag,output,population,scores] = gaunc(FitnessFcn,nvars, ...
Error in myfun (line 16)
[x,fval] = ga(@(f)myfcn ,3)
Caused by:
Failure in initial user-supplied fitness function evaluation. GA cannot continue.
>> myfun
Not enough input arguments.
Error in myfun/myfcn (line 12)
t=(k1*f(1)+k2*f(2)+k3*f(3));
Error in myfun>@(f)myfcn (line 8)
[x,fval] = ga(@(f)myfcn ,3)
Error in createAnonymousFcn>@(x)fcn(x,FcnArgs{:}) (line 11)
fcn_handle = @(x) fcn(x,FcnArgs{:});
Error in makeState (line 47)
firstMemberScore = FitnessFcn(state.Population(initScoreProvided+1,:));
Error in gaunc (line 40)
state = makeState(GenomeLength,FitnessFcn,Iterate,output.problemtype,options);
Error in ga (line 398)
[x,fval,exitFlag,output,population,scores] = gaunc(FitnessFcn,nvars, ...
Error in myfun (line 8)
[x,fval] = ga(@(f)myfcn ,3)
Caused by:
Failure in initial user-supplied fitness function evaluation. GA cannot continue.
>> myfun
Not enough input arguments.
Error in myfun/myfcn (line 12)
t=(k1*f(1)+k2*f(2)+k3*f(3));
Error in myfun>@(f)myfcn (line 8)
x = ga(@(f)myfcn ,3)
Error in createAnonymousFcn>@(x)fcn(x,FcnArgs{:}) (line 11)
fcn_handle = @(x) fcn(x,FcnArgs{:});
Error in makeState (line 47)
firstMemberScore = FitnessFcn(state.Population(initScoreProvided+1,:));
Error in gaunc (line 40)
state = makeState(GenomeLength,FitnessFcn,Iterate,output.problemtype,options);
Error in ga (line 398)
[x,fval,exitFlag,output,population,scores] = gaunc(FitnessFcn,nvars, ...
Error in myfun (line 8)
x = ga(@(f)myfcn ,3)
Caused by:
Failure in initial user-supplied fitness function evaluation. GA cannot continue.
>>
Geoff Hayes
on 25 Mar 2020
amir - replace this line
[x,fval] = ga(@(f)myfcn ,3)
with just
[x,fval] = ga(@myfcn ,3)
You do not need to specify the imput parameter.
Amir Hosseini
on 25 Mar 2020
wow thanks man it worked but I didn't get the desirable results of my article. However, youre answer was just accepted but do you think the wrong result may have caused by wrong f because we didn't assign the f for GA
Geoff Hayes
on 25 Mar 2020
there could be any number of reasons why you didn't get the desirable results. Are you comparing against the results from a published article? Did they use a genetic algorithm? Do you know which crossover methods they used or mutation?
Amir Hosseini
on 25 Mar 2020
yes I am comparing with a published article and they used GA but they didn't mention the type of mutations and crossovers.how should I find out?
and in this code which kind of mutation and crossover we are using?
Geoff Hayes
on 25 Mar 2020
Edited: Geoff Hayes
on 25 Mar 2020
You could contact the author to find out what are the parameters they used for their GA. In the MATLAB code, you would be using the default crossover and mutation operators (you might find out which from Find minimum of function using genetic algorithm).
Amir Hosseini
on 25 Mar 2020
thank you very much indeed dear Geoff that was really nice of you!
will you please send me an empty email to amirhsn51@gmail.com please?
Geoff Hayes
on 25 Mar 2020
amir - unfortunately, I only correspond with MATLAB users via this forum.
Amir Hosseini
on 25 Mar 2020
yes thank you that is understandable. I wanted to make an offer of working together and expand my article to publish it soon.
I would really appreciate it if you let me know in case of your willingness by personal email.
yours truly!
More Answers (0)
See Also
Categories
Find more on Genetic Algorithm 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!An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)