How to export data from built in database to excel after Parameter Variation Run in AnyLogic? - anylogic

Currently, I am writing various timeMeasureEnd data to excel files after simulation runs during Parameters Variation experiment. I am currently using excelFile.setCellValue(root.timeMeasureEnd.distribution.mean(), 1, 1, row + 1); row++; in the After simulation run field of Parameter Variation Experiment. How can I export a table from the internal database, for example flowchart_stats_time_in_state_log that is cumulative of all of the simulation runs of a parameters variation experiment?

Related

AnyLogic Sensitivity analysis visualization

I am new to this and lost about how to create visualization for the AnyLogic sensitivity analysis. Here is the summary:
I have dataset that captures dependent and independent variables at the end of simulation run (just captures one pair at the end). Trying to vary independent variable to see the impact on dependent variable. The resulting dataset is correct (when I copy and paste in Excel) but the chart looks blank.
Also, output states that it completed 5 iterations but I specified 10 and the data shows that there were in fact 10 iterations.
There are no parameters for the chart data (see the screenshot) but I am guessing it is automatically populated at the end simulation based on the code (also copied below)? Otherwise, I cannot figure out what goes into the chart data (tried to manually enter variables/datasets to no avail).
This is the code after each simulation runs:
Color color = lerpColor( (getCurrentIteration() - 1) / (double) (getMaximumIterations() - 1), blue, red );
chart0.addDataSet( root.died_friend, format( root.SocFriendBrave ), color, true, `Chart.INTERPOLATION_LINEAR, 1, Chart.POINT_NONE );`
I realize this is a very basic question but I am lost and cannot get on the right path based on the help I found. thank you.
Let's say you have 2 experiments: simulation (normal one) and sensitivity analysis (new one)
The only way for the chart to look blank is if your dataset died_friend is empty. This can happen for many reasons, but the reason I suspect it happens here, is because you fill this dataset with information at the end of the simulation run, which means that you are probably using the java actions of the simulation experiment.
The sensitivity experiment DOES NOT read what you write on the java actions of the simulation experiment, so that might be the problem.
If this is not the case, you need to check other possible reasons on why your dataset is empty when you run the sensitivity analysis.
Remember: the fact that your dataset has data in your simulation experiment, does not necessarily mean the dataset will not be empty on the sensitivity experiment.
Since the Sensitivity Analysis experiment is set up via the wizard, it would be better if you showed us how you'd set that up, rather than the resultant AnyLogic-generated code.
From what you've said here, it looks like you may be incorrectly trying to use datasets instead of scalar values. If there's one 'dependent variable' (model output of interest) then the 'independent variable' needs to be a model parameter (parameter of your top-level agent, normally Main, which the experiment will be varying).
So you should specify:
Varying the relevant parameter (looks like you wanted it to be 0, 0.25, 0.5, 0.75, 1 so min 0, max 1 with step size of 0.25)
Producing a chart for the scalar output you want. (If this was, say, a variable called outputValue, you'd use expression root.outputValue when setting up the chart in the wizard.)
Also you should only be getting more than 5 iterations if you set up your parameter variation criteria incorrectly.
(Dataset outputs in the experiment's charts are typically for where your model produces a time series as an output --- i.e., a dataset of sim time vs. value --- and you want the Sensitivity Analysis experiment to show charts with each run's time series as separate lines (i.e., time on the X-axis). Scalar charts are for where the X-axis is the varying parameter and the Y-axis the output of interest.)

How do I create monte carlo results on a graph using parameter variation experiment on anylogic?

I am working on the evacuation time of pedestrians by running 100 runs using monte carlo. I have trouble creating the graphs and am very confused in the steps. Something is missing and I am not sure what it is.
I have created a parameter variation experiment page and included a Histogram2D data and a graph from analysis but I don't know how to read the data I want from the main.
Below image is the data I am trying to acquire from the timeMeasureEnd which isn't linked yet.
I use the code
root.timeEnd
but get this error as shown below
You can directly access the timeMeasureEnd's internal dataset: timeMeasureEnd.dataset. See the AnyLogic example model Measuring Length of Stay, referenced in the documentation.
As for your error, your statement gets interpreted as a variable declaration. As soon as you put some useful code in there it works, eg. a simple assignment to a local dataset in your Experiment class:
dataset = root.timeMeasureEnd.dataset;
To save the longest measured time after each run:
add a dataset, deactivate update automatically
add a integer variable iteration to save the current iteration index
use the following in After Simulation run code:
iteration++;
dataset.add(iteration,root.timeMeasureEnd.dataset.getYMax());

How to obtain certain data from main on a graph using parameter variation in anylogic?

I am having trouble with the coding process or steps to extract data from the main and onto the parameter variation experiment in anylogic. I am currently working on total evacuation time due to random fire obstruction.
For now I have successfully obtained the total max evacuation time for 100 runs in my study but I also need another set of data for the number of exits obstructed during each run. My main has the collection (of 3 exits) availableExits and I can see what is obstructed during the simulation.
Furthermore, I would like to obtain data for the number of people evacuating at a particular time (for example number of pedestrians using exit at 120 seconds). I can see this in main from timeMeasureEnd and creating a histogram distribution graph, which shows number of pedestrians escaping at each time. I've managed to create one in parameter variation but when I run the experiment, I am unable to store or view the data as it keeps changing after every run.
Here is the code from analysis Histogram Data which is entered in after simulation run
data = root.timeMeasureEnd.distribution;
i would recommend to add a dataset to your main which would store all the values you want to keep in parameter variation. Dataset differs from histogram data in a way that it doesnt aggregate, it is just a raw array of values, and later you will not have a problem of "aggregating aggregated data".
So, after each simulation run you can access your dataset in main via "root" reference (as you are already doing it) and loop through it to store all the values one by one.

Some questions about split_train_test() function

I am currently trying to use Python's linearregression() model to describe the relationship between two variables X and Y. Given a dataset with 8 columns and 1000 rows, I want to split this dataset into training and test sets using split_train_test.
My question: I wonder what is the difference between train_test_split(dataset, test_size, random_test = int) vs train_test_split(dataset, test_size).Also, does the 2nd one (without setting random_test=int) give me a different test set and training set each time I re-run my program? Also, does the 1st one give me the same test set and training set every time I re-run my program? What is the difference between setting random_test=42 vs random_test=43, for example?
In python scikit-learn train_test_split will split your input data into two sets i) train and ii) test. It has argument random_state which allows you to split data randomly.
If the argument is not mentioned it will classify the data in a stratified manner which will give you the same split for the same dataset.
Assume you want a random split the data so that you could measure the performance of your regression on the same data with different splits. you can use random_state to achieve it. Each random state will give you pseudo-random split of your initial data. In order to keep track of performance and reproduce it later on the same data you will use the random_state argument with value used before.
It is useful for cross validation technique in machine learning.

Average results from multiple simulations

I have a simulation with a lot of random components, so I would like to run many simulations and average the results (the result is determined by a variable called score).
How would you do this in Netlogo?
Currently I'm working on a program that will export the results to csv, then I plan to use python/excel to average them. I don't like this because I want to run 100+ simulations (so there will be 100+ files)... I'm hoping there is a better solution
EDIT or an implementation of what I described (I have to relearn enough python/vba to solve this, so it's going to take me some time)
This should be simple enough if you use BehaviorSpace.
In your experiment definition, put score in the Measure runs using these reporters textbox and uncheck Measure run at every step.
When you run your experiment, save your results using Table output. It will produce a csv that you can open in your spreadsheet application. From there, producing an average of the score column should be trivial.