Error with function handle while running Genetic Algorithm code

조회 수: 19 (최근 30일)
Nick
Nick 2021년 4월 30일
댓글: Nick 2021년 5월 1일
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
Jan
Jan 2021년 4월 30일
What is x here:
my_plot = @(options,state,flag) plot_it (options,state,flag,x);
Nick
Nick 2021년 4월 30일
x is the solution to the problem

댓글을 달려면 로그인하십시오.

답변 (1개)

Steven Lord
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
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.
Nick
Nick 2021년 5월 1일
That's true! I thought the orange ones were not so important. Here is the whole error message: What do you think is the problem?
Warning: All constraints are ignored for 'custom' population.
> In gacommon (line 42)
In ga (line 336)
In eqga (line 28)
Undefined operator '^' for input arguments of type 'cell'.
Error in fitness (line 4)
scores=abs(x^2+sqrt(x^2+1)-2);
Error in eqga>@(x)fitness(x)
Error in createAnonymousFcn>@(x)fcn(x,FcnArgs{:}) (line 11)
fcn_handle = @(x) fcn(x,FcnArgs{:});
Error in makeState (line 64)
Score = FitnessFcn(state.Population(initScoreProvided+1:end,:));
Error in gaunc (line 40)
state = makeState(GenomeLength,FitnessFcn,Iterate,output.problemtype,options);
Error in ga (line 371)
[x,fval,exitFlag,output,population,scores] = gaunc(FitnessFcn,nvars, ...
Error in eqga (line 28)
[x,fval,reason,output]= ga(FitnessFcn,numberOfVariables,A,b,Aeq,beq,lb,ub,nonlcon,options)

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Genetic Algorithm에 대해 자세히 알아보기

제품


릴리스

R2017b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by