How to save data from Genetic Algorithm in case MATLAB crashes?
显示 更早的评论
Hi there,
I'm using the GA to find input parameters for my model. My fitness function is computationally expensive and takes about 2 days. In situation if MATLAB unexpectedly crashes, is there a way to save the data?
I have used the 'OutputFcn' option however this seems to save the data once my MATLAB finish the simulation. I purposely stop MATLAB and re-open it but can't seem to access the saved data.
If anyone have any useful tips to save simulation data while the simulation is running so I can access in case it crashes, I would be very grateful.
Many thanks,
How
10 个评论
"My fitness function is computationally expensive and takes about 2 days"
Do you mean that your fitness function takes two day to calculate, or the GA does?
"In situation if MATLAB unexpectedly crashes, is there a way to save the data?"
Once it has crashed, not really... but you could regularly save the GA population (e.g. using OutputFcn), and then start the GA again using the latest population to set the InitialPopulationMatrix.
Syahmeer How
2019-12-16
编辑:Syahmeer How
2019-12-16
Define the outputFcn following the documentation:
Pay careful attention to the flag values, the input arguments, the output arguments. You would need to save the data on every 'init' call, or if that slows down your code too much, then use persistent and a simple counter to save the data every N calls (for an N of your choice).
Syahmeer How
2019-12-16
Star Strider
2019-12-16
It would likely be worth experimenting to see of OutputFcn can include a way of using save to append the Best score for the current generation to a .mat file or text file, likely opening it and closing it each time so that nothing is lost. The documentation states that Best is part of the state structure, so it should be available. The documentation on Custom Output Function for Genetic Algorithm offers guidance.
I have never attempted this with ga (my optimisations rarely take more than a few hours at most), so I have no personal experience with it. (In the past, I wrote my own genetic algorithm codes, and saved the best individual to a text file in each generation.)
@Syahmeer How: I strongly recommend that you use the flag input to initilalize your function, and select when to save the data, and clean up when complete.
When you wrote save I presumes that you actually meant save, as in "export the data to file". If you expect MATLAB to crash or hang, there is little point in your approach of just storing that data in some variable/s. Appending to a .mat file is probably the simplest.
Syahmeer How
2019-12-16
Syahmeer How
2019-12-16
Star Strider
2019-12-16
@Syahmeer How — My pleasure. I have some code that specifically uses ga, so I will experiment with that and will post back here if I have a successful result.
Syahmeer How
2019-12-16
采纳的回答
更多回答(0 个)
类别
在 帮助中心 和 File Exchange 中查找有关 Genetic Algorithm 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!