In this post, I’m going to show you a simple binary genetic algorithm in Python. Please note that to solve a new unconstrained problem, we just need to update the objective function and parameters of the binary genetic algorithm; the rest of the Python code, including the crossover, mutation, selection, decoding, and the main program, can be kept the same.

Did you know that genetic algorithm is the most popular stochastic optimization algorithm? The data from Scopus database shows the popularity of the genetic algorithm, as we can see from this Figure.

Here are the details of the benchmark problem, with the proven global optimal solution.

Let’s see how it works:

For more videos like this, check my YouTube channel here.

Python Code

from numpy.random import rand, randint
import numpy as np
import matplotlib.pyplot as plt

def objective_function(I):
    x = I[0]
    y = I[1]
    Objective_min = 0.26*(x**2 + y**2) - 0.48*x*y
    Objective_max = 1/(1 + Objective_min) # Convert the min to max problem
    
    return Objective_max

# Parameters of the binary genetic algorithm
bounds = [[-10, 10], [-10, 10]]
iteration = 200
bits = 20 # number of bits for each variable
pop_size = 100
crossover_rate = 0.8
mutation_rate = 0.2


#-----------------------------------------------------------------------------
# the rest of the python code can be kept the same
def crossover(pop, crossover_rate):
    offspring = list()
    for i in range(int(len(pop)/2)):
        p1 = pop[2*i-1].copy() # parent 1
        p2 = pop[2*i].copy() # parent 2   
        ...
    
    return offspring


def mutation(pop, mutation_rate):
    offspring = list()
    for i in range(int(len(pop))):
    ...
    
    return offspring


# roulette wheel selection
def selection(pop, fitness, pop_size):
    next_generation = list()
    elite = np.argmax(fitness)
    next_generation.append(pop[elite])  # keep the best
    ...

    return next_generation


def decoding(bounds, bits, chromosome):
 real_chromosome = list()
 for i in range(len(bounds)):
    ...

 return real_chromosome


# Initial population
pop = [randint(0, 2, bits*len(bounds)).tolist() for _ in range(pop_size)]

# main program
best_fitness = []
for gen in range(iteration):
.
.
.
Sorry! This is only a half of the Python code.   

Notice: It would take you from 1 to 3 hours to re-type the Python code yourself; or with just €1.99 (the cost of a cup of coffee), you can download/copy the whole Python code within 2 minutes. It’s your choice to make.

Original price is €4.99 but today it’s only €1.99 (save €3 today – available for a limited time only)

Download the whole Python code here (Membership Code ID: 029)

No need to build the Python code from scratch because it’s very time-consuming. My idol, Jim Rohn, once said: “Time is more value than money. You can get more money, but you cannot get more time”. If you think this code can be used in your research/teaching work, you should download it and then customize/modify/apply it to your work, without any obligation of citing the original source if you don’t want. However, redistribution (i.e., downloading the code/script here and then making it available on another site on the Internet) is strictly prohibited.

If you have any question or problem, please contact Dr. Panda by email: learnwithpanda2018@gmail.com

Thank you very much and good luck with your research!

Dr.Panda

View Comments

Recent Posts

Adaptive Re-Start Hybrid Genetic Algorithm in Matlab

Hello everyone! In this post, I am going to show you my innovative version of…

8 months ago

Test Your Understanding About Genetic Algorithm (Test 2)

Hello everyone. Let’s take a test to check your understanding about genetic algorithm, with multiple…

8 months ago

Adaptive Restart Hybrid Genetic Algorithm

Hello everyone! In this post, I am going to show you my innovative version of…

8 months ago

Adaptive Re-Start Hybrid Genetic Algorithm (Test the Performance in Case Studies)

Hello everyone. In this post, I am going to show you my innovative version of…

1 year ago

Adaptive Re-Start Hybrid Genetic Algorithm in Matlab

Hello everyone! Let’s see how my innovative version of Genetic Algorithm, called Adaptive Re-start Hybrid…

1 year ago

Crypto Quiz (Test Your Knowledge About Cryptocurrency)

Hello everyone! Let’s take a short quiz, to test your knowledge about crypto-currency, or crypto.…

2 years ago