parfor problem with fmincon body
2 次查看(过去 30 天)
显示 更早的评论
Hi, I was wondering if someone can help me to use here parfor. If the code below is run without parfor, code works well.
When I put parfor rather than for loop I get a msg that " The variable S1 in a parfor cannot be classified."
For each i, I run the code 9 times, where for each k, the variable init_s(i), get the last value of previous S1(end,k,i).
When the code finishes with k=9, the new init_S is got from line 2 and than the process continues as before and so on.
Does anyone know how to adjust the code so I can run parallel computing for each i. So I can use my 12 cores and run each i on one core for k=1:9?
Thanks a lot!!!!
parfor i = 1:M
init_s=maxS;
for k=1:9
options = optimset('MaxFunEvals',Inf,'MaxIter',10000,...
'Algorithm','interior-point','Display','iter');
tic
[x1(:,k,i),fval1(:,k,i)] = fmincon(@(x)revenue(x,N,sumprice1(k,1),init_s(i),inFlow1(:,k,i),alpha_par(i),b_par(i)),x01(:,k,i),A,b(:,k,i),Aeq,beq(k,i),LB(:,k,i),UB(:,k,i),[],options); %good
toc
S1(:,k,i)= storage(init_s(i),inFlow1(:,k,i),x1(:,k,i),N);
init_s(i)=S1(365,k,i);
end
end
0 个评论
回答(1 个)
OCDER
2018-8-28
Here's a work around, though I don't like the fact that this copies S1... How big is S1? If it's small, maybe not a huge issue.
parfor i = 1:M
init_s = maxS;
for k = 1:9
options = optimset('MaxFunEvals',Inf,'MaxIter',10000,'Algorithm','interior-point','Display','iter');
[x1(:,k,i),fval1(:,k,i)] = fmincon(@(x)revenue(x,N,sumprice1(k,1),init_s(i),inFlow1(:,k,i),alpha_par(i),b_par(i)),x01(:,k,i),A,b(:,k,i),Aeq,beq(k,i),LB(:,k,i),UB(:,k,i),[],options); %good
S1(:,k,i) = storage(init_s(i),inFlow1(:,k,i),x1(:,k,i),N);
T = S1(:,k,i);
init_s(i) = T(365,k,i)
end
end
The trick is that whenever you use parfor, all variables that use the parfor counter variable ("i" in your case) must CONSISTENTLY use the same indexing scheme.
parfor i = 1:M
A(i, 1) = i;
A(i, 2) = i+1; %NOT OK. "(i, 1)" and "(i, 2)" are 2 different indexing.
end
parfor i = 1:M
T = A(i, :);
T(1, 1) = i;
T(1, 2) = i+1;
A(i, :) = T; %OK. Always used the same indexing, "(i, :)".
end
2 个评论
OCDER
2018-8-28
S seems small enough. To see if parfor is faster, you'll have to do some time tests. Often, it's hard to be certain if the parfor is faster than regular for loops until you test it for your application. Sometimes, it's slower due to overhead in data transfer.
tic
parfor i 1:M
...
end
toc
tic
for i = 1:M
...
end
toc
How fast is it without parfor?
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!