0% found this document useful (0 votes)
25 views

Intro GA

Introduction to Genetic Algorithm

Uploaded by

Varun Singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
25 views

Intro GA

Introduction to Genetic Algorithm

Uploaded by

Varun Singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 14
This is your last free member-only story this month. Sign up for Medium and get an extra one @ Towards Al Editorial Team ( Jan 18,2021 . 11min read O Listen CY save ‘Source: Derivative from gimono and comfreak on Pixabay COMPUTER SCIENCE, EDITORIAL, PROGRAMMING Genetic Algorithm (GA) Introduction with Example Code An introduction to genetic algorithms, optimization, and implementations with code examples in Python Author(s): Sujan Shirol, Roberto Iriondo each step diagrammatically. As always, we are including code for reproducibility purposes. We have split the code when required while exploring the different steps involved during our implementation. Make sure to check the full implementation from this tutorial on either Google Colab or Github. Join us | ‘Towards Al is a community that discusses artificial intelligence, data science, data visualization, deep... towardsai.net What is a Genetic Algorithm? A genetic algorithm belongs to a class of evolutionary algorithms that is broadly inspired by biological evolution. We are all aware of biological evolution [1] — it is a selection of parents, reproduction, and mutation of offsprings. The main aim of evolution is to reproduce offsprings that are biologically better than their parents. A genetic algorithm mainly bases on Darwin's Theory of Evolution by Natural Selection, and it tries to simulate the same. The basic intuition is selecting the best individuals as parents from the population, asking them to extend their generation by reproducing and having their children during the reproduction process where genes of both the parent's crossover there occurs an error known as mutation. These children are again asked to reproduce their offsprings, and the process goes on, leading to healthier generations. This theory has inspired evolutionary computation to solve optimization problems, feature selection, classic knapsack problem, and many more. Let’s understand the application of the genetic algorithm with a knapsack problem. Suppose we are on a treasure hunt, and after all the efforts and hard work, we finally find the treasure in a deep-down cave full of gold and diamond ornaments. The first thing we desire to do is fill our backpack with as many ornaments as possible. However, a few parameters have to be taken care of in our problem, and our backpack has limited space. It cannot carry a weight of more than 35 kilograms. Open in app. should not damage the other within the backpack — this where a genetic algorithm comes into play to optimize our problem by taking care of all the parameters. Now that we have a basic idea of genetic algorithms. Let’s see the steps involved and code our implementation with Python. Steps in a Genetic Algorithm 1. Initialize population 2. Select parents by evaluating their fitness 3. Crossover parents to reproduce 4. Mutate the offsprings 5. Evaluate the offsprings 6. Merge offsprings with the main population and sort “Ina genetic algorithm, a population of candidate solutions (called individuals, creatures, or phenotypes) to an optimization problem is evolved toward better solutions. Each candidate solution has a set of properties (its chromosomes or genotype) that can be mutated and altered; traditionally, solutions are represented in binary as strings of Os. and 1s, but other encodings are also possible [2] [31”. Chromosome Gene Population 1. Ini ‘The algorithm generally starts with the randomly generated population. The size of the jalization population depends on the nature of the problem. We can use 0s and 1s encoding. However, in this tutorial, we will be using uniformly distributed numbers to represent each gene. # Placeholder for population = (} # population size npop = 20 # lower bound varmin = -10 # upper bound varmax = 10 # cost function costfune = sphere # each inidivdual has pos for i in range (npop): population[i] = ("position': None, 'cost': None on (chromosomes) and cost range (npop) : ulation[i] ["position'] = np.random.uniform(varmin, varmax, num_var) population[i] ['cost'] = costfunc (population|i] ["position']) We create a dictionary to hold the population, and cach individual is associated with chromosomes(position) and a cost. The position is filled with randomly generated uniformly distributed numbers(genes) with a lower limit -10 and an upper limit +10. Cost is the cost function we are trying to optimize. In this tutorial, we will be optimizing the sum of squares of x, where xis the individual gene of each chromosome. # cost function def sphere (x): return sum(x**2) 2. Parent Selection During each successive generation, a portion of the existing population is selected to ial Q « Random selection: This is the simplest and most inefficient way of selecting parents. In this method, we shuffle the population by performing permutation and select the first two individuals as parents for breeding. This method is not recommended because it does not follow “Darwin's Theory of Evolution by Natural Selection,” wherein individuals are selected based on their fitness, not randomly. q = np. random.permutation (npop) pl = population(a[0]] p2 = population(q{1] * Tournament selection: This method is based on the probability of selection of each individual. We run several tournaments among a randomly selected group of individuals, select one individual from each group as the winner, and again run the tournament by grouping winners from the first iteration, repeat the process until we converge to two winners parents for breeding. The best member of each group in every iteration has the highest probability of selection. + Roulette wheel selection: This is a widely used and most efficient method for selecting parents; hence we will be using it today in our algorithm. We all know how the roulette wheel works in casinos, drop the ball, spin the wheel, and wait till the wheel stops to see which pot the ball falls in. Let's take a deeper dive into the implementation part. Roulette Wheel method for parent selection ‘Source: Unsplash The only difference between the casino roulette wheel and the roulette wheel method for parent selection is that in the casino roulette wheel, each pot has an equal probability of holding the ball when the wheel stops rotating. However, here we define the probability for each pot(individual of the population). The probability of each individual is called the fitness of the individual. P1= 10% P3 = 30% P4 = 40% Total = 100% P3 rotated. When the wheel stops rotating, the parent where the arrow points to is chosen for breeding—the greater the probability larger the area on the wheel, leading to a higher probability of being selected. Now, how do we implement the roulette wheel programmatically? We open the wheel into a uniform line and divide the line into the number of parents in the population, and each parent occupies the space on the line equal to its probability of being selected, and each cut point is the cumulative sum of probability. Generating arandom number between 0 and 1 will act like the arrow that selects the parent for breeding. Here, the random number is 0.28; hence the winner is P2. Pa 10% v P4 40% Random Number = 0.28 Figure 3: Generating a pseudorandom number to select the parent for breeding To make it even simpler, we calculate each parent's probability’s cumulative sum, multiply its sum with a randomly generated number. Then get the index of the first parent whose cumulative value is greater than the random number. For example, Pl has accumulative value of 0.1, P2 has 0.3, P3 has 0.6, and P4 has 1. If the random number generated is 0.28, then the first parent whose cumulative value is greater than 0.28 is P2 hence the winning parent for breeding. Function argwhere() returns an array of Trues, and Falses based on the expression passed as a parameter. wheel_selection(p): r = sum(p) * np.random.rand() ind = np.argwhere(r <= c) return ind(0] [0 pl = population [roulette wheel_selection (probs) ] p2 = population[roulette wheel selection (probs) | ability for roulette wheel selection # beta = 1 for i in range (len (population) ): # list of all the population cost *cost']) ulating costs. append (population [i] np-array (costs) np .mean (costs) costs = cos probs = np.exp(-beta*costs) 3. Crossover Now that we got our two parents for breeding, the next step is to perform crossover/mating/breeding. Crossover refers to the process where certain genes from both the parent chromosomes are overlapped or mixed or swapped to produce new offspring. Since the offspring is the result of the parent chromosomes’ crossover, it inherits both the parents! characteristics. There are three methods to perform crossover. * Single-point crossover: In this method, both the parent chromosomes are cut at the same random point, and the leftover parts are swapped to produce two new offspring chromosomes. Yellow-colored genes represent the cutoff part of the chromosome. Parent 1 | t}ojolalojajajaj= t JH me Parent 2 2 — 0, oj|1 Figure 4: Single-point crossover. + Two-point crossover: A method similar to the single-point crossover, but the only difference is that the parent chromosomes are cut at two random points. Again, the yellow-colored cut off part of the chromosome is swapped. rpeyzypepayayzyay _ Chil Figure 5: Two-point crossover. + Uniform crossover: We first randomly choose which genes are supposed to be inherited from both the parent chromosomes and genes not inherited are marked in yellow color. Then, we model them as 0s and 1s, which are written in green color. The gene to be inherited is encoded as 1, and the gene that should not be inherited is encoded as 0. This series of 0s and 1s will be referred to as alpha from now on. Multiply the gene value with the corresponding alpha value for both the parents and then add the results to generate a single gene of the offspring chromosome. Let's consider the first gene of each parent chromosome, For parent-1, the gene value is 1, and the corresponding alpha value is also 1; hence, 1x1=1. For parent-2, gene value is 0 and the corresponding alpha value is also 0 hence, 0x0=0. The first gene of the offspring chromosome is 1+0=1, and so on — this way, we get offspring-1, to reproduce the offspring-2, we take the compliment values of alpha and carry out the same process. 1 0 1 0 o 4 Q 1 43% 2S @ ew 1] 0 el] Oo) ay) 2 | a a ao he ath ae a 1 1[1]o/1|a| ao [2 lols lof: . © 2 D f @ aH 4 Figure 6: Uniform crossover. Programatically, we copy both the parents into the child variable: cl, ¢2. Randomly generate uniformly distributed alpha values between 0 and 1, which is the parent chromosome’s shape (position). The rest of the process remains the same, except, in theory, we take the complement of alpha values to produce offspring-2, whereas, in the program, we swap the parents while multiplying with alpha, which is the same as taking the complement of alpha values. on"] 4. Mutation Mutation is a natural process that occurs due to an error in replication or copying of genes. While performing crossover, we replicated the parent chromosomes by mix- matching genes of both the parents. There is no guarantee that the copying of the parent gene is 100% accurate. There always occurs an error, which leads to the scope of exploration. For example, if both of your parents have brown eyes and blue eyes, that is probably because of a mutation that occurred due to an error while copying your parents’ genes, and your subsequent generation might carry forward that characteristic. Mutating the chromosome in the genetic algorithm is necessary because it may result in revolutionary results that will help solve our problem more efficiently. So, we have three parameters: the child chromosome(c), the mutation rate(mu), and the step size(sigma). ‘The mutation rate(mu) determines the percentage of the child chromosome that undergoes mutation. To define which genes will be mutated, we generate random numbers and compare them to the mutation rate then we find the indices of the child chromosome(position) that have values less than the mutation rate using the argwhere() function. Replace those indices with new(mutated) genes generated by multiplying the step size(sigma) with randomly generated value and adding it to the original gene. def mutate(c, mu, sigma): # ma - am mx % of gene to be modified # sigma - step size of mutation # mutation = original gene + (step size * random number) y = copy.deepcopy (c) # array of True and Flase, indicating the mutation position ag = np.random.rand(*(c¢["position').shape)) <= mu ind = np.argwhere (flag) "][ind] += sigma * np.random.randn (*ind. shape) y['position return y ial Q Open in app. aluate first off spring tion of cost fun nild 1 = costfunc(ci["position']) tsol_cost float: # replacing best solution in every generation/iteration if cl['cost'] < bestsol_cost: bestsol_cost = copy.deepcopy (cl) # replacing best solution in every generation/iteration if cl['cost'] < bestsol_cost['cost"]: bestsol_cost = copy.deepcopy (cl) # Evaluate second off spring e2[" ] < bestso: ‘ost ['co: 50l_cost ~ copy.deepcopy (c2) 6, Merge Offsprings with the Main Population and Sort Merging the offsprings is vital for them to be considered as parents to reproduce the next generation. Upon sorting the new population, we have better individuals at the top. Since the population size remains the same as the first iteration(npop), the number of individuals at the bottom of the sorted population equal to the number of new offsprings produced in the previous iteration are eliminated from the selection process to breed new offsprings, and the process continues — this is how the process of elimination takes place. en sees population ot we of size Sort Elimination considered we run 300 iterations. Iteration Iteration Iteration Iteration Iteration Iteration Iteration Iteration Iteration Iteration Iteration Genetic Algorithm 100 200 300 400 500 Generations Figure 8: Generating results of our genetic algorithm (GA) implementation We can see how the cost reduces at every iteration, and at approximately 490 iterations, the cost reduces to 0.134 and remains the same throughout the rest of the 10 iterations. Consequently, giving us our optimal solution. DISCLAIMER: The views expressed in this article are those of the author(s) and do not (al All images are from the author(s) unless stated otherwise. Published via Towards AI Resources Tutorial Companion Github repository. Google Colab implementation. Further Learning Yarpiz on Youtube References [1] “Evolution”, 2021. En. Wikipedia Org. https:/en.wikipedia.org/wiki/Evolution. [2] “Genetic Algorithm”. 2011. En.Wikipedia.Org. https://en.wikipedia.org/wiki/Genetic_algorithm. [3] A Genetic Algorithm Tutorial, Darrel Whitley, 2021. Cobweb.Cs.Uga.Edu. http://cobweb.cs.uga.edu/~potter/CompIntell/ga_tutorial.pdf. Sign up for This Al newsletter is all you need By Towards Al We have moved our newsletter. Subscribe —> httpsi/Iws.towardsainet/subscribe Take a look, By signing up, you wil create « Medium account if you dont already have one. Review ‘our Privacy Policy fr more information about our privacy practices ~ 5A n \ en openinane ( )

You might also like