parallel computing in Genetic algorithm
21 views (last 30 days)
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.
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?
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.