Class AgentSet

java.lang.Object
modelarium.agents.AgentSet
All Implemented Interfaces:
Iterable<Agent>

public class AgentSet extends Object implements Iterable<Agent>
A collection class for managing Agent instances, with support for:
  • Optional deep copying of agents on insertion
  • Fast lookup by agent name
  • Filtering, duplication, and setup routines
  • Randomised iteration

This class is iterable and designed to support both sequential and parallel simulation use cases.

  • Constructor Details

    • AgentSet

      public AgentSet(boolean isStoringAgentCopies)
      Constructs an empty agent set with optional deep copy behaviour.
      Parameters:
      isStoringAgentCopies - whether added agents should be deep copied
    • AgentSet

      public AgentSet(List<Agent> agentsList, boolean isStoringAgentCopies)
      Constructs a new agent set from a list of agents, with optional deep copying.
      Parameters:
      agentsList - list of agents to add
      isStoringAgentCopies - whether to store deep copies
    • AgentSet

      public AgentSet(List<Agent> agentsList)
      Constructs a new agent set from a list of agents, without deep copying.
      Parameters:
      agentsList - list of agents to add
    • AgentSet

      public AgentSet()
      Constructs an empty agent set without deep copying.
  • Method Details

    • setIsStoringAgentCopiesFlag

      public void setIsStoringAgentCopiesFlag(boolean isStoringAgentCopies)
      Updates the internal flag for whether agent copies should be stored.
      Parameters:
      isStoringAgentCopies - true to enable deep copy on addition
    • add

      public void add(Agent agent)
      Adds an agent to the set. If the agent already exists, it will be replaced.
      Parameters:
      agent - the agent to add
    • add

      public void add(List<Agent> agents)
      Adds a list of agents to the set.
      Parameters:
      agents - list of agents to add
    • add

      public void add(AgentSet agentSet)
      Adds all agents from another AgentSet that do not already exist in this set. Existing agents (by name) are not modified or replaced.
      Parameters:
      agentSet - the agent set to add from
    • get

      public Agent get(String agentName)
      Retrieves an agent by name.
      Parameters:
      agentName - the agent's unique name
      Returns:
      the agent instance
    • get

      public Agent get(int index)
      Retrieves an agent by index.
      Parameters:
      index - the index of the agent
      Returns:
      the agent at the given position
    • getAsList

      public List<Agent> getAsList()
      Returns the list of agents in this set.
      Returns:
      a list of agent instances
    • size

      public int size()
      Returns the number of agents in the set.
      Returns:
      the size of the agent set
    • clear

      public void clear()
      Clears the agent set entirely.
    • doesAgentExist

      public boolean doesAgentExist(String agentName)
      Checks if an agent exists in the set by name.
      Parameters:
      agentName - the name to check
      Returns:
      true if the agent exists
    • update

      public void update(AgentSet otherAgentSet)
      Updates this set with all agents from another set. Existing agents are replaced if names match.
      Parameters:
      otherAgentSet - the other agent set to pull from
    • getFilteredAgents

      public AgentSet getFilteredAgents(Predicate<Agent> agentFilter)
      Returns a filtered view of the agent set.
      Parameters:
      agentFilter - a predicate to apply to each agent
      Returns:
      a new AgentSet containing only matching agents
    • getRandomIterator

      public Iterator<Agent> getRandomIterator()
      Returns a randomised iterator over the agents in this set.
      Returns:
      an iterator that yields agents in random order
    • setup

      public void setup()
      Calls setup() on all agents in the set. Should be called before the simulation begins.
    • duplicate

      public AgentSet duplicate()
      Returns a duplicate of this agent set. If deep copy is enabled, agents will be duplicated as well.
      Returns:
      a new AgentSet with the same agents
    • iterator

      public Iterator<Agent> iterator()
      Standard iterator over the agents in the order they were added.
      Specified by:
      iterator in interface Iterable<Agent>
      Returns:
      an iterator over the agent list