Global State

Last updated on 29th April 2024

The global state is an object that can be read by all agents. If a model doesn't need global variables, the model and all of its agents must be parameterized with the GlobalState type to compile correctly. If a model does need global variables, create a class for those global variables which extends GlobalState, then parameterize the model and its agents with that type.

// Model and agent using GlobalState.
public class MyModel extends AgentBasedModel<GlobalState> {}
public static final class MyAgent extends Agent<GlobalState> {}

// Custom globals class, with model and agent using this custom class.
public static final class Globals extends GlobalState {
     public float preference = 0.35f;
}
public class MyModel2 extends AgentBasedModel<Globals> {}
public static final class MyAgent2 extends Agent<Globals> {}

Use the getGlobals() function, which is part of the GlobalState, to get global values and update them as necessary. Do not attempt to change the values of globals from inside agents. Since agent actions happen in parallel, updating global values from agents may result in unexpected behavior.

Annotations can either set global state values from the console or report those values to the console.

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