Error with passing a fitness function into a GA command
이전 댓글 표시
Hi There,
I created a fitness function in an m file called NSD_Fitness as follows:
*******************************************
function y = NSD_Fitness(X)
global CombinedYieldRank CombinedPPIRank r
y = 1/...
( ( X(1) + X(2) + X(3) ) * ( r * CombinedYieldRank(1,1) + (1-r) * CombinedPPIRank(1,1) ) + ( X(4) + X(5) + X(6) ) * ( r * CombinedYieldRank(1,2) + (1-r) * CombinedPPIRank(1,2) ) + ( X(7) + X(8) + X(9) ) * ( r * CombinedYieldRank(1,3) + (1-r) * CombinedPPIRank(1,3) ) + ( X(10) + X(11) + X(12) ) * ( r * CombinedYieldRank(1,4) + (1-r) * CombinedPPIRank(1,4) ) + ...
...
( X(85) + X(86) + X(87) ) * ( r * CombinedYieldRank(8,1) + (1-r) * CombinedPPIRank(8,1) ) + ( X(88) + X(89) + X(90) ) * ( r * CombinedYieldRank(8,2) + (1-r) * CombinedPPIRank(8,2) ) + ( X(91) + X(92) + X(93) ) * ( r * CombinedYieldRank(8,3) + (1-r) * CombinedPPIRank(8,3) ) + ( X(94) + X(95) + X(96) ) * ( r * CombinedYieldRank(8,4) + (1-r) * CombinedPPIRank(8,4) ) );
end
*******************************************
In another m file called NSD_Main, I used below statement to create a function handle of NSD_Fitness and passed it into a GA command like below:
*******************************************
...
FitFcn = @NSD_Fitness;
options = optimoptions( 'ga','ConstraintTolerance',1e-3,'CreationFcn',[],'MaxStallGenerations',500,...
'FunctionTolerance',0,'PopulationSize',200,...
'CrossoverFraction',0.9,...
'PlotFcn',{@gaplotbestf,@gaplotstopping});
[x,Fval,exitFlag,Output] = ga(FitFcn,nvars,A,b,Aeq,beq,lb,ub,nonlcon,IntCon,options);
*******************************************
Interestingly, I was able to see the GA plots but received the error message below after:
*******************************************
Undefined function 'NSD_Fitness' for input arguments of type 'double'.
Error in createAnonymousFcn>@(x)fcn(x,FcnArgs{:}) (line 11)
fcn_handle = @(x) fcn(x,FcnArgs{:});
Error in gapenalty
Error in gapenalty
Error in gaminlp
Error in ga (line 394)
[x,fval,exitFlag,output,population,scores] = gaminlp(FitnessFcn,nvars, ...
Error in NSD_Main (line 169)
[x,Fval,exitFlag,Output] = ga(FitFcn,nvars,A,b,Aeq,beq,lb,ub,nonlcon,IntCon,options);
*******************************************
All the other parts of my code have been checked correct except for the function handle. What's wrong and how should I code it? Thank you very much.
댓글 수: 2
Geoff Hayes
2020년 9월 15일
Ling - in your second call to ga
ga(@(x)NSD_Fitness(x,{CombinedYieldRank,CombinedPPIRank,r})
what are you trying to do with the above?
Ling Kev
2020년 9월 15일
채택된 답변
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Data Type Conversion에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!