Path: news.mathworks.com!not-for-mail
From: <HIDDEN>
Newsgroups: comp.soft-sys.matlab
Subject: use parallel option in ga algorithm and parfor
Date: Fri, 12 Jul 2013 21:12:07 +0000 (UTC)
Organization: The MathWorks, Inc.
Lines: 50
Message-ID: <krprf7$jt4$1@newscl01ah.mathworks.com>
Reply-To: <HIDDEN>
NNTP-Posting-Host: www-02-blr.mathworks.com
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
X-Trace: newscl01ah.mathworks.com 1373663527 20388 172.30.248.47 (12 Jul 2013 21:12:07 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Fri, 12 Jul 2013 21:12:07 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 2886349
Xref: news.mathworks.com comp.soft-sys.matlab:799261

I was reading (http://www.mathworks.com/matlabcentral/answers/41749)  about combining parfor and Parallel optimizatioin. It seems that some people suggest that it may actually be possible to take advantage of parallelll loops when they are inside of the optimization routine. 

I have fixed up the following example. But i only have 4 workers on  this machine and when i include the parfor inside the objective function routine running time goes up (this is not surprising). I am wondering that if I actually had excess capacity i.e. 100 workers and the size of initial population set to 12. Would using parfor inside the objective function speed things up?
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% create fake data to use in the estimation
% create a vector y Nx1 and vector x Nx1

x=randn(1000,1);
e_true=randn(1000,1);
y=.2.*x.*x+e_true;


%set up options structure for 
needNewWorkers = (matlabpool('size') == 0);
if needNewWorkers
    
    matlabpool open 4
end
options = gaoptimset;
 options = gaoptimset(options,'PopulationSize', 3);
options = gaoptimset(options,'MutationFcn', {  @mutationadaptfeasible 5 [] });
options = gaoptimset(options,'Display', 'iter');
options = gaoptimset(options,'FitnessLimit', 0);
options = gaoptimset(options,'UseParallel', 'always');
options = gaoptimset(options,'Generations', 100);
nvars=1;
tic
% call ga function
[beta fval] = ga(@(beta)fitnessfun(beta,x,y), nvars, options);
toc

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% objective function  called by ga
% Create fitness function
function obj=fitnessfun(beta,x,y)
errors=zeros(100,1);
parfor i=1:100
	%create e_simulated
	
    e_simulated=randn(1000,1);
	y_sim=beta.*x.*x+e_simulated;
	errors(i)=sum((y_sim-y).*(y_sim-y));
 end

obj=mean(errors);

end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

Sincerely , Lea