The presentation of the paper titled "Cython for Speeding-up Genetic Algorithm". Find it at IEEE Explore: https://ieeexplore.ieee.org/document/9113210
The abstract of the paper:
This paper proposes a library for implementing the genetic algorithm using Python mainly in NumPy and speeding-up its execution using Cython. The preliminary Python implementation is inspected for possible optimizations. The 4 main changes include statically defining data types for the NumPy arrays, specifying the data type of the array elements in addition to the number of dimensions, using indexing for looping through the arrays, and finally disabling some unnecessary features in Cython. Using Cython, the NumPy array processing is 1250 times faster than CPython. The Cythonized version of the genetic algorithm is 18 times faster than the Python version.
Cython for Speeding-up
Ahmed Fawzy Gad
Fatima Ezzahra Jarmouni
ICEIT’20, Rabat, Morocco
▰ Python is the most popular programming
language used at this era.
▰ According to the Python Developers Survey
conducted in 2018 by the co-operation between
Python Software Foundation and JetBrains, out
of over 20,000 developers, 84% use Python as
their primary language.
Fast Prototyping using
Easy to Learn
Python for Data Science
▰ Python is regarded the best tool for building data science applications.
▰ Many tools are found in Python: TensorFlow, Keras, Scikit-Learn, Scikit-
Image, Pandas, NumPy, and more.
▰ Despite having many algorithms implemented in Python, there is no official
implementation for the genetic algorithm (GA).
▰ This paper proposes:
1. An open source project for implementing GA.
2. Using Cython for enhancing performance.
Genetic Algorithm Overview
▰ The genetic algorithm is an optimization technique
that is based on Dawin’s theory survival of the
▰ Given a vector X and a function F, what is the best
value for X to optimize a function F(x)? This
question is answered using the genetic algorithm.
4Ahmed Fawzy Gad. Practical computer vision applications using deep learning with CNNs. Apress, 2018.
Open Source Project
▰ The open source project is available at GitHub and received 127 stars:
▰ The project has a primary Python module named ga.py for holding the
implementation of the genetic algorithm.
▰ The functions within the module are:
1. cal_pop_fitness: Calculating the fitness value.
2. select_mating_pool: Selecting the mating pool.
3. crossover: Applying crossover.
4. mutation: Applying mutation.
▰ Given a function 𝐲 = 𝟒𝒘 𝟏 − 𝟐𝒘 𝟐 + 𝟑. 𝟓𝒘 𝟑 + 𝟓𝒘 𝟒 − 𝟏𝟏𝒘 𝟓 − 𝟒. 𝟕𝒘 𝟔, what
are the best parameters 𝒘 𝟏 to 𝒘 𝟔?
How to Speed-up the Process?
▰ For optimizing a problem, the genetic
algorithm might take hundreds of thousands
of generations. Thus, the execution should be
fast as possible.
▰ Is Python fast?
▰ Using Python, the code is interpreted each
time it is executed. It slows down the
▰ Using Cython, the code is executed at C
speed and compiled only once. 7
Genetic Algorithm using Cython
▰ Cython is a superset of Python and thus the Python code is already a Cython
code without any changes. So, we do not have to rewrite the code again.
▰ To convert the Cython code into C code, here are the steps:
1. Write the code into .pyx file not .py
2. Convert the .pyx file into C file according to the following code.
8▰ The Cython version of the GA is 18 times faster than Python.
▰ This paper proposed an open source project for
implementing the genetic algorithm in Python.
▰ Using Cython, the implementation is 18 times faster.
For More Information