parallel computing in Genetic algorithm

22 ビュー (過去 30 日間)
Ashok Das
Ashok Das 2021 年 9 月 2 日
コメント済み: Ashok Das 2021 年 9 月 3 日
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.

回答 (2 件)

Alan Weiss
Alan Weiss 2021 年 9 月 2 日
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 件のコメント
Alan Weiss
Alan Weiss 2021 年 9 月 3 日
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
Ashok Das
Ashok Das 2021 年 9 月 3 日
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.

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


John D'Errico
John D'Errico 2021 年 9 月 3 日
編集済み: John D'Errico 2021 年 9 月 3 日
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 件のコメント
Ashok Das
Ashok Das 2021 年 9 月 3 日
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.

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

カテゴリ

Help Center および File ExchangeGenetic Algorithm についてさらに検索

製品


リリース

R2015a

Community Treasure Hunt

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

Start Hunting!

Translated by