Developing Control Architectures
A control architecture decides what the agents of a species do at each step. In GAML, the architecture is chosen with the control: facet of a species:
species walker control: fsm {
state resting initial: true {
transition to: walking when: flip(0.1);
}
state walking {
transition to: resting when: flip(0.2);
}
}
Without control:, a species uses reflex: its reflex statements are executed in declaration order at each scheduled step. GAMA provides several other architectures (see Built-in architectures). A plugin can add new ones.
Before you start, read Developing Skills and Developing Statements: an architecture is a special skill, and it usually comes with its own behavior statements (like state for fsm or task for weighted_tasks). For the plugin set-up, see Developing Plugins.
Java classes​
There is no dedicated annotation for architectures. An architecture is a class annotated with @skill that implements gama.api.kernel.skill.IArchitecture. The name of the skill is the keyword used in control:. GAMA recognizes the class as an architecture because it implements IArchitecture.
IArchitecture(gama.api/src/gama/api/kernel/skill/IArchitecture.java) extends bothISkillandIStatement. It is a skill (it can declare attributes and actions) and a statement (it is executed for each agent, and its children are the behaviors of the species).AbstractArchitecture(gama.api/src/gama/api/kernel/skill/AbstractArchitecture.java) gives default implementations of the statement methods.ReflexArchitecture(gama.core/src/gama/gaml/architecture/reflex/ReflexArchitecture.java) collectsinit,abort,reflexandteststatements. Several built-in architectures extend it to keep the reflex behavior while adding their own statements.
How GAMA uses an architecture​
GAMA creates one instance of the architecture for each species (in GamlSpecies), not one per agent. Store the state of each agent in the agent's attributes, as fsm does with the current state. The methods are called in this order:
| Method | When | Use |
|---|---|---|
setChildren(Iterable<? extends ISymbol>) | Once, when the species is built | Receives the behavior statements of the species (reflex, init, state, task...) and stores them. |
verifyBehaviors(ISpecies) | Once, after setChildren | Checks the behaviors and prepares the species (for example, fsm finds the initial state). |
boolean init(IScope) | When an agent is initialized (MinimalAgent._init_) | Runs architecture-specific initialization. Returning false skips initialization of that agent's subpopulations. |
void preStep(IScope, IPopulation) | When the population is stepped, before its agents | Performs population-level work for that step. |
Object executeOn(IScope) | At each step, for each agent (scope.execute(architecture, agent, null)) | Runs the behavior of the agent. Get the agent with getCurrentAgent(scope) (inherited from Skill). |
boolean abort(IScope) | When an agent is killed; also when an experiment agent is disposed | Runs architecture-specific cleanup or final behavior. |
The population schedule and its frequency: determine when preStep and the agent steps run. The experiment's architecture is also aborted when the experiment is disposed.
Example: reflex​
ReflexArchitecture keeps reflex and test statements in the order received. Its executeOn method delegates to the method that runs that list. This complete method is from ReflexArchitecture:
@Override
public Object executeOn(final IScope scope) throws GamaRuntimeException {
return executeReflexes(scope);
}
When you extend ReflexArchitecture, override:
addBehavior(IStatement): keep your own behavior statements, and callsuper.addBehavior(c)for the others;clearBehaviors(): clear your lists, and callsuper.clearBehaviors();executeOn(IScope): callsuper.executeOn(scope)to run the reflexes, then run your own behaviors;verifyBehaviors(ISpecies)if you need to check or prepare something once.
Example: fsm​
FsmArchitecture (gama.core/src/gama/gaml/architecture/fsm/FsmArchitecture.java) declares the state and states attributes, collects state statements, and runs the current state after the reflexes. These complete methods show how it handles state declarations and execution:
@Override
public void addBehavior(final IStatement c) {
if (c instanceof final FsmStateStatement state) {
states.put(state.getName(), state);
} else {
super.addBehavior(c);
}
}
@Override
public Object executeOn(final IScope scope) throws GamaRuntimeException {
super.executeOn(scope);
return executeCurrentState(scope);
}
As in any skill, the getters and setters of an architecture receive the agent as a parameter, because the same instance is shared by all the agents of the species.
Behavior statements​
The behaviors of an architecture are statements of kind ISymbolKind.BEHAVIOR. Their @inside annotation gives the name of the architecture in symbols, so that they are only accepted in species that use it. On FsmStateStatement (gama.core/src/gama/gaml/architecture/fsm/FsmStateStatement.java), this complete annotation restricts state to FSM species:
@inside (
symbols = IKeyword.FSM,
kinds = { ISymbolKind.SPECIES, ISymbolKind.EXPERIMENT, ISymbolKind.MODEL })
The task statement uses the same mechanism with the three task-based architecture names: weighted_tasks, sorted_tasks and probabilistic_tasks.
Example: weighted_tasks​
WeightedTasksArchitecture (gama.core/src/gama/gaml/architecture/tasks/WeightedTasksArchitecture.java) runs reflexes, computes each task's weight and executes the task with the highest weight above Double.MIN_VALUE. The complete executeOn and chooseTask methods are:
@Override
public Object executeOn(final IScope scope) throws GamaRuntimeException {
super.executeOn(scope);
final WeightedTaskStatement active = chooseTask(scope);
if (active != null) { return scope.execute(active).getValue(); }
return null;
}
protected WeightedTaskStatement chooseTask(final IScope scope) throws GamaRuntimeException {
Double max = Double.MIN_VALUE;
WeightedTaskStatement active = null;
for (final WeightedTaskStatement task : tasks) {
final Double weight = task.computeWeight(scope);
if (weight > max) {
active = task;
max = weight;
}
}
return active;
}
SortedTasksArchitecture and ProbabilisticTasksArchitecture extend it. They override chooseTask and setChildren (and executeOn for sorted_tasks).
Existing architectures​
control: | Java class | Plugin |
|---|---|---|
reflex (default) | architecture/reflex/ReflexArchitecture | gama.core |
fsm | architecture/fsm/FsmArchitecture | gama.core |
weighted_tasks, sorted_tasks, probabilistic_tasks | architecture/tasks/...TasksArchitecture | gama.core |
rules | architecture/rules/RuleBasedArchitecture | gama.core |
user_first, user_last, user_only | architecture/user/User...ControlArchitecture (extend FsmArchitecture) | gama.core |
simple_bdi | gama.extension.bdi/src/gama/extension/bdi/SimpleBdiArchitecture.java | gama.extension.bdi |
parallel_bdi | gama.extension.bdi/src/gama/extension/bdi/SimpleBdiArchitectureParallel.java | gama.extension.bdi |
The paths in gama.core are relative to gama.core/src/gama/gaml/. The BDI architecture is a large example of an architecture defined in an extension plugin: it extends ReflexArchitecture, declares many attributes and actions, and comes with its own behavior statements (plan, rule, perceive...).
See also Developing Skills, Developing Statements and the index of annotations.