Demo models‎ > ‎MacroABM‎ > ‎

Data Output

The collector class (MacroCollector.java) and the MacroStatistics class (MacroStatistics.java).


The JAS-mine collector manager is a standard class in a JAS-mine project, which collects statistics and manages the persistence of the simulation outputs to the database or to .csv files. It extends the AbstractSimulationCollectorManager interface and requires, similarly to the Model, the implementation of a buildObjects() method and a buildSchedule() method.

The MacroStatistics class is used to store the macro-level statistics of the simulation (as opposed to the micro-level statistics of the agents).  MacroStatistics is an Entity class (it contains the @Entity annotation), which means that if exporting to .csv files, a separate .csv MacroStatistics file will be created, which will be the location to store the values of the MacroStatistics fields.  In addition, if exporting to the output database, a separate table in the output database will be created to store the field of the MacroStatistics class because it is an Entity class.  The MacroCollector has a MacroStatistics object, which it updates during the simulation, along with managing the export of data from the micro-level agent objects.
 


MacroCollector

The MacroCollector class contains 'GUI parameters' that allow the user to adjust the values of these parameters via the JAS-mine GUI, usually at run-time before the model is built and executed.  The GUI parameters of the MacroCollector class are shown in Box 1.  

 

@GUIparameter(description = "Number of periods during which data won't be recorded")

Integer initialTimestepsToIgnore = 100;


@GUIparameter(description = "Toggle to export snapshot to .csv files")

boolean exportToCSV = true; //If true, data will be recorded to .csv files in the output directory


@GUIparameter(description = "Toggle to export snapshot to output database")

boolean exportToDatabase = false; //If true, data will be recorded in the output database in the output                                         //directory


@GUIparameter(description = "Toggle to export commercial bank data")

boolean exportBankData = true;


@GUIparameter(description = "Toggle to export individual data on firms")

boolean exportFirmsData = true;


@GUIparameter(description = "Set the time between snapshots to be exported to the database and/or .csv files")

Double timestepsBetweenSnapshots = 1.;


Box 1. The GUI parameters of the MacroCollector can be adjusted by the user in the JAS-mine GUI at run-time, before the simulation begins.
 
The initialTimestepsToIgnore field allows the user to set a number of time-steps in which to not export data, if the user wants to ignore the initial transient that is common in such dynamic simulations.  

