Playing God: An Intro to Genetic Algorithms

turtlelegz
The Zerone
Published in
5 min readDec 14, 2023

Is it a bird? Is it a plane? Wait, why do planes look like birds? Well since birds are already good at flying, man decided it’d be wise to learn from them.

From birds and planes to kingfishers and bullet trains, many of man’s inventions imitate Gaia’s creations.

This is no coincidence, of course, nature is really good at optimizing and humans are really good at copying nature. ‘Biomimicry’ is the official term that describes the act of using nature as a blueprint for our technologies. This does however beg an interesting question: How does nature invent things? Correct me if I’m wrong but humans are smarter than birds by a good amount, so how can nature be so proficient at making such specialized tools? Fortunately, this was answered in 1859 by biologist Charles Darwin.

Darwin proposed in his theory, that all living organisms have changed to fit their environments through natural selection. The whole process of evolution is indeed quite remarkable. The knowledge of a parent is not usually passed down to their children, as their genetics are enough. Evolution is flexible to changes to the environment and the results are so good that we humans base our technology on it.

So what would happen if we try to replicate evolution itself?

What if we engineer an algorithm that can improve by undergoing it’s own version of ‘natural selection’. Such an algorithm would tend to an optimal solution that may not be easily determined by logic alone. That my friends is what we call a genetic algorithm.

The Theory Of Evolution

So enough teasing, let’s get our hands dirty. Genetic algorithms are a class of optimization algorithms that imitate the process of natural selection. Just in case you don’t remember your high school biology class, I’m gonna give a quick recap on the theory of evolution.

Every story has a beginning, and for most living things that beginning is their ‘genome’, a part of their cells that encodes their traits and characteristics. The features of this genome can be advantageous or sub-optimal, depending on the environment of the living being. “The survival of the fittest”, as Darwin put it, is nature’s way of weeding out unfavorable attributes, so that only useful ones are passed down. While this ensures that every subsequent generation becomes stronger, there is one more trick that greatly improves the process of evolution.

Every now and then, either by random chance or by external factors like radiation, the genome may not replicate perfectly. These slight variations can cause new features to appear in the species. If this new feature is good i.e. it increases the creature’s survival chance, it will most likely be passed down to its offspring becoming a permanent trait of the species. In organisms that reproduce sexually, the genes of two parents are combined to create more variety and stronger descendants. Over millions of years and many generations, these species evolve into refined creatures that we try to mimic today.

Putting Theory Into Practice

Now then, how do we translate this mechanism into a computer algorithm? Well let us walk through a simplified example step by step. Remember, our goal is to produce a sequence of steps, which when carried out, result in an optimal solution to a given problem.

To start, our ‘genome’ can be a binary string where each sequence of bits encodes a unique step of the solution. We need to put our genomes into a hostile environment, which can be done using a ‘fitness test’. The fitness test will judge and rank the algorithms based on how close they are to the desired solution. We then discard low scoring solutions and use the fraction that performed best to make the next generation.

Then, we can introduce random changes to the string or ‘genome’ in every new generation, thereby introducing mutations. The mutations introduce a factor of randomness which provides an opportunity for new and creative solutions to arise. Finally, similar to evolution, by mixing and matching two good genes in a ‘crossover’ step, we can create more variety in our genes leading to a better solution set.

And we are done! By starting with a random set of genomes as our population and repeating this process of fitness testing, elimination and mutation, we can run our genetic algorithm for multiple generations until we find a solution that we are satisfied with.

So what’s the big deal?

At first glance this technology may seem too simple to accomplish anything. But believe me, what we have created is a very powerful tool. We have the power of evolution in our hands, the same gift that transformed single celled organisms into complex biological marvels, and the possibilities of what we can do with it are endless.

Instead of just copying god’s creations, we can now copy god’s process of creation itself.

You don’t have to look far to see the early applications of genetic algorithms. Tesla are using it to fine tune their neural networks to make better self-driving cars. Google’s DeepMind team have applied it to artificial intelligence to develop new revolutionary protein folding algorithms. Boeing have utilized genetic algorithms to optimize the wing design of their planes and it’s better than what any human engineer has managed to accomplish. And the exciting part is we’re just getting started.

But There’s Always A Catch

Of course , it would be dishonest of me to not tell you about some caveats. While I’ve described the core concept of genetic algorithms, the actual implementation is far more complex. If you’re adept at coding, you may even feel confident enough to implement it yourself. However, you’ll soon find out that converging to the solution is no easy task. The factors such as mutation rate, fitness test, initial population and crossover rate heavily affect our chances of solving the problem and it involves a lot of fine tuning of parameters by us. Sometimes, our algorithm can converge and get stuck on a local maxima which prevents it from finding the global optimum. So we should treat genetic algorithms with a grain of salt, as they’re not exactly a magic wand from a fairy tale.

Closing Thoughts

Revisiting the topic of planes, in 1903 The New York Times published an article which predicted that “man won’t fly till another million years”. Only 9 weeks later, the Wright brothers conducted the first successful manned flight, forever changing the course of history.

Today, we are at a similar turning point, at the dawn of a new age that will mark a shift in human civilization. Genetic algorithms are not yet perfect but as they are sure to improve, our future will be altered in ways that we can’t imagine. Right now, we are much like Homo Erectus discovering fire for the first time, not fully understanding what it’s capable of, but marking the next step in OUR evolution.

--

--