MATLAB Answers

How to find function and variable value at each iteration for genetic algorithm with Parallel Computing ?

4 views (last 30 days)
Ankur Shah
Ankur Shah on 24 Jan 2021
Commented: Ankur Shah on 26 Jan 2021
Hello everyone,
I have prepared code in matlab for genetic algorithm from toolbox for number of iteration (nit)
for i=1:nit
[x,fval,exitflag,output,population,score] = GAcode(nvars,lb,ub,InitialPopulationRange_Data,PopulationSize_Data,EliteCount_Data,CrossoverFraction_Data,MaxGenerations_Data,FunctionTolerance_Data,ConstraintTolerance_Data);
H1(i,1)=fval;
H2(i,1:nvars)=x;
end
where at the end H1 and H2 gives function and variable value for all the iterations respectively. 'GAcode' is file generated from toolbox. The code works properly.
I am trying to use parallel computing for increasing speed. Firstly started parallel pool and in preferences defined number of workers as 8. In file 'GA code' i have given
options = optimoptions(options,'UseParallel', true);
While using, parfor for i=1:nit, following error is obtained "Unable to classify the variable 'H2' in the body of the parfor-loop." If i remove H2 code runs properly.
How can i obtain H1 and H2 value after each iteration with parallel computing ?
For current setup even when the "for" loop for number of iterations is removed and done for single iteration no speed improvement is observed in parallel computing applying above options rather with parallel it is 63 sec and without it is 58 sec. The results are ok. What am i doing wrong ?
Thanks for your help.

Accepted Answer

Raymond Norris
Raymond Norris on 25 Jan 2021
This doesn't address your issue, but you can't have a nested parfor loop/spmd block, which is in essense, what you're suggesting.
parfor i=1:nit
[~] = GAcode();
H1(i,1)=fval;
H2(i,1:nvars)=x;
end
Roughly, I'm assuming GAcode is similar to
...
parfor j = 1:X
% Some code
end
...
Simplifyling this to
parfor i=1:nit
parfor j = 1:X
% Some code
end
H1(i,1)=fval;
H2(i,1:nvars)=x;
end
In your specific case (by calling GAcode), the "inner parfor" will result in a for-loop. You may want to test which loop is better to parallelize.
If you try the outer for-loop, the issue with H2 is addressed here
Form of Indexing. Within the list of indices for a sliced variable, one index is of the form i, i+k, i-k, k+i, or k-i.
  • i is the loop variable.
  • k is a constant or a simple (nonindexed) variable.
  • Every other index is a constant, a simple variable, colon, or end.
The last bullet item addresses H2. Would this work instead?
H2(i,:)=x;
  1 Comment
Ankur Shah
Ankur Shah on 26 Jan 2021
Thank you for replying.
The H2(i,:)=x; resolves issue of error in the code.
It seems each iteration is seperately calculated and if nit=2 both results are calcuated simultaneously. Without parallel it is 115 sec and with parallel is 63 sec
The other issue is as asked in question is when i just place following (Single GA) without any parfor loops
[x,fval,exitflag,output,population,score] = GAcode(nvars,lb,ub,InitialPopulationRange_Data,PopulationSize_Data,EliteCount_Data,CrossoverFraction_Data,MaxGenerations_Data,FunctionTolerance_Data,ConstraintTolerance_Data);
Use of parallel in GA code does not increase the speed of GA for example with parallel it is 63 sec and without it is 58 sec. I am following parallel GA procedure as in Main question Normally it should decrease time by factor of 2 but its not happening in this case. What can be done ?

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!

Translated by