Jon Wire

A Software Engineer exploring the world with code.

Genetic circle drawing for fun and ... circles, I guess

A few weeks ago, I wrote a quick Easter post demonstrating Conway's Game of Life. I briefly explained the rules and showed a simple implementation running on my handy little Interweb VGA.

The algorithm produces behavior that appears chaotic, but it is ultimately just the execution of simple predefined rules. We make it "interesting" by initializing the world randomly — and also by being limited humans who have less than perfect knowledge. Still, the rules for each "creature" (pixel) are static. And, while the execution of the rules can appear chaotic and hard to predict, even our limited human minds can make enough sense of these rules to find and create "colonies" (pixel patterns) that are actually predictable (and sometimes even programmable).

Interestingly, "Conway's Creatures" could be described as having simple, fixed genetics. "God's Creatures", on the other hand, have massive, highly diverse genetics. The added complexity and variety produce what we might call "emergent, rational problem solving". Each species primarily solves the problem of its own survival. But then, within the scope of the whole game — as if the game were "designed" to self-perpetuate forever, there are countless niche problems that "God's Game of Life" continuously searches out and solves.

Broadly speaking, the "water problem" is solved by swimming creatures. The "land problem" by walking creatures. The "air problem" by flying creatures. And the "oxygen problem" by stationary oxygen-farting green creatures. (Not necessarily in that order.)

If there's a gap in the ecosystem, a problem to solve, "God's Game of Life" solves it.

Thankfully, and much more importantly to us modern sophisticated chaps, we can take God's example to solve problems with genetic algorithms in code too. (You may begin curling your moustache now.)

To get a feel for how genetic algorithms work, let's see if we can make a simple "drawing" creature evolve to draw a circle of radius 50. There are several ways we could do this. But, for this experiment, the creature will just paint individual points, and its genetics will just be the list of points.

Its genetics will consist specifically of 314 points. It will produce 15 offspring in each generation, each offspring having exactly one completely random mutation — one point swapped out for another completely random point. The original creature will also produce a clone of itself to compete with its other offspring. Then, the most fit living creature will survive.

The remainder will die.

Interweb VGA

As you can probably see, we eventually end up with a circle-ish thing. And, if we had literally no idea how to draw a circle, this algorithm could have successfully shown us what a circle looks like — and, perhaps equally importantly, what a circle does not look like.

It should go without saying, but we actually do know how to draw circles. Using a genetic algorithm to draw a circle is possibly the least efficient way to make circles. It's all quite silly, actually. A more useful genetic algorithm is one where the approach for a problem isn't even "known" or where there are many related sub-problems with opportunities for a variety of species to "thrive" and provide niche solutions. (Perhaps a problem space with many uniquely useful local maxima/minima.)

In any case, this has been fun. I was actually pleasantly surprised with the visual. I hope you found it neat. And, I hope you also find some time to write some equally silly code this week.

If you enjoyed anything about this article, subscribe so you don't miss out!

* indicates required

Intuit Mailchimp

Published: 5/21/2025.
Topics: play, javascript, typescript, artificial intelligence, machine learning, genetic algorithm