There are many traditional ways to tackle these kinds of problems without resorting to brute-force, like a calculus-based “hill-climbing ” technique and random search algorithms; each has its own pros and cons. One approach which has some advantages over traditional methods is to use genetic algorithms.

Genetic Algorithms Overview

Genetic algorithms are search algorithms that are inspired by processes observed in natural evolution. They are randomized but exploit historical information to intelligently search the solution space which maximize or minimize a particular function.

The idea is that we have a population of candidate solutions ( individuals ) and they theoretically evolve to better solutions over each iteration. Each candidate solution has properties ( DNA ) .The more fit individuals are more likely to produce children and these children are more likely to be better than their parents . The children’s DNA are produced using operations inspired by how actual DNA s are altered. Each successive population is more likely to be better than the previous generation.

DNA and FITNESS

To illustrate how genetic algorithms work, let’s apply it to the travelling salesman problem. Say we have a set of five cities : 0, 1, 2, 3, and 4 each on a location represented by two numbers ( Px , Py ) given a range of float numbers.

First let’s make a genetic representation of the solution domain ( DNA ). In this case, this is the order of the cities . The solution is a permutation of cities like [1, 0, 2, 3, 4] and [4, 3, 1, 0, 2] which we can represent as an array of integers. We also have to define a fitness function which we will use to quantify and evaluate if the solution is better or worst. In this case, we want to get the shortest route, we can define our fitness function to be one divided by the sum of the squares of the distances from one city to the next.

How it Works

Now we simulate the evolution process.

First we initialize a set of individuals with DNA . We can seed the first solution space if we already know which set order of cities are more likely candidates . For simplicity, let’s just randomly select a set of city orders. We can evaluate the individual ’s fitness given its DNA and our fitness function. The fittest individual in this population is best solution we’ve seen so far, let’s call it the champion .

For each iteration, we should generate a new population from the previous one. Each population has its own fittest individual, a challenger which could replace the champion . Essentially, each iteration we find the champion which is the best solution we’ve seen so far . After the series of iterations, the final champion’s DNA is the best solution we’ve seen. We can call this a run . We might have to run a simulation several times to get the best solution, if at all, because the algorithm is probabilistic in nature. Repeat this until you’re happy with the results!

The population size of each and number of iterations per run is something that we can tune.

How to generate the next population?

As mentioned earlier, the more fit individuals are more likely to produce children . We need to define a way to probabilistically select individuals that produce children based on their fitness . Let’s make it such that the probability of selecting these individuals are proportional to their relative fitness relative to the population , this is also known as weighted choice or roulette wheel selection.

Let’s take two parents and use them to generate two children until we have a population the size that we’ve indicated. We can actually use any number of parents to produce any number of children but for simplicity let’s use two parents and two children .

Also mentioned earlier, the children ’s DNA are produced using operations inspired by how actual DNA s are altered based on the parent ’s DNA . There are many ways to do this, for simplicity, let’s probabilistically alter using types of crossover and mutate operations. We’ll discuss these two in the next section. Let’s define two more parameters to tune which is the probability of crossover and probability of mutation. The crossover probability is the probability that we will perform the crossover operation. We don’t need to perform the crossover all the time, we can directly clone the parents sometimes. There is a probability that the parents are already a good candidate solution and that the crossover is an unnecessary expensive operation that might make the solution set worse. Similarly, mutation probability is the probability that a mutation is performed.

Crossover and Mutate