# Evolutionary Art using a Genetic Algorithm

Articles —> Evolutionary Art using a Genetic Algorithm

The genetic algorithm is a powerful algorithm that simulates the process of evolution *in silico*. The algorithm works by iteratively operating on a population of 'organisms' - during each iteration, also called a generation, organisms are ranked by fitness: while less fit organisms do not survive through to the next generation, more fit organisms not only survive but can mate with each other. Mating recombines their characteristics to produce new individual organisms and repopulates the species. To assure diversity never runs out, random characteristics can be added to the population at a certain rate via mutation.

The genetic algorithm has been applied in the past to generate artistic work, an example of which is the work done by Karl Sims. Below I used a similar approach described by Sims to create 2-dimensional images - images defined by a red, green, blue, and alpha (or transparency) component (or channel) - each 8-bit - at pixel position x and y. The value in each channel of an image is dependent upon a function, the function itself dependent upon the pixel position x and y:

**channel = f (x, y)**.

The function f(x,y) is an equation defined computationally as a binary tree (fig 1) - nodes represent either a mathematical operation (operation nodes) or a numeric value (constant nodes). Numeric values can be one of either a constant value or that of the input x or y value themselves (constant nodes have no children, and are thus leafs in the tree).

Figure 1: Two examples of a function/equation (top) and how they may be represented as a binary tree (bottom).

Mathematical expressions can be represented by many different operations, including +, -, /, *, sin, cos, tan, asin, acos, atan, abs, pow, log, log10, and, or, xor, min, max, and exp (some operational nodes, given their expression operates on a single value (for instance sin, cos, and tan) must be restricted to a single child node). To calculate the value at position x/y the tree is recursively traversed beginning at the root node - for each non-leaf node the child value(s) are retrieved and the appropriate math performed, the result of which is returned to its parent. Of course, values from some equations can return values which fall outside the 8-bit range for a pixel (0-255). One can deal with this by rounding to the nearest value (0 or 255), or normalizing each value against the minimum and maximum values (eg 255 * (value - min) / (max - min) ). In the examples below I used the former approach.

To create the images below, a population was generated consisting of randomly generated functions and their respective images. After the initial and each subsequent generation, images were selected visually and their respective equations carry over to the next generation. During re-population, new equations (and its respective image) were generated by 'mating' (merging randomly selected branches of two trees) and mutation (nodes can be removed, added from a randomly generated tree, and/or constants modified). Typically in the first few generations many noisy, messy, or constant color images turn up, however these images can be quickly selected out in favor of more symmetric and colorful images.

Some example images created using a Genetic Algorithm, each pixel is represented by an underlying mathematical equation for each channel...

While the number of possible equations is infinite, I often observed patterns manifest themselves time and again, the patterns typically polygonal, cellular automata like, and often symmetric around the diagonal. This suggests that the evolutionary landscape may be restrictive to these patterns. This being said, there are many variant approaches one can take to change the landscape and produce more variant images. For instance, biasing how often each mathematical operation is used, constraining constant values, enforcing dependencies between pixel positions, and/or applying kernels to the resulting images (who themselves may be governed by a genetic algorithm). Further, one can carry out a similar process in 3D, or add time to the equation to generate animations.

## Resources

There are no comments on this article.