Usage

Context Hierarchy

Context Hierarchy

For most models this context hierarchy is convenient. However, it is possible to deviate from this standard (see configuring the context classes to use).

Initialisation

SesamGIM is component based, i.e. the initialisation process is divided in several tasks, and for each task there are components. The user chooses the component to use by setting the component object or a class object as particular initialiser parameter (depending on the kind of component, see below). SesamGIS then uses the object or instantiates the initialiser of this class and uses it for the specific initialisation step. Thus, it is possible to define custom initialisers and set their instance or class as parameter. In the latter case they need to be available on the class path.

Initialiser are responsible for the particular initialisation step whereas InitialiseServices always return an instance of a specific kind of class, e.g. agents. For example, an instance of GimAreaInitialiser reads shapefiles and calls a GimAreaInitialiseService to provide a concrete instance of GimAreaContext.

UML Sequence Diagram of SesamGIM Initialisation

Init SesamGIM Manager

Let SesamGIM know about the root context of the RS model (the SesamGIM manager is then initialised automatically):

GManager.getMan().setRootContext(rootContext);

NOTE: This getMan() method may not be called before parameters are set up because it calls GManager#init() which initialises random number generators and uses random seed parameters!

Run the initialisation

Finally call this code to start the initialisation based on the initialisation components defined above:

GimModelInitialiser<AgentType> initialiser = new GDefaulModelInitialiser<AgentType>();
initialiser.initAll();

Parameter Settings

Configure the initialisation

The most convenient way is to use the GimInitialiser<AgentType> and to assign all initialiser and initialise service parameters directly to the parameters defined in SesamGIM (see below).

These initialisation components are used and may be defined:

  • GimAreaInitialiser to initialise the areas for the model (set as class parameter: GInitialisersPa.INITIALISER_AREA).
  • GimMarketCellInitialiser to initialise the market cells for the model (set as class parameter: GInitialisersPa.INITIALISER_MARKETCELL).
  • GimMarketCellInitialiser to initialise the group contexts below market cell context. Set the class parameter GInitialisersPa.INITIALISER_GROUP. If set to null (default), group context are not initialised.
  • GimAgentInitialiser to initialise the agents for the model (set as class parameter: GInitialisersPa.INITIALISER_AGENT).
    • GAreaAgentInitialiser inits agents according to Microm (R) data and determines agent coordinates eg. adjacent to the Market Cell coordinates.
    • GDbAgentInitialiser reads agent milieus and coordinate from the Database
    • GFileAgentInitialiser reads agent milieus and coordinates from a CSV file specified by GBasicPa.AGENT_POS_FILENAME.
    • GPlzAreaAgentInitialiser
    • GGroupAreaAgentInitialiser
    • GSimpleAgentInitialiser

Configuring the initialise services (agents and contexts)

There are initialise services for agents and each context layer. If the model wants to use a specific service implementation it needs to assign the particular service as a parameter. This is mandatory for the agent initialise service! If no services are defined for the contexts, default implementations are used.

  • GimAreaInitialiser to initialise the areas for the model (set as class parameter: GInitialisersPa.IS_AREA).
  • GimMarketCellInitialiser to initialise the market cells for the model (set as class parameter: GInitialisersPa.IS_MARKETCELL).
    • GimGroupInitialiser to initialise the group contexts for the model (set as class parameter: GInitialisersPa.IS_GROUP).
  • GimAgentInitialiser to initialise the agents for the model (set as class parameter: GInitialisersPa.IS_AGENT).

Additional important parameters

DB settings

Database access is important to load market cell milieu data.

PmParameterManager.setParameter(PmFrameworkPa.DB_SETTINGS_FILE, "./config/param/DBSettings_MySQL3.xml");
I do not want to initialise Market-Cell-Shapes

In order to prevent SesamGIM from reading market-cell shapes (maybe because you do not have one) set this parameter:

PmParameterManager.setParameter(GMilieuPa.ALLOW_AGENTS_TO_LEAVE_MC_BOUNDARIES, Boolean.TRUE);

Storing Agents into DB

Agents can be stored into DB including their milieu and GIS position using GAgentDbStoring, whereas agents is a collection of GimMilieuAgents and 99 is the ID that is associated with the data record:

GAgentDbStoring.storeAgents(agents, 99);

Of course, the procedure needs to be called after agent initialisation. It uses GSqlPa.TBLNAME_INIT_AGENTS as target table name. Furthermore, the column names need to correspond with the ones defined in DB and can be set using ParMa (see ). An SQL creation script can be found in ./config/test/sql/TestAgentStorageSQL.sql.

