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.
3 次查看(过去 30 天)
显示 更早的评论
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 个评论
采纳的回答
Geoff Hayes
2020-3-25
编辑:Geoff Hayes
2020-3-25
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 个评论
Amir Hosseini
2020-3-25
thanks for youre attention Geoff!
actually I did it but still getting the same error. what should I do?
Amir Hosseini
2020-3-25
编辑:Geoff Hayes
2020-3-25
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
2020-3-25
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
2020-3-25
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
2020-3-25
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
2020-3-25
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
2020-3-25
编辑:Geoff Hayes
2020-3-25
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
2020-3-25
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?
Amir Hosseini
2020-3-25
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!
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Genetic Algorithm 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!发生错误
由于页面发生更改,无法完成操作。请重新加载页面以查看其更新后的状态。
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 MathWorks 国家/地区网站并未针对您所在位置的访问进行优化。
美洲
- América Latina (Español)
- Canada (English)
- United States (English)
欧洲
- 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)
亚太
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)