MATLAB Answers

Genetic Algorithm in Matlab - Not terminating as expected

7 views (last 30 days)
Huck Febbo
Huck Febbo on 30 Apr 2015
Answered: Alan Weiss on 6 May 2015
I am trying to solve a problem using GA in Matlab and the optimization is running through the set number of generations and decreasing the function value as expected ( seen in figure below). But what is not expected is that it is not plotting the percent criteria of the generation me, for instance in the figure below It should be just under 40% done, but it is not.
Here is my code:
IntCon=[]; % Set integer variables
vfun=@(x)objective(x, univ_names, univ_data, weight_names, weight_data, net_names, net_data, normalized_name, normalized_data);
nonlcon=@(x)constraint(x,net_names, net_data);
pop = 40;
gen =50;
time = inf; % time in (s)
options = gaoptimset('TimeLimit', time, 'InitialPopulation',ini,'PopulationSize',pop,'Generations',gen,'PlotFcns',{@gaplotbestfun, @gaplotstopping});
[x,fval,exitflag,output] = ga(vfun,nvars,[],[],[],[],x_L,x_U,nonlcon,IntCon,options)
Then, if I let it continue, it starts over at the beginning ( at a worse function value) and at this point it starts to think that it it has met some percentage of the termination criteria for the number of generations
Then after letting it complete, I get:
Optimization terminated: average change in the fitness value less than options.TolFun and constraint violation is less than options.TolCon.
x =3.3242 1.8450 0.5918 0.6000
fval = 3.5208e+03
exitflag = 1
output =
problemtype: 'nonlinearconstr'
rngstate: [1x1 struct]
generations: 3
funccount: 6160
message: [1x140 char]
maxconstraint: 0
So, somehow it picked a worse function value than it found in the first run! Why is it doing this "second" and "third run"?
In another example, I include the iteration information and it is obvious that it is not including the best function value from one generation to the next. For instance on the graph it shown that just after 50 generations it reduces the function value to almost 425 as seen in the bottom set of triangles on the graph below:
But then it think that the min is 435.011? And these 50 generations are only one. Seen below: Generation f-count f(x) constraint Generations 1 1060 435.011 0 0 2 2100 434.396 0 0 3 3140 434.267 0 0
But, the output in the matlab terminal thinks that that after just about 50 generations on graph is 1 generation and it prints to the screen
Then after the optiization is terminated it looks like:
Any ideas? Thanks!

Accepted Answer

Alan Weiss
Alan Weiss on 6 May 2015
ga behaves differently depending on whether there are integer constraints, nonlinear constraints, or neither. If you have no integer constraints, do not include an IntCon argument, just call ga without it:
[x,fval,exitflag,output] = ga(vfun,nvars,[],[],[],[],x_L,x_U,IntCon,options)
Because you seem to have nonlinear constraints, you will not get very many output points in your graph, as you noticed. ga goes through a large number of subproblem iterations before plotting. See this example, where you hardly see anything in the plots.
Alan Weiss
MATLAB mathematical toolbox documentation

More Answers (1)

Huck Febbo
Huck Febbo on 30 Apr 2015
The issue is that the Matlab's implementation of Genetic Algorithm with a non-linear constraint cannot handle the design problem if there are no integer variables! I changed one of may variables to an integer variable as
IntCon=[1]; % Set integer variables
This works now.\ BUt what if none of my variables are integers??!!

Community Treasure Hunt

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

Start Hunting!