GA is suddenly much slower (no other visible effects)

4 ビュー (過去 30 日間)
Tom Brenner
Tom Brenner 2022 年 7 月 18 日
編集済み: Tom Brenner 2022 年 7 月 23 日
Out of the bluw, a code I use to run a GA optimization has become much (>ten-fold) slower. The specifics are as follows:
Ubuntu 20.04, 64Gb/2.4 GHz RAM, i9-7900X CPU @ 3.30GHz. Not using GPUs for any of my MATLAB (2018a) operations.
I won't paste the whole code, but a short pseudocode is provided for the function:
fopt = @(x)MyFun(x,pars);
tic;[x,fval] = ga(fopt,N,[],[],[],[],Lower,Upper,[],[],opt.options);toc
function f = MyFun(x,pars)
%LOTS OF STUFF
for j = 1:numel(pars(1))
%LOTS OF STUFF
end
%f = result of lots of stuffy calcuations
Let's have a look at opt.options:
opt.options
ans =
ga options:
Set properties:
ConstraintTolerance: 1.0000e-08
CrossoverFraction: 0.5000
Display: 'iter'
EliteCount: 55
FunctionTolerance: 3.0000e-31
InitialPopulationMatrix: [65×7 double]
MaxGenerations: 444
MaxStallGenerations: 1
NonlinearConstraintAlgorithm: 'penalty'
OutputFcn: @getoutput
PopulationSize: 400
UseParallel: 1
Default properties:
CreationFcn: @gacreationuniform
CrossoverFcn: @crossoverscattered
FitnessLimit: -Inf
FitnessScalingFcn: @fitscalingrank
HybridFcn: []
InitialPopulationRange: []
InitialScoresMatrix: []
MaxStallTime: Inf
MaxTime: Inf
MutationFcn: {@mutationgaussian [1] [1]}
PlotFcn: []
PopulationType: 'doubleVector'
SelectionFcn: @selectionstochunif
UseVectorized: 0
This code used to run @1.3 s per GA generation with a popualtion of 400. One day, out of the blue, from one run to the next, without even restarting MATLAB, the run became much slower (over a minute per generation). I couldn't think of anything, so instead of reinstalling MATLAB, I recovered an earlier OS image. The code now ran again @1.3 s per GA generation for two times, but the third time I ran it.... again, about ten times slower.
The things to note:
  1. The time for GA generation seems to be independent of population size! I have never encountered anything like this. I've checked population sizes in the range 66 to 400, and all now run at around 18 s per generation.
  2. Other calculations, including using parpools, are unaffected. For instance, with an active parpool, the command:
tic;parfor j = 1:30; inv(rand(3e3));end;toc
used to be resolved on average in around seven seconds. This calculation time is not affected by whatever is plaguing the GA module.
Any suggestions would be greatly appreciated.
  7 件のコメント
Tom Brenner
Tom Brenner 2022 年 7 月 21 日
Matt J: nope and nope. The only way to get back the original performace is by recovering the OS image from before the recent update. As soon as ubuntu is updated, though, it reverts back to very slow execution.
Tom Brenner
Tom Brenner 2022 年 7 月 23 日
Matt J's first comment on his answer forms a solution of sorts, even if unintentional.

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

採用された回答

Matt J
Matt J 2022 年 7 月 21 日
編集済み: Matt J 2022 年 7 月 21 日
As a troubleshooting strategy, I suggest putting a tic...toc as the first and last lines of the fitness function. This way you can see if the slowdown is in the execution of the fitness function itself or something else. If the fitness function is slowing down, maybe the slowdown occurs for very specific population members.
  4 件のコメント
Matt J
Matt J 2022 年 7 月 22 日
You can also insert keyboard() commands or breakpoints to pause execution and examine the workspace of the fitness function when the execution time exceeds a certain threshold, e.g,
function f=fitnessFunc(x)
tic;
f=norm(x);
t=toc;
if t>threshold
keyboard
end
end
Tom Brenner
Tom Brenner 2022 年 7 月 23 日
編集済み: Tom Brenner 2022 年 7 月 23 日
Because the offending function requires additional input parameters, I haven't found to way to implement your code to obtain the Times parameters including individual tocs. My code looks like this:
function Times=main(pars,N,Lower,Upper,opt)
Times=[];
xopt=ga(@(x)MyFun(x,pars),N,[],[],[],[],Lower,Upper,[],[],opt.options);
function f=MyFun(x,pars)
tic;
%%%Lots of code
t=toc;
Times=[Times,t];
end
end
This implementation does not build up the Times parameter, but instead retains only a single value (just to be clear: I've managed to get it to build up the Times parameter with all t values when running it for other functions that don't require additional input parameters, using your original code).
While I can't use your method, however, the running time per generation has now gone back down to about 1.3 s, instead of 18 s!!!! This is truly baffling, but since it is doing what I want it to do, I will accept your answer. To be clear, running the code directly without nesting in within the main function still runs at about 18 s per generation.
Thanks again!

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeParallel Computing Fundamentals についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by