The according test class is de.cesr.sesamgim.testing.util.GAgentStoringTest.

Initialising Agents from DB

Agent can be initialised from DB using the GDbAgentInitialiser:

PmParameterManager.setParameter(GInitialisersPa.INITIALISER_AGENTS, GDbAgentInitialiser.class);

The database should provide agent milieu and GIS positions. SesamGIM then assigned contexts according to GIS coordinates. The ID that is associated with the desired agent record needs to be specified in GBasicPa.INIT_PARAM_ID. Further parameters regard the structure of the table:

  • COL_NAME_RECORDIDG
  • SqlPa.TBLNAME_INIT_AGENTS
  • GSqlPa.COL_NAME_AGENTID
  • GSqlPa.COL_NAME_MILIEU_GROUP
  • GSqlPa.COL_NAME_X_COORD
  • GSqlPa.COL_NAME_Y_COORD

    If the model does not consider GimMarketCellContexts the GMarketCellInitialiser should be set to null:

    PmParameterManager.setParameterValue(GInitialisersPa.INITIALISER_MARKETCELL, null);
    

Special Applications

Assigning agents directly to AreaContext (without MarketCell - and GroupContexts)

GGeoAreaContextAssigner.assignAgentsToAreaContexts(agents);

Custom SQL statements for GimMarketCellInitialiser

Since the GMarketCellInitialiser has a protected method generateSqlStatement(), one may create a custom subclass of GMarketCellInitialiser and override this method in order to easily use custom sources of the data. This is sometimes beneficial when the effort to compute the data is very high and one wants to use specific MySQL tables per simulated area.

Follow these steps to apply a custom initialiser:

  1. Run a "normal" simulation and log the SQL-statement generated in GMarketCellInitialiser. Therefore, add the following line to your log4j configuration:
    log4j.logger.de.cesr.sesamgim.init.mc.GMarketCellInitialiser = DEBUG
    
  2. Create a MYSQL table using the SQL statement from step 1:
    CREATE TABLE <name> select <statement from step 1>
    
  3. Create your custom GimMarketCellInitialiser by extending GMarketCellInitialiser and override generateSqlStatement():
    protected String generateSqlStatement(Context<Object> rootContext) {
            return  "SELECT * FROM <your MYSQL table from step 2>" +
                                    " ORDER BY mz_id, num_persons, year, milieu_group_id";
            }
    
  4. Choose your custom GimMarketCellInitialiser for GInitialisersPa#INITIALISER_MARKETCELL

Random Numbers and Streams

GesamGIM uses its own instance of URandomService. Basically, it manages different random number generators:

  • RANDOM_STREAM_INITIALISATION
  • RANDOM_STREAM_AREA_INITIALISATION
  • RANDOM_STREAM_GROUP_INITIALISATION
  • RANDOM_STREAM_AGENT_INITIALISATION
  • RANDOM_STREAM_POP_UPDATE
  • RANDOM_STREAM_AGENT_AGGREGATION

By default, and when only a global random seed (RANDOM_SEED) is defined, these point to a single global random number generators (RND_STREAM). However, these streams can be fed by distinct random seeds:

  • RANDOM_SEED_INITIALISATION
  • RANDOM_SEED_AREA_INITIALISATION
  • RANDOM_SEED_GROUP_INITIALISATION
  • RANDOM_SEED_AGENT_INITIALISATION
  • RANDOM_SEED_POPDYN
  • RANDOM_SEED_AGENT_AGGREGATION

To influence the random number generation of certain parts of SesamGIM change the according random number seed, e.g.:

PmParameterManager.setParameter(GRandomPa.RANDOM_SEED_INITIALISATION, new Integer(2));

Furthermore, it is possible to register custom random number generators for SesamGIM:

GManager.getURandomService().registerGenerator((String) PmParameterManager
        .getParameter(GRandomPa.RANDOM_STREAM), new MersenneTwister(3));

Alternatively, you may register your custom random number generator and adapt the stream name parameter accordingly (the last part is mandatory - otherwise you will replace more general generators). See the URaNuS manual and the next section for further information.

Behaviour of Random Number Generator according to Parameter Definitions

