0

I'm trying to develop a cellular automata simulation and the problem is I want to get the close neighbours and far neighbours of each cell (illustrated as blue and beige) and determine which cells are dead and using some rules bring them to life. So at each iteration I'll be running through all the cells in the array and I want to somehow efficiently get all the close and far neighbours of these cells.

enter image description here

However depending on the position of the cell on the grid, only some of the neighbours will be available, and the only way I thought of doing this so far is having a getNeighbours(cell) method which will return a list with all the available neighbours of that cell that I will have to iterate to get the non-living ones.

getNeighbours(cell):
   If cell.row > 0:
       neighbours.add((coordinate,value),CLOSE_TOP_MIDDLE)
   If cell.row > 1:
       neighbours.add((coordinate,value),FAR_TOP_MIDDLE)
   [...]

However that is a lot of overhead and a lot of comparisons to be done for each cell in the grid!

Is there any generic approach that is generally used with cellular automations? Maybe any data structures I can use? Because with what I have so far each iteration will take a lot of time if the grid is large enough.

ᴘᴀɴᴀʏɪᴏᴛɪs
  • 7,169
  • 9
  • 50
  • 81

1 Answers1

1

Depending on the programming language that you use, there may be packages which provide the desired functionality. In Java, for example, there exists a package called JCASim: Cellular automata simulation system.

Finding neighbours in a CA can be a non-trivial task (e.g., if you use hexagonal cells etc). Even the term 'neighbor' has to be defined: Moore neighborhood or von Neumann neighborhood (these Wikipedia-articles also provide some pseudo-code).

In your case, you can implement the neighbor-search yourself: Let's assume your CA consists of n rows with n columns (labelled from 0,..., n-1) as shown in your picture.

  1. Your getNeighbour-function has to check all next-neighbor cells (grey background color in your image).
    • If you use periodic boundary conditions, you can use the the modulus-operator (%) to get the 9 next-neighbor cells. With periodic boundary conditions the neighbour cells of cell (x,y) are: (x+1 % n, y), (x, y+1 % n), (x+1 % n, y+1 % n), (x+n-1 % n, y), (x, y+n-1 %n), ...)
    • With open boundaries you have to discard all neighbours where x+1 > n-1, y+1 > n-1 or x-1 < 0, y-1 < 0
  2. This way, you can check all cells with a grey background color in your picture.
  3. Call the same function on each of the grey cells. This way you also check the cells with a blue background color.
  4. Now, you have checked all cells in the neighborhood that you defined
F. Knorr
  • 3,045
  • 15
  • 22