Subscripted assignment dimension mismatch in Genetic Algorithm

6 views (last 30 days)
I'm getting the following error when trying to use the GA optimizer:
Subscripted assignment dimension mismatch.
Error in C:\Program
Files\MATLAB\R2012b\toolbox\globaloptim\globaloptim\private\gaminlppenaltyfcn.p>i_convectorizer
(line 135)
Error in C:\Program
Files\MATLAB\R2012b\toolbox\globaloptim\globaloptim\private\gaminlppenaltyfcn.p>gaminlppenaltyfcn
(line 57)
Error in C:\Program
Files\MATLAB\R2012b\toolbox\globaloptim\globaloptim\private\gaminlpengine.p>@(x)gaminlppenaltyfcn(x,problem,conScale)
(line 73)
Error in makeState (line 65)
Score = FitnessFcn(state.Population(initScoreProvided+1:end,:));
Error in galincon (line 18)
state = makeState(GenomeLength,FitnessFcn,Iterate,output.problemtype,options);
Error in C:\Program
Files\MATLAB\R2012b\toolbox\globaloptim\globaloptim\private\gaminlpengine.p>gaminlpengine
(line 37)
Error in C:\Program
Files\MATLAB\R2012b\toolbox\globaloptim\globaloptim\private\gaminlp.p>gaminlp (line 37)
Error in ga (line 342)
[x,fval,exitFlag,output,population,scores] = gaminlp(FitnessFcn,nvars, ...
Error in denemeC3PO (line 12)
[x, fval] = ga(@minusProfit, nVars,[],[],[],[], LB, UB, @constr, IntCon);
Caused by:
Failure in initial user-supplied fitness function evaluation. GA cannot continue.
Since at the end of the error message it says the problem is with the fitness function, I've tested it by itself, without using it in GA, and it works without any error...
I can also share my code for fitness and constraint functions if that would help.

Answers (2)

Jan
Jan on 4 May 2013
To be exact, the error message tells you:
Failure in initial user-supplied fitness function evaluation.
This does not mean, that there is a bug in the fitness function itself. You can test this using the debugger by setting a breakpoint in the fitness function.
  1 Comment
Sukru
Sukru on 5 May 2013
Thanks. I did that, but found nothing wrong with the fitness function. On the other hand, the optimization worked when I commented out lines between 15-41 of my constrint function here: https://gist.github.com/shasdemir/18eaeda95833b6618113
It is weird, because the constraint function works when I call it by itself outside of ga.
I'm puzzled...

Sign in to comment.


Alessandro
Alessandro on 14 Jul 2014
Hi Sukru,
I'm experiencing the same issue as you had and I'm really stuck 'cause, as you said, both fitness function and constraint function work well. Actually, I'm running the genetic algorithm several times inside a loop to test different stuffs and this problem comes quite randomly but never at the first time.
Did you solve your problem without commenting the constraint function? (by the way, the link is no more available, so I don't know how did you figure out the problem with the constraint function)
Thank you for your help.
Alessandro
  3 Comments
Abdelmoumene ARAFI
Abdelmoumene ARAFI on 18 Feb 2021
I have the same problem here, the fitness function and the constraints function work well, whene i run them outside ga (optimtool). I didn't figure out your solution could you please explaine to me more.
Thank you
Walter Roberson
Walter Roberson on 18 Feb 2021
Alessandro had a nonlinear constraints function that looked something like
c(1:5) = x(1:5).^2 - pi %x^2<=pi
if x(6) > x(7)/2
c(6:7) = sin(x(6:7)*5*pi) - 0.3;
else
c(6) = 0;
end
Notice that in some of the cases the computed c is length 6 and in other cases it is length 7. For nonlinear constraints to work, you always need to return the same length.
Also, it is best not to re-use slots for different purposes, so nothing like
if x(6) > x(7)/2
c(1:2) = sin(x(6:7)*5*pi) - 0.3;
c(3:5) = 0;
else
c(1:5) = x(1:5).^2 - pi %x^2<=pi
end
Where the number of slots is consistent, but they mean different things at different times. If you do that, then MATLAB will not be able to figure properly which direction to go to satisfy constraints near the boundaries.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!