parfor with fminunc where fminunc returns a vector

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 comentarios

J
J el 14 de Jul. de 2012
sorry, i should mention that km1 is supposed to be an array with two elements... code works fine when i use a for loop as opposed to a parfor loop
We will need to see firmprob_2cap_noprices
J
J el 14 de Jul. de 2012
Editada: Walter Roberson el 14 de Jul. de 2012
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.

Iniciar sesión para comentar.

 Respuesta aceptada

Walter Roberson
Walter Roberson el 14 de Jul. de 2012
Editada: Walter Roberson el 14 de Jul. de 2012

0 votos

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.

Más respuestas (1)

J
J el 14 de Jul. de 2012

0 votos

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?

Categorías

Más información sobre Performance and Memory en Centro de ayuda y File Exchange.

Preguntada:

J
J
el 14 de Jul. de 2012

Community Treasure Hunt

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

Start Hunting!

Translated by