anopara / genetic-drawing

A genetic algorithm toy project for drawing
MIT License
2.18k stars 196 forks source link

Algorithm explanation #9

Closed empireshades closed 4 years ago

empireshades commented 4 years ago

Hi @anopara. Thank you for sharing this awesome toy exercise. I was wondering if you or someone else could explain how the algorithm works? Despite repeated review of the code, my python fu is limited.

sgmoratilla commented 4 years ago

@empireshades roughly speaking, a GA is random search guided by a target function. In this case, the target function is how much differs the image proposed by the algorithm and a greyscale version of the original image.

The algorithm codes the brush strokes in six values: (x,y) position, brush size, type of brush (there are 4), color and rotation. At every step, it generates a random combination of those and tests how good that stroke is. If it is better than the previous ones, it keeps it. Otherwise, it drops it and try again. Of course, there are a ton of details that I'm skipping :)

In the notebook you can see that the algorithm is called twice. The first call draws a sketch, the second call draws the details (using a mask if you provide one).

Hope it helps!

empireshades commented 4 years ago

@sgmoratilla Thank you for the explanation. This was my general understanding of the algorithm as well. It’s those mini details that I’m not really understanding ;) I feel like it’s the type of thing I won’t truly get unless I coded it from scratch myself.

anopara commented 4 years ago

@empireshades I'd love to write a more in-depth explanation at some point, but atm cannot allocate a lot of headspace to this project atm. Hopefully, one day!

Quick'n'dirty expansion on @sgmoratilla 's very nice explanation of the genetic algorithm part:

empireshades commented 4 years ago

Thanks all. This def helps clarify the logic, especially the directions of the strokes against the edges.