The exportToCSV and exportToDatabase are boolean fields that allow the user to specify how data should be exported from the simulation.  If exportToCSV is set to true, data will be exported to .csv files during run-time, and if exportToDatabase is set to true, data will be exported to an output database.  See Box 2 for how these fields are used in the code.  Note that in general, exporting to .csv files is quicker than exporting to the output database, because the output database is a relational database that has the option of maintaining relationships between database tables and fields.  So if there are time-constraints to running the simulation, for example in the case of running the simulation many times over a set of parameters (see How to run a simulation many times (design of experiments), the user may find it quicker to set exportToCSV = true and exportToDatabase = false and just use data from the .csv files to analyse the behaviour of the model.

The fields exportBankData and exportFirmsData are boolean fields that allow the user to choose which aspects of the data are exported.  For example, in the simulation there is only one bank, but there are many more firms (the sum of the Capital Goods 'KFirms' and Consumption Goods 'CFirms'), so if the user is only interested in data from the bank, or even just macro level data, the user is likely to find a significant speed up in the simulation run-time by setting exportFirmsData to false.  Note that there is currently no GUI parameter allowing the user to turn off exporting macro level data, other than by not exporting any data at all by setting exportToCSV and exportToDatabase to false.  See Boxes 2 and 3 for how these fields are used in the code.


The buildObjects() method creates several CrossSection objects, which collect specific values from each individual in the population (Box 2).
 
 

public void buildObjects() {

    // Initialize the statistics object

    statistics = new MacroStatistics();

    

    // Create the export objects 

    //Macro-level statistics

    exportStatistics = new DataExport(statistics, exportToDatabase, exportToCSV);

    

    //Micro-level Firm data

    if(exportFirmsData){

exportCFirms = new DataExport(model.getCFirms(), exportToDatabase, exportToCSV);

exportKFirms = new DataExport(model.getKFirms(), exportToDatabase, exportToCSV);

    }


    //Micro-level Bank data

         if(exportBankData) {

exportBank = new DataExport(model.getBank(), exportToDatabase, exportToCSV);

    }

    


    // Create all the cross sectional objects

    csProductivity_kFirms = new CrossSection.Double(model.getKFirms(), KFirm.class, "getProductivityNow", true);

    csMachineProductivity = new CrossSection.Double(model.getKFirms(), KFirm.class, "getProdMachine", true);


    csLaborDemand_cFirms = new CrossSection.Double(model.getCFirms(), Firm.class, "getLaborDemand", true);


    csLaborDemandForProduction_kFirms = new CrossSection.Double(model.getKFirms(), KFirm.class, "getLaborDemandForProduction", true);


    csLaborDemandForRd = new CrossSection.Double(model.getKFirms(), KFirm.class, "getLaborDemandForRd", true);


...

    csUnfilledDemand = new CrossSection.Double(model.getCFirms(), CFirm.class, "getUnfilledDemand", true)


...



    // Create all the objects computing functions on the previous cross-sections (max, mean, sum etc.)

    fMaxProductivity_kFirms = new MaxArrayFunction.Double(csProductivity_kFirms);


    fMaxMachineProductivity = new MaxArrayFunction.Double(csMachineProductivity);

...


    fSumLaborDemand_cFirms = new SumArrayFunction.Double(csLaborDemand_cFirms);


    fSumLaborDemandForProduction_kFirms = new SumArrayFunction.Double(csLaborDemandForProduction_kFirms);


...


    fMeanUnfilledDemand = new MeanArrayFunction(csUnfilledDemand);


...


}

Box 2. The MacroCollector.buildObjects() method.
 
The Collector’s schedule is made up of four processes {DumpStatistics, DumpBank, DumpCFirms, DumpKFirms}, which take place at every simulation period (see Box 3).  These processes specify to export the data of the MacroStatistics class, the Bank class, the set of CFirms and the set of KFirms respectively.  The data is exported ('dumped') either to the output database or to .csv files, depending on whether the collector's exportToDatabase and exportToCSV boolean GUI parameters are set to true or false.   Note that the the CrossSection objects and functions are updated automatically when the charts are updated in the MacroObserver class schedule (see Charts).
 
 

public void buildSchedule() {

EventGroup eventGroup = new EventGroup();


eventGroup.addEvent(this, Processes.DumpStatistics);


if(exportBankData) {

eventGroup.addEvent(this, Processes.DumpBank);

}


if(exportFirmsData){


         // export data on firms only if wiling to obtain individual information on them 

eventGroup.addEvent(this, Processes.DumpCFirms);

eventGroup.addEvent(this, Processes.DumpKFirms);

}

// Note: store information only after the burn-in phase 


getEngine().getEventQueue().scheduleRepeat(eventGroup, initialTimestepsToIgnore, Parameters.COLLECTOR_ORDERING, timestepsBetweenSnapshots);


}

Box 3. The MacroCollector.buildSchedule() method.
 
 
The Collector also implements the EventListener interface, featuring the Enum Processes and onEvent() method (see Box 4).
 
 

public enum Processes {

Update,

TechFrontier,

AggregateComputation,

DumpBank,

DumpInStatistics,

DumpStatistics,

DumpCFirms,

DumpKFirms;

}


public void onEvent(Enum<?> type) {

switch ((Processes) type) {

case Update:

update();

break;

case TechFrontier:

techFrontier();

break;

case AggregateComputation:

aggregateComputation();

break;


case DumpBank:

try{

exportBank.export();

} catch(Exception e){

log.error(e.getMessage());

}

break;


case DumpInStatistics:

dumpInStatistics();

break;

// events to export the data to either the database or the .csv files

case DumpStatistics:

try{

exportStatistics.export();

} catch(Exception e){

log.error(e.getMessage());

}

break;

case DumpCFirms:

try{

exportCFirms.export();

} catch(Exception e){

log.error(e.getMessage());

}

break;

case DumpKFirms:

try{

exportKFirms.export();

} catch(Exception e){

log.error(e.getMessage());

}

break;

}

}

Box 4. Implementation of the EventListener interface in MacroCollector.
 
 
The EventListener implementation contains a variety of processes related to the updating of variables and the exporting of data, such as DumpStatistics, which exports the macro-level statistics, DumpCFirms and DumpKFirms which export data about Consumption Good Firms and Capital Good Firms respectively, and DumpBank, which exports bank data.


Previous: The Bank class        Next: Charts