Example Code

Simulating a Cellular Automaton in LabVIEW

Code and Documents

Attachment

Download All

main.png

In this example we'll look at 3 VIs created to simulate a cellular automaton (http://en.wikipedia.org/wiki/Cellular_automaton).

The primary VI (CellularAutomataSimulation.vi) can be run to simulate the automaton. It requires NI Vision (which is used to allow you to record the activity to an AVI file) to run, but you can delete the sections of code that reference IMAQ you'll be able to run it without.

The secondary VI (SimpleAutomata.vi) runs on the automaton data, executing the rule on each element of the matrix and outputting the next state.

The helper VI (ModifyMatrix.vi) is used to do simple addition and subtraction to the automaton (it interfaces with the mouse so you can artifically add energy to the system). Interestingly enough, LabVIEW does not coerce the values for addition and subtraction to 0 and Max_Int. This performs the coercion manually.

The code seems a bit complex because of the interface with the mouse and the controls on the front, but it really comes in 2 steps, a setup and a loop.

The setup

initialize.png

randomly initializes an array of the size specified with Unsigned 32-bit integers.

The primary application happens in the subVI within the application loop.

step.png

Most of the code is obviously part of different systems which make the program more user friendly (and unfortunately, the code rather difficult to read at times). Although some of the code could be split into subVIs, the application is simple enough that this was not implemented. If further expansion is to be done other than writing new Automaton Rules (which are obviously in a SubVI), it may be worth modifying the code for readability.

There are a couple different things that can be learned from this example.

-Cellular Automaton Processing

-Image Manipulation

-Mouse Input

-AVI Creation

If you have the patience to let it run, you can also observe round-off error in action. The automaton in use will eventually converge to 0 with no wrapping and no mouse clicks. If you allow the automaton to run until it is no longer changing from step to step (requires an extra comparison which is no in the program, but if you leave it over night and eyeball it it'll be done) it will have a blue coloring. This is because the levels of each number are low enough that their averages round off and can never be dissipated.

Interesting questions and futher experiments (some of which I've done, but won't spoil the answers if you don't already know):

-Why do the colors seem to have 3 distinct layers? (Red then Green then Blue)

-Why does the phenomena on the walls happen when wrapping is enabled?

-What is the significance of the waves that propagate towards given points on the image?

-Will the system always converge to 0? How would you modify to the rule to make it converge to MaxInt? Can you modify it to not converge to either?

-Try adding a rule of your own (see http://en.wikipedia.org/wiki/Conway's_Game_of_Life)

-Try adding a color mask to manipulate the way the data is represented.

And the most important question,

-What's the point of a Cellular Automaton? (my answer, they're cool)

(Added a version without the IMAQ components.)

Example code from the Example Code Exchange in the NI Community is licensed with the MIT license.

Comments
BSGMike
Member
Member
on

This looks fascinating. Would it be possible to have an 8.5 version please?

Kevin_H.
NI Employee (retired)
on

Posted an 8.5 version (assuming I converted it correctly). Let me know if it works for ya! (I'm a little worried about the IMAQ components going back that many versions, but like I said in the summary, you can delete those. They were just there so I could have a video of what happened for really slow renders/larger automata)

BSGMike
Member
Member
on
Thanks for that Kevin. I don't actually have IMAQ  so I've deleted those bits but the rest of it works fine. What a fascinating program. I want to look at the code to understand how it works but I can't  stop myself from just watching it run !
Thanks for that Kevin. I don't actually have IMAQ  so I've deleted those bits but the rest of it works fine. What a fascinating  application. I want to look at the code to understand how it works but I can't  stop myself from just watching it run !

Kevin_H.
NI Employee (retired)
on

That's exactly the reaction I had when I discovered these things. If you're interested in seeing the effects of different rules without building your own VIs to implement them, might I recommend http://psoup.math.wisc.edu/mcell/mjcell/mjcell.html .

There are an infinite number of possible rules that could be applied to these dynamic systems, all which produce different emergent phenomena. Let me know if you have any questions about it! I love talking about them.

MrSmith
Member
Member
on

Very nice! I'm been playing with these for a long time and did a somewhat cruder version that allows me to see interactions zoomed in to the cell level. It's unusual in that I don't have a fixed array size and expand the array as the patterns grow in size.  This only works up to a point, however as the algorith is very crude and starts to chew up a lot of CPU very quickly. Here's a link to my version... https://decibel.ni.com/content/docs/DOC-7013

Contributors