Skip to main content
Version: 2.0

Global State

The global state is an object that can be read by all agents.

If your model doesn't need to make use of a global state, Agents and the AgentBasedModel need to be typed as GlobalState in order for the model to compile.

Generifying agents with default GlobalState (java)

public class MyModel extends AgentBasedModel<GlobalState> {}

public static final class MyAgent extends Agent<GlobalState> {}

If you have some global state to be shared across the system, create a Globals class that extends GlobalState, and pass as a type to the AgentBasedModel. Agents will now need to be typed as Globals in order to access values inside Globals.

Creating a custom global object(java)

public class MyModel extends AgentBasedModel<MyModel.Globals> {
public static final class Globals extends GlobalState {
public float preference = 0.35f;
}
}
public static final class MyAgent extends Agent<Globals> {}

To get the globals values and update them from inside the model setup or step

Accessing a global from model (java)

public class MyModel extends AgentBasedModel<MyModel.Globals> {
public static final class Globals extends GlobalState {
float fillFactor = 0.25f;
}

public void setup() {
getGlobals().fillFactor = 0.5;
super.setup();
}
}

To get the globals values from inside the Agents

Accessing a global from and agent (java)

public static final class MyAgent extends Agent<Globals> {
private void updateHappiness() {
long similar = // calculate similar
long total = // calculate total

float preference = getGlobals().preference;
this.person.happy = (float) similar / total > preference;
}
}

Important

Do not attempt to change the values of globals from inside Agents. The results may not be as expected.

###Annotations and Global State Annotations can be used inside the global state to set the values of the global state from the console, or to report a global state value.

Annotating a global variable(java)

public static final class Globals extends GlobalState {
@Input public float preference = 0.35f;
}