You can save the fittest individuals in each generation during the optimisation, then use the last (or all) of them to re-start ga. I wrote a function that I named ‘SaveOut’ to save the fittest individual in each generation for situations in which the compouter running the ga optimisation crashes or is interrupted such as in the one you describe. You can use it to re-start ga from where you previously left off.
An Example Using The ‘SaveOut’ Function To Save The Best Individuals In Each Generation —
t=[0.1
0.2
0.4
0.6
0.8
1
1.5
2
3
4
5
6];
c=[0.902 0.06997 0.02463 0.00218
0.8072 0.1353 0.0482 0.008192
0.6757 0.2123 0.0864 0.0289
0.5569 0.2789 0.1063 0.06233
0.4297 0.3292 0.1476 0.09756
0.3774 0.3457 0.1485 0.1255
0.2149 0.3486 0.1821 0.2526
0.141 0.3254 0.194 0.3401
0.04921 0.2445 0.1742 0.5277
0.0178 0.1728 0.1732 0.6323
0.006431 0.1091 0.1137 0.7702
0.002595 0.08301 0.08224 0.835];
theta0=[1;1;1;1;1;1];
ftns = @(theta) norm(c-kinetics(theta,t));
PopSz = 100;
Parms = 10; % Last Four Parameters Are Initial Conditions to rkinetics’ Differential Equations
% opts = optimoptions('ga', 'PopulationSize',PopSz, 'InitialPopulationMatrix',randi(1E+4,PopSz,Parms)*1E-3, 'MaxGenerations',2E3, 'PlotFcn',@gaplotbestf, 'PlotInterval',1, 'OutputFcn',@SaveOut);
Ansopts = optimoptions('ga', 'PopulationSize',PopSz, 'InitialPopulationMatrix',randi(1E+4,PopSz,Parms)*1E-3, 'MaxGenerations',2E3, 'OutputFcn',@SaveOut);
t0 = clock;
fprintf('\nStart Time: %4d-%02d-%02d %02d:%02d:%07.4f\n', t0)
[theta,fval,exitflag,output] = ga(ftns, Parms, [],[],[],[],zeros(Parms,1),Inf(Parms,1),[],[],Ansopts);
t1 = clock;
fprintf('\nStop Time: %4d-%02d-%02d %02d:%02d:%07.4f\n', t1)
GA_Time = etime(t1,t0)
DT_GA_Time = datetime([0 0 0 0 0 GA_Time], 'Format','HH:mm:ss.SSS');
fprintf('\nElapsed Time: %23.15E\t\t%s\n\n', GA_Time, DT_GA_Time)
fprintf(1,'\tRate Constants:\n')
for k1 = 1:length(theta)
fprintf(1, '\t\tTheta(%2d) = %8.5f\n', k1, theta(k1))
end
tv = linspace(min(t), max(t));
Cfit = kinetics(theta, tv);
figure
hd = plot(t, c, 'p');
for k1 = 1:size(c,2)
CV(k1,:) = hd(k1).Color;
hd(k1).MarkerFaceColor = CV(k1,:);
end
hold on
hlp = plot(tv, Cfit);
for k1 = 1:size(c,2)
hlp(k1).Color = CV(k1,:);
end
hold off
grid
xlabel('Time')
ylabel('Concentration')
legend(hlp, compose('C%d(t)',1:size(Cfit,2)), 'Location','N')
fprintf(1,'\n\n')
Cfit_mtx = kinetics(theta, t); % Calculate R² For Each Compartment
for k = 1:size(Cfit,2)
ypred = Cfit_mtx(:,k);
SSE = sum((c(:,k)-ypred).^2);
SST = sum((c(:,k)-mean(c(:,k))).^2);
Rsq(k) = 1 - (SSE/SST);
fprintf('\t\tR² c(%2d) = %7.4f\n',k, Rsq(k))
end
BestInGen = load('SaveBest.mat')
LastFive = BestInGen.Var(end-4:end,:)
function C=kinetics(theta,t)
c0 = theta(7:10);
[T,Cv]=ode45(@DifEq,t,c0);
%
function dC=DifEq(t,c)
dcdt=zeros(4,1);
dcdt(1)=-theta(1).*c(1)-theta(2).*c(1);
dcdt(2)= theta(1).*c(1)+theta(4).*c(3)-theta(3).*c(2)-theta(5).*c(2);
dcdt(3)= theta(2).*c(1)+theta(3).*c(2)-theta(4).*c(3)+theta(6).*c(4);
dcdt(4)= theta(5).*c(2)-theta(6).*c(4);
dC=dcdt;
end
C=Cv;
end
function [state,options,changed,str] = SaveOut(options,state,flag)
file_name = 'SaveBest.mat'; % Name File
if strcmp(flag,'init')
Var = state.Population;
save(file_name, 'Var') % Write ‘Best Individual’ To File
elseif 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 Cide, Use App
end
EDIT — (5 Jun 2023 at 14:39)
Improved description, code unchanged.
.