Run rng Function Before Each Simulation Run

2 visualizzazioni (ultimi 30 giorni)
Hi,
I would like to run a set of Simulink simulations by parsim function with different random number generator setting in each consecutive run. I tried the way below but it does not work:
numofSims=4;
simIn(1:numofSims)=Simulink.SimulationInput(model);
for i=1:numofSims
simIn(i).PreSimFcn=@(x) rng(i);
end
simOut=parsim(simIn, 'TransferBaseWorkspaceVariables', true, 'showProgress', true);
It gives the error:
'Error executing ''PreSimFcn'' on SimulationInput object. Caused by: Expected input to be one of these types:
Simulink.SimulationInput
Instead its type was struct.'
How can I achieve it? In Simulink model I have an Embedded MATLAB Function block and in that block I call rand function to generate predictable (due to pre-run rng function) random number.
Thanks in advance,
Baris
  1 Commento
Elliott Rachlin
Elliott Rachlin il 23 Ott 2018
Similar question: I am creating a SimEvents model programmatically and repeatedly running it multiple times from a script that executes the following sequence: rng(x);sim(xxx) where x is the number of the simulation being run (and xxx is all the parameters that sim requires). I find that all simulations give the same result, apparently ignoring the rng(x) command given just prior to each simulation made with sim(xxx). Does sim(xxx) ignore the current seed and reinitialize the random number generator? If so, how can I set a seed in a SimEvents model (which is initiated by sim(xxx))?

Accedi per commentare.

Risposta accettata

Rahul Kumar
Rahul Kumar il 29 Ago 2018
The PreSimFcn expects a SimulationInput object to be returned (in case any output is returned) and in this case 'rng' returns a struct causing the PreSimFcn to error out. You can achieve what you are looking for by defining a local function which does not return any output
numofSims=4;
simIn(1:numofSims)=Simulink.SimulationInput(model);
for i=1:numofSims
simIn(i).PreSimFcn=@(x) seedRNG(i);
end
simOut=parsim(simIn, 'TransferBaseWorkspaceVariables', true, 'showProgress', true);
function seedRNG(seedValue)
rng(seedValue)
end

Più risposte (0)

Categorie

Scopri di più su Run Multiple Simulations in Help Center e File Exchange

Prodotti


Release

R2017a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by