MATLAB Answers

parallel computing in Genetic algorithm

9 views (last 30 days)
Ashok Das
Ashok Das on 2 Sep 2021
Commented: Ashok Das on 3 Sep 2021
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.

Answers (2)

Alan Weiss
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
Ashok Das
Ashok Das on 3 Sep 2021
Thanks Alan for your reply.
I have eliminated the global variables.
I will try it with paramsearch with CompletePoll option. I will notify whether it is working or not.

Sign in to comment.


John D'Errico
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.
  1 Comment
Ashok Das
Ashok Das on 3 Sep 2021
Knowledge is a beautiful thing. You earn it to spread to others. However, you need to make sure that you don't behave like a troll or nerd, who thinks that others know nothing.
I already mentioned that I am not an expert in this field. So used global parameters. Other than this, the code is a iterating simulation of some physical phenomenon with very small time step. The problem is not the bottleneck, it is the number of iterations, which is taking very large time. So, if you remove also the unnecessary things, it will not much be of an help in terms of run time. (FYI, there exist many softwares like EDEM and others which does simulate 1 sec of process in some days)
Then also, thanks for your reply.

Sign in to comment.

Products


Release

R2015a

Community Treasure Hunt

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

Start Hunting!

Translated by