Monday, November 28, 2005

Artificial Life

About 3 or 4 years ago I was tinkering with writing an artificial life simulation. The idea was to use genetic algorithms with some fairly simple heirarchy of rules for behavior, such as, if object x is a certain distance away, then do action y. The behaviors would be randomly generated, so you could get really strange behaviors like, if a a piece of food is 5 or less spaces away, then move toward the nearest hiding place. Plus, each creature would have a list of these rules to evaluate in order, and that order would also be randomly determined in each creature. The program would evaluate each rule in order until a mathcing condition and action was performed. If no rule matched, then the creature would just sit there.

The creatures would initially be randomly generated each with its own set of rules, however the creatures had the ability to reproduce with other creatures, in which case the offspring would get a mix of the rules from the parents. It might take 1/2 a rule from one part and half a rule from another parent, so if parent X had the rule, "If food is less than 5 spaces away, move towards the nearest hiding place" and parent Y had a rule, "If hiding place is less than 10 spaces away, move away from the sun", the offspring might end up with a rule that says, "if hiding place is less than ten spaces away, move towards the nearest hiding place." Then, the offspring would actually have a potentially useful rule that generated "hiding behavior".

To make things interesting, the creatures needed to eat, and if they didn't, after a certain amount of time they would die. Also, the creatures could attack each other, and the only source of food was the dead bodies of the creatures. It was hoped that over a long amount of time, there might be some sort of behavior that evolved that might produce a group of creatures that could live a long amount of time.

So, in a bit more detail, the random rules were basically made up of a specific structure:

If X is {less, greater} than Y, then Z.

Choices for X could be a hunger value, an object (such as food, another creature, the sun, a hiding place, the edge of the world, etc.).

The choice of "less than" or "greature than" was also randomly selected.

Y could be any numeric value from 0 to 100.

Action Z could be things like "attack nearest creature", "move away from", or "move towards", or "eat", etc.

I've over-simplified a bit of the rule format, but hopefullly that explains enough of the details. The goal was to have rules that could easily be randomly generated, and that had a fixed structure so they could be spliced together to form simple types of behavior.

Did it work? You can be the judge. You can download the program here. Unpack the zip file into a folder and run the program life.exe. One caveat - the program is a bit CPU intensive when it runs, especially if the creature populations start to grow. If you notice things slowing down, hit ESC to exit the program. NOTE: this is Windows program. It won't run on a Mac. Sorry. At some point, if I get some free time, I might make a Java version that can run in a web browser.

Here's a screen shot of what the program looks like when running. A static image looks kinda boring - it's definitely more interesting to see the real thing in action. The creatures move around quite quickly.



In the center of the world is a sun which has gravity. Orbiting the sun are some red spheres that act as cover or hiding places for creatures. All the little colored dots are the creatures. The tiny tiny dots (only visable if you click on the screen shot for a larger view) are dead bodies. Any creature in a red sphere can not be seen by any other creatures. If a creature touches the sun, it dies and is removed from the world. The idea of adding a sun with gravity in the center or the world was to provide an automatic "clean-up" mechanism to prevent the world from getting overpopulated with dead bodies or creatures that just sorta sat around reproducing and eating their offspring. With gravity, such creatures will eventually get sucked into the sun, unless they have a behavior that tries to avoid the sun.

Anyhow, feel free to play with it. I find it kinda relaxing to just watch the creatures run around for a bit. It's sorta like watching a fish tank. Here's an example of what happened when I let the thing run over-night. Eventually, some sort of successful life form developed and took over the world. I really ought to add a feature that would allow displaying the rules of the creatures that successfully developed.