Genetic Algorithms with Python
Genetic Algorithms with Python
About the Book
Genetic algorithms are one of the tools you can use to apply machine learning to finding good, sometimes even optimal, solutions to problems that have billions of potential solutions. This book gives you experience making genetic algorithms work for you, using easy-to-follow example problems that you can fall back upon when learning to use other machine learning tools and techniques. Each chapter is a step-by-step tutorial that helps to build your skills at using genetic algorithms to solve problems. Download the sample chapters for a brief introduction to genetic algorithms and the writing style used in this book.
Python is used as the teaching language in this book because it is a high-level, low ceremony, and powerful language whose code can be easily understood even by entry-level programmers. Because Python is used for teaching, but is not being taught, the use of Python-specific features that might make the code harder to follow for non-Python programmers has been minimized. This means that if you have experience with another programming language then you should have no difficulty learning Python by induction while concentrating on learning about genetic algorithms. Additionally, it should not be difficult for you to translate the working code used in this book to your favorite programming language on-the-fly, depending on the capabilities and support libraries available for your preferred language.
The code in this book is open source, licensed under the Apache License, Version 2.0. The final code from each chapter is available for download using a link at the end of the chapter.
A brief introduction to genetic algorithms Chapter 1: Hello World! See the sample.
- Guess a password given the number of correct letters in the guess. Build a mutation engine.
Chapter 2: One Max Problem See the sample.
- Produce an array of bits where all are 1s. Expands the engine to work with any type of gene.
Chapter 3: Sorted Numbers
- Produce a sorted integer array. Demonstrates handling multiple fitness goals and constraints between genes.
Chapter 4: The 8 Queens Puzzle
- Find safe Queen positions on an 8x8 board and then expand to NxN. Demonstrates the difference between phenotype and genotype.
Chapter 5: Graph Coloring
- Color a map of the United States using only 4 colors. Introduces standard data sets and working with files. Also introduces using rules to work with gene constraints.
Chapter 6: Card Problem
- More gene constraints. Introduces custom mutation, memetic algorithms, and the sum-of-difference technique. Also demonstrates a chromosome where the way a gene is used depends on its position in the gene array.
Chapter 7: Knights Problem
- Find the minimum number of knights required to attack all positions on a board. Introduces custom genes and gene-array creation. Also demonstrates local minimums and maximums.
Chapter 8: Magic Squares
- Find squares where all the rows, columns and both diagonals of an NxN matrix have the same sum. Introduces simulated annealing.
Chapter 9: Knapsack Problem
- Optimize the content of a container for one or more variables. Introduces branch and bound and variable length chromosomes.
Chapter 10: Solving Linear Equations
- Find the solutions to linear equations with 2, 3 and 4 unknowns. Branch and bound variation. Reinforces genotype flexibility.
Chapter 11: Generating Sudoku
- A guided exercise in generating Sudoku puzzles.
Chapter 12: Traveling Salesman Problem (TSP)
- Find the optimal route to visit cities. Introduces crossover and a pool of parents.
Chapter 13: Approximating Pi
- Find the two 10-bit numbers whose dividend is closest to Pi. Introduces using one genetic algorithm to tune another.
Chapter 14: Equation Generation
- Find the shortest equation that produces a specific result using addition, subtraction, multiplication, &c. Introduces symbolic genetic programming.
Chapter 15: The Lawnmower Problem
- Generate a series of instructions that cause a lawnmower to cut a field of grass. Genetic programming with control structures, objects and automatically defined functions (ADFs).
Chapter 16: Logic Circuits
- Build circuits that behave like basic logic gates, gate combinations and finally a 2-bit adder using tree nodes and hill climbing.
Chapter 17: Regular Expressions
- Find regular expressions that match wanted strings. Introduces chromosome repair and growth control.
Chapter 18: Tic-tac-toe
- Create rules for playing the game without losing. Introduces tournament selection.
The Leanpub 60-day 100% Happiness Guarantee
Within 60 days of purchase you can get a 100% refund on any Leanpub purchase, in two clicks.
See full terms
80% Royalties. Earn $16 on a $20 book.
We pay 80% royalties. That's not a typo: you earn $16 on a $20 sale. If we sell 5000 non-refunded copies of your book or course for $20, you'll earn $80,000.
(Yes, some authors have already earned much more than that on Leanpub.)
In fact, authors have earnedover $12 millionwriting, publishing and selling on Leanpub.
Learn more about writing on Leanpub
Free Updates. DRM Free.
If you buy a Leanpub book, you get free updates for as long as the author updates the book! Many authors use Leanpub to publish their books in-progress, while they are writing them. All readers get free updates, regardless of when they bought the book or how much they paid (including free).
Most Leanpub books are available in PDF (for computers) and EPUB (for phones, tablets and Kindle). The formats that a book includes are shown at the top right corner of this page.
Finally, Leanpub books don't have any DRM copy-protection nonsense, so you can easily read them on any supported device.