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:
- Amplitude
- Length
- 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.
The output
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 code
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.