mivandev
mivandev

Reputation: 321

Export one dataset each replication from a Parameter Variations experiment

In a parameter variation experiment I am plotting data from a dataset located in Main. Like this: enter image description here

I use the following code to display data in the plot:

if(root.p_X == 7) {
plot.addDataSet(root.ds_waitTime,
    "X = " + root.p_X,
    transparent(red, 0.5), true, Chart.INTERPOLATION_LINEAR,
1, Chart.POINT_NONE);
else if(root.p_X == 14) {
plot.addDataSet(root.ds_waitTime,
    "X = " + root.p_X,
    transparent(red, 0.5), true, Chart.INTERPOLATION_LINEAR,
1, Chart.POINT_NONE);

My question is related to exporting the underlying data. I am not sure what the best way is to export the data. For each simulation run, I want to export dataset root.ds_waitTime to a csv or excel file, annotated with the value of root.p_X == 14. I know you can add datasets to a 2d histogram dataset, but this also transforms the data, so that doesn't seem a good option. Also, I do not have set up an external database, and I'd prefer not to.

Is it possible to save root.ds_waitTime to column 1 and 2 for simulation run 1, to column 3 and 4 for simulation run 2, and so on?

Upvotes: 1

Views: 334

Answers (1)

Emile Zankoul
Emile Zankoul

Reputation: 2213

In the Experiment window do the following:

  1. Add a variable called i and assign 0 as its initial value
  2. Add an Excel File element and link it to the file you want on your PC in its properties. Name the block "excelFile" which is the default name.

Then, in the Experiment window properties under "Java Actions" in the field "After Simulation Run", use the following code (where dataSet is the name of your dataset):

excelFile.writeDataSet(dataSet, 1, 2, 1 + i*2);
i++;

This way, after each simulation run, the dataset is written to the next 2 rows.

The syntax I used refers to the:

  • Sheet Number i.e. 1
  • Row Number i.e. 2; I use 2 to leave space for a top row in which you can add in your headers outside AnyLogic, to the file directly
  • Column Number i.e. 1 + i*2

For the header row, you can alternatively add the following code:

excelFile.setCellValue(dataset name, 1, 1, 1 + i*2);

Upvotes: 2

Related Questions