Class AgentSet

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

public final 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(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

    • 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.
      Parameters:
      agentSet - the agent set to add from
    • addDeepCopy

      public void addDeepCopy(Agent agent)
      Adds a deep copy of an agent to the set. If the agent already exists, it will be replaced.
      Parameters:
      agent - the agent to deep clone and add
    • addDeepCopy

      public void addDeepCopy(List<Agent> agents)
      Adds a deep copy of each agent in a list to the set.
      Parameters:
      agents - list of agents to deep clone and add
    • addDeepCopy

      public void addDeepCopy(AgentSet agentSet)
      Adds a deep copy of each agent from another AgentSet.
      Parameters:
      agentSet - the agent set to deep clone and 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
    • isEmpty

      public boolean isEmpty()
      Returns whether the set contains no agents.
      Returns:
      true if the set is empty, false otherwise
    • 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, boolean areDeepCopied)
      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
      areDeepCopied - whether the agents should be deep cloned before being stored
    • getFilteredAgents

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

      public AgentSet getFilteredAgents(Predicate<ReadOnlyAgent> agentFilter, boolean includeDeadAgents)
      Returns a filtered view of the agent set.
      Parameters:
      agentFilter - a predicate to apply to each agent
      includeDeadAgents - a boolean which determines if dead agents are to be included in the output agent set or not
      Returns:
      a new AgentSet containing only matching agents
    • getRandomIterator

      public Iterator<Agent> getRandomIterator(RandomGenerator randomGenerator)
      Returns a randomised iterator over the agents in this set.
      Parameters:
      randomGenerator - the random generator used to shuffle the agents
      Returns:
      an iterator that yields agents in random order
    • getAsImmutable

      public ReadOnlyAgentSet getAsImmutable()
      Returns a read-only view of this agent set.
      Returns:
      a new ReadOnlyAgentSet instance wrapping this set
    • 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