Class FlockingAgent

java.lang.Object
org.episteme.natural.computing.ai.simulation.FlockingAgent
All Implemented Interfaces:
Runnable, Agent

public class FlockingAgent extends Object implements Agent
Implements an agent that flocks with other similar agents. This code is based on Mike Miller's Java code conversion for The Computational Beauty of Nature by Gary William Flake. The code has been converted to the Generation5 SDK style and system (using Visualizable etc.).
Author:
James Matthews, Mike Miller, Gary William Flake
See Also:
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    protected static double
    The radius for the separation (avoidance) rule.
    protected static double
    The weight for the separation (avoidance) rule.
    protected static double
    The radius for the cohesion (centroid) rule.
    protected static double
    The weight for the cohesion (centroid) rule.
    protected static double
    The radius for the alignment (copy) rule.
    protected static double
    The weight for the alignment (copy) rule.
    protected static double
    The damping/inertia factor (0.0 to 1.0).
    protected static double
    The time step for the simulation.
    protected static double
    The minimum velocity magnitude.
    protected FlockingAgent[]
    The flock this agent is in.
    protected double
    The x-velocity to be used in the next frame.
    protected double
    The y-velocity to be used in the next frame.
    protected double
    The x-position of this agent.
    protected double
    The positionY-position of this agent.
    protected static double
    The weight for random motion (noise).
    protected static Random
    The random seed used to generate positional data.
    protected static double
    The visual avoidance angle (in radians).
    protected static double
    The radius for visual avoidance.
    protected static double
    The weight for the visual avoidance rule.
    protected static double
    The viewing angle of the agent.
    protected double
    The x-velocity of this agent.
    protected double
    The y-velocity of this agent.
    protected int
    The height of the flocking agent's world.
    protected int
    The width of the flocking agent's world.
  • Constructor Summary

    Constructors
    Constructor
    Description
    Creates a new instance of FlockingAgent.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Adds a behavior to this agent.
    void
    Computes the new velocity vector for this agent based on Boids rules.
    protected static double
    dist(double x1, double y1, double x2, double y2)
    Calculates the Euclidean distance between two points.
    protected static double
    dot(double x1, double y1, double x2, double y2)
    Calculates the dot product of two 2D vectors.
    Helper to get the current environment this agent is in.
    Returns the unique identifier of this agent.
    Returns the name of this agent.
    static void
    initMisc(Random r, int rr, int cc, double va, double vaa, double mv)
    Initializes miscellaneous simulation parameters.
    static void
    initRadii(double cr, double ccr, double ar, double vr)
    Initializes the interaction radii for the Boids rules.
    static void
    initTime(double t, double tt)
    Initializes time parameters.
    static void
    initWeights(double cw, double ccw, double aw, double vw, double rw)
    Initializes the weights for the Boids rules.
    void
    interact(Agent other)
    Interacts with another agent.
    protected static double
    len(double x, double y)
    Calculates the Euclidean length of a 2D vector.
    protected static double[]
    normalize(double x, double y)
    Normalizes a 2D vector.
    void
    Receives a message from the environment.
    void
    Removes a behavior from this agent.
    void
    render(Graphics graphics, int sx, int sy)
    Renders the agent.
    void
    run()
    The main execution cycle of the agent.
    void
    Sets the environment for this agent.
    void
    Updates the reference to the entire flock.
    void
    setWorldDimensions(int rows, int cols)
    Sets the dimensions of the toroidal world.
    void
    Updates the agent's position and velocity based on the computed heading.

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • rnd

      protected static Random rnd
      The random seed used to generate positional data.
    • worldRows

      protected int worldRows
      The width of the flocking agent's world.
    • worldCols

      protected int worldCols
      The height of the flocking agent's world.
    • viewA

      protected static double viewA
      The viewing angle of the agent.
    • vAvoidA

      protected static double vAvoidA
      The visual avoidance angle (in radians).
    • minV

      protected static double minV
      The minimum velocity magnitude.
    • copyR

      protected static double copyR
      The radius for the alignment (copy) rule.
    • centroidR

      protected static double centroidR
      The radius for the cohesion (centroid) rule.
    • avoidR

      protected static double avoidR
      The radius for the separation (avoidance) rule.
    • vAvoidR

      protected static double vAvoidR
      The radius for visual avoidance.
    • copyW

      protected static double copyW
      The weight for the alignment (copy) rule.
    • centroidW

      protected static double centroidW
      The weight for the cohesion (centroid) rule.
    • avoidW

      protected static double avoidW
      The weight for the separation (avoidance) rule.
    • vAvoidW

      protected static double vAvoidW
      The weight for the visual avoidance rule.
    • randW

      protected static double randW
      The weight for random motion (noise).
    • dt

      protected static double dt
      The time step for the simulation.
    • ddt

      protected static double ddt
      The damping/inertia factor (0.0 to 1.0). High values mean more inertia.
    • myFlock

      protected FlockingAgent[] myFlock
      The flock this agent is in.
    • positionX

      protected double positionX
      The x-position of this agent.
    • positionY

      protected double positionY
      The positionY-position of this agent.
    • vx

      protected double vx
      The x-velocity of this agent.
    • vy

      protected double vy
      The y-velocity of this agent.
    • nvx

      protected double nvx
      The x-velocity to be used in the next frame. Remember that the flocking agents are updated "simultaneously", so they should all have their new values computed using computeNewHeading, then all have them updated using update.
    • nvy

      protected double nvy
      The y-velocity to be used in the next frame. Remember that the flocking agents are updated "simultaneously", so they should all have their new values computed using computeNewHeading, then all have them updated using update.
  • Constructor Details

    • FlockingAgent

      public FlockingAgent()
      Creates a new instance of FlockingAgent. Random values are automatically assigned to the positional and velocity variables.
  • Method Details

    • getId

      public UUID getId()
      Description copied from interface: Agent
      Returns the unique identifier of this agent.
      Specified by:
      getId in interface Agent
      Returns:
      the agent's UUID.
    • getName

      public String getName()
      Description copied from interface: Agent
      Returns the name of this agent.
      Specified by:
      getName in interface Agent
      Returns:
      the agent's name.
    • addBehavior

      public void addBehavior(Behavior behavior)
      Description copied from interface: Agent
      Adds a behavior to this agent. Behaviors define the agent's actions and responses.
      Specified by:
      addBehavior in interface Agent
      Parameters:
      behavior - the behavior to add.
    • removeBehavior

      public void removeBehavior(Behavior behavior)
      Description copied from interface: Agent
      Removes a behavior from this agent.
      Specified by:
      removeBehavior in interface Agent
      Parameters:
      behavior - the behavior to remove.
    • getEnvironment

      public Environment getEnvironment()
      Description copied from interface: Agent
      Helper to get the current environment this agent is in.
      Specified by:
      getEnvironment in interface Agent
      Returns:
      the environment or null if not registered.
    • setEnvironment

      public void setEnvironment(Environment environment)
      Description copied from interface: Agent
      Sets the environment for this agent. Called by the environment upon registration.
      Specified by:
      setEnvironment in interface Agent
      Parameters:
      environment - the environment wrapper.
    • receive

      public void receive(ACLMessage message)
      Description copied from interface: Agent
      Receives a message from the environment.
      Specified by:
      receive in interface Agent
      Parameters:
      message - the ACL message to process.
    • interact

      public void interact(Agent other)
      Description copied from interface: Agent
      Interacts with another agent.
      Specified by:
      interact in interface Agent
      Parameters:
      other - the agent to interact with.
    • run

      public void run()
      Description copied from interface: Agent
      The main execution cycle of the agent. Usually invoked by the agent container or scheduler.
      Specified by:
      run in interface Agent
      Specified by:
      run in interface Runnable
    • setWorldDimensions

      public void setWorldDimensions(int rows, int cols)
      Sets the dimensions of the toroidal world.
      Parameters:
      rows - height of the world
      cols - width of the world
    • initMisc

      public static void initMisc(Random r, int rr, int cc, double va, double vaa, double mv)
      Initializes miscellaneous simulation parameters.
      Parameters:
      r - random number generator
      rr - unused parameter (legacy)
      cc - unused parameter (legacy)
      va - view angle in degrees
      vaa - visual avoidance angle in degrees
      mv - minimum velocity
    • initRadii

      public static void initRadii(double cr, double ccr, double ar, double vr)
      Initializes the interaction radii for the Boids rules.
      Parameters:
      cr - alignment (copy) radius
      ccr - cohesion (centroid) radius
      ar - separation (avoid) radius
      vr - visual avoidance radius
    • initWeights

      public static void initWeights(double cw, double ccw, double aw, double vw, double rw)
      Initializes the weights for the Boids rules.
      Parameters:
      cw - alignment (copy) weight
      ccw - cohesion (centroid) weight
      aw - separation (avoid) weight
      vw - visual avoidance weight
      rw - random perturbation weight
    • initTime

      public static void initTime(double t, double tt)
      Initializes time parameters.
      Parameters:
      t - time step (delta time)
      tt - damping/inertia factor
    • setFlock

      public void setFlock(FlockingAgent[] flock)
      Updates the reference to the entire flock.
      Parameters:
      flock - array of all agents in the flock
    • normalize

      protected static double[] normalize(double x, double y)
      Normalizes a 2D vector.
      Parameters:
      x - x component
      y - y component
      Returns:
      the normalized vector {x, y}
    • len

      protected static double len(double x, double y)
      Calculates the Euclidean length of a 2D vector.
      Parameters:
      x - the x component
      y - the y component
      Returns:
      the length
    • dist

      protected static double dist(double x1, double y1, double x2, double y2)
      Calculates the Euclidean distance between two points.
      Parameters:
      x1 - x-coordinate of the first point
      y1 - y-coordinate of the first point
      x2 - x-coordinate of the second point
      y2 - y-coordinate of the second point
      Returns:
      the distance
    • dot

      protected static double dot(double x1, double y1, double x2, double y2)
      Calculates the dot product of two 2D vectors.
      Parameters:
      x1 - x component of vector 1
      y1 - y component of vector 1
      x2 - x component of vector 2
      y2 - y component of vector 2
      Returns:
      the dot product
    • computeNewHeading

      public void computeNewHeading(int self)
      Computes the new velocity vector for this agent based on Boids rules.
      Parameters:
      self - index of this agent in the flock array
    • update

      public void update()
      Updates the agent's position and velocity based on the computed heading. Handles toroidal world wrap-around.
    • render

      public void render(Graphics graphics, int sx, int sy)
      Renders the agent.
      Parameters:
      graphics - the graphics context
      sx - scroll x offset
      sy - scroll y offset