Drawing with genetic algorithms

Rodrigo Silveira
Jan 7 · 2 min read

Live demo

Try out the demo on my personal blog.

Explanation

I recently started researching Neuroevolution in the context of generating/optimizing neural networks. As a refresher exercise on the basics of genetic algorithms, I decided to code up a slightly contrived, yet not incredibly trivial application of genetic algorithms.

My formulation here was simple: given some continuous line, use genetic algorithm to find a vector representation of that same line.

Some piece of art drawn by baby Michelangelo

The representation I went with for that line is a sequence of <x, y> coordinates. The “chromosomes” are modeled as a sequence of <angle, distance> pairs, such that after drawing the first point, subsequent points can be derived by computing the next point given the angle and length of the line relative to the current point:

An ideal set of vectors representing the above line

Not the most industry disrupting concept, but fun enough to code up over a weekend.

A successful breeding of many dumb artists

The way the demo renders each generation of the algorithm is by drawing 10 or so of the lines with the worst fitness values in thick, faint, dark red. Another 10 or so low-ranking instances of the current generation in slightly darker color, and the top 10 or so instances of the current generation in thin, dark red. The target line is rendered in that lively, all natural green.

Next Steps

My next personal goal is to apply genetic algorithms to finding high performing neural nets, as demonstrated in the literature. I realize there is often no real benefit in doing it this way vs using back propagation with grid search or some other hyper parameter optimization strategy, but to follow the advice of wise people, “a paper a week keeps the doctor away”.

Rodrigo Silveira

Written by

Software engineer fascinated with learning, practicing, and teaching.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade