Sliced variable issue - variable cannot be classified

조회 수: 1 (최근 30일)
Axel Groniewsky
Axel Groniewsky 2019년 5월 3일
댓글: Axel Groniewsky 2019년 5월 8일
Dear All,
I'm trying to use parallel workers in an optimization problem but I cannot define X and Y as sliced variables. X contains the population, Y contains the fitness values. As the error message says: variable X & Y in a parfor cannot be classified.
First I define X, (it has to be outside the parfor loop otherwise I get new X-es in every single loop); than parfor follows:
parfor t=1:T
TmpZ=Func(Z(1,:));TmpX=Func(X(t,:));
if TmpZ < TmpX
X(t,:)=Z(1,:); Y(t,1)=TmpZ;
else
X(t,:)=X(t,:); Y(t,1)=TmpX;
end
end
Thanks

채택된 답변

Edric Ellis
Edric Ellis 2019년 5월 7일
I augmented your code a tiny bit to make it executable, and it works fine in MATLAB R2019a, and I checked all the way back to R2013b...
T = 7;
Func = @sum;
Z = rand(1, 3);
X = rand(T, 3);
parfor t=1:T
TmpZ=Func(Z(1,:));TmpX=Func(X(t,:));
if TmpZ < TmpX
X(t,:)=Z(1,:); Y(t,1)=TmpZ;
else
X(t,:)=X(t,:); Y(t,1)=TmpX;
end
end

추가 답변 (1개)

Axel Groniewsky
Axel Groniewsky 2019년 5월 7일
Dear Edric,
Thank you for dealing with my question. This part of the code runs fine yet, the error message says that 'The variable X in a parfor cannot be classified.' The other part of the code I have inside the parfor loop is:
R=randperm(T);
R(find(R==t))=[];
V=X(R(1),:)+F*(X(R(2),:)-X(R(3),:));
I do not alter X rather creating V. But you are right, without V it runs smoothly. Is there a way to define V in a parfor supported way?
Thanks!
  댓글 수: 2
Edric Ellis
Edric Ellis 2019년 5월 7일
This code is not order-independent, and cannot run inside parfor. Basically, the problem is that you're accessing elements of X that might or might not yet have been updated.
Axel Groniewsky
Axel Groniewsky 2019년 5월 8일
Another idea I tried is to save X after every iteration and load it before V vector is calculated. This way I end up having Transparency violation error. Is there another way you can think of which could dissolve this issue and let me use population-based metaheuristics with parfor? (I already tried the built-in GA but my own codes provide more accurate results.)
Thanks!

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

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by