How to restart optimization with ga ?

4 views (last 30 days)
EMANUELE STICCHI
EMANUELE STICCHI on 5 Jun 2023
Edited: Star Strider on 5 Jun 2023
I'm using the matlab function ga() to perform an optimization. The optmization run on a cluster which queues have limited job time (72hours). My answer is:
Once the Job has been killed (because maximum Job time has been exceeded), is there any way to restart the optmization?

Answers (1)

Star Strider
Star Strider on 5 Jun 2023
Edited: Star Strider on 5 Jun 2023
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)
Start Time: 2023-06-05 12:14:30.8835
[theta,fval,exitflag,output] = ga(ftns, Parms, [],[],[],[],zeros(Parms,1),Inf(Parms,1),[],[],Ansopts);
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
t1 = clock;
fprintf('\nStop Time: %4d-%02d-%02d %02d:%02d:%07.4f\n', t1)
Stop Time: 2023-06-05 12:15:09.2254
GA_Time = etime(t1,t0)
GA_Time = 38.3419
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)
Elapsed Time: 3.834190800000000E+01 00:00:38.341
fprintf(1,'\tRate Constants:\n')
Rate Constants:
for k1 = 1:length(theta)
fprintf(1, '\t\tTheta(%2d) = %8.5f\n', k1, theta(k1))
end
Theta( 1) = 0.17026 Theta( 2) = 1.00021 Theta( 3) = 6.68932 Theta( 4) = 12.72032 Theta( 5) = 1.03712 Theta( 6) = 0.28688 Theta( 7) = 1.03864 Theta( 8) = 0.04773 Theta( 9) = 0.02107 Theta(10) = 0.00006
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
R² c( 1) = 0.9675 R² c( 2) = 0.6535 R² c( 3) = 0.3701 R² c( 4) = 0.9521
BestInGen = load('SaveBest.mat')
BestInGen = struct with fields:
Var: [1227×10 double]
LastFive = BestInGen.Var(end-4:end,:)
LastFive = 5×10
0.1703 1.0002 6.6893 12.7203 1.0371 0.2869 1.0386 0.0477 0.0211 0.0001 0.1703 1.0002 6.6893 12.7203 1.0371 0.2869 1.0386 0.0477 0.0211 0.0001 0.1703 1.0002 6.6893 12.7203 1.0371 0.2869 1.0386 0.0477 0.0211 0.0001 0.1703 1.0002 6.6893 12.7203 1.0371 0.2869 1.0386 0.0477 0.0211 0.0001 0.1703 1.0002 6.6893 12.7203 1.0371 0.2869 1.0386 0.0477 0.0211 0.0001
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.
.

Categories

Find more on Get Started with Optimization Toolbox in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!