Model Initialisation

Last updated on 16th July 2024

The model's schema is used to describe the model's data output. It is important to initialise models correctly to ensure that a models schema remains the same while the simulation is being run.

The model#init() method is used to used to initialise and register all model components that might be output, and so therefore need to be present in the schema. The schema should be fixed after the model#init call. All updates to the schema after this call will trigger an error.

Accumulators

Accumulators should be created before or during the model#init() call. Attempted creation of new accumulators after this will throw an exception. To learn more about accumulators creation and use, refer to the Outputting Data section. You can create accumulators during model#init.

Creating Accumulators

@Override
public void init() {
  createDoubleAccumulator("AccumulatorA");
  createLongAccumulator("AccumulatorB");
}

As of v2.5 you can also specify an initial value for accumulators. This can be useful especially for view output of accumulators where the inital value of 0 or NaN would be incorrect.

Creating Accumulators with Initial Value

@Override
public void init() {
  createDoubleAccumulator("AccumulatorA", 0);
  createLongAccumulator("AccumulatorB", variableA);
}

Agents

Agents must be registered during model#init. If you try to generate groups of agents or spawn unregistered agents, then this will throw an exception. The best place to register Agent types is in the init method. Simply call AgentBasedModel#registerAgentTypes and pass your agent types. This method takes a variable number of arguments so you can pass in as many Agents as you need.

Registering Agents

@Override
public void init() {
	registerAgentTypes(AgentA.class, AgentB.class);
}

Links again must be registered during model#init. If you try to generate a group with a link that is not registered or add a link to an agent during a step that has not been registered this will throw an exception. Simply call AgentBasedModel#registerLinkTypes passing in the link types your model uses.

Registering Links

@Override
public void init() {
	registerLinkTypes(LinkA.class, LinkB.class);
}