필터 지우기
필터 지우기

How to use Parallel Computing inside ga (genetic algorithm)

조회 수: 95 (최근 30일)
Camilo
Camilo 2015년 9월 21일
댓글: Terry 2023년 12월 13일
Hello everyone,
I´m stuck in my code. I´m trying to maximize a function using genetic algorithm and recently, I read that Parallel Computing could reduce the calculation time that ga takes to show its result, but when I include in gaoptimset('UseParallel',true) the code ends very quickly (at first iteration) and do not optimize my function. I'm using the following code in Matlab:
Note: - Please obviate the first (/if true/ and the last /end/) because I employed {}code to put my code. - the function to optimze is named @func1
I do not know what is wrong in my code because I read in matlab help and all the comments said that if you want to use Parallel Computing you only need to add 'UseParallels',true into gaoptimize.
Thanks for your help.
if true
% The code that I'm using is:
% ***************************
function ga_optim(func)
close all
global fin y_opt
fin = 50000;
y_opt = 9999;
% Number of variable to optimize
nvars = 6;
if nargin == 0
func = 1;
end
if func == 1
ObjectiveFunction = @func1;
end
ConstraintFunction = @simple_constraint;
LB = [7; 7; 70; -150; 200; 300]; % Lower bound
UB = [15; 12; 300; 50; 450; 650]; % Upper bound
popsize = 600;
gensize = 600;
opts = gaoptimset('PopulationSize', popsize, 'Generations', gensize, 'Display', 'off', 'TolFun', 1e-2,'UseParallel', true);
t1 = tic;
[x,fval,exitFlag,output,population,scores] = ga(ObjectiveFunction,nvars, [], [], [], [], LB, UB, ConstraintFunction, 1:2, opts);
tiempo = toc(t1);
x = x';
% End of the code
% ***************
end

답변 (4개)

Alan Weiss
Alan Weiss 2015년 9월 21일
What error messages are you seeing, if any? Did you try to create a parallel pool before starting GA? It is possible that you have an older version of Global Optimization Toolbox where you should set UseParallel to 'always'. It is also possible that you do not have a Parallel Computing Toolbox license. Without more details, it is hard to know what might be happening.
Alan Weiss
MATLAB mathematical toolbox documentation

Camilo
Camilo 2015년 9월 21일
Hello Alan, I also tried with 'UserParallel','always' but the same thing occurs, GA terminates its executions at first iteration. I do not receive error mensages. There is sb interesting in your comment. How can I create a parallel pool before starting my GA?
  댓글 수: 1
Alan Weiss
Alan Weiss 2015년 9월 21일
How to use parallel processing has that information. Depending on your software version, and license, this pool can start automatically.
Alan Weiss
MATLAB mathematical toolbox documentation

댓글을 달려면 로그인하십시오.


Camilo
Camilo 2015년 9월 21일
I use the following code to start Parallel computing:
opts = gaoptimset('PopulationSize', popsize, 'Generations', gensize, 'Display', 'off', 'TolFun', 1e-2, 'UseParallel', true);
And I've also tried 'UseParallel','always' but the GA ends at first iteration and GA does not find me a suitable optimize set
  댓글 수: 1
Alan Weiss
Alan Weiss 2015년 9월 21일
Please read the link I gave before about how to start a parallel pool.
Alan Weiss
MATLAB mathematical toolbox documentation

댓글을 달려면 로그인하십시오.


wonderkismet
wonderkismet 2019년 9월 23일
You can use vectorize method.
  1. Start a pool
  2. In ga options, Enable vectorized
  3. process the vectorized generation input with your fitness function. Inside the fitness function, use a parfor to process each row of the generation. The generation is a matrix with population number of rows, segment the rows into the number of works you have and sent them to each work to calculate.
  댓글 수: 1
Terry
Terry 2023년 12월 13일
Can you give more details about step 3? It's very complex and hard to understand. Thank you!

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Genetic Algorithm에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by