agent¶
Syntax¶
Parameters¶
<variable_type>-
The type of the variable being declared. Valid variable types are:
Important
For
agent-scoped variables,tableandlist_tablevariable types are currently disabled, as they would be extremely memory-intensive with fairly ordinary numbers of agents.
<variable_name>- A name to give the newly declared variable. If multiple are present, they must be separated by a space. Variable names must start with a letter and contain no symbols.
Description¶
A scope keyword used to define an agent-scoped variable in a FRED model.
This statement can be used when declaring variables in a variables block.
Prefixing a variable declaration with the agent scope indicates that each
agent in the simulation will have its own copy of the variable, independent of
other agent variables.
Warning
Because each agent will have its own copy of each agent variable, caution should be exercised when working with large numbers of agents, as large amounts of memory may be used when running a simulation.
If you only want one instance of a variable in a simulation, see shared.
Examples¶
simulation {
locations = none
start_date = 2020-Jan-01
end_date = 2020-Jan-02
default_model = none
}
variables {
agent numeric my_num
shared numeric first_agent_id
shared numeric second_agent_id
}
agent_startup {
my_num = 999
}
startup {
# Spawn two agents for use in the simulation. Track their IDs so that we
# can send them to different states in the condition.
first_agent_id = spawn_agent()
second_agent_id = spawn_agent()
}
condition AGENT_DEMO {
start_state = Start
state Start {
wait(24)
next_if(FirstAgentState, id() == first_agent_id)
default(SecondAgentState)
}
state FirstAgentState {
print("First agent has entered first agent state!")
print("First agent my_num is: ", my_num)
my_num = -1
print("First agent my_num is now: ", my_num)
wait(0)
default(Excluded)
}
state SecondAgentState {
print("Second agent has entered second agent state!")
print("Second agent my_num is: ", my_num)
my_num = -1
print("Second agent my_num is now: ", my_num)
wait(0)
default(Excluded)
}
}
Prints the following:
First agent has entered first agent state!
First agent my_num is: 999
First agent my_num is now: -1
Second agent has entered second agent state!
Second agent my_num is: 999
Second agent my_num is now: -1