Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
use parallel option in ga algorithm and parfor

Subject: use parallel option in ga algorithm and parfor

From: lena

Date: 12 Jul, 2013 21:12:07

Message: 1 of 1

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

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us