Genetic Algorithm in Matlab - Not terminating as expected
4 views (last 30 days)
Show older comments
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.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/149633/image.png)
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;
ini=rand(pop,nvars);
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
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/149635/image.png)
Then after letting it complete, I get:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/149636/image.png)
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:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/149637/image.png)
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
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/149639/image.png)
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
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/149640/image.png)
Then after the optiization is terminated it looks like:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/149641/image.png)
Any ideas? Thanks!
0 Comments
Accepted Answer
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
0 Comments
More Answers (1)
See Also
Categories
Find more on Genetic Algorithm in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!