Number of variables (NVARS) must be a positive integer.

27 views (last 30 days)
I want to run a project using genetic algorithm but I get this error every time.
I know what is it but I don't know how to fix it.
this is my project that I attached and the main function is GAmodule.
you can start with these numbers :
235
2
1
4
0
0
0
10
0
0
5
0
10
0
0
1000
g
700
Thank you
  1 Comment
dpb
dpb on 23 Aug 2014
Don't know what is in the zip file, but note that '0' is not a positive integer.

Sign in to comment.

Accepted Answer

Star Strider
Star Strider on 23 Aug 2014
Edited: Star Strider on 23 Aug 2014
In this line (Line 163):
[x,fval]=ga(@(x) penalty2(x,available,bc,Dof,E,f,fixedcoord,na,nb,neft,nevt,nl,nn,numbitCoordX,numbitCoordY,numbitCoordZ,numbitEdof,numbitEp,sq,TOPGSM,xspan,yspan,zspan),(nn-nb-nl)*(numbitCoordX+numbitCoordY+numbitCoordZ)+(neft+nevt)*numbitEp+2*nevt*numbitEdof,options);
the ga function is taking this value:
(nn-nb-nl)*(numbitCoordX+numbitCoordY+numbitCoordZ)+(neft+nevt)*numbitEp+2*nevt*numbitEdof
to be ‘nvars’. It has to be an integer >0. I would use the ceil function to round it up to the next integer value, and include a check that it is >0.
For example, put this assignment and if block somewhere before your ga call:
Nvars = ceil((nn-nb-nl)*(numbitCoordX+numbitCoordY+numbitCoordZ)+(neft+nevt)*numbitEp+2*nevt*numbitEdof);
if Nvars <= 0
error('NVARS must be greater than zero.')
end
then when it meets the criteria, use ‘Nvars’ (or whatever you want to call it) in your ga call.
  2 Comments
Hessam
Hessam on 24 Aug 2014
Thanks, It is a good idea.
The problem was all Y-coordinates = 0, therefore yspan=0 and numbitcoordY=-Inf

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!