parallel computing in Genetic algorithm
22 views (last 30 days)
Show older comments
Hello everyone,
I am using the genetica algorithm code 'ga' to optimize my function. However, it is taking very large amount of simulation time.
I have tried adding the "'UseParallel',true" inside the gaoptimset. But now, the code is getting stopped after the first iteration.
Please help me to solve it.
Thanks in advance.
0 Comments
Answers (2)
Alan Weiss
on 2 Sep 2021
You have to be careful using parallel computing. Are you using Simuulink® for the simulation? I am not sure that it can run in parallel inside ga because ga uses a parfor call, not a parsim call. If you are using a different software package to run the simulation, you have to ensure that it can run in parallel. If you have any global variables, well, you cannot run the simulation in parallel.
Are you sure that ga is the best optimizer for you? Can you try patternsearch or surrogateopt?
Good luck,
Alan Weiss
MATLAB mathematical toolbox documentation
3 Comments
Alan Weiss
on 3 Sep 2021
If you want to run in parallel then you have to get rid of any global variables. They are a bad idea in any case. If you use them to pass data or parameters, stick the data in a structure called params and pass params as extra data; see Passing Extra Parameters. For example, to pass an array A and a vector B,
params.A = A;
params.B = B;
% Then in your objective function
function y = objfun(x,params)
A = params.A;
B = params.B
% Now put in the rest of your code
end
% You call the objective like this:
fun = @(x)objfun(x,params)
[x,fval] = patternsearch(fun,...)
I suggest that you use patternsearch as a much more efficient solver than ga. You have to set the CompletePoll option to 'on' to use parallel processing with patternsearch.
Good luck,
Alan Weiss
MATLAB mathematical toolbox documentation
John D'Errico
on 3 Sep 2021
Edited: John D'Errico
on 3 Sep 2021
Whenever I see someone say they need to use parallel computing to make their code run faster, I wonder if the real problem is they just need to learn to write better, more efficient code. Hoping for parallel computing to make your code magically run fast is the lazy solution. Why improve your code? Just wave a magic wand at it.
This conclusion is made more likely when I see you are using global variables as a means to pass things around. Globals are a poor tool, used mainly by novice programmers in MATLAB. And that alone tells me you can surely write better code. How much better? It is easy to get orders of magnitude speed bumps, just by writing better code. Do I know this to be true in your code? Of course not, since I have not seen your code, nor will I be willing to spend what may be a lot of time to rewrite your code.
The idea is to start with the profile utility in MATLAB. See where the bottlenecks are. What lines are using the most time? Then focus your efforts on those lines. Can you improve them? Sometimes this will require a complete re-thinking of what you are doing, changing how you will solve the problem.
is that a likely thing to happen? Yes, i know it to be entirely possible, since I personally had some code that I wrote and re-wrote as I was learning MATLAB long ago, achieving speed boosts of many orders of magnitude along the way.
See Also
Categories
Find more on Genetic Algorithm 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!