| Genetic Algorithm and Direct Search Toolbox | ![]() |
Vectorize Option
The genetic algorithm usually runs faster if you vectorize the fitness function. This means that the genetic algorithm only calls the fitness function once, but expects the fitness function to compute the fitness for all individuals in the current population at once. To vectorize the fitness function,
write the M-file using the following code:
The colon in the first entry of x indicates all the rows of x, so that x(:, 1) is a vector. The .^ and .* operators perform element-wise operations on the vectors.
The following comparison, run at the command line, shows the improvement in speed with Vectorize set to On.
tic;ga(@rastriginsfcn,20);toc elapsed_time = 4.3660 options=gaoptimset('Vectorize','on'); tic;ga(@rastriginsfcn,20,options);toc elapsed_time = 0.5810
| Using a Hybrid Function | Using Direct Search | ![]() |
| © 1994-2009 The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |