Monthly Archive for January, 2005

Learning Ruby

A friend of mine pointed me at Ruby, so I spent most of the day scouring online documentation to learn this cool new thing. Once I felt okay with it I started working on a genetic algorithm. It prompts the user for a phrase to evolve and a population size. Then it generates a bunch of random characters for each “citizen” (inside joke) and sets about breeding and mutating them, and the fittest ones survive into the next generation. And that’s a terrible introduction to genetic algorithms, but nobody’s looking anyway. So here is the source and here is a screenshot. Once it was able to do that in 314 generations. Oh well.

Red Green Color Swarm

A different implementation of my swarm code. This one is probably better. Run the applet here.

My First Swarm

My first swarm is based on Axelrod’s Culture Model. Axelrod’s Culture Model supposes a population of individuals with attributes, for example, numbers. One individual might look like 13579 and another might look like 97531. They are arranged in a two-dimensional array, such as a 10×10 grid. For each individual, pick one of the four adjacent neigbors at random (up, down, left or right). The chance of these two individuals interacting is based on how similar they are. The more they have in common, the more likely they are to interact. So we go through both individuals attributes and compare them. In our example, they have 1 in 5 symbols in common, which evaluates to 20%. That means there’s a 20% chance they will interact. Now, supposing that they do interact, we pick one of the symbols that DIFFER
and copy it from the neighbor to the individual. If we were operating on 13579 and were interacting with the neighbor 97531, we might copy the 7 from the neighbor and get 17579. Or we might copy the 1 from the neighbor and get 13571. It’s picked at random.

I decided to give the individuals in my population three attributes, values from 0 to 5, which would stand for red, green, or blue. Here are screenshots from the same execution:

Sometimes the population will balance out with one or more huge patches of one color, and the swarm doesn’t change any more. But sometimes it just keeps going, with clouds of color swirling around. You can watch the applet here. The Generation 5 JDK was immensely helpful in the creation of this simulation.

Code Blocks

Here’s some cool code to make a nice looking block of code in CSS.

p.code {
    font-family: Courier;
    font-size: 8pt;
    white-space: pre;
  }

Conway’s Life

Here’s the first applet I wrote with the Generation5 JDK. It’s Conway’s Life, and since screenshots don’t really do it justice you should just watch it yourself.

Hello World

A long time ago I wrote a genetic algorithm to evolve the phrase “Hello World!” You can view Python source code here. You can view a screenshot here.

Langton’s Ant

Screenshots from the Langton’s Ant simulation I wrote using the Generation5 JDK.


Termites

Screenshots from my termite simulation, using the Generation5 JDK. I ran this with 32 termites, and although BMP to JPG conversion washes out the red (termites), it’s clear to see that they slowly organize the “wood chips” into piles.

A termite follows three basic instructions:

  • Move in a “random” direction
  • If it collides with a woodchip and is not carrying one, it picks it up
  • If it collides with a woodchip and is carrying one, it puts it down

Set lots of these termites loose and they do some cool stuff.

Beginning, after 2 minutes, and after 7 minutes…


View source code or just run the applet.