Hands-On Genetic Algorithms with Python

Book description

Explore the ever-growing world of genetic algorithms to solve search, optimization, and AI-related tasks, and improve machine learning models using Python libraries such as DEAP, scikit-learn, and NumPy

Key Features

  • Explore the ins and outs of genetic algorithms with this fast-paced guide
  • Implement tasks such as feature selection, search optimization, and cluster analysis using Python
  • Solve combinatorial problems, optimize functions, and enhance the performance of artificial intelligence applications

Book Description

Genetic algorithms are a family of search, optimization, and learning algorithms inspired by the principles of natural evolution. By imitating the evolutionary process, genetic algorithms can overcome hurdles encountered in traditional search algorithms and provide high-quality solutions for a variety of problems. This book will help you get to grips with a powerful yet simple approach to applying genetic algorithms to a wide range of tasks using Python, covering the latest developments in artificial intelligence.

After introducing you to genetic algorithms and their principles of operation, you'll understand how they differ from traditional algorithms and what types of problems they can solve. You'll then discover how they can be applied to search and optimization problems, such as planning, scheduling, gaming, and analytics. As you advance, you'll also learn how to use genetic algorithms to improve your machine learning and deep learning models, solve reinforcement learning tasks, and perform image reconstruction. Finally, you'll cover several related technologies that can open up new possibilities for future applications.

By the end of this book, you'll have hands-on experience of applying genetic algorithms in artificial intelligence as well as in numerous other domains.

What you will learn

  • Understand how to use state-of-the-art Python tools to create genetic algorithm-based applications
  • Use genetic algorithms to optimize functions and solve planning and scheduling problems
  • Enhance the performance of machine learning models and optimize deep learning network architecture
  • Apply genetic algorithms to reinforcement learning tasks using OpenAI Gym
  • Explore how images can be reconstructed using a set of semi-transparent shapes
  • Discover other bio-inspired techniques, such as genetic programming and particle swarm optimization

Who this book is for

This book is for software developers, data scientists, and AI enthusiasts who want to use genetic algorithms to carry out intelligent tasks in their applications. Working knowledge of Python and basic knowledge of mathematics and computer science will help you get the most out of this book.

Publisher resources

Download Example Code

