Increasing simulation time in a Monte Carlo simulation in Matlab/Simulink
Asked Answered
S

1

6

I'm running Monte Carlo simulation for a Simulink model with a Matlab script that looks more or less like this :

model = 'modelName';

load_system(model)

for ii = 1 : numberOfMC
    % Some set_param...
    % Some values are set

    sim(model);
    results{ii, 1} = numberOfMC;
    % ect...
end
close_system(model,0);

As the number of Monte Carlo trials increase, the time of one simulation increases as well like n^2.

Is there a simple explanation for that and is there a solution to have something linear in time?

Thank you!

EDIT:

When I split my simulation in 6 batchs, and I run them in series, the sum of the simulation times is far less than when I run the entire simulation ine one shot.

Stridor answered 4/12, 2013 at 15:5 Comment(13)
Are you preallocating results? Maybe it's growing dynamycally, and that costs timeIsbell
Yes, results is preallocated before the loop starts.Stridor
Do you really use results{ii, 1} = numberOfMC? What's the purpose of that line? numberOfMC seems to be a constantIsbell
To be exact, values located in the Workspace from the simulation are stored in results. numberOfMC is not really tracked, I put it there for the example.Stridor
Well, if your result variables are very large, that could cause a steeper increase of time. Try removing that storage of results and check if it's linear thenIsbell
I tried removing results, but it doesn't affect the simulation time that much.Stridor
I'm highly interested in knowing the answer to that kind of problem as well (memory problem of having the 'sim(model)' inside a loop, loaded before the loop)Perplexed
Is there anything about the parameter changes that makes the system stiff, and hence the solver is having problems? How many time steps are being taken by the model each time through the loop?Anklet
The time steps are variable (Variable-step) because we use a toolbox named TrueTime that must be used with Variable-step. But I don't think the problem is there. When I split the Monte Carlo simulation in 6 batch, the sum of the simulation time is far less than when I run the entire simulation in one shot.Stridor
My bet would be memory issues, if you want to eliminate this see whether the problem still occurs if you don't store the result in the first place, simply remove this line: results{ii, 1} = numberOfMC;. Also confirm that you don't have other growing variables or that you accidentally make the input more complex as you go. It is probably not relevant, does the time also increase like this if you do all simulations in reversed order? Or if you do the full amount of iterations, but each time with exactly the same input?Telethon
My guess is problems with the memory garbage collector. If after a Simulink run, you store variables in the workspace, try the following: clear vars_from_Simulink; memory;Weismannism
@Perplexed If you are actually struggling with this issue, perhaps you could add the minimal relevant code to reproduce this behavior?Telethon
@DennisJaheruddin I use the same kind of approach for running multiple times the same Simulink model. I'm curious at what is causing the OP simulation increase, because I might in the future get the same problems with multiple runs.Perplexed
T
1

As it seems that there is a limit to what one can do without feedback from the asker I will just post my comment as an answer:

My bet would be memory issues, if you want to eliminate this see whether the problem still occurs if you don't store the result in the first place, simply remove this line:

results{ii, 1} = numberOfMC;

Also confirm that you don't have other growing variables or that you accidentally make the input more complex as you go. It is probably not relevant, does the time also increase like this if you do all simulations in reversed order? Or if you do the full amount of iterations, but each time with exactly the same input?

Telethon answered 2/4, 2014 at 9:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.