Utopia 2
Framework for studying models of complex & adaptive systems.
|
The agent manager manages the agents living in a model. More...
#include <agent_manager.hh>
Public Types | |
using | Self = AgentManager< AgentTraits, Model > |
The type of this AgentManager. | |
using | Space = typename Model::Space |
The type of the space. | |
using | Agent = Utopia::Agent< AgentTraits, Space > |
The type of the managed agents. | |
using | Entity = Agent |
Alias for entity type; part of the shared interface of entity managers. | |
using | AgentState = typename AgentTraits::State |
The type of the agent state. | |
using | SpaceVec = SpaceVecType< dim > |
The type of the vectors that represent physical quantities. | |
using | MoveFunc = std::function< void(Agent &, const SpaceVec &)> |
The type of the move function type. | |
using | PosFunc = std::function< SpaceVec(const SpaceVec &)> |
The type of the function that prepares the position of a new agent. | |
using | RuleFunc = std::function< AgentState(const std::shared_ptr< Agent > &)> |
The type of a rule function acting on agents of this agent manager. | |
using | VoidRuleFunc = std::function< void(const std::shared_ptr< Agent > &)> |
The type of a void rule function acting on agents of this agent manager. | |
using | RNG = typename Model::RNG |
The random number generator type. | |
Public Member Functions | |
AgentManager (const Model &model, const DataIO::Config &custom_cfg={}) | |
Construct an agent manager. | |
AgentManager (const Model &model, const AgentState initial_state, const DataIO::Config &custom_cfg={}) | |
Construct an agent manager, using the same initial state for all agents. | |
const auto & | log () const |
const DataIO::Config & | cfg () const |
Return the configuration used for building this AgentManager. | |
const std::shared_ptr< RNG > & | rng () const |
Return a reference to the shared random number generator. | |
const std::shared_ptr< Space > & | space () const |
Return pointer to the space, for convenience. | |
const AgentContainer< Agent > & | agents () const |
Return const reference to the managed agents. | |
const AgentContainer< Agent > & | entities () const |
Return const reference to the entities managed by this manager: agents. | |
IndexType | id_counter () const |
Return the ID counter. | |
void | move_to (const std::shared_ptr< Agent > &agent, const SpaceVec &pos) const |
Move an agent to a new position in the space. | |
void | move_to (Agent &agent, const SpaceVec &pos) const |
Move an agent to a new position in the space. | |
void | move_by (const std::shared_ptr< Agent > &agent, const SpaceVec &move_vec) const |
Move an agent relative to its current position. | |
void | move_by (Agent &agent, const SpaceVec &move_vec) const |
Move an agent relative to its current position. | |
const std::shared_ptr< Agent > & | add_agent (const AgentState &state, const SpaceVec &pos) |
Create an agent and associate it with this AgentManager. | |
const std::shared_ptr< Agent > & | add_agent (const SpaceVec &pos, const Config &custom_cfg={}) |
add_agent overload for auto-constructed agent states | |
const std::shared_ptr< Agent > & | add_agent (const Config &custom_cfg={}) |
add_agent overload for auto constructed states and random position | |
void | remove_agent (const std::shared_ptr< Agent > &agent) |
Removes the given agent from the agent manager. | |
template<typename UnaryPredicate > | |
void | erase_agent_if (UnaryPredicate &&condition) |
Remove agents if the given condition is met. | |
void | update_agents () |
Update the agents. | |
template<SelectionMode mode, class... Args> | |
AgentContainer< Agent > | select_agents (Args &&... args) |
Select agents using the Utopia::select_entities interface. | |
AgentContainer< Agent > | select_agents (const Config &sel_cfg) |
Select entities according to parameters specified in a configuration. | |
SpaceVec | displacement (const std::shared_ptr< Agent > &a, const std::shared_ptr< Agent > &b) const |
Returns the (shortest) displacement vector between two agents. | |
template<class NormType = std::size_t> | |
double | distance (const std::shared_ptr< Agent > &a, const std::shared_ptr< Agent > &b, const NormType p=2) const |
Returns the (shortest) distance between two agents. | |
AgentContainer< Agent > | neighbors_of (const std::shared_ptr< Agent > &agent, const double radius) const |
Returns a container of all agents within a certain radius. | |
Static Public Attributes | |
static constexpr DimType | dim = Space::dim |
The dimensionality of the space. | |
Private Member Functions | |
SpaceVec | random_pos () const |
Returns a valid (uniformly) random position in space. | |
Config | setup_cfg (const Model &model, const Config &custom_cfg) |
Set up the agent manager configuration member. | |
SpaceVec | initial_agent_pos () |
Setup helper used to determine a single agent's initial position. | |
void | setup_agents (const AgentState &initial_state) |
Set up the manager's agent container with initial states. | |
void | setup_agents () |
Set up agents container via config or default constructor. | |
MoveFunc | setup_move_to_func () const |
Depending on periodicity, return the function to move agents in space. | |
PosFunc | setup_prepare_pos_func () const |
Private Attributes | |
IndexType | _id_counter |
Counts the number of agents created with this manager, used for new IDs. | |
const std::shared_ptr< spdlog::logger > | _log |
The logger (same as the model this manager resides in) | |
const DataIO::Config | _cfg |
Agent manager configuration node. | |
const std::shared_ptr< RNG > | _rng |
The model's random number generator. | |
const std::shared_ptr< Space > | _space |
The physical space the agents are to reside in. | |
AgentContainer< Agent > | _agents |
Storage container for agents. | |
MoveFunc | _move_to_func |
Function that will be used for moving agents, called by move_* methods. | |
PosFunc | _prepare_pos |
Function that will be used to prepare positions for adding an agent. | |
The agent manager manages the agents living in a model.
The agent manager holds a container with all agents that live in a model space. It provides dynamic functions such as to move models within a space and ensures that the agents move correctly and are never allowed to leave the allowed space. Further, all agents get an ID that is unique among all existing agents (also with respect to multiple agent managers).
AgentTraits | Specialized Utopia::AgentTraits describing the kind of agents this manager should manage |
Model | The model this AgentManager resides in |
using Utopia::AgentManager< AgentTraits, Model >::Agent = Utopia::Agent<AgentTraits, Space> |
The type of the managed agents.
using Utopia::AgentManager< AgentTraits, Model >::AgentState = typename AgentTraits::State |
The type of the agent state.
using Utopia::AgentManager< AgentTraits, Model >::Entity = Agent |
Alias for entity type; part of the shared interface of entity managers.
using Utopia::AgentManager< AgentTraits, Model >::MoveFunc = std::function<void(Agent&, const SpaceVec&)> |
The type of the move function type.
using Utopia::AgentManager< AgentTraits, Model >::PosFunc = std::function<SpaceVec(const SpaceVec&)> |
The type of the function that prepares the position of a new agent.
using Utopia::AgentManager< AgentTraits, Model >::RNG = typename Model::RNG |
The random number generator type.
using Utopia::AgentManager< AgentTraits, Model >::RuleFunc = std::function<AgentState(const std::shared_ptr<Agent>&)> |
The type of a rule function acting on agents of this agent manager.
This is a convenience type def that models can use to easily have this type available.
using Utopia::AgentManager< AgentTraits, Model >::Self = AgentManager<AgentTraits, Model> |
The type of this AgentManager.
using Utopia::AgentManager< AgentTraits, Model >::Space = typename Model::Space |
The type of the space.
using Utopia::AgentManager< AgentTraits, Model >::SpaceVec = SpaceVecType<dim> |
The type of the vectors that represent physical quantities.
using Utopia::AgentManager< AgentTraits, Model >::VoidRuleFunc = std::function<void(const std::shared_ptr<Agent>&)> |
The type of a void rule function acting on agents of this agent manager.
This is a convenience type def that models can use to easily have this type available. Unlike RuleFunc, this
|
inline |
Construct an agent manager.
With the model available, the AgentManager can extract the required information from the model without the need to pass it explicitly. Furthermore, this constructor differs to the one with the initial_state
and num_agents
parameters such that the way the initial state of the agents is determined can be controlled via the configuration.
model | The model this AgentManager belongs to |
custom_cfg | A custom config node to use to use for grid and agent setup. If not given, the model's configuration is used to extract the required entries. |
|
inline |
Construct an agent manager, using the same initial state for all agents.
model | The model this AgentManager belongs to |
initial_state | The initial state of all agents |
custom_cfg | A custom config node to use to use for grid and agent setup. If not given, the model's configuration is used to extract the required entries. |
|
inline |
Create an agent and associate it with this AgentManager.
Adds an agent with the specified state to the managed container of agents. It is checked whether the given position is valid; if space is periodic, any position outside the space is mapped back into the space.
state | The state of the agent that is to be added |
pos | The position of the agent that is to be added |
|
inline |
add_agent overload for auto constructed states and random position
Add an agent with a random position and an auto-constructed AgentState depending on the setup: Either default constructed if the flag was set in the AgentTraits or config-constructed with or without use of the RNG.
|
inline |
add_agent overload for auto-constructed agent states
Adds an agent with a specified position and an auto-constructed AgentState depending on the setup, either default-constructed if the flag was set in the AgentTraits or config-constructed with or without use of the RNG.
pos | The position of the newly created agent |
custom_cfg | A custom configuration. If not given, will use the configuration given at initialization. |
|
inline |
Return const reference to the managed agents.
|
inline |
Return the configuration used for building this AgentManager.
|
inline |
|
inline |
|
inline |
Return const reference to the entities managed by this manager: agents.
|
inline |
Remove agents if the given condition is met.
Uses the erase-remove idiom
UnaryPredicate | type of the UnaryPredicate |
condition | The condition under which the agent is to be removed. This should be a callable that takes the agent shared pointer as argument and returns bool. |
|
inline |
Return the ID counter.
|
inlineprivate |
Setup helper used to determine a single agent's initial position.
|
inline |
– Getters ---------------------------------------------------------— Return the logger of this AgentManager
|
inline |
Move an agent relative to its current position.
|
inline |
Move an agent relative to its current position.
|
inline |
Move an agent to a new position in the space.
|
inline |
Move an agent to a new position in the space.
|
inline |
Returns a container of all agents within a certain radius.
This does not include the agent itself
agent | The agent whose neighborhood is to be constructed |
radius | The radius within which agents other than agent are considered to be part of the neighborhood |
|
inlineprivate |
Returns a valid (uniformly) random position in space.
|
inline |
Removes the given agent from the agent manager.
|
inline |
Return a reference to the shared random number generator.
|
inline |
Select agents using the Utopia::select_entities interface.
Returns a container of agents that were selected according to a certain selection mode. This is done via the Utopia::select_entities interface.
mode | The selection mode |
\args args Forwarded to Utopia::select_entities
|
inline |
Select entities according to parameters specified in a configuration.
Via the mode
key, one of the selection modes can be chosen; for available oens, see Utopia::SelectionMode.
Depending on that mode, the other parameters are extracted from the configuration. See Utopia::select_entities for more info.
sel_cfg | The configuration node containing the expected key-value pairs specifying the selection. |
|
inlineprivate |
Set up agents container via config or default constructor.
If no explicit initial state is given, this setup function is called. There are three modes: If the Utopia::AgentTraits are set such that the default constructor of the agent state is to be used, that constructor is required and is called for each agent.
Otherwise, the AgentState needs to be constructible via a const DataIO::Config&
argument, which gets passed the config entry agent_params
from the AgentManager's configuration.
If a constructor with the signature (const DataIO::Config&, const std::shared_ptr<RNG>&)
is supported, that constructor is called instead.
|
inlineprivate |
Set up the manager's agent container with initial states.
Creates a container with agents that get an initial state and are set up with a random position.
initial_state | The initial state of the agents |
num_agents | The number of agents |
|
inlineprivate |
Set up the agent manager configuration member.
Determines whether to use a custom configuration or the one provided by the model this AgentManager belongs to
|
inlineprivate |
Depending on periodicity, return the function to move agents in space.
The function that is used to move an agent to a new position in space depends on whether the space is periodic or not. In the case of a periodic space the position is automatically mapped into space again across the borders. For a nonperiodic space a position outside of the borders will throw an error.
|
inlineprivate |
Depending on periodicity, return the function to prepare positions of agents before they are added The function that is used to prepare a position before an agent is added (if passed explicitly) depends on whether the space is periodic or not. In the case of a periodic space the position is automatically mapped into space again across the borders. For a nonperiodic space a position outside of the borders will throw an error.
|
inline |
Return pointer to the space, for convenience.
|
inline |
Update the agents.
This method is used in the case of synchronous update. It updates the state and position from the agent's state and position cache variables.
|
private |
Storage container for agents.
|
private |
Agent manager configuration node.
|
private |
Counts the number of agents created with this manager, used for new IDs.
|
private |
The logger (same as the model this manager resides in)
|
private |
Function that will be used for moving agents, called by move_* methods.
|
private |
Function that will be used to prepare positions for adding an agent.
|
private |
The model's random number generator.
|
private |
The physical space the agents are to reside in.
|
staticconstexpr |
The dimensionality of the space.