What Elon Musk Can Teach Us About How to Get Projects Done

Elon Musk has had lots of success over the years and there lots he can teach us about how to do projects better. In this we will look at some of the principles that Elon Musk uses to get stuff done…

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




Simple Genetic Algorithm in Python from scratch

Creating a Simple Genetic Algorithm (SGA) in python can be a good way to start learning the basic concepts of optimization problems and how Artificial Intelligence (AI) works behind the scenes. In this post, I will use Python language to guide you in the creation of SGA. However, the basic idea behind it can be perfectly developed under any other language. Even in excel, using solver.

Well, generally speaking, the idea of SGA is to create an algorithm that performs a specific task, finding the best solution using the concepts of an evolutionary process in nature. Something like the survival of the fittest, or the hunger games. The better you are, the better the chances of reproduction and survival.

Broadly speaking, a SGA is composed of two main steps: A population bag initialization and a generational process, which is based on selection, crossover and mutation operators. Of course, there are some other extensions of the problem, but the idea is the same.

Basically, at the beginning of the process, you start by initializing the bag with random individuals (more advanced techniques can use more complex initial conditions) and then you start the generational process. During each generation (you decided how many beforehand) you replace the individuals in the bag for new individuals that are generated by selecting two parents and making a crossover between them and after a child is produced then a mutation operation could be applied to the child. These new individuals are, in theory, a better population in terms of their fitness.

Here is a basic flowchart with the steps:

In order to make things more clear, I’m going to use the Travel Salesman Problem (TSP) as an example to explain the SGA. For those how are wondering what is the TSP. It’s basically a problem where we have different cities (points in a map) and we want to create a route that connects all the points in the best possible way, this is done by minimizing the distance between the cities.

In every problem that we create, we need to construct first a set of functions and parameters that will be used by the algorithm.

Considering a TSP where we have 5 cities, then:

The fitness function is well defined by the sum of all distances in the encoding. And the idea is to minimize this value.

Just to clarify, imagine that we have the route [3 4 2 0 1]. Then, the total distance or fitness value for it will be the cost of travel from city 3 to city 4 plus the cost of travel from 4 to 2, plus the cost of travel from 2 to 0, plus the cost from 0 to 1. This is 51.11 + 57.38 + 17.12 + 28.02 = 153.63.

Before we start, the set of packages that we need to use are numpy, random and pandas.

Now that we have the initial population established, then we can start the evolutionary process of creating the generations. Each generation is going to be represented by each step in a for loop in python. Inside each generation, we’ll need to perform three basic genetic operators. Selection, crossover, and mutation.

After applying these operators, we’ll get one new individual that we’ll need to add to the new bag of individuals. Since this process only creates one child, we’ll need to perform this internal loop until it reaches the maximum number of individuals in the population bag, which is represented by the population size variable, or 10 individuals in this example.

After all this process, we’ll get a set of new individuals. Then, we’ll need to select the individual with the best fitness of that set and it’ll represent the best individual of that generation. After that, we can continue with the second generation.

This generational process will be performed in the same way until we reach the maximum number of generations. In this case, 200 generations. In the end, the selection of the best individual among all generations will represent the best solution for our SGA.

The process of selection is the first step to create a new population for the next generation. There are three well-known methodologies to approach this: the Roulette Wheel method, the Rank method, and the Tournament Size method. In this case, we are just going to focus on the first one. However, all of them try to achieve the same purpose, the selection of two (or more) individuals called parents.

As its name suggests, the Roulette Wheel method is basically a selection process where we pick one individual from our population bag by “spinning a Roulette Wheel”. In this case, the probability of each individual being selected depends directly on its fitness value. Let’s see the code:

The function pickOne takes the population bag as input and evaluates the fitness function for each element in order to select one individual randomly but based on its fitness.

After the evaluation process, we proceed to pick one individual with uniform distribution. After this selection, we create another random number but now between 0 and 1. This number will be used to check whether or not the individual picked is accepted. So, if the probability of been accepted is larger than that random number we pick that individual, otherwise we reject and try again until we find one. This whole process is repeated twice in order to select two parents.

Roulette Wheel Flowchart

Now that we have selected the parents, it is time to recombine them in order to create children. This recombination of chromosomes is called the crossover operator, and it can return one or more children depending on the type of method that we are using.

There are many methods of parent’s recombination. Actually, you can create your own! However, if you want to trust in other’s creations, the most common methods are Order 1, Cycle, Partially mapped, Order Multiple and Insertion. Take into consideration that all these methods are based on permutation problems.

The approach that we are going to use here is the Order 1 Crossover, which is a fairly simple method where the idea is to combine the two parents and create only one child trying to maintain the semantics. The idea is as follows:

For instance, imagine that we have

Parent 1 : [3 2 0 1 4]

Parent 2: [0 2 1 3 4]

Once we create the child using crossover, we can start performing the mutation operator. There are several ways to achieve this; we can use swap, scramble, insert, reverse or any other kind of mutation that fits with our design.

In this case, we’ll use swap mutation. That is essentially when we take two elements at random positions of the child and we swap these positions in the arrange. For instance, if we have the child [2 0 1 4 3] and we chose randomly positions 1 and 4, then the mutated individual would be [4 0 1 2 3], where we swapped the number 2 at position 1 for the number 4 at position 4.

Now that we have the basic ingredients to evolve the individuals over each generation. It’s time to put everything together. To accomplish this task, we simply need to iterate over each step using a for loop and inside each step we need to call the genetic operators that we already established in order to create the new bag, always keeping track of the best individual so far. So we come up with something like this:

After all this evolutionary process of 200 generations our best individual using the SGA resulted to be the route [4 1 3 2 0] which graphically looks like the most effective path to visits all the 5 cities in this example.

So, we have developed a SGA that after a few generations can determine an optimal solution for a specific problem. Of course, this was a toy example. However, this idea can be extended to a much more complex problem. The SGA will be pretty much the same, while the definition of the problem will change.

I hope you liked this explanation and if you did, don’t forget to clap and subscribe to Data Cat to stay tuned for more Data Science topics.

Add a comment

Related posts:

What is HD derivation path?

What is HD derivation path?. The HD derivation path is a string that makes it easy to handle multiple crypto currencies (assuming they all use the same signature….

Most Americans Have Not Reported Cryptocurrency Gains in Their Taxes This Year

Most Americans are not claiming their cryptocurrency gains, cryptocurrency mining causing energy shortage in Iceland, top 5 gainers and losers over the past 24 hours, and more!

ICSE Syllabus for Class 9

Class 9 is just as significant a turning point as Class 10 in a student’s life. It is also a starting point for classes 10,11, and 12, and many other competitive entrance exams. Class 9 is the…