Skip to content

agent

Syntax

agent <type> <variable_name>
agent <type> <variable_name1> <variable_name2>...

Parameters

<variable_type>

The type of the variable being declared. Valid variable types are:

Important

For agent-scoped variables, table and list_table variable 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.

Initialization of Agent Variables

When agent-scoped variables are initialized in a variables block, each agent will be evaluated individually. For example, the following initialization will assign each agent a distinct temperature drawn from a normal distribution with a mean of 98.6 and a standard deviation of 1.0.

variables {
    agent my_temperature
    my_temperature = normal(98.6, 1.0)
}

Examples

simulation {
    locations = none
    start_date = 2020-Jan-01
    end_date = 2020-Jan-02
    default_model = none
}

variables {
    agent numeric my_num
    my_num = 999

    shared numeric first_agent_id
    shared numeric second_agent_id
}

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 {
        if (id() == first_agent_id) then next(FirstAgentState)

        wait(24)
        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

See Also