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!
Hello everyone! In this post, I am going to show you my innovative version of…
Hello everyone. Let’s take a test to check your understanding about genetic algorithm, with multiple…
Hello everyone! In this post, I am going to show you my innovative version of…
Hello everyone. In this post, I am going to show you my innovative version of…
Hello everyone! Let’s see how my innovative version of Genetic Algorithm, called Adaptive Re-start Hybrid…
Hello everyone! Let’s take a short quiz, to test your knowledge about crypto-currency, or crypto.…
View Comments
I am trying to buy the code but it tells me it is for members only.
Become a member and then you can copy the code.
Hi Panda!
Last time I left a comment on your YouTube channel. My question is for Influence maximization problems, could explain how to formulate the objective function and initial population. thank you.
Thanks for the comment. It's hard for me to explain it here
Hi Mr. Panda.
Can you explain how to find database from scopus for shows the popularity of the genetic algorithm ? I need this data for one of 3 reason why I use the genetic Algorithm.
Best Regards
Afghan
Bandung Institute of technology, Indonesia
Hi, go to Scopus and search for the term "genetic algorithm". You will get a lot of interesting data
I got it. Thank you, sir
Hey! I wished to understand the codes for the remaining functions that you said can remain the same, where can I get that?
Hello, you can get the full code by clicking the "red" link at the end of this post. Thanks
sumaiya send me your email ok
You can see the email at the end of the website
code can work
comsol message "real_chromosome = [decoding (bounds,bits,p) for p in pop] " and
File "C:/Users/samsung/PycharmProject/GA/main.py", line 81, in decoding
integer = int(chars,2) # convert to integer
ValueError: invalid literal for int() with base 2: ''
Can you send me the whole code to check
I paid but I can't access to the code
Sorry to hear that. Please send me an email.
what can i get with the membership 01 and 04 ?
i just got a code
You will have one day access to copy these two codes.
Hi Dr. Panda I used paid Membership 029 for the binary genetic Algotihm code but I do not find it in your website?
Please share it today best
Hello, I've sent it to you by email.
I bought a Membership 029. Nevertheless I couldnt dowload the code....
Please contact me by email (learnwithpanda2018@gmail.com), I will send to code to you by email.