Genetic algorithm to approximate a target image using semi-transparent rectangles.
Inspired by this blog post and this book.
go run pop.go -i "input.jpg"
- Generate a number of DNA elements with randomly sized and colored rectangles.
- Repeat the following:
- Calculate a fitness value for every element by comparing it to the target image.
- Repeat the following until a new population of the same size is created:
- Select two elements while favoring elements with high fitness value.
- Combine the two elements into a new DNA element.
- Randomly mutate the newly created DNA.
After waiting for a good ten minutes we get something that vaguely resembles the Mona Lisa:
On the other hand, it can replicate a red square quite easily:
And it gets pretty close to the Windows XP wallpaper as well: