Error with function handle while running Genetic Algorithm code
9 次查看(过去 30 天)
显示 更早的评论
Hello! I wrote the following code in Matlab trying to solve a problem using Genetic Algorithms. When it runs it gives me these errors:
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 336)
NonconFcn,options,Iterate,type] = gacommon(nvars,fun,Aineq,bineq,Aeq,beq,lb,ub, ...
Error in eqga (line 23)
[x,fval,reason,output] = ga(FitnessFcn,numberOfVariables,[],[],[],[],lb,ub,options);
clc;
type create_population.m
type crossover.m
type mutate.m
type fitness.m
type plot_it.m
lb=0;
ub=2;
FitnessFcn = @(x) eq_fitness(x);
my_plot = @(options,state,flag) plot_it (options,state,flag,x);
options = optimoptions('ga','PopulationType', 'custom', ...
'InitialPopulationRange', [0;2]);
options = optimoptions('ga', options, ...
'CreationFcn', @create_population, ...
'CrossoverFcn', @crossover, ...
'MutationFcn', @emutate, ...
'PlotFcn', my_plot, ...
'Generations',2000,'PopulationSize', 100, ...
'StallGenLimit',400, 'Vectorized', 'on');
numberOfVariables=1;
[x,fval,reason,output] = ga(FitnessFcn,numberOfVariables,[],[],[],[],lb,ub,options);
But when I take out the options at [x,fval,reason,output] it works fine and it gives me the result I want.
I don't know what's wrong. Is there any mistake in this code? Or there might be mistake in the functions for crossover, mutataion etc?
2 个评论
回答(1 个)
Steven Lord
2021-4-30
According to the documentation for the ga function you could be trying to call one of these syntaxes (ignoring the number of ourput arguments for now):
x = ga(fun,nvars,A,b,Aeq,beq,lb,ub,nonlcon)
x = ga(fun,nvars,A,b,Aeq,beq,lb,ub,nonlcon,options)
x = ga(fun,nvars,A,b,[],[],lb,ub,nonlcon,IntCon)
x = ga(fun,nvars,A,b,[],[],lb,ub,nonlcon,IntCon,options)
Your call was (again ignoring outputs):
ga(FitnessFcn,numberOfVariables,[],[],[],[],lb,ub,options);
So A is [], b is [], Aeq is [], beq is [], lb is lb and ub is up. Now in all these syntaxes the next input is the nonlinear constraint function nonlcon. But options is not a nonlinear constraint function.
To solve the problem:
If you want to use the second of those syntaxes I copied from the doc, put a [] between ub and options.
If you want to use the fourth, put [] and an IntCon vector.
3 个评论
Steven Lord
2021-4-30
That doesn't look like the whole error message. I expect there were some lines displayed in red and/or orange before the "Error in fitness (line 4)" line that you copied and pasted. Those lines will be useful in determining what's going on.
I suspect I know the problem but those additional lines would confirm or disprove my suspicion.
另请参阅
类别
在 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!