# Painting with Genetic Algorithms

in #programming5 years ago

Using genetic algorithms to build images from layers of translucent polygons.

Inspired by this and also this.

Just learning Python! The code is here: https://github.com/darkFunction/PolygonPainter.

The `old_painter` directory contains an non-breeding strategy in which a single entity or 'artist' is mutated according to a set of weightings, and un-mutated if the fitness is worse than it was before the change. This is similar to the Mona Lisa implementation linked above. We use an object heirarchy of shapes, points, and colours,and use the Command Pattern to encapsulate and optimise changes to these objects.

I found it to not work as well as I had hoped, and was blindly playing trial and error with the parameters to tweak the quality of the results. The program would quickly reached a local maxima, especially when given full freedom of range on the alpha channel.

For the new version I wanted to use a 'pure' genetic algorithm approach, so each artist is represented by a string of 'DNA', of the format:

``````numPolys, polyVertCount, R, G, B, A, x1, y1, x2, y2... R, G, B, A, x1, y1...
``````

We initialise a population and run it through an evolution cycle as follows:

• The two fittest pass into the next generation unchanged
• The rest are bred together in pairs by splicing the DNA sequence to produce an offspring which is then mutated
• The pairs which are chosen to breed are randomly selected with a bias towards the fittest

## New Improvements

• Mutations are either hard, medium, or soft, and we switch between these levels every 100 generations. Hard mutations are the changing of all of a single polygon's properties to new values. Medium changes a single parameter in the DNA string to a random value, and soft changes a single value by a small amount.
• Polygons are initialised around a point and with a limited size, to encourage regular shapes.
• DNA is spliced together polygon by polygon as opposed to value by value, since it makes more sense to treat polygons as atomics.
• Elitism is increased by discarding the bottom few from each generation.
Sort:

You have my upvote : ) Implemented very similar algorithm out of curiosity years ago. Not a python coder myself, but like your code. Cheers!

Thanks! I was very surprised how much better the pure GA approach was than the 'hill-climbing' method. It really works.

Congratulations @darkfunction! You have received a personal award!

Happy Birthday - 1 Year
Click on the badge to view your own Board of Honor on SteemitBoard.

By upvoting this notification, you can help all Steemit users. Learn how here!

Congratulations @darkfunction! You have received a personal award!

2 Years on Steemit
Click on the badge to view your Board of Honor.

Do not miss the last post from @steemitboard:
SteemitBoard World Cup Contest - Semi Finals - Day 1

Participate in the SteemitBoard World Cup Contest!
Collect World Cup badges and win free SBD
Support the Gold Sponsors of the contest: @good-karma and @lukestokes

Do you like SteemitBoard's project? Then Vote for its witness and get one more award!

Congratulations @darkfunction! You received a personal award!

 Happy Birthday! - You are on the Steem blockchain for 3 years!

You can view your badges on your Steem Board and compare to others on the Steem Ranking