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_simy).*(y_simy));
end
obj=mean(errors);
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Sincerely , Lea
