Class FlockingAgent
java.lang.Object
org.episteme.natural.computing.ai.simulation.FlockingAgent
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
FieldsModifier and TypeFieldDescriptionprotected static doubleThe radius for the separation (avoidance) rule.protected static doubleThe weight for the separation (avoidance) rule.protected static doubleThe radius for the cohesion (centroid) rule.protected static doubleThe weight for the cohesion (centroid) rule.protected static doubleThe radius for the alignment (copy) rule.protected static doubleThe weight for the alignment (copy) rule.protected static doubleThe damping/inertia factor (0.0 to 1.0).protected static doubleThe time step for the simulation.protected static doubleThe minimum velocity magnitude.protected FlockingAgent[]The flock this agent is in.protected doubleThe x-velocity to be used in the next frame.protected doubleThe y-velocity to be used in the next frame.protected doubleThe x-position of this agent.protected doubleThe positionY-position of this agent.protected static doubleThe weight for random motion (noise).protected static RandomThe random seed used to generate positional data.protected static doubleThe visual avoidance angle (in radians).protected static doubleThe radius for visual avoidance.protected static doubleThe weight for the visual avoidance rule.protected static doubleThe viewing angle of the agent.protected doubleThe x-velocity of this agent.protected doubleThe y-velocity of this agent.protected intThe height of the flocking agent's world.protected intThe width of the flocking agent's world. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidaddBehavior(Behavior behavior) Adds a behavior to this agent.voidcomputeNewHeading(int self) Computes the new velocity vector for this agent based on Boids rules.protected static doubledist(double x1, double y1, double x2, double y2) Calculates the Euclidean distance between two points.protected static doubledot(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.getId()Returns the unique identifier of this agent.getName()Returns the name of this agent.static voidInitializes miscellaneous simulation parameters.static voidinitRadii(double cr, double ccr, double ar, double vr) Initializes the interaction radii for the Boids rules.static voidinitTime(double t, double tt) Initializes time parameters.static voidinitWeights(double cw, double ccw, double aw, double vw, double rw) Initializes the weights for the Boids rules.voidInteracts with another agent.protected static doublelen(double x, double y) Calculates the Euclidean length of a 2D vector.protected static double[]normalize(double x, double y) Normalizes a 2D vector.voidreceive(ACLMessage message) Receives a message from the environment.voidremoveBehavior(Behavior behavior) Removes a behavior from this agent.voidRenders the agent.voidrun()The main execution cycle of the agent.voidsetEnvironment(Environment environment) Sets the environment for this agent.voidsetFlock(FlockingAgent[] flock) Updates the reference to the entire flock.voidsetWorldDimensions(int rows, int cols) Sets the dimensions of the toroidal world.voidupdate()Updates the agent's position and velocity based on the computed heading.
-
Field Details
-
rnd
The random seed used to generate positional data. -
worldRows
protected int worldRowsThe width of the flocking agent's world. -
worldCols
protected int worldColsThe height of the flocking agent's world. -
viewA
protected static double viewAThe viewing angle of the agent. -
vAvoidA
protected static double vAvoidAThe visual avoidance angle (in radians). -
minV
protected static double minVThe minimum velocity magnitude. -
copyR
protected static double copyRThe radius for the alignment (copy) rule. -
centroidR
protected static double centroidRThe radius for the cohesion (centroid) rule. -
avoidR
protected static double avoidRThe radius for the separation (avoidance) rule. -
vAvoidR
protected static double vAvoidRThe radius for visual avoidance. -
copyW
protected static double copyWThe weight for the alignment (copy) rule. -
centroidW
protected static double centroidWThe weight for the cohesion (centroid) rule. -
avoidW
protected static double avoidWThe weight for the separation (avoidance) rule. -
vAvoidW
protected static double vAvoidWThe weight for the visual avoidance rule. -
randW
protected static double randWThe weight for random motion (noise). -
dt
protected static double dtThe time step for the simulation. -
ddt
protected static double ddtThe damping/inertia factor (0.0 to 1.0). High values mean more inertia. -
myFlock
The flock this agent is in. -
positionX
protected double positionXThe x-position of this agent. -
positionY
protected double positionYThe positionY-position of this agent. -
vx
protected double vxThe x-velocity of this agent. -
vy
protected double vyThe y-velocity of this agent. -
nvx
protected double nvxThe 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 usingcomputeNewHeading, then all have them updated usingupdate. -
nvy
protected double nvyThe 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 usingcomputeNewHeading, then all have them updated usingupdate.
-
-
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
-
getName
-
addBehavior
Description copied from interface:AgentAdds a behavior to this agent. Behaviors define the agent's actions and responses.- Specified by:
addBehaviorin interfaceAgent- Parameters:
behavior- the behavior to add.
-
removeBehavior
Description copied from interface:AgentRemoves a behavior from this agent.- Specified by:
removeBehaviorin interfaceAgent- Parameters:
behavior- the behavior to remove.
-
getEnvironment
Description copied from interface:AgentHelper to get the current environment this agent is in.- Specified by:
getEnvironmentin interfaceAgent- Returns:
- the environment or null if not registered.
-
setEnvironment
Description copied from interface:AgentSets the environment for this agent. Called by the environment upon registration.- Specified by:
setEnvironmentin interfaceAgent- Parameters:
environment- the environment wrapper.
-
receive
Description copied from interface:AgentReceives a message from the environment. -
interact
-
run
-
setWorldDimensions
public void setWorldDimensions(int rows, int cols) Sets the dimensions of the toroidal world.- Parameters:
rows- height of the worldcols- width of the world
-
initMisc
Initializes miscellaneous simulation parameters.- Parameters:
r- random number generatorrr- unused parameter (legacy)cc- unused parameter (legacy)va- view angle in degreesvaa- visual avoidance angle in degreesmv- 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) radiusccr- cohesion (centroid) radiusar- separation (avoid) radiusvr- 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) weightccw- cohesion (centroid) weightaw- separation (avoid) weightvw- visual avoidance weightrw- 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
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 componenty- 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 componenty- 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 pointy1- y-coordinate of the first pointx2- x-coordinate of the second pointy2- 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 1y1- y component of vector 1x2- x component of vector 2y2- 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
Renders the agent.- Parameters:
graphics- the graphics contextsx- scroll x offsetsy- scroll y offset
-