Code covered by the BSD License  

Highlights from
Speeding Up Optimization Problems with Parallel Computing

image thumbnail

Speeding Up Optimization Problems with Parallel Computing

by

Stuart Kozola

 

Files from the webinar: Speeding up optimization problems with parallel computing

Editor's Notes:

This file was selected as MATLAB Central Pick of the Week

DemoGAwPCT.m
%% Parallelization of GA Portfoilio Optimization Problem
% Target Return Portfolio Optimization
% Oren Rosen
% Copyright 2009 The MathWorks, Inc.
% 4/14/2009
%
% Genetic Algorithms are inherently parrallelizable. This script
% demonstrates the potential execution time speed up that is possible with
% this feature. This script is a wrapper for a version of the
% "computebestportfolio" function that utilizes the parallel GA option.
% What is happening internally is that for each generation, individuals are
% tested against the objective function simultaneously using a "parfor"
% loop. In order to benefit, the objective function needs to be
% computationally intensive enough to take longer than the overhead inherent
% to parallelization. In my experiments, processing time of 0.5 seconds or 
% more is plenty. For this reason we test this solver against a
% slightly larger universe (100 stocks) with a maximum portfolio size of
% around 60 stocks. We also need to limit the number of generations the GA
% runs through so the demo does not take to much time. About 4 generations
% seems to work well. Compare the performance of this routine with/without
% parallelization and different portfolio sizes and/or number of generations
% to get a better feel for this.
%
% Note: To take advantage of parallelization start up a local pool of at
% least 2 MATLAB workers before running this script.
%
% Note: The GA uses the same custom crossover, mutation and initial
% population routines as the main seminar script. Make sure these are
% already present in a shared directory.

%% 
% Clear workspace and load expected return/covariance data for 100 stocks
clear variables;
load DataGAwPCT.mat;
%%
% A good portfolio size for this demo is 60 stocks (demonstrates speed up
% without taking to long. ~19 secs vs. ~11 secs with dual core T-60)
portSize = 60;
%%
% Time how long it takes for the optimization to complete.
% See "computebestportfolioPCT.m" for more details on optimization.
tic;

[portSubStd, portSubRet, portSubWts, portSubIndx] = ...
    computebestportfolioPCT(expRet,expCov,portSize,targetRet); %#ok<*NASGU,*ASGLU>

garuntime = toc;

% Report results
fprintf('GA Run Time is: %f seconds\n',garuntime);
%% Run on 2 Cores
matlabpool open local 2
tic;

[portSubStd, portSubRet, portSubWts, portSubIndx] = ...
    computebestportfolioPCT(expRet,expCov,portSize,targetRet);

garuntime = toc;

% Report results
fprintf('GA Run Time is: %f seconds\n',garuntime);
matlabpool close
%% Run on 4 Cores
files = dir;
files = {files(3:end).name};
matlabpool('open','speedy-4core',4,'FileDependencies',files)
tic;

[portSubStd, portSubRet, portSubWts, portSubIndx] = ...
    computebestportfolioPCT(expRet,expCov,portSize,targetRet);

garuntime = toc;

% Report results
fprintf('GA Run Time is: %f seconds\n',garuntime);
matlabpool close

Contact us