Last updated on 16th July 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 MyGlobals extends GlobalState {
public float preference = 0.35f;
}
public class MyModel2 extends AgentBasedModel<MyGlobals> {}
public static final class MyAgent2 extends Agent<MyGlobals> {}
Use the getGlobals()
function, which is part of the GlobalState
, to get global values and update them as necessary.
Annotations can either set global state values from the console or report those values to the console.
public static final class MyGlobals extends GlobalState {
@Input public float preference = 0.35f;
}