There is a kind of complex algorithm on what happens for the SesamGIM parts (creating contexts and agents, population updating, aggregation):

  1. If all parameters (seed, distributions, generators) are left to their default values, SesamGIM generates one random stream with the default random seed (1) which is used by all processes.
  2. In case there is only a general random seed defined (GRandomPa#RANDOM_SEED) this is used to instantiate a new random number generator used for all parts in case there is no random generator defined as well.
    PmParameterManager.setParameter(GRandomPa.RANDOM_SEED, new Integer(1));
    
  3. If there is a general random number generator defined (GRandomPa#RANDOM_STREAM) this is used for all parts. Definitions of GRandomPa#RANDOM_SEED are ignored in this case. However, the defined generator should be based on the defined random seed.
  4. In case there is a random seed defined for any partial initialisation (e.g. GRandomPa#RANDOM_SEED_AGENT_INITIALISATION) this is used to instantiate a new random number generator used for this part in case there is no random generator defined as well.
    PmParameterManager.setParameter(GRandomPa.RANDOM_SEED_AGENT_INITIALISATION, new Integer(2));
    
  5. If there is a random number generator defined for any partial initialisation (e.g. GRandomPa#RANDOM_STREAM_AGENT_INITIALISATION) this is used for this specific part. Definitions of e.g. GRandomPa#RANDOM_SEED_AGENT_INITIALISATION are ignored in this case. However, the defined generator should be based on the defined random seed for the specific part.
    GManager.getURandomService().registerGenerator((String) PmParameterManager
            .getParameter(GRandomPa.RANDOM_STREAM_AGENT_INITIALISATION), new MersenneTwister(3));
    
  6. If for any part (area, group, agents) a distribution is registered for the predefined distribution name (see GRandomPa) the registered distribution is used.

    NOTE: To achieve independent random number streams for different parts it is sufficient to set the random seeds accordingly, even if they are set to the same value.

    For further details, see URaNuS documentation and/or the source of GManager.init().

Group Initialisation

SesamGIM support the initialisation of Group context to manage agents as sub-contexts of GimMilieuContexts. In order to initialise group contexts you need to:

PmParameterManager.setParameter(GInitialisersPa.INITIALISER_GROUP, GGroupOrganiser.class));
PmParameterManager.setParameter(GInitialisersPa.INITIALISER_AGENTS, GGroupAreaAgentInitialiser.class);

The GGroupAreaAgentInitialiser or a sub-class as GimAgentInitialiser is required to set the GimGroupContext st the agents.

There is a random stream identifier GRandomPa.RANDOM_STREAM_GROUP_INITIALISATION that can be set to any available random stream and defaults to GRandomPa.RANDOM_STREAM_INITIALISATION.

Spatial Information

The root geography is accessible through the GManager, and geometry objects can be obtained be calling:

GManager.getMan().getRootGeography().getGeometry(object);

Reset

Do not forget to reset the manager after the simulation (in case you do batch runs):

GManager.reset();

Validation and Debugging

Logging Framework

SesamGIM applies the Apache Log4J-Framework. To use it, copy ./config/log4j/log4j.properties to a place of your choice, adapt it to your needs and make it available in the classpath. See Apache Log4J for more information.

Checks

To test the resulting model setup, there are two features:

Checking Context Hierarchy

This tools creates an GraphML file that can be layout as hierarchy, e.g. in yED.

GContextUtils.printContextHierarchy(GManager.getMan().getRootContext(), new File("./output/contexts/ContextHierarchie.graphml"));
Context Hierarchy

Use node attributes as labels in yed as follows:

  1. Click Edit, Properties Mapper.
  2. Click the little plus sign under Configurations, and then ‘New Configuration for Nodes.’
  3. Now click the plus sign next to ‘Mappings’ and choose the fields for the mapping as desired
  4. You may now select all nodes (CTRL+A) and adapt "Positioning" and "Rotation" in the properties window
Checking GIS locations / Ouput Shapefiles

You may output the contents of the GIS geography as shape files and control it in an GIS editor, e.g. QuantumGIS):

GShapefileService.writeShapeFile(GManager.getMan().getRootGeography(), "./output/gis/");
Resulting Geography

It is also possible to specify the classes of objects in the geography that shall be output by using the feature attribute:

List<String> features = Arrays.asList(
                "de.cesr.model.agents.Household",
                "de.cesr.model.environment.ContextEnvironment");
GShapefileService.writeShapeFile(GManager.getMan().getRootGeography(), "./output/gis/", features);

NOTE: The procedure calls all getter methods without parameters that return a simple data type including String. To prevent side effects you should ensure that getter methods of classes of objects in the geography are correctly implemented, i.e. do not cause any changes upon their call. Also consider super classes!

There is a QGIS project file ./config/qgis/TestOutputShapes.qgs. However, you need to run the JUnit test in de.cesr.sesamgim.testing.init.plz.GPlzInitialiserTest to produce required shape files.