Collecting & Outputting Data from AnyLogic...–More flexible than string output to file –Can...

Post on 25-Aug-2021

1 views 0 download

transcript

Collecting & Outputting Data from AnyLogic

Nathaniel Osgood

March 15, 2011

Recording of Results

• A frequent modeler need is to record some components of model state over time – State variables (e.g. stocks) – States of agents – Summaries of model state – We informally term this a “trajectory file”

• Trajectory recording is only supported by AnyLogic Professional

• AnyLogic does allow for – Definition of DataSets that record recent values of

parameters – Statistics summarizing model state – Reporting on values of data sets as a graph or table

Techniques for Outputting Data

• Ad-Hoc Exports from variables

• Manual copies from visible datasets

• Export to files

• Writing to console

• Export to databases

• [AnyLogic Professional] Dataset archiving

• Capturing images of graphs

Cross-Method Output Tips

• A convenient mechanism is to periodically output data using events (e.g. every time unit)

• Beyond output, be sure to save information on context of run

– Model version (Use unique id that increment whenever change model)

– Parameter assumptions

– Intention

• Think carefully about whether want to save away intermediate data

Hands on Model Use Ahead

Load Sample Model: SIR Agent Based Calibration

(Via “Sample Models” under “Help” Menu)

Techniques for Collecting & Outputting Data

• Ad-Hoc Exports from variables

• Pre-Prepared methods – Statistics

– Charts

– Manual copies from visible datasets

– Export to files

– Writing to console

– Export to databases

– [AnyLogic Professional] Dataset archiving

– Capturing images of graphs

Add an Experiment

Add an Experiment

Save the Resulting Model (To Avoid Overwriting the Other Model)

Run the Experiment (To Verify Functionality)

Click on Variable “nInfectious”

Graph of Variable

Right-Click to Copy the Numeric Data

Pasting Into Excel

Press Red “Stop” Button to Terminate Execution

Techniques for Collecting & Outputting Data

• Ad-Hoc Exports from variables

• Pre-Prepared methods – Statistics

– Charts

– Manual copies from visible datasets

– Export to files

– Writing to console

– Export to databases

– [AnyLogic Professional] Dataset archiving

– Capturing images of graphs

Statistics & Charts • A population of agents can have associated statistics

that calculate values

• Examples of things that can be computed with using AnyLogic’s statistics – Count of agents in the population for which certain

condition (“predicate”) evaluates to true

– Function of the values of some expression over the population • Maximum value

• Minimum value

• Average value

• Sum (total) over population

– Statistics can be defined as properties of the population

Select “People”, and Choose “Statistics”

Also, Expand “Statechart” Under “Person”

Click “Add Statistics”

Fill in the “Condition” (Predicate) on Person

Continue Typing

Full Expression

Expression: item.statechart.isStateActive(Person.Susceptible)

Example Statistics

The population in which the statistics are to be calculated

What statistics we wish to calculate

Name the Statistic “countSusceptible”

Run the Model, and Click on “people” The Statistic should be Visible

Close the Model after you’re done

Drag a “Time Plot” from the Palette to the “Main” Canvas

Enlarge the Chart

Click “Add Data Item”

Put in “people.” and Press Ctrl-Space

Choose “Count Susceptible”

Now Run the Model

Notice that this only goes back to time 100

Stop the Simulation, and Click on the Plot. Change Time Window & Display Size to 200

This Captures the Full Time Range

Techniques for Collecting & Outputting Data

• Ad-Hoc Exports from variables

• Pre-Prepared methods – Statistics

– Charts

– Manual copies from visible datasets

– Export to files

– Writing to console

– Export to databases

– [AnyLogic Professional] Dataset archiving

– Capturing images of graphs

Datasets

• Datasets store recent values of some quantities from the model

• Datasets can be exported easily using custom code

– This can simply call the dataset’s to string method

Output: Datasets

Run the Experiment & Click on “Infectious DS”

Click on “InfectiousDS” to See Data in Dataset

Right Click and Select “Copy”

Call Up Excel and Paste into It

Dataset Properties

Chart Use of Datasets

Ad-hoc Export

Begins as a Small Chart

Copying Data

Data Exported from Ad-Hoc Chart

Techniques for Outputting Data

• Ad-Hoc Exports from variables

• Manual copies from visible datasets

• Capturing images of graphs

• Export to files

• Writing to console

• [AnyLogic Professional] Dataset archiving

• Export to databases

Manual Output from Datasets

Right Clicking Gives Context Menu

Copied Data Can be Pasted into Excel

Declaratively Specifying Datasets

Supported Dataset Types

• Simple

– holds values only -- no timestamps

• Timed

– holds values and timestamps

• Phase

– holds pairs of values but no timesamps

• Histogram

– can define bins for data set

– data set will record # falling in each bin

Techniques for Outputting Data

• Ad-Hoc Exports from variables

• Manual copies from visible datasets

• Capturing images of graphs

• Export to files

• Writing to console

• [AnyLogic Professional] Dataset archiving

• Export to databases

Output to Console

• Pros – Easy to program

• ActiveObject.traceln(Stringstr) outputs string to console

• System.out.println(Stringstr)

– Readily visible

– Copy & Paste to another document

• Cons – May be mixed with other output (easy to miss other

output)

– Limited length

– Depends on memory to copy

Techniques for Outputting Data

• Ad-Hoc Exports from variables

• Manual copies from visible datasets

• Capturing images of graphs

• Export to files

• Writing to console

• [AnyLogic Professional] Dataset archiving

• Export to databases

Data Output to File

• Pros

– Simple to perform

– Relatively easy to import into e.g. Excel, R, etc.

– Files can be readily archived

• Cons

– Awkward to draw combine from multiple files

– Denormalization: Requires either

• Duplication of scenario-wide information (e.g. parameter values) on each row

• Separate header section & later section

Example code to Export Dataset to File

FileOutputStream fos = new FileOutputStream(“Filename”);

PrintStream p = new PrintStream(fos);

p.println(datasetName.toString()); // outputs tab delimited values

Techniques for Outputting Data

• Ad-Hoc Exports from variables

• Manual copies from visible datasets

• Capturing images of graphs

• Export to files

• Writing to console

• [AnyLogic Professional] Dataset archiving

• Export to databases

Output to Databases: Tradeoffs • Pros

– More flexible than string output to file

– Can query from diverse tools (e.g. excel, R, SPSS, SAS, etc.)

– Can easily clean up

– For larger databases

• Transactional (either writes entirely or not at all)

• Can query from remote machines

• Cons

– More programming

– Need to set up a database

Output to Databases: Steps

• One Time:

– Install database on computer

– Add reference to database libraries

• Each time during simulation

– Open database connection at start of model

– Optionally, “insert” model version & parameter information into the database

– Periodically during simulation

• “insert” values into databases

– At end of model execution, close database connection

Database Dependencies (MySQL database)

Example Simple Database Class

Setup for Database Class

Example Database Output Code A database query language (SQL) statement

Requesting that the database class execute the SQL statement

Checking to make sure that the insert worked properly