Generate GA Plot after the algorithm has finnished

Hey Guys,
i have a very complex function to optimize with GA, its already need about 5 hours without a plot activated, so i want to generate a plot with the results after the algorithm is finished. So far, i always saved the command window information with "diary", so the savings kinda looks like this and repeat :
....
x: 30.80606 105.0739 5 0.8621733 0.3187652 0.1 0.1 0.3426245 3.578397 obj: 3.2365
x: 96.6515 60.2254 91.6727 5.78384 5.89348 5.14982 6.43606 2.48569 4.53104 obj: 1.78
x: 79.81447 78.14723 129.7414 1.610127 8.481399 6.818627 6.844434 4.471865 3.497146 obj: 2.0136
x: 73.6651 98.8845 83.1789 1.94892 0.758555 3.55138 8.41613 3.0194 8.43609 obj: 3.0492
x: 64.6236 65.0341 94.9168 3.92559 1.34072 0.766707 0.114988 3.19566 7.77677 obj: 3.0613
x: 136.7348 21.8462 117.6277 7.317458 9.027388 0.8396103 2.702008 0.551372 7.5124 obj: 1.8702
x: 131.5978 44.16512 132.7881 2.824445 9.458292 5.58881 3.89878 8.731667 7.22164 obj: 2.3449
x: 33.4331 144.1815 21.22591 9.646141 7.317277 6.503243 1.924164 1.892613 1.564883 obj: 1.1898
x: 78.5224 79.2316 97.9015 1.87417 0.963924 0.498753 8.43496 8.31711 0.845001 obj: 3.0582
Warning: Reference to non-existent field 'xx'.
> In Simulink/SimulationOutput/get
In Simulink/SimulationOutput/subsref
In Simulink/SimulationOutput/numArgumentsFromSubscript
.....
so far my main code looks like this :
options = optimoptions('ga','Display','iter','OutputFcn', @myoutputFunc,'UseParallel', true,'ConstraintTolerance',1e-5,'PopulationSize',1000,'TimeLimit',60*60*60);
[xopt,fval,exitflag,output,population,scores] = ga(@objective,9,[],[],[],[],lb,ub,@constraint,options);
Is it possible to generate a plot out of these information or do i need to change something ?
Thank you

 Risposta accettata

I am not certain what you want to do.
If you want to save the interim results to plot later (or to save them in the event that MATLAB crashes for some reason), see: How to save data from Genetic Algorithm in case MATLAB crashes? It offers a way to save the ‘fittest’ individual to a .mat file at each iteration.
.

6 Commenti

Ah ok then i try to be more specific:
I know that u can plot the GA function after each generation, but this slows down the algorithm speed. So i want the GA to run without generating a plot. Maybe a fews days later, when i write everything down, i want to generate a plot out of the results without having to run the whole GA again. I just want to rely on the results i already have. So im asking how i should build up my code so then the post-production is quite easy for me.
Hope this makes it more understandable
I also looked at the link u provided. So for example if i have 500 iteration, your code generate a matrix 500x1 Matrix with the 500 best values of each generation?
Thank for your help
My pleasure!
So for example if i have 500 iteration, your code generate a matrix 500x1 Matrix with the 500 best values of each generation?
No. My code save the best individual from each generation, so if you have 500 generations, it would save the one best individual for each generation, for a total of 500 vectors. If an individual were defined as having ‘N’ parameters, the matrix would be (500xN). The original intent is that this would become the new 'InitialPopulationMatrix' if something were to go wrong. In the context of your Question, it is simply the record of the best individual from each generation.
Perfect, then i only have on last question: is the matrix only saving the parameters of the function or also the function value? Thats what i would need. Then i could just plot the value column afterwards
So Star Strider, thanks again for u help u saved me a great ammount of time :) Have a nice Weekend
My pleasure!
My code saves only the parameters. However it would be straightforward to calculate the function value from them after ga has finished.
Alternatively, you could change my code to include the function value, however that would require a separate function call within my code, likely slowing the ga function further. It would also be more difficult to use the matrix as the 'InitialPopulationMatrix' later if that was necessary, since that would require first removing the saved function values from the matrix.
It would be easier and more efficient to use a for loop calculate the function values later from the saved parameter values.
Hey Star Strider,
sorry to bother again but i still have problems implementing your code: Where do i have to implement/call your SaveOut function to make it work in the GA
Thanks
No worries!
It is an output function (formally: 'OutputFcn'), so it needs to be included in the optimoptions ‘opts’ structure. (I could not find the code I originally used to test it — unusual for me — so I had to reconstruct some example code that I am attaching here. I apologise for the resulting delay.)
The optimoptions call to include it is:
opts = optimoptions('ga', 'PopulationSize',PopSz, 'InitialPopulationMatrix',randi(1E+4,PopSz,Parms)*1E-3, 'MaxGenerations',2E3, 'PlotFcn',@gaplotbestf, 'PlotInterval',1, 'OutputFcn',@SaveOut);
↑ ← HERE
The attached code includes the ‘SaveOut’ function and all the necessary coding infrastructure required to use it. (I tested this code to be certain that it works and does what I intend it to do.)

Accedi per commentare.

Più risposte (0)

Community Treasure Hunt

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

Start Hunting!

Translated by