- 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.
How to find function and variable value at each iteration for genetic algorithm with Parallel Computing ?
4 次查看(过去 30 天)
显示 更早的评论
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.
0 个评论
采纳的回答
Raymond Norris
2021-1-25
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.
The last bullet item addresses H2. Would this work instead?
H2(i,:)=x;
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Genetic Algorithm 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!