Matlab Fitness Function Genetic Algorithm Error

7 次查看(过去 30 天)
Hey everyone, I am trying to use genetic algorithm in matlab to perform SMM. However, I am having difficulties with passing extra parameters through the ga function. I have successfully used this function (and successfully passed the parameters through) with lsqnonlin, so I suspect there is some formatting error I am not understanding.
Here is where I call the function using ga:
xL=[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,0,0,0,0,0,0,1.5,1.5];
xU=[.5,.5,.6,.6,.95,10,5, 5, .15, .15,.95,1,1,.95,.95,2,2,10,7,7];
FitFunc=@(x) lsqnonlindiscretecalibrate_V2_AR(x,A1,A2,r,discount,fracc,matchfrac1,matchfrac2,wageprem,N,targetu1,targetu2,sepfrac,thetatarget,...
percent1,percent2,moverate_target,jfptarget,jsptarget,lstarget,avgutarget,replacetarget,etarget,duration_target1,duration_target2,...
fsize,emplyfrac_target,lag1target,lag2target,ssq1target,ssq2target,durtarget,sims,burn,yfrac_target);
options2=optimoptions(@ga,'Display','Iter','FunctionTolerance',1e-30,'MaxGenerations',3,'UseParallel',true);
[vars_parameters,error_param]=ga(FitFunc,20,[],[],[],[],[],xL,xU,options2);
Here is the beginning of the function file itself:
function SSRc=lsqnonlindiscretecalibrate_V2_AR(x,A1,A2,r,discount,fracc,matchfrac1,matchfrac2,wageprem,N,targetu1,targetu2,sepfrac,thetatarget,percent1,percent2,moverate_target,jfptarget,jsptarget,lstarget,avgutarget,replacetarget,etarget,duration_target1,duration_target2,fsize,emplyfrac_target,lag1target,lag2target,ssq1target,ssq2target,durtarget,sims,burn,yfrac_target)
z1=x(1);
z2=x(2);
rho1=x(3);
rho2=x(4);
beta=x(5);
k1=x(6);
mu1=x(7);
mu2=x(8);
deltac=x(9);
deltad=x(10);
ps=x(11);
alpha1=x(12);
alpha2=x(13);
Lp1=x(14);
Lp2=x(15);
psig1=x(16);
psig2=x(17);
k2=x(18);
p1bar=x(19);
p2bar=x(20);
....Some stuff... end
And here is the error I get:
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 discrete_calibration_V2_AR (line 92) [vars_parameters,error_param]=ga(FitFunc,20,[],[],[],[],[],xL,xU,options2);
Apologies for the lengthy names, I need to keep everything straight. I understand that I am inputting something in a way Matlab doesn't like, but I cannot figure out what it should be. I have read the documentation for passing parameters in ga, and I think this is correct. Any help would be greatly appreciated, I assume it's something simple I do not understand/missed!

采纳的回答

Alan Weiss
Alan Weiss 2017-8-16
Your ga call is not correct:
ga(FitFunc,20,[],[],[],[],[],xL,xU,options2);
It should be
ga(FitFunc,20,[],[],[],[],xL,xU,[],options2);
Sometimes it is easier to keep track of things by adding empty variables:
A = [];
b = [];
Aeq = [];
beq = [];
nonlcon = [];
[vars_parameters,error_param]=ga(FitFunc,20,A,b,Aeq,beq,xL,xU,nonlcon,options2)
Alan Weiss
MATLAB mathematical toolbox documentation
  2 个评论
Joseph Stalin
Joseph Stalin 2021-9-15
Hey I was trying to do something similar except I am using a function handle which takes in an input vector x with all elements being binary(0 or 1). It is a quadratic mixed integer programmig problem however I cannot get it to work with the ga solver. Here is my function call:
fun = @(x) (x'*Co*x)-S'*x;
intcon = [];
for i=1:n
intcon = [intcon i];
end
A = []; b = []; Aeq = []; beq = []; nonlcon = [];
lb = zeros(n,1);
ub = ones(n,1);
options = optimoptions('ga','PlotFcn',@gaplotbestf);
x = ga(fun,nres,A,b,Aeq,beq,lb,ub,nonlcon,intcon,options);
Here the input x is a nx1 vector, Co is nxn matrix and S is a nx1 vector. The output of the function fun is a scalar. Coud anyone point to anything I might have missed? is there any special syntax for vector arguments?

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Genetic Algorithm 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by