org.jscience.computing.ai.cellularautomaton
Class CellularAutomataLayered

java.lang.Object
  extended by org.jscience.computing.ai.cellularautomaton.CellularAutomata
      extended by org.jscience.computing.ai.cellularautomaton.CellularAutomataLayered
All Implemented Interfaces:
Steppable, Visualizable

public abstract class CellularAutomataLayered
extends CellularAutomata

Expands upon the cellular automata class by adding a separate layer of automata that are independent of the world states. This is useful for classes that require a greater degree of interaction with the world. For example, each cell in Conway's Life simply requires knowledge of its 8 immediate neighbours, and does not move location. Other, more complicated, CAs move about and must keep internal states as well as world states. Termites are a good example of this, where the world and the woodchips are independent of the termites.

See Also:
CellularAutomata, org.jscience.tests.computing.ai.demos.ConwaysLifeCA, org.jscience.tests.computing.ai.demos.TermitesCA

Field Summary
protected  java.awt.Color[] clrStates
          The colours representing the automaton states.
protected  boolean collisionDetection
          Collision detection can be used for CellularAutomataLayered-derived classes to ensure that two agents do not occupy the same world position.
protected  boolean[][] positionTest
          Stores boolean values corresponding to cell occupancy.
 
Fields inherited from class org.jscience.computing.ai.cellularautomaton.CellularAutomata
bufferPosition, bufferSize, caSize, caWorld, caWorld_x, caWorld_y, clrBackground, clrGrid, clrWorld, DOUBLE_BUFFERING, doubleBuffering, drawGrid, ENCLOSED, geometryType, INFINITE, TORODIAL, X_AXIS, Y_AXIS
 
Constructor Summary
CellularAutomataLayered()
          Default constructor.
CellularAutomataLayered(int size_x, int size_y)
          Create an instance of CellularAutomataLayered with world size information.
CellularAutomataLayered(int size_x, int size_y, int options)
          Create a new instance of CellularAutomataLayered with positional information and optional settings.
 
Method Summary
 void addAutomaton(CAAgent agent)
          Adds an automaton of type CAAgent.
 void addAutomaton(int pos_x, int pos_y, int state)
          Add an automaton to the world with state and position information.
 CAAgent getCA(int caIndex)
          Retrieve the corresponding CAAgent, which in turn holds positional, state and other information.
 boolean getCollisionDetection()
          Returns the collision detection status of this cellular automata world.
 int getNumCAs()
          Retrieve the number of automata in the world, this should be equal to the number of calls to addAutomaton.
protected  boolean isCellFree(int x, int y)
          Check to see if a world cell is free or not.
 void moveCAAbsolute(int caIndex, int new_x, int new_y)
          Move a given automaton to a new position.
 void moveCARelative(int caIndex, int dx, int dy)
          Move a given automaton to a new position relative to its current.
 void removeAll()
          Remove all the automata from the world.
 void render(java.awt.Graphics graphics, int pw, int ph)
          Draws the world state, then each automata.
 void setCollisionDetection(boolean cd)
          Set or reset collision detection in the world.
 void setStateColour(int state, java.awt.Color colour)
          Sets the colour of the automata states.
 void setWorldSize(int size_x, int size_y)
          Sets the world size.
 
Methods inherited from class org.jscience.computing.ai.cellularautomaton.CellularAutomata
clearWorld, doStep, drawGrid, flipBuffer, getBackgroundColor, getCASize, getGeometry, getSizeX, getSizeY, getWorldAt, init, iterateCA, iterateCA, reset, setBackgroundColor, setCASize, setGeometry, setWorldAt, setWorldAtEx, setWorldAtRelative, setWorldColors, setWorldColour, translateGeometry, writeImage
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

positionTest

protected boolean[][] positionTest
Stores boolean values corresponding to cell occupancy.


collisionDetection

protected boolean collisionDetection
Collision detection can be used for CellularAutomataLayered-derived classes to ensure that two agents do not occupy the same world position. Collision detection is turned off by default.


clrStates

