Search code examples
algorithmmatlaboptimizationparallel-processingparfor

Random number generator in parallel computing (MATLAB), different initial random numbers in every parallel loop?


Suppose that I have this code in MATLAB:

% Predefined data
SX = [1, 2, 3, 4];
    parfor xx = 1:4
        naming2 = SX(xx);
        [BestM, BestX{xx},  fina_M{xx}, final_D{xx}, BestAA{xx}, final_Data{xx}] = Optmz(naming2,  v_data); 
    end

Optmz is an optimization algorithm. This optimization algorithm should run to optimize a regression model (with different output and optimized inputs-feature selection). As you know heuristic optimization algorithms are based on random numbers. We have different initial random numbers in every parlor loop? Is this a proper structure to decrease time of my application? I'm currently using for loop in above structure.

This is a part of printed output. Iterations are not sorted. Any problem? Based on above code we should have four iterations with same number. I need total different calculations in all 4 workers with different initial random number generator. For example, like the way we run our calculations in sequence without parallel computing. Run first one, second one, third one and finally fourth one.

******Iteration 24,   Best Cost = 0.041201******
******Iteration 26,   Best Cost = 0.034994******
******Iteration 26,   Best Cost = 0.036624******
******Iteration 26,   Best Cost = 0.039317******
******Iteration 25,   Best Cost = 0.039584******
******Iteration 27,   Best Cost = 0.034994******
******Iteration 27,   Best Cost = 0.036624******
******Iteration 27,   Best Cost = 0.039317******
******Iteration 28,   Best Cost = 0.034994******
******Iteration 26,   Best Cost = 0.039242******
******Iteration 28,   Best Cost = 0.036624******
******Iteration 28,   Best Cost = 0.03931******
******Iteration 29,   Best Cost = 0.034994******
******Iteration 29,   Best Cost = 0.036624******
******Iteration 27,   Best Cost = 0.039048******
******Iteration 29,   Best Cost = 0.03931******
******Iteration 30,   Best Cost = 0.034994******
******Iteration 30,   Best Cost = 0.036624******
******Iteration 28,   Best Cost = 0.039048******

Solution

  • Maybe this is what you are asking, maybe not, but if you want different seeds for each loop you can seed the random number generator with a time stamp or something like that. This will make sure that each loop has a different seed and therefor a different set of random numbers

    nLoops = 10
    parfor ii = 1:nLoops
         timeVals = strsplit(sprintf('%.9f',now),'.')
         rng(str2double(timeVals{2}))
         % do some stuff
    end
    

    Or you can use MATLAB's built in shuffle function for rng

    nLoops = 10
    parfor ii = 1:nLoops
         rng('shuffle')
         % do some stuff
    end
    

    If you don't want it to be random in every loops, just create an array before you go into the loops, of whatever size you need, and have each loop access this same information. It is highly recommended that none of the loops edit anything about this array though

    nLoops = 10;
    randNums = rand(1,100)
    parfor ii = 1:nLoops
         %do something with randNums(someNum)
    end
    

    Either option is relatively easy. If you are doing an optimization problem, you likely want to ensure that your random numbers are different though, that is kind of the point of optimization.