Table of contents

  1. Title Page
  2. Copyright and Credits
    1. Hands-On Genetic Algorithms with Python
  3. Dedication
  4. About Packt
    1. Why subscribe?
  5. Contributors
    1. About the author
    2. About the reviewer
    3. Packt is searching for authors like you
  6. Preface
    1. Who this book is for
    2. What this book covers
    3. To get the most out of this book
      1. Download the example code files
      2. Download the color images
    4. Code in Action
      1. Conventions used
    5. Get in touch
      1. Reviews
  7. Section 1: The Basics of Genetic Algorithms
  8. An Introduction to Genetic Algorithms
    1. What are genetic algorithms?
      1. Darwinian evolution
      2. The genetic algorithms analogy
        1. Genotype
        2. Population
        3. Fitness function
        4. Selection
        5. Crossover
        6. Mutation
    2. The theory behind genetic algorithms
      1. The schema theorem
    3. Differences from traditional algorithms
      1. Population-based
      2. Genetic representation
      3. Fitness function
      4. Probabilistic behavior
    4. Advantages of genetic algorithms
      1. Global optimization
      2. Handling complex problems 
      3. Handling a lack of mathematical representation
      4. Resilience to noise
      5. Parallelism
      6. Continuous learning
    5. Limitations of genetic algorithms
      1. Special definitions
      2. Hyperparameter tuning
      3. Computationally-intensive
      4. Premature convergence
      5. No guaranteed solution
    6. Use cases of genetic algorithms
    7. Summary
    8. Further reading
  9. Understanding the Key Components of Genetic Algorithms
    1. Basic flow of a genetic algorithm
      1. Creating the initial population
      2. Calculating the fitness
      3. Applying selection, crossover, and mutation
      4. Checking the stopping conditions
    2. Selection methods
      1. Roulette wheel selection
      2. Stochastic universal sampling
      3. Rank-based selection
      4. Fitness scaling
      5. Tournament selection
    3. Crossover methods
      1. Single-point crossover
      2. Two-point and k-point crossover
      3. Uniform crossover
      4. Crossover for ordered lists
        1. Ordered crossover 
    4. Mutation methods
      1. Flip bit mutation
      2. Swap mutation
      3. Inversion mutation
      4. Scramble mutation
    5. Real-coded genetic algorithms
      1. Blend crossover
      2. Simulated binary crossover
      3. Real mutation
    6. Understanding elitism
    7. Niching and sharing
      1. Serial niching versus parallel niching
    8. The art of solving problems using genetic algorithms
    9. Summary
    10. Further reading
  10. Section 2: Solving Problems with Genetic Algorithms
  11. Using the DEAP Framework
    1. Technical requirements
    2. Introduction to DEAP
    3. Using the creator module
      1. Creating the Fitness class
        1. Defining the fitness strategy
        2. Storing the fitness values
      2. Creating the Individual class
    4. Using the Toolbox class
      1. Creating genetic operators
      2. Creating the population
      3. Calculating the fitness
    5. The OneMax problem
    6. Solving the OneMax problem with DEAP
      1. Choosing the chromosome
      2. Calculating the fitness
      3. Choosing the genetic operators
      4. Setting the stopping condition
      5. Implementing with DEAP
        1. Setting up
        2. Evolving the solution
        3. Running the program
    7. Using built-in algorithms
      1. The Statistics object
      2. The algorithm
      3. The logbook
      4. Running the program
      5. Adding the hall of fame
    8. Experimenting with the algorithm's settings
      1. Population size and number of generations
      2. Crossover operator
      3. Mutation operator
      4. Selection operator
        1. Tournament size and relation to mutation probability
        2. Roulette wheel selection
    9. Summary
    10. Further reading
  12. Combinatorial Optimization
    1. Technical requirements
    2. Search problems and combinatorial optimization
    3. Solving the knapsack problem
      1. The Rosetta Code knapsack 0-1 problem
      2. Solution representation
      3. Python problem representation
      4. Genetic algorithms solution
    4. Solving the TSP
      1. TSPLIB benchmark files
      2. Solution representation
      3. Python problem representation
      4. Genetic algorithms solution
      5. Improving the results with enhanced exploration and elitism
    5. Solving the VRP
      1. Solution representation
      2. Python problem representation
      3. Genetic algorithms solution
    6. Summary
    7. Further reading
  13. Constraint Satisfaction
    1. Technical requirements
    2. Constraint satisfaction in search problems
    3. Solving the N-Queens problem
      1. Solution representation
      2. Python problem representation
      3. Genetic algorithms solution
    4. Solving the nurse scheduling problem
      1. Solution representation
      2. Hard constraints versus soft constraints
      3. Python problem representation
      4. Genetic algorithms solution
    5. Solving the graph coloring problem
      1. Solution representation
      2. Using hard and soft constraints for the graph coloring problem
      3. Python problem representation
      4. Genetic algorithms solution
    6. Summary
    7. Further reading
  14. Optimizing Continuous Functions
    1. Technical requirements
    2. Chromosomes and genetic operators for real numbers
    3. Using DEAP with continuous functions
    4. Optimizing the Eggholder function
      1. Optimizing the Eggholder function with genetic algorithms
      2. Improving the speed with an increased mutation rate
    5. Optimizing Himmelblau's function
      1. Optimizing Himmelblau's function with genetic algorithms
      2. Using niching and sharing to find multiple solutions
    6. Simionescu's function and constrained optimization
      1. Constrained optimization with genetic algorithms
      2. Optimizing Simionescu's function using genetic algorithms
      3. Using constraints to find multiple solutions
    7. Summary
    8. Further reading
  15. Section 3: Artificial Intelligence Applications of Genetic Algorithms
  16. Enhancing Machine Learning Models Using Feature Selection
    1. Technical requirements
    2. Supervised machine learning
      1. Classification
      2. Regression
      3. Supervised learning algorithms
    3. Feature selection in supervised learning
    4. Selecting the features for the Friedman-1 regression problem
      1. Solution representation
      2. Python problem representation
      3. Genetic algorithms solution
    5. Selecting the features for the classification Zoo dataset
      1. Python problem representation
      2. Genetic algorithms solution
    6. Summary
    7. Further reading
  17. Hyperparameter Tuning of Machine Learning Models
    1. Technical requirements
    2. Hyperparameters in machine learning
      1. Hyperparameter tuning
      2. The Wine dataset
      3. The adaptive boosting classifier
    3. Tuning the hyperparameters using a genetic grid search
      1. Testing the classifier's default performance
      2. Running the conventional grid search
      3. Running the genetic algorithm-driven grid search
    4. Tuning the hyperparameters using a direct genetic approach
      1. Hyperparameter representation
      2. Evaluating the classifier accuracy
      3. Tuning the hyperparameters using genetic algorithms
    5. Summary
    6. Further reading
  18. Architecture Optimization of Deep Learning Networks
    1. Technical requirements
    2. Artificial neural networks and deep learning
      1. Multilayer Perceptron
      2. Deep learning and convolutional neural networks
    3. Optimizing the architecture of a deep learning classifier
      1. The Iris flower dataset
      2. Representing the hidden layer configuration
      3. Evaluating the classifier's accuracy
      4. Optimizing the MLP architecture using genetic algorithms
    4. Combining architecture optimization with hyperparameter tuning
      1. Solution representation
      2. Evaluating the classifier's accuracy
      3. Optimizing the MLP's combined configuration using genetic algorithms
    5. Summary
    6. Further reading
  19. Reinforcement Learning with Genetic Algorithms
    1. Technical requirements
    2. Reinforcement learning
      1. Genetic algorithms and reinforcement learning
    3. OpenAI Gym
      1. The env interface
    4. Solving the MountainCar environment
      1. Solution representation
      2. Evaluating the solution
      3. Python problem representation
      4. Genetic algorithms solution
    5. Solving the CartPole environment
      1. Controlling the CartPole with a neural network
      2. Solution representation and evaluation
      3. Python problem representation
      4. Genetic algorithms solution
    6. Summary
    7. Further reading
  20. Section 4: Related Technologies
  21. Genetic Image Reconstruction
    1. Technical requirements
    2. Reconstructing images with polygons
    3. Image processing in Python
      1. Python image processing libraries
        1. The Pillow library
        2. The scikit-image library
        3. The opencv-python library
      2. Drawing images with polygons
      3. Measuring the difference between images
        1. Pixel-based Mean Squared Error
        2. Structural Similarity (SSIM)
    4. Using genetic algorithms to reconstruct images
      1. Solution representation and evaluation
      2. Python problem representation
      3. Genetic algorithm implementation
        1. Adding a callback to the genetic run
      4. Image reconstruction results
        1. Using pixel-based Mean Squared Error
        2. Using the SSIM index
        3. Other experiments
    5. Summary
    6. Further reading
  22. Other Evolutionary and Bio-Inspired Computation Techniques
    1. Technical requirements
    2. Evolutionary computation and bio-inspired computing
    3. Genetic programming
      1. Genetic programming example – even parity check
      2. Genetic programming implementation
        1. Simplifying the solution
    4. Particle swarm optimization
      1. PSO example – function optimization
      2. Particle swarm optimization implementation
    5. Other related techniques
      1. Evolution strategies
      2. Differential evolution
      3. Ant colony optimization
      4. Artificial immune systems
      5. Artificial life
    6. Summary
    7. Further reading
  23. Other Books You May Enjoy
    1. Leave a review - let other readers know what you think

Product information

  • Title: Hands-On Genetic Algorithms with Python
  • Author(s): Eyal Wirsansky
  • Release date: January 2020
  • Publisher(s): Packt Publishing
  • ISBN: 9781838557744