protected java.awt.Color[] clrStates
The colours representing the automaton states.

Constructor Detail

CellularAutomataLayered

public CellularAutomataLayered()
Default constructor.

See Also:
CellularAutomata.CellularAutomata(int,int)

CellularAutomataLayered

public CellularAutomataLayered(int size_x,
                               int size_y)
Create an instance of CellularAutomataLayered with world size information. Only two world colours are set, state 0 to black and state 1 to red.

Parameters:
size_x - the world x-size.
size_y - the world y-size.

CellularAutomataLayered

public CellularAutomataLayered(int size_x,
                               int size_y,
                               int options)
Create a new instance of CellularAutomataLayered with positional information and optional settings.

Parameters:
size_x - the x-size of the world.
size_y - the y-size of the world.
options - additional options.
Method Detail

setWorldSize

public void setWorldSize(int size_x,
                         int size_y)
Sets the world size. This function also allocates the maximum number of CA automaton as size_x * size_y.

Overrides:
setWorldSize in class CellularAutomata
Parameters:
size_x - the world x-size.
size_y - the world y-size.
See Also:
CellularAutomata.setWorldSize(int,int)

setCollisionDetection

public void setCollisionDetection(boolean cd)
Set or reset collision detection in the world.

Parameters:
cd - turn collision detection on or off.

getCollisionDetection

public boolean getCollisionDetection()
Returns the collision detection status of this cellular automata world.

Returns:
the status of collision detection.

isCellFree

protected boolean isCellFree(int x,
                             int y)
Check to see if a world cell is free or not. This function calls translateGeometry before accessing the positionTest array.

Parameters:
x - the x-position to check.
y - the y-position to check.
Returns:
a boolean value corresponding to the value in positionTest.

addAutomaton

public void addAutomaton(int pos_x,
                         int pos_y,
                         int state)
Add an automaton to the world with state and position information.

Parameters:
pos_x - the x-position of the new automaton
pos_y - the y-position of the new automaton
state - the state of the new automaton

addAutomaton

public void addAutomaton(CAAgent agent)
Adds an automaton of type CAAgent. This allows you to add CAAgent-derived classes (such as DirectedCAAgent) to the world.

Remember to initialize parameters such as positional and state information.

Parameters:
agent - the agent to add.
See Also:
CAAgent

removeAll

public void removeAll()
Remove all the automata from the world.


moveCARelative

public void moveCARelative(int caIndex,
                           int dx,
                           int dy)
Move a given automaton to a new position relative to its current. If the current position of the CA is (x,y), then the new position is (x+dx, y+dy).

Parameters:
caIndex - the index of the automaton to move.
dx - x-delta position.
dy - y-delta position.

moveCAAbsolute

public void moveCAAbsolute(int caIndex,
                           int new_x,
                           int new_y)
Move a given automaton to a new position. If the current position of the CA is (x,y), then the new position is (new_x, new_y).

Parameters:
caIndex - the index of the automaton to move.
new_x - the new x-position.
new_y - the new y-position.

getCA

public CAAgent getCA(int caIndex)
Retrieve the corresponding CAAgent, which in turn holds positional, state and other information.

Parameters:
caIndex - the index of the automaton to return.
Returns:
the corresponding CAAgent.
See Also:
CAAgent

getNumCAs

public int getNumCAs()
Retrieve the number of automata in the world, this should be equal to the number of calls to addAutomaton.

Returns:
the number of cellular automata in the world.
See Also:
addAutomaton(int,int,int)

render

public void render(java.awt.Graphics graphics,
                   int pw,
                   int ph)
Draws the world state, then each automata.

Specified by:
render in interface Visualizable
Overrides:
render in class CellularAutomata
Parameters:
graphics - the graphics context.
pw - the width of the context.
ph - the height of the context.
See Also:
CellularAutomata.caSize

setStateColour

public void setStateColour(int state,
                           java.awt.Color colour)
Sets the colour of the automata states. This is separate to setWorldColour.

Parameters:
state - the state index (0-255).
colour - the colour of the state.