Contenido principal

Reproduce Results

Identical Answers with Pseudorandom Numbers

GlobalSearch and MultiStart use pseudorandom numbers in choosing start points. Use the same pseudorandom number stream again to:

  • Compare various algorithm settings.

  • Have an example run repeatably.

  • Extend a run, with known initial segment of a previous run.

Both GlobalSearch and MultiStart use the default random number stream.

Steps to Take in Reproducing Results

  1. Before running your problem, store the current state of the default random number stream:

    stream = rng;
  2. Run your GlobalSearch or MultiStart problem.

  3. Restore the state of the random number stream:

    rng(stream)
  4. If you run your problem again, you get the same result.

Example: Reproducing a GlobalSearch or MultiStart Result

This example shows how to obtain reproducible results for Find Global or Multiple Local Minima. The example follows the procedure in Steps to Take in Reproducing Results.

  1. Store the current state of the default random number stream:

    stream = rng;
  2. Create the sawtoothxy function file:

    function f = sawtoothxy(x,y)
    [t r] = cart2pol(x,y); % change to polar coordinates
    h = cos(2*t - 1/2)/2 + cos(t) + 2;
    g = (sin(r) - sin(2*r)/2 + sin(3*r)/3 - sin(4*r)/4 + 4) ...
        .*r.^2./(r+1);
    f = g.*h;
    end
  3. Create the problem structure and GlobalSearch object:

    problem = createOptimProblem("fmincon",...
        objective=@(x)sawtoothxy(x(1),x(2)),...
        x0=[100,-50],options=...
        optimoptions(@fmincon,Algorithm="sqp"));
    gs = GlobalSearch(Display="iter");
  4. Run the problem:

    [x,fval] = run(gs,problem)
     Num Pts                 Best       Current    Threshold        Local        Local                 
    Analyzed  F-count        f(x)       Penalty      Penalty         f(x)     exitflag        Procedure
           0      200       555.7                                   555.7            0    Initial Point
         200     1463   1.547e-15                               1.547e-15            1    Stage 1 Local
         300     1564   1.547e-15     5.709e+04        1.074                              Stage 2 Search
         400     1664   1.547e-15     1.646e+05         4.16                              Stage 2 Search
         500     1764   1.547e-15     2.262e+04        11.84                              Stage 2 Search
         600     1864   1.547e-15          1680        30.95                              Stage 2 Search
         700     1964   1.547e-15     1.138e+04        65.25                              Stage 2 Search
         800     2064   1.547e-15     1.573e+05        163.8                              Stage 2 Search
         900     2164   1.547e-15     3.676e+04        409.2                              Stage 2 Search
         977     2441   1.547e-15           669        707.8        622.5            0    Stage 2 Local
         980     2525   1.547e-15         529.3          669        39.75            1    Stage 2 Local
        1000     2545   1.547e-15          1634        505.5                              Stage 2 Search
    
    GlobalSearch stopped because it analyzed all the trial points.
    
    2 out of 4 local solver runs converged with a positive local solver exit flag.
    
    x = 1×2
    1.0e-07 *
    
        0.0414    0.1298
    
    fval = 1.5467e-15

    You might obtain a different result when running this problem, since the random stream was in an unknown state at the beginning of the run.

  5. Restore the state of the random number stream:

    rng(stream)
  6. Run the problem again. You get the same output.

    [x,fval] = run(gs,problem)
     Num Pts                 Best       Current    Threshold        Local        Local                 
    Analyzed  F-count        f(x)       Penalty      Penalty         f(x)     exitflag        Procedure
           0      200       555.7                                   555.7            0    Initial Point
         200     1463   1.547e-15                               1.547e-15            1    Stage 1 Local
         300     1564   1.547e-15     5.709e+04        1.074                              Stage 2 Search
         400     1664   1.547e-15     1.646e+05         4.16                              Stage 2 Search
         500     1764   1.547e-15     2.262e+04        11.84                              Stage 2 Search
         600     1864   1.547e-15          1680        30.95                              Stage 2 Search
         700     1964   1.547e-15     1.138e+04        65.25                              Stage 2 Search
         800     2064   1.547e-15     1.573e+05        163.8                              Stage 2 Search
         900     2164   1.547e-15     3.676e+04        409.2                              Stage 2 Search
         977     2441   1.547e-15           669        707.8        622.5            0    Stage 2 Local
         980     2525   1.547e-15         529.3          669        39.75            1    Stage 2 Local
        1000     2545   1.547e-15          1634        505.5                              Stage 2 Search
    
    GlobalSearch stopped because it analyzed all the trial points.
    
    2 out of 4 local solver runs converged with a positive local solver exit flag.
    
    x = 1×2
    1.0e-07 *
    
        0.0414    0.1298
    
    fval = 1.5467e-15

Parallel Processing and Random Number Streams

You obtain reproducible results from MultiStart when you run the algorithm in parallel the same way as you do for serial computation. Runs are reproducible because MultiStart generates pseudorandom start points locally, and then distributes the start points to parallel processors. Therefore, the parallel processors do not use random numbers.

To reproduce a parallel MultiStart run, use the procedure described in Steps to Take in Reproducing Results. For a description of how to run MultiStart in parallel, see How to Use Parallel Processing in Global Optimization Toolbox.

See Also

Topics