# How to add graphs to genetic algorithm

9 views (last 30 days)
Gauri on 4 Mar 2024
Commented: Alan Weiss on 11 Mar 2024
Hello
Ive attached my objective function and genetic algorithm file. I used the code from the optim task to make the genetic agorithm code. I need some guidance on how to get graphs that can show how the objective value (total cost) changes over the course of genetic algorithm running.
The graphs that i can get via the optim task do not seem to show that. If they do please tell.
Also what is "penalty value?"
what is meaning of "ga stopped because the average change in the penalty function value is less than options.FunctionTolerance and the constraint violation is less than options.ConstraintTolerance."
Thanks
note:
Copy_2_of_check2 is genetic algorithm file
Copy_2_of_totalCostFunction is main objective function
InventoryUpdate is accessory to the objective function

Alan Weiss on 6 Mar 2024
The gaplotbestf plot is in the third row, second column of the Screenshot 2024-03-04 144951.png file that you attached. This plot shows the best function value is 32520, and did not seem to change much over the 50 generations plotted.
Because some or all of your variables are integer, ga uses a penalty function algorithm. See Integer ga algorithm. This section contains a definition of the penalty function.
Alan Weiss
MATLAB mathematical toolbox documentation
Gauri on 11 Mar 2024
"This plot shows the best function value is 32520, and did not seem to change much over the 50 generations plotted."
Is that ok? does it mean that my coding is wrong? or is it the nature of the problem? How can i explain this to my teacher? Also, are any other plots in the screenshot useful?
Thanks
Alan Weiss on 11 Mar 2024
"Is that ok?" I don't know how to answer this question. It is what happened. Whether or not it is OK is up to you.
"are any other plots in the screenshot useful?" It depends what you are looking for.
"does it mean that my coding is wrong?" I don't know, I didn't read your code. One way to check is to evaluate the fitness function at several points and see if your code gives the right values.
Good luck,
Alan Weiss
MATLAB mathematical toolbox documentation

Walter Roberson on 6 Mar 2024
what is meaning of "ga stopped because the average change in the penalty function value is less than options.FunctionTolerance and the constraint violation is less than options.ConstraintTolerance."
Consider that ga() is applying a "black box function" -- it effectively cannot "peak inside" the objective function you give it in order to analyze the behavior of the function.
As such, there are a few different outcomes possible:
• the objective function returned a -inf . There is no possible "better" return than -inf so ga might as well stop there
• the objective function returned nan. If there is at least one set of parameters inside the population that returned non-nan then you can just drop the failing members from the population; if the entire population is returning nan then ga() has to give up
And otherwise, the objective function returned some valid value, say -17292.35 . But is that the minimum possible return value? You cannot know that it is -- the objective function might be coded as (-17292.35 - (x == 312.14752)*10000000) which returns a much better result if the input happens to be exactly 312.14752 .
So for any finite outputs, ga() does not have a natural stopping point: there is always the possibility that if you ran another hundred billion iterations that you might shave 1e-15 off of the output value.
ga() therefore has some heuristics about when to stop. One of the heuristics is if over all of the iterations that it did for a generation, none of the outputs improved by more than a preset tolerance, and none of the constraints are violated by more than a (different) preset tolerance. In other words, a number of iterations were done but the results didn't improve.
This is a normal outcome of ga(). ga() is never going to say "This result is definitely the minimum!", because it can never know that; it can, however, know that it gave it a number of tries and nothing improved over the tries.
Gauri on 11 Mar 2024

### Categories

Find more on Genetic Algorithm in Help Center and File Exchange

R2023b

### Community Treasure Hunt

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

Start Hunting!