How to save data from Genetic Algorithm in case MATLAB crashes?

20 ビュー (過去 30 日間)
Syahmeer How
Syahmeer How 2019 年 12 月 16 日
コメント済み: Star Strider 2022 年 11 月 30 日
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 件のコメント
Star Strider
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
Syahmeer How 2019 年 12 月 16 日
@starstrider Thank you so much :) I look forward to hearing back from you

サインインしてコメントする。

採用された回答

Star Strider
Star Strider 2019 年 12 月 17 日
It took a bit of trial and error, however this appears to work:
function [state,options,changed] = SaveOut(options,state,flag)
file_name = 'SaveBest.mat'; % Name File
if strcmp(flag,'iter')
ibest = state.Best(end);
ibest = find(state.Score == ibest,1,'last');
bestx = state.Population(ibest,:);
previous = load('SaveBest.mat');
var = [previous.var; bestx]; % Read Previous Results, Append New Value
save(file_name, 'var') % Write ‘Best Individual’ To File
end
changed = true; % Necessary For Code, Use Appropriate Value
end
It reads the existing saved values of ‘var’, appends the new value to the end of the matrix, then writes the new matrix to the .mat file.
It never occurred to me that this was even possible! I will use it in my longer optimisations from now on.
  9 件のコメント
Manjur Basnet
Manjur Basnet 2022 年 11 月 30 日
Hi Star Strider, I have been trying to use the Saveout function to save best value from each generation but I keep getting this error after 1 generation
"Unrecognized field name "Best".
Error in SaveOut (line 7)
ibest = state.Best(end);
Error in gaoutput (line 51)
[state,optnew,changed] = feval(functions{i},options.OutputPlotFcnOptions,state,flag,args{i}{:});
Error in gamultiobjsolve (line 86)
[state,options] = gaoutput(FitnessFcn,options,state,currentState);
Error in gamultiobj (line 338)
[x,fval,exitFlag,output,population,scores,residuals] = gamultiobjsolve(FitnessFcn,nvars, ...
Error in pareto (line 28)
[x,fval,exitFlag,output] = gamultiobj(fun,5,A,B, Aeq, beq, lb,ub,nlcon, intcon, options);"
Can you help me with this? I tried searching for this kind of error but could find nothing.
Thank you so much.
Star Strider
Star Strider 2022 年 11 月 30 日
Unfortunately, Shahrbanoo Shamekhi-Amiri didn’t accept it in spite of my posting a working solution.
See if that Answer solves your problem.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeGenetic Algorithm についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by