parfor with fminunc where fminunc returns a vector

조회 수: 5 (최근 30일)
J
J 2012년 7월 14일
Hi,
I am running a parfor loop inside which I use fminunc and I get the following error:
Starting matlabpool using the 'local' profile ... connected to 12 labs.
Error using parallel_function (line 589)
User supplied objective function must return a scalar
value.
Error stack:
fminunc.m at 324
Error in untitled8_noprices (line 72)
parfor j=1:J % loop for firms (column of matrices)
Is there some way to fix if I screwed something or to go around any limitations that I might be running into?
I attach my code under so that you can look at it/make suggestions
------------------------------------------------
while dif>tol && its<maxits
parfor j=1:J
for i=1:N
for ii=1:N
k0=kmat(i,j);
m0=mmat(ii,j);
km0=[kmat(i,j),mmat(ii,j)];
km1 = fminunc(@(km) firmprob_2cap_noprices(km,j,vold,k0,m0,Agrid),km0);%,options);
vnew(i,ii,j) = -firmprob_2cap_noprices(km1,j,vold,k0,m0,Agrid);
kopt(i,ii,j) = km1(1);
mopt(i,ii,j) = km1(2);
Ymat(i,ii,j)=Agrid(j)*(k0^alpha)*(m0^alpha)-km1(1)-km1(2);
end
end
end
if its>=maxits;
fprintf('Ran out of iterations \n');
end
for jj=1:J
diff(1,jj)=norm(vnew(:,:,jj)-vold(:,:,jj));
end
dif=max(diff)
vold = vnew;
its = its+1;
end
  댓글 수: 3
Walter Roberson
Walter Roberson 2012년 7월 14일
We will need to see firmprob_2cap_noprices
J
J 2012년 7월 14일
편집: Walter Roberson 2012년 7월 14일
function val=firmprob_2cap_noprices(km,j,vold,k0,m0,Agrid)
global beta delta alpha eta
% do the interpolation
X=[km(1),km(2)];
gg=interpne(vold(:,:,j),X);
profit = (Agrid(j).*(km(1).^alpha).*(km(2).^(alpha)))-(km(1)-k0*(1-delta))-(km(2)-m0*(1-delta));
if profit<=0
val = -9999999 - 999*abs(profit);
else
val= profit+ beta.*gg;
end
val = -val; % make it negative since we're maximizing and code is to minimize.

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

채택된 답변

Walter Roberson
Walter Roberson 2012년 7월 14일
편집: Walter Roberson 2012년 7월 14일
Is "interpne" from the Factors on Demand FEX contribution?
What are size(alpha), size(beta), size(delta) ?
Please put in a conditional breakpoint on the last line of the function, to stop if length(val) ~= 1
My speculation is that beta might be a vector rather than a scalar.
Note: you are safer passing alpha, beta, delta, eta into the function as parameters rather than as globals. Using global with parfor is unsafe if the functions can somehow write to the globals.
And of course there would be problems if the globals ended up initialized to [] because they had not been passed into the workspace of the worker.

추가 답변 (1개)

J
J 2012년 7월 14일
Hi,
Even though alpha, beta and delta are scalar and not vectors (0.2,0.9 and 0.1 respectively), when i passed them through the function rather than as globals, the parfor worked!!!!!!!!!!
So greatful for your help, I really appreciate it!
I have an added question, my fminunc prints 'Local Minimum found'. Is there some way to stop that to quicken the code?

카테고리

Help CenterFile Exchange에서 Performance and Memory에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by