Genetic Art

I was experimenting with a lot of Genetic/Hill Climbing algorithms to generate Art. I started out by trying to approximate a Grayscale image with just lines of varying width and color intensity.

The genetic algorithm is roughly like this:

Algorithm: GA(n, χ, µ)
    // Initialise generation 0:
    k = 0;
    Pk = a population of n randomly-generated individuals;
    // Evaluate Pk:
    Compute fitness(i) for each i  Pk;
    do
        { 
        // Create generation k + 1:
        // 1. Copy:
        Select (1  χ) × n members of Pk and insert into Pk+1;
        // 2. Crossover:
        Select χ × n members of Pk; pair them up; produce offspring; 
        insert the offspring into Pk+1;
        // 3. Mutate:
        Select µ × n members of Pk+1; invert a randomly-selected bit in each;
        // Evaluate Pk+1:
        Compute fitness(i) for each i  Pk;
        // Increment:
        k = k + 1;
    }
    while fitness of fittest individual in Pk is not high enough;
    return the fittest individual from Pk;

The first image after several geneartions..

ga

I lost the original image I was trying to approximate.. but doesn’t that look like a deer? 🙄 I modified the algorithm several times to approximate my face. This is the original image..

FYI this is what I look like

ga

First Attempt: Program too slow. Had to stop after 10 Hours.

ga

Second Attempt: Rewrote the program to use Numpy arrays and Bezier Curves instead on just Lines. Also now in Technicolor!

ga

Bugfixes & Third Attempt: Atleast it’s converging to something

ga

But I didn’t look like this at all :/ . The problem here was that the fitness function was

$ \sum_{pixel=1}^{NPIXELS} abs(original[pixel] - Generated[pixel]) $

I tried squaring. squaring increases error distance. So fitter drawings would have a higher chance of being selected.

$ \sum_{pixel=1}^{NPIXELS} (original[pixel] - Generated[pixel])^2 $

and it worked (sort of. from an artistic point of view)!

ga

I used this code to generate a lot of images, of different shapes.

ga

My eventual plans were to create a bot which tweets these images. I even wrote a small program that gets a random Picassa image and bezierifies it. Unfortunately, someone else has already done it. And it’s much better than mine. But I learned a lot doing this project.



Written by Atul Vinayak on 01 October 2016