Mimimize initializing time for SimEvent model

I'm working on a project where I'm supposed to simulate a production facility using SimEvent.
The model it self works as intended but as it has grown the initializing time is extremely long in comparison to runtime. At the moment it takes takes about 100-120 seconds to initialize, compiling another 20 seconds and the runtime about 0.2 seconds. So the set simulation time does not affect, e.g. I can simulate one year or 20 years for about the same time.
In the model I make use of multiple "simulink function" (about 35 of them) all of them contains MATLAB Functions, which are called in order to open Entity gates and to keep a certain amount of entities in storage (just like in this example: https://se.mathworks.com/help/simevents/ug/use-an-attribute-queue-to-model-a-storage-tank.html ) or to define the assemle order.
For the assembly I use Composite Entity Creators along with resource Aquires/Releasers and Entity Servers. Each entity is a part of the final product which adds up to a total of 35, that are composed together along the manufacturing. That is the model in a nut shell.
I've tried to use the performance advisor and disabled Zero Crossing, changed to discrete variable step solver and only to compile when dependencies are found/known but it only helped a little. However, when change parameters by just running a few lines like mark and press F9, the initialize time takes just about 20 seconds but if I run the script it will take about 100-120 seconds (the script only contains assignment to variables).
This would be fine if I only wanted to try a few parameter change but my task involves a Sensitivity analysis which takes forever since each iteration takes 100-120 seconds (Parallelization does help thought), here I want to use the Sensitivity Analyzer App (sim. Design Optimization toolbox) since I would like to do a few hundred simulations.
Either way, is there any way for me to speed up the initializing, I'm asking here because I really dont know what Simulink/Matlab is doing while initializing and therefore is it hard to improve.

 Accepted Answer

If you are doing a sensitivity analysis and you execute hundreds of simulations, the correct workflow is to use SimulationInput objects.
There you set parameter values for each of your experiments in one SimulationInput object, run all of the simulations with one line of code, and get the results in one SimulatinOutput object.
In addition to this point that this is how multiple Simulink simulations are intended to run, there is a feature that is there for your problem: Fast restart. Turning fast restart on causes Simulink to compile the model once, and use the compiled model for the whole experiments in the SimulationInput object. Since model compilation takes a lot of time, often more than model execution, especially in large complex models, fast restart helps A LOT.

4 Comments

Thanks for the tips!
I will look into the use of SimulationInput Objects. But what you mean here is to not use the Sensitivity Analyzer tool but instead to it all manually?
However, I've tried to use Fast restart before but it is not compatable with a lot of blocks in the SimEvent toolbox as most of the blocks uses nontunable parameters. Therefore it needs to recomplie for each parameter change.
Is there any way of getting around that problem? The only lead I've at the moment is to use MATLAB-discrete event system-block. But here you're supposed to be able to define whether the parameters are tuneable or not? This is however quite cumbersome since I've never used it before and would prefer to use standard blocks.
You are right only when you enable fast restart via this button
And run the model like this:
for i1 = 1:NumExperiments
Variable1 = 1;
Variable2 = 2;
SimOut = sim ('Model');
end
This is the worst way to run multiple simulations, and the wrong way to run multiple simulations using fast restart. The standard way that Simulink is designed to run multiple simulations, and I mentioned as the SimulationInput methodology, is something like this:
SimIn = Simulink.SimulationInput ('Model');
for i1 = 1:NumExperiments
SimIn (i1) = SimIn(i1).setVariable('Variable1', 1, 'Workspace', 'Model');
SimIn (i1) = SimIn(i1).setVariable('Variable2', 2, 'Workspace', 'Model');
end
SimOut = sim (SimIn, 'UseFastRestart', 'on');
Using fast restart in the correct way (the second approach) compile the structure of the model once, and change the parameters for each experiment. But if you use it via the first approach (the button in the ribbon), you need to activate and deactivate it for every parameter change.
In addition, if you are using random numbers for intergeneration times, service times, etc, you need some replications for each parameter set. The second approach also gives you control the random number generation seeds, because those seeds must be fixed, predetermined numbers.
Thanks a lot! I will try this out.
I did however try another approach mean while. I used the statistics from each block where I before had parameters that I wanted to change and then did the logics/calculation with standard simulink blocks. Now I can use fast restart and the model just compiles/initialize once and runs extremly fast afterwards! This also works with the Sensitivity Analyzer App/toolbox.
I like the thing you said about controlling the random seed and might go for your approach!
Thanks a lot, again!
I'm glad your problem is solved. I also learned that using Simulink blocks for calculating statistics makes the model run faster.

Sign in to comment.

More Answers (0)

Categories

Find more on Discrete-Event Simulation in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!