Parallelization of a Genetic Algorithm in Matlab

966 Views Asked by At

I'm writing a parallel Genetic Algorithm in Matlab, specifically a Dual Species Genetic Algorithm (DSGA) (more information can be found in this paper here), and I'm having some trouble parallelizing part of the code.

Now I'm a physics and math major and I'm just getting started with my programming to help with my research projects. I've already implemented a serial genetic algorithm in Matlab with a somewhat parallelized fitness function, but I'm interested in implementing this Dual Species Genetic Algorithm.

Okay so the question: how can i run 2 different for loops that are doing different things on different matlab workers? The 2 for loops will have different data and different code, but I need them to be running simultaneously. I don't think i could use a parfor loop for that. After reading through the documentation for the parallel tool box it looks like using the Single Program Multiple Data (spmd) function may be an option? I'm not really sure if its the best option of it would really work at all.

Any advice would be appreciated.

Thanks.

1

There are 1 best solutions below

1
On BEST ANSWER

First step is to wrap both loops in a function, this simplifies the code. Then create a cell with all functions you want to evaluate in parallel. Check the documentation for "function handle" if you don't understand the syntax. I use some dummy functions here:

jobs={@()exp(1),@()exp(2),@()sin(3)}

Now you can easily evaluate the jobs in parallel:

result=cell(size(jobs))
parfor ix=1:numel(jobs),result{ix}=jobs{ix}(), end

There are other solutions, but I prefer this implementation because the same code can be executed on PCs without parallel computing (parfor is interpreded as for).