2

In my anylogic Project, I want to terminate my execution and run the simulation for N times. in each of the simulation I store my output in an excel file which depends on the run count. Instead of stopping and running by my click, I want to do it automatically. How can I do that? I try to use an event and write by while loop (myparm<=N) and in loop I wrote getEngine().run, but it didn't work! if it is possible please help me.

Thanks

  • 1
    Do you really want to run the model multiple times from a 'single-run' Simulation experiment? If so, why? Just to avoid reading inputs / initialising more than once? (There are other ways to get round that, but you'd only typically care if the set up was very computationally expensive.) The answers given assume that you want to do this special case, rather than using multi-run experiments (like Parameter Variation) which are designed for this purpose. – Stuart Rossiter Jan 28 '17 at 13:54

2 Answers2

2

Below is an overview of a methodology of how you can do it using the existing simulation framework used by AnyLogic

You need to make use of the simulation setup in order to run multiple runs of the model and save the output. My suggested setup will be the following:

Have a button on your Simulation Experiment page (The first page you see when running the model) that you will use to start off the multiple model runs. In here you set the engine to not run in real time mode by using

getEngine().setRealTimeMode(false);

you might also want to set the initial seed and some other model parameters that you might also want to change and perhaps save after model execution. When you have setup the model the way you want use run() to start running the model.

Now under the Simulation Experiment setup page under the 'Java actions' section you need to specify what the model must do after it finished running the model. In the 'After simulation run' section write some code to save the data from the model into your Excel files. To access variables and objects from the model use root, e.g.

saveSomeData(root.myDataset);

where saveSomeData is a function on the Simulation page to save my data set found on the model, called myDataset, to an Excel file. It would be great to also save the seed and the specific parameters, if you changed any, to the Excel file for future reference.

Once you have saved the data output from the model you can specify a new seed and perhaps change parameters again and then call the run() again to run the model for another iteration. When the model has finished running it will again call the 'After simulation run' code here, so do put a stop condition otherwise it will just continue running one iteration after the other. You can access the number of model runs by using

getEngine().getRunCount()

Also, your model needs to have some stop condition, otherwise once it starts running it will never stop. You can specify this in the Simulation Experiment page under the 'Model time' section or programatically in your model using

finishSimulation();
Jaco-Ben Vosloo
  • 3,770
  • 2
  • 16
  • 33
1

In order to run the model cyclically, please use the following code in the Action field of a timeout triggered event or On destroy field of the top-level agent:

new Thread(){
    public void run(){
        // stops the model
        getExperiment().stop();
        try {
            // delay
            this.sleep(1000);
        } catch(Exception e) {};
        // runs it again
        ((Simulation) getExperiment()).button.action();
    }
}.start();

The model results should be written to the Excel file before executing this code. As Jaco-Ben suggested, you can specify getEngine().getRunCount() as condition of restarting the Simulation experiment.

Tatiana Gomzina
  • 274
  • 1
  • 4