(This page has no text content)
Learning Genetic Algorithms with Python Empower the Performance of Machine Learning and AI Models with the Capabilities of a Powerful Search Algorithm Ivan Gridin www.bpbonline.com
FIRST EDITION 2021 Copyright © BPB Publications, India ISBN: 978-81-94837-756 All Rights Reserved. No part of this publication may be reproduced, distributed or transmitted in any form or by any means or stored in a database or retrieval system, without the prior written permission of the publisher with the exception to the program listings which may be entered, stored and executed in a computer system, but they can not be reproduced by the means of publication, photocopy, recording, or by any electronic and mechanical means. LIMITS OF LIABILITY AND DISCLAIMER OF WARRANTY The information contained in this book is true to correct and the best of author’s and publisher’s knowledge. The author has made every effort to ensure the accuracy of these publications, but publisher cannot be held responsible for any loss or damage arising from any information in this book. All trademarks referred to in the book are acknowledged as properties of their respective owners but BPB Publications cannot guarantee the accuracy of this information. Distributors:
BPB PUBLICATIONS 20, Ansari Road, Darya Ganj New Delhi-110002 Ph: 23254990/23254991 MICRO MEDIA Shop No. 5, Mahendra Chambers, 150 DN Rd. Next to Capital Cinema, V.T. (C.S.T.) Station, MUMBAI-400 001 Ph: 22078296/22078297 DECCAN AGENCIES 4-3-329, Bank Street, Hyderabad-500195 Ph: 24756967/24756400
BPB BOOK CENTRE 376 Old Lajpat Rai Market, Delhi-110006 Ph: 23861747 Published by Manish Jain for BPB Publications, 20 Ansari Road, Darya Ganj, New Delhi-110002 and Printed by him at Repro India Ltd, Mumbai www.bpbonline.com
Dedicated to: My lovely little daughters: Ksenia and Elena. Your endless love and energy charge me every day. And to my beautiful and patient wife Tamara. That’s all thanks to you. You are the light of my life. I love you
About the Author Ivan Gridin is a Mathematician, Fullstack Developer, Data Scientist, and Machine Learning Expert living in Moscow, Russia. Over the years, he worked on distributive high-load systems and implemented different machine learning approaches in practice. One of the key areas of his research is the design and analysis of predictive time series models. Ivan has fundamental math skills in probability theory, random process theory, time series analysis, machine learning, deep learning, and optimization. He also has in-depth knowledge and understanding of various programming languages such as Java, Python, PHP, and MATLAB. Loving father, husband, and collector of old math books. Linkedin: http://www.linkedin.com/in/survex/
About the Reviewer Satyajeet Dhawale is a professional Data Scientist having a strong experience in machine learning, deep learning, computer vision, inferential and descriptive statistical analysis. He has worked on many projects that involve complex machine learning and deep learning algorithms and used a variety of data sets from a different domain. In his career, he has successfully delivered many machine learning and deep learning solutions for complex data problems. You can find more professional details about Satyajeet on LinkedIn.
Acknowledgement There are a few people I want to thank for the idea and the motivation for writing this book. I thank my adorable wife Tamara; her patience and beauty inspired me every day. I thank my elder daughter Ksenia; her courage and determination motivated me in exhaustion moments. And my little daughter Elena for waking me up earlier – you’re my energizer! Thanks to my mom for her love and the genetics she gave me. I am eternally grateful to my company AT Consulting. This company has done a lot to make me the specialist I am. And I especially want to thank Alexey Korotaev - one of the best managers I’ve ever seen. I’m proud to be a part of this big team. Thanks to my friends, who helped me in all my endeavors. To Petr Rostov for his help in learning mathematics and programming. To my friend Yuri Babaev for his inspiration and humor that he brings into my life. To Denis Paramanov for sacrificing his vacation for my future wife and me. To Dmitry Suvorov for his effective gym workout. To Sergey Korsikov and Alena Gulyaeva for helping us to get used to this big city. This book would be impossible without all of them. I want to give the warmest hugs to my new big family in Perm and Khabarovsk. I especially want to hug Konstantin Volosatov, the
most handsome and intelligent policeman I have ever met, and his beautiful wife, Elena Volosatova. We love you all very much! My gratitude also goes to the book reviewer Satyajeet Dhawale. His participation and useful advice have made this book much better. Special thanks to BPB Publications for support, advice, and assistance in creating and publishing this book.
Preface There is a lot of talk about Machine Learning, Deep Learning, Neural Networks, and many other terms and technologies that provides Artificial Intelligence in our life. These technologies are used everywhere in our daily life. Image and terrain recognition techniques are being used in autonomous vehicles that are already driving worldwide. And the number of autonomous cars is increasing at a tremendous rate. Artificial intelligence frameworks and libraries are beginning to penetrate very deeply into all areas of programming. An ordinary programmer needs to have at least a basic understanding of what machine learning is, what kind of tasks it solves, and how to work with it. Machine learning contains a broad set of tools. Most of the books and teaching materials describe only a few of them, while the other approaches based on the evolutionary search for solving problems remain uncovered. This book is about Genetic Algorithms. It tells how the principles formulated by Charles Darwin in his book “The Origin of Species” in 1859 in our time help to solve the most complex problems that contain billions and billions of solutions. Genetic Algorithms solve problems that cannot be solved analytically. It mimics the process of evolution and natural selection in a population, where each individual represents a solution to a problem. The more vital individuals from the population outlive the weaker ones and produce the next generation of individuals. So after the evolution
process, after many generations, we get some solution to the problem, which will highly likely satisfy the requirements. The main disadvantage of most machine learning tools can be formulated following way: “They solve problems, but they don’t solve the problem - how to solve problems.” Genetic algorithms provide a method for solving a problem, i.e., how to solve problems in the absence of human experience. Genetic algorithms are a relatively simple and very effective method for solving a large class of problems. They are intuitive, simple, and can be an excellent introduction to machine learning. The book consists of thirteen chapters, in which the reader will learn the following: Chapter 1 is the introductory chapter giving the basic principles of evolution. It defines the genetic algorithm, the genetic algorithm’s nature, its applicability, and the pros and cons. Chapter 2 will discuss genetic algorithm architecture, its main logical concepts: individual, fitness function, population, selection, crossover, and mutation.
Chapter 3 will focus solely on the Selection method. It explains the selection in the sense of evolution, how it works, and how it affects the evolution process. We will cover the following selection methods: Tournament Selection, Proportional Selection, Stochastic universal sampling Selection, Rank Selection, Elite Selection. Chapter 4 concentrates only on the Crossover operation. It describes the crossover, why it is important, how it works, and how it influences the solution search. We will study the following crossover methods: One Point Crossover, N-Point Crossover, Uniform Crossover, Linear Combination Crossover, Blend Crossover, Order Crossover, and Fitness Driven Crossover. Chapter 5 discusses the last evolution operation called Mutation. Evolution would be impossible without the Mutation, and it is one of the most crucial parts of the genetic algorithm. Following mutation methods are discussed: Random Deviation Mutation, Exchange Mutation, Shift Mutation, Bit Flip Mutation, Inversion Mutation, Shuffle Mutation, and Fitness Driven Mutation. Chapter In this chapter, we will explore a way to compare the effectiveness of architectures of genetic algorithms. It defines what the best individual is. Explore the Genetic Algorithm as a random variable and cover the handy technique to compare two random variables called Monte-Carlo simulation. Chapter 7 is the last theoretical chapter and is dedicated to parameter tuning. It shows how global parameters like population size, crossover, and mutation probability govern genetic algorithm
flow dynamics. It studies each parameter influence and explains how each parameter affects the algorithm intuitively. Chapter 8 starts the practical section of real-world problems. It covers one of the most common tasks - finding the black-box function’s maxima. It covers which types of individuals can be created and design genetic algorithm architecture for this task. Chapter 9 covers the first type of combinatorial problems, named binary encoded combinatorial problems. It designs the solution for the classical knapsack and schedule problem. And also, we will study a complex radar problem. Chapter 10 studies the second type of combinatorial problem called ordered encoded combinatorial problems. Here we will discuss the traditional traveling salesman problem; also, we will investigate an original football manager problem. Chapter 11 shows some other types of problems. It shows how to solve the general system of equations using genetic algorithms and another common graph coloring problem. Chapter 12 brings the genetic algorithm to another level, from Machine Learning to Deep Learning. It shows how to design an adaptive genetic algorithm that can be used as a universal approach with self-tuning feature during the evolution process. Chapter 13 is all about performance. It shows how to speed up the genetic algorithm with various techniques.
(This page has no text content)
Downloading the code bundle and coloured images: Please follow the link to download the Code Bundle and the Coloured Images of the book: https://rebrand.ly/6ad4a All the chapters have associated code scripts in the book’s Code Bundle. Some parts required for the code to run may not be included in the text to save space. You should be able to run all of the code by yourself. Errata We take immense pride in our work at BPB Publications and follow best practices to ensure the accuracy of our content to provide with an indulging reading experience to our subscribers. Our readers are our mirrors, and we use their inputs to reflect and improve upon human errors, if any, that may have occurred during the publishing processes involved. To let us maintain the quality and help us reach out to any readers who might be having difficulties due to any unforeseen errors, please write to us at :
errata@bpbonline.com Your support, suggestions and feedbacks are highly appreciated by the BPB Publications’ Family. Did you know that BPB offers eBook versions of every book published, with PDF and ePub files available? You can upgrade to the eBook version at www.bpbonline.com and as a print book customer, you are entitled to a discount on the eBook copy. Get in touch with us at business@bpbonline.com for more details. At you can also read a collection of free technical articles, sign up for a range of free newsletters, and receive exclusive discounts and offers on BPB books and eBooks.
BPB is searching for authors like you If you're interested in becoming an author for BPB, please visit www.bpbonline.com and apply today. We have worked with thousands of developers and tech professionals, just like you, to help them share their insight with the global tech community. You can make a general application, apply for a specific hot topic that we are recruiting an author for, or submit your own idea. The code bundle for the book is also hosted on GitHub at In case there's an update to the code, it will be updated on the existing GitHub repository. We also have other code bundles from our rich catalog of books and videos available at Check them out! PIRACY If you come across any illegal copies of our works in any form on the internet, we would be grateful if you would provide us with the location address or website name. Please contact us at business@bpbonline.com with a link to the material. If you are interested in becoming an author
If there is a topic that you have expertise in, and you are interested in either writing or contributing to a book, please visit REVIEWS Please leave a review. Once you have read and used this book, why not leave a review on the site that you purchased it from? Potential readers can then see and use your unbiased opinion to make purchase decisions, we at BPB can understand what you think about our products, and our authors can see your feedback on their book. Thank you! For more information about BPB, please visit
Table of Contents 1. Introduction Structure 1.1 Nature of genetic algorithm 1.2 Applicability of genetic algorithms 1.3 Pros and cons of genetic algorithms 1.4 Your first genetic algorithm Conclusion Questions 2. Genetic Algorithm Flow Structure 2.1 Individual 2.2 Fitness function 2.3 Population 2.4 Selection 2.5 Crossover 2.6 Mutation 2.7 Genetic algorithm flow Conclusion Points to remember: Multiple choice questions: Answers Questions Key terms 3. Selection Structure
Comments 0
Loading comments...
Reply to Comment
Edit Comment