Genetic Algorithms for Beginners
Tags: beginners
Published: 2021-08-27 08:23:15

Genetic algorithms are part of the family of optimization algorithms. They operate on the theory of evolution, more particularly, genetic evolution. Each solution is a chromosome that's made up of genes, and is evaluated to determine how well it performs. This repeats until a good solution is found.

Evolution suggests that the living organisms that we see today did not suddenly exist that way, but evolved through millions of years of subtle changes, with each generation adapting to its environment. See this article for more: [https://rhurbans.com/intelligence-through-evolution/](https://rhurbans.com/intelligence-through-evolution/).

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mv5lcis6m1ft1raba9a3.jpg)

Genetic algorithms are optimization algorithms. They are used to evaluate massive search spaces for good solutions fast. See this article for more about optimization algorithms: [https://rhurbans.com/optimization-finding-the-best-solutions/](https://rhurbans.com/optimization-finding-the-best-solutions/). Let's dive into how genetic algorithms (GAs) work next.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kaptgtn24riteg2f3ryu.png)

When we use a GA, it is paramount to do the encoding step correctly. It requires careful design of the representation of possible states. In this case we want to figure out which items to include in the knapsack to maximize the total value and not exceed the weight limit.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wanmxsxfpowpl53nfpd8.jpg)

Binary encoding represents a gene in terms of 0 or 1, so a chromosome is represented by a string of binary bits. For the knapsack problem, 0 = exclude the item, and 1 = include the item. We can then sum the included items' values to find how well that chromosome performs.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/84xwrywvgsl6s9gy4c4y.jpg)

When the search space is small, it's fairly easy for us to figure out a good solution. But what if there were 26 possible items to add to the knapsack? It becomes much more difficult for a human to solve it by hand. Here's where GAs are useful.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yplbvwe4am3hazh7tkse.jpg)

A genetic algorithm evaluates the performance of each chromosome and uses mixing to make new solutions that hopefully perform better - like human reproduction. Mixing chromosomes can happen in different ways. A simple approach is to take half and half.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dur20cwibkbfjmpo96w7.jpg)

Genetic algorithms also use the concept of mutation. Like in nature, a random gene might be changed after reproduction between two parent chromosomes. This entire process runs for many cycles, called generations, until a good solution is found.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/19yxz6d12kj6x9qr7wxm.jpg)

GAs can perform significantly faster than other methods. But remember, they're not guaranteed to find the BEST solution. I'll be elaborating on genetic algorithms with more visual explanations in upcoming articles. Follow [@RishalHurbans](https://twitter.com/RishalHurbans) for updates.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/y9u8dzunwgjo8gyyw8tc.jpg)

Thank you for reading. If you're keen to learn at your leisure, see Grokking AI Algorithms with Manning Publications: [http://bit.ly/gaia-book](http://bit.ly/gaia-book). Or join my mailing list for infrequent knowledge drops: [https://rhurbans.com/subscribe](https://rhurbans.com/subscribe).