Learning Genetic Algorithms with Python: Empower the performance of Machine Learning and AI models with the capabilities of a powerful search algorithm (English Edition)
By Ivan Gridin
4/5
()
About this ebook
Each of the chapters gives the reader an intuitive understanding of each concept. You will learn how to build a genetic algorithm from scratch and implement it in real-life problems. Covered with practical illustrated examples, you will learn to design and choose the best model architecture for the particular tasks. Cutting edge examples like radar and football manager problem statements, you will learn to solve high-dimensional big data challenges with ways of optimizing genetic algorithms.
Read more from Ivan Gridin
Time Series Forecasting using Deep Learning: Combining PyTorch, RNN, TCN, and Deep Neural Network Models to Provide Production-Ready Prediction Solutions Rating: 0 out of 5 stars0 ratings
Related to Learning Genetic Algorithms with Python
Related ebooks
Advanced Machine Learning with Python Rating: 0 out of 5 stars0 ratingsMachine Learning with Tensorflow: A Deeper Look at Machine Learning with TensorFlow Rating: 0 out of 5 stars0 ratingsPython Machine Learning By Example Rating: 4 out of 5 stars4/5Python Machine Learning Projects: Learn how to build Machine Learning projects from scratch (English Edition) Rating: 0 out of 5 stars0 ratingsAI and ML for Coders: AI Fundamentals Rating: 0 out of 5 stars0 ratingsMachine Learning for Finance Rating: 5 out of 5 stars5/5Python Data Structures and Algorithms Rating: 5 out of 5 stars5/5Machine Learning with Spark and Python: Essential Techniques for Predictive Analytics Rating: 0 out of 5 stars0 ratingsArtificial Intelligence with Python Rating: 4 out of 5 stars4/5Hands-On Machine Learning with Microsoft Excel 2019: Build complete data analysis flows, from data collection to visualization Rating: 0 out of 5 stars0 ratingsA Practical Approach for Machine Learning and Deep Learning Algorithms: Tools and Techniques Using MATLAB and Python Rating: 0 out of 5 stars0 ratingsAdvanced Deep Learning with Python: Design and implement advanced next-generation AI solutions using TensorFlow and PyTorch Rating: 0 out of 5 stars0 ratingsDeep Learning Fundamentals in Python Rating: 4 out of 5 stars4/5Markov Models Supervised and Unsupervised Machine Learning: Mastering Data Science And Python Rating: 2 out of 5 stars2/5Deep Learning with Keras Rating: 4 out of 5 stars4/5Ultimate Neural Network Programming with Python Rating: 0 out of 5 stars0 ratingsLearning PyTorch 2.0, Second Edition: Utilize PyTorch 2.3 and CUDA 12 to experiment neural networks and deep learning models Rating: 0 out of 5 stars0 ratingsReinforcement Learning Algorithms with Python: Learn, understand, and develop smart algorithms for addressing AI challenges Rating: 0 out of 5 stars0 ratingsPython Machine Learning Illustrated Guide For Beginners & Intermediates:The Future Is Here! Rating: 5 out of 5 stars5/5Deep Learning with TensorFlow Rating: 5 out of 5 stars5/5Convolutional Neural Networks in Python: Beginner's Guide to Convolutional Neural Networks in Python Rating: 0 out of 5 stars0 ratings
Information Technology For You
An Ultimate Guide to Kali Linux for Beginners Rating: 3 out of 5 stars3/5Creating Online Courses with ChatGPT | A Step-by-Step Guide with Prompt Templates Rating: 4 out of 5 stars4/5CompTia Security 701: Fundamentals of Security Rating: 0 out of 5 stars0 ratingsLearning Microsoft Endpoint Manager: Unified Endpoint Management with Intune and the Enterprise Mobility + Security Suite Rating: 0 out of 5 stars0 ratingsCompTIA A+ CertMike: Prepare. Practice. Pass the Test! Get Certified!: Core 1 Exam 220-1101 Rating: 0 out of 5 stars0 ratingsA Mind at Play: How Claude Shannon Invented the Information Age Rating: 4 out of 5 stars4/5Data Analytics for Beginners: Introduction to Data Analytics Rating: 4 out of 5 stars4/5How to Write Effective Emails at Work Rating: 4 out of 5 stars4/5Health Informatics: Practical Guide Rating: 0 out of 5 stars0 ratingsDevOps Handbook: What is DevOps, Why You Need it and How to Transform Your Business with DevOps Practices Rating: 4 out of 5 stars4/5Unlocking the Power of Agentic AI: Transforming Work and Life Rating: 5 out of 5 stars5/5COMPUTER SCIENCE FOR ROOKIES Rating: 0 out of 5 stars0 ratingsCompTIA Security+: Network Attacks Rating: 5 out of 5 stars5/5Personal Knowledge Graphs: Connected thinking to boost productivity, creativity and discovery Rating: 5 out of 5 stars5/5Oracle Fusion:Transforming Hospital Finance Rating: 5 out of 5 stars5/5CompTIA Network+ CertMike: Prepare. Practice. Pass the Test! Get Certified!: Exam N10-008 Rating: 0 out of 5 stars0 ratingsPractical Ethical Hacking from Scratch Rating: 5 out of 5 stars5/5AWS Certified Cloud Practitioner: Study Guide with Practice Questions and Labs Rating: 5 out of 5 stars5/5How Computers Really Work: A Hands-On Guide to the Inner Workings of the Machine Rating: 0 out of 5 stars0 ratingsAWS Certified Cloud Practitioner - Practice Paper 1: AWS Certified Cloud Practitioner, #1 Rating: 5 out of 5 stars5/5Cybersecurity for Beginners : Learn the Fundamentals of Cybersecurity in an Easy, Step-by-Step Guide: 1 Rating: 0 out of 5 stars0 ratingsLinux Command Line and Shell Scripting Bible Rating: 3 out of 5 stars3/5DNS in Action Rating: 0 out of 5 stars0 ratingsUnity Game Development Essentials Rating: 5 out of 5 stars5/5Introduction to Information Systems: Information Technology Essentials, #1 Rating: 0 out of 5 stars0 ratingsCreating your MySQL Database: Practical Design Tips and Techniques Rating: 3 out of 5 stars3/5
Reviews for Learning Genetic Algorithms with Python
1 rating0 reviews
Book preview
Learning Genetic Algorithms with Python - Ivan Gridin
CHAPTER 1
Introduction
As we know, evolution is one of the most perfect adaptation mechanisms. It is a way to achieve extraordinary and complex solutions. Understanding the principles of evolution gave us a new approach called genetic algorithms . We will now explore this rather beautiful, simple, and effective approach to problem-solving.
Structure
In this chapter, we will discuss the following topics:
Nature of genetic algorithm
Applicability of genetic algorithms
Pros and cons of genetic algorithms
Your first genetic algorithm
1.1 Nature of genetic algorithm
The rapid development in AI is made possible for humans to obtain solution to abstract problems. Complex computational problems that are very difficult to solve by classical methods can now be solved by AI.
One of the most powerful techniques to solve such complex problems is genetic algorithms (GA), which is based on the principle of an evolutionary approach.
In the late 60s, American researcher J. Holland proposed to find solutions to optimization problems using methods and evolution models of animal populations in nature. Since the evolution’s basic laws were investigated and described by genetics, the proposed approach was called genetic algorithms. GA is a randomly directed search algorithm based on mechanisms of natural selection and natural genetics. It implements the principle of survival of the fittest, forming and changing the search algorithm based on evolutionary modeling.
The basic steps in natural evolution are as follows:
Selection: According to Charles Darwin, natural selection laws were formulated in the book On the Origin of Species. The central postulate is that individuals who can better solve problems, survive and reproduce more. In GAs, each individual is a solution to some problem. According to this principle, individuals who solve the problem better have a greater chance of surviving and leaving offsprings.
Crossover: This means that the offspring chromosome is made up of parts that are derived from the parents’ chromosomes. This principle was discovered in 1865 by G. Mendel.
Mutation: In 1900, H. de Vries discovered the principle of random change. Initially, this term was used to describe significant changes in descendants’ properties that were not present in their parents. By analogy, genetic algorithms use a similar mechanism to change offspring’s properties, thereby increasing individuals’ diversity in a population.
Genetic algorithms have the following characteristics:
Easy to implement
Used for a wide range of tasks
They do not require any additional information about the nature of the problem
Easy and convenient to parallelize
1.2 Applicability of genetic algorithms
As a solution, the GA tries to find the extremum of some function that characterizes the quality of the solution to the problem. Generally, the GA does not guarantee that the solution found is the best of all that’s possible. Usually, this is not required, but it is only important that the found solution satisfies the meaning of the problem being solved.
The areas of application of GAs include the following:
Search for extremum of various functions
Finding the shortest paths (traveling salesman problem)
Combinatorial optimization
Tasks of placement and scheduling
Automatic programming tasks
AI tasks (choosing the structure and parameters of artificial neural networks)
In real time scenarios, GAs are used to develop AI systems, like designing tasks for aircraft routes at airports, finding the optimal behavior of robots, problems of constructing investment portfolios, and so on.
1.3 Pros and cons of genetic algorithms
Like any other approach to problem-solving, GAs have their pros and cons as well. Understanding these features will allow you to solvе the practical problems in a better way.
The pros of genetic algorithms are as follows:
A wide range of tasks to be solved: GA is successfully applied in the following areas – combinatorial optimization, finance (portfolio optimization), machine learning (feature extraction, neural network hyper-parameter optimization), code-breaking, game theory, natural sciences, and so on.
Ease of implementation: The algorithm implies the presence of steps – natural selection, crossing, and mutation. This conceptual simplicity makes this method available to a wide range of developers.
Resistance to dynamic changes in problem conditions: The GA is able to retrain if the conditions of the problem change when searching for a solution.
The ability for self-adaptation: GAs are able, after a certain period of evolution, to adapt to the conditions of the problem being solved.
Ease of scaling: Can easily be used on big data where the data is spread over the distributed systems. GAs, as a highly parallel process, can be easily parallelized, which makes it possible to proportionally accelerate the finding of a solution with an increase in computing power.
Solving problems for which there is no solution experience: One of the biggest advantages of GAs is their ability to investigate problems for which there is no relevant solution experience. It should be noted that expert assessments are often used to solve difficult-to-formalize problems, but they sometimes give less acceptable solutions than automated methods.
The cons of genetic algorithms are as follows:
The complexity of representing an individual in a population and determining the fitness function.
For real problems, it is initially not-at-all obvious in what form it is necessary to present a set of individual genes for a successful solution to the problem, and also determine the assessment of the quality of a particular individual.
The choice of parameters of the architecture of the GA.
There are no effective criteria for the termination of the algorithm.
Not effective for finding an extremum for smooth functions with one extremum.
They require large enough computing resources.
When solving problems, there are cases of premature convergence, and therefore, generally, they do not guarantee in finding the global extremum.
1.4 Your first genetic algorithm
Well, let’s try to build our first GA solution. We will start from a trivial example which shows us the basics.
Let’s say we have the following function, sin(x) - 0.2 * abs(x). Refer to the following figure 1.1:
Figure 1.1: sin(x) - |x|
We will find the maxima of the preceding function.
This function has several local maximums. All individuals in the population in our GA will try to climb as high as possible.
Let’s see the GA in action. Execute the following code (we will cover the details in future chapters) ch1/your_first_genetic_algorithm.py :
Import part
import random
from typing import List
import numpy as np
import matplotlib.pyplot as plt
Auxiliary GA operations
def _utils_constraints(g, min, max):
if max and g > max:
g = max
if min and g < min:
g = min
return g
def crossover_blend(g1, g2, alpha, min = None, max = None):
shift = (1. + 2. * alpha) * random.random() - alpha
new_g1 = (1. - shift) * g1 + shift * g2
new_g2 = shift * g1 + (1. - shift) * g2
return _utils_constraints(new_g1, min, max), _utils_constraints(new_g2, min, max)
def mutate_gaussian(g, mu, sigma, min = None, max = None):
mutated_gene = g + random.gauss(mu, sigma)
return _utils_constraints(mutated_gene, min, max)
def select_tournament(population, tournament_size):
new_offspring = []
for _ in range(len(population)):
candidates = [random.choice(population) for _ in range(tournament_size)]
new_offspring.append(max(candidates, key = lambda ind: ind.fitness))
return new_offspring
def func(x):
return np.sin(x) - .2 * abs(x)
def get_best(population):
best = population[0]
for ind in population:
if ind.fitness > best.fitness:
best = ind
return best
def plot_population(population, number_of_population):
best = get_best(population)
x = np.linspace(-10, 10)
plt.plot(x, func(x), ‘--’, color = ‘blue’)
plt.plot([ind.get_gene() for ind in population], [ind.fitness for ind in population], ‘o’, color = ‘orange’)
plt.plot([best.get_gene()], [best.fitness], ‘s’, color = ‘green’)
plt.title(fGeneration number {number_of_population}
)
plt.show()
plt.close()
Individual class
class Individual:
def __init__(self, gene_list: List[float]) -> None:
self.gene_list = gene_list
self.fitness = func(self.gene_list[0])
def get_gene(self):
return self.gene_list[0]
@classmethod
def crossover(cls, parent1, parent2):
child1_gene, child2_gene = crossover_blend(parent1.get_gene(), parent2.get_gene(), 1, -10, 10)
return Individual([child1_gene]), Individual([child2_gene])
@classmethod
def mutate(cls, ind):
mutated_gene = mutate_gaussian(ind.get_gene(), 0, 1, -10, 10)
return Individual([mutated_gene])
@classmethod
def select(cls, population):
return select_tournament(population, tournament_size = 3)
@classmethod
def create_random(cls):
return Individual([random.randrange(-1000, 1000) / 100])
GA flow
random.seed(52)
# random.seed(16) # local maximum
POPULATION_SIZE = 10
CROSSOVER_PROBABILITY = .8
MUTATION_PROBABILITY = .1
MAX_GENERATIONS = 10
first_population = [Individual.create_random() for _ in range(POPULATION_SIZE)]
plot_population(first_population, 0)
generation_number = 0
population = first_population.copy()
while generation_number < MAX_GENERATIONS:
generation_number += 1
# SELECTION
offspring = Individual.select(population)
# CROSSOVER
crossed_offspring = []
for ind1, ind2 in zip(offspring[::2], offspring[1::2]):
if random.random() < CROSSOVER_PROBABILITY:
kid1, kid2 = Individual.crossover(ind1, ind2)
crossed_offspring.append(kid1)
crossed_offspring.append(kid2)
else:
crossed_offspring.append(ind1)
crossed_offspring.append(ind2)
# MUTATION
mutated_offspring = []
for mutant in crossed_offspring:
if random.random() < MUTATION_PROBABILITY:
new_mutant = Individual.mutate(mutant)
mutated_offspring.append(new_mutant)
else:
mutated_offspring.append(mutant)
population = mutated_offspring.copy()
plot_population(population, generation_number)
Now, let’s examine how individuals of each population behave during each generation. Refer to the following graphs:
Figure 1.2: Generation 1
In the preceding figure 1.2, the first-generation is just the random distribution of points on the curve. We denote the green point as the