Some more sample results from a drawing app I am working on that uses a genetic algorithm to generate drawing (click for higher resolution):
Above: face 1, Below: face 2 (same source image as face 1)
Above: one generation of faces (same source as face 1 and 2)
Above: face 3 (different source image), Below: landscape.
The effects of mutations in one generation of spirographs. Choosing only one graph to survive from the previous generation (not a natural thing to do) results in 36 offspring from the one graph. It is an good way to check the impacts of mutations in the genetic algorithm and has an interesting outcome. The parent graph is below.
This Spirograph algorithm is not a strict interpretation of the physical Spirograph device but it generates some very similar and interesting results. The basic algorithm is to blend three circular equations together. Specifically, for values of i from 0 to 2 pi, an x and a y value are generated with :
x = r1 * cos(nr1 * i + o1) + r2 * cos(nr2 * i + o2) + r3 * cos(nr3 * i + o3); y = r1 * sin(nr1 * i + o1) + r2 * sin(nr2 * i + o2) + r3 * sin(nr3 * i + o3);
In many ways, this is like having three lines of length (set as r1, r2, and r3) being placed end to end with each one rotated a given number of times (set as nr1, nr2, and nr3). Each of the rotations also can be offset (set by o1, o2, o3).
Two different graphs can be drawn in this way and each of those graphs can be duplicated by changing the number of iterations (o iterations would hide graph). Each iteration can be offset by an angle or have its radius scaled.
This year, I’m using a new drawing app that I’ve been working on to generate files to load into Inkscape and draw using the Eggbot. In general the app processes an image file and creates an SVG based on it. These are several different renderers. The four eggs above are the most abstract. The algorithm draws wandering lines based on the brightness values of pixels in the image.
The image below uses a hatching program that can be quite photo realistic. I have not yet drawn it successfully using the Eggbot and sharpies. I did manage to draw it with pencil, only to have it smear off the eggshell.
This year’s addition to my eggbot programs is a genetic algorithm that evolves complex sine waves. Each of the eggs in the photo above was printed from the same program with waves evolved from random seeds.
The individual designs
Each design is a sine wave with eight different parameters:
- The number of waves to be strung together
- Amplitude of the first modulation applied to the amplitude
- Length of the first modulation applied to the amplitude
- Amplitude of the second modulation applied to the amplitude
- Length of the second modulation applied to the amplitude
- The modulations to be used (either 1 or both 1 & 2)
The video above shows how the 8 parameters modulate the wave and how patterns build up over time.
How the genetic algorithm works
Each time the program is run, an initial population is created with 50 individual designs — each with random values assigned to the eight parameters. You then rate each individual design before evolving the next generation. The algorithm chooses individuals to carry on to the next generations (highly rated designs are more likely to be carried forward, but low rated designs may still be used).
Designs that are carried forward are likely to be bred with other designs chosen for the next generation. Breeding involves picking a random integer N between 1 and 8 and taking the first N parameters of one individual and swapping them with the first N parameters of the other. This cross-over of the parameters creates new variations of the designs with qualities of both individuals (the parents). There is also a slim chance that one or more of the child’s parameters will change (mutate) between generations.
The evolution process makes it relatively easy to search through a very wide range of possible combinations of parameter values by simply choosing designs with aspects that you like and seeing what different combinations of these qualities look like.
I found that populations of 50 individuals are broad enough to get very interesting results in fewer than 10 generations while not having to rate too many design with each evolution.
Below is a collection of 10 different designs generated in the same session.
Each column in the image above shows the lineage of the design in the top row. This top row is the 5th generation. The next row down is the highest rated parent from the 4th generation. Coincidentally, neither of these designs were changed between the 3rd and 4th generation.
The program is written in processing and the source is available on github.
The algorithm is not limited to sine waves. To implement your own design, modify the Indivdual.pde file with any number of parameters. All my parameters are floats between 0 and 1, but this is not a requirement so long as you adjust your this class’ mutate function to fit your variable types.
The other classes in the repository set up the UI and manage the evolution or individuals over multiple generations. You may choose to manipulate the following three variables in GA_eggbot.pde to work with different population sizes and different probabilities for crossovers and mutations.
If you do make a design of your own, let me know in the comments below.
Sine waves were a popular choice for this year’s eggs. And the Minecraft pigs were in demand as always.
All the eggs pictures except the pigs were plotted directly from Processing, the pigs were drawn using Inkscape.
I’ll post the files to Github within a couple weeks.
update: code and svg files are now available on Github.