Jump to ratings and reviews
Rate this book

Genetic Algorithms with Python

Rate this book
Get a hands-on introduction to machine learning with genetic algorithms using Python. Step-by-step tutorials build your skills from Hello World! to optimizing one genetic algorithm with another, and finally genetic programming; thus preparing you to apply genetic algorithms to problems in your own field of expertise.

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 projects 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 using Python.

Python is a high-level, low ceremony and powerful language whose code can be easily understood even by entry-level programmers. If you have experience with another programming language then you should have no difficulty learning Python by induction.

Contents

Chapter 1: Hello World! - Guess a password given the number of correct letters in the guess. Build a mutation engine.

Chapter 2: One Max Problem - 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, etc. 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 - Generate circuits that behave like basic gates, gate combinations and finally a 2-bit adder. Introduces 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.

Source code: https://github.com/handcraftsman/Gene...

PDF

Published April 29, 2016

59 people are currently reading
70 people want to read

About the author

Clinton Sheppard

29 books5 followers
I am a polyglot programmer with more than 15 years of professional programming experience. My personal projects include photography, playing board games, genealogy and genetic programming.

Ratings & Reviews

What do you think?
Rate this book

Friends & Following

Create a free account to discover what your friends think of this book!

Community Reviews

5 stars
10 (50%)
4 stars
6 (30%)
3 stars
2 (10%)
2 stars
1 (5%)
1 star
1 (5%)
Displaying 1 - 5 of 5 reviews
Profile Image for Nate Gaylinn.
82 reviews10 followers
July 5, 2022
A hands-on guide to writing genetic algorithms in Python.

This book is organized around a collection of practice problems. Each chapter presents a harder problem, requiring some new genetic programming techniques to solve. Sheppard leads the reader through framing each problem effectively, coding up a solution, and fleshing out a genetic algorithm framework along the way which gets reused throughout the book. The text is light on theory and explanations. It mostly just introduces ideas, provides implementations, and leaves further research to the reader if they want it.

I love this book's structure and hands-on approach. I quickly learned the skills and concepts I was after, so the book served its purpose well. However, there's a lot I dislike about the author's code style and the design of his framework. It's gross, frankly. As an experienced Python programmer, I just rewrote everything to my satisfaction as I went along, which was actually a big help for mastering the core ideas. On the other hand, a Python novice might have a hard time following along, or might pick up some bad coding habits.

I recommend this book for getting up and running with genetic algorithms fast, just so long as you don't mind learning from messy, tricky code with no comments.

This book focuses on one particular kind of GA, a few of its variations, and a small set of practice problems. For a more general overview of GA theory and a thorough exploration of their many forms and applications, check out An Introduction to Genetic Algorithms.
Profile Image for Gábor Hajba.
139 reviews3 followers
Want to read
November 8, 2020
This book gives a nice introduction to genetic algorithms with examples and tips how you can tune and give your application more performance.
Profile Image for Matti Aho.
1 review
April 1, 2022
I am novice sailor in the vast blue sea of programming. Actually I wanted to understand how AI works. So I encountered python programming and started to study python by myself. At the age of 64 I am pretty hesitant to take any courses (no remote, nor in person). So I started to read beginners books and experiment with python editors. Here I am now, enjoying this book. Not actually able to write my own code but gradually beginning to be able to read python. And thanks to this book I claim I have seen the light of object oriented programming. I reproduced (not copy-paste) all examples in my own computer and managed to cut down various errors and got all examples to work. It took some 3 months. Great fun of learning new things and concepts.
Finally I recognized in Chapter 11 "Generating Sudoku" a code error in the book at page 119. Example does not work with the described code. The 9 sections of Sudoku are indicated by code "sections[int(row / 3) + int(column / 3) + 3].add(value)". However, the right code is "sections[int(row / 3) * 3 + int(column / 3)].add(value)". Later on in this Chapter 11 a rule based solution is introduced. It is definitely more elegant and also faster than this earlier solution [which works (slowly) with this correction].
Profile Image for John Doe.
68 reviews11 followers
March 9, 2021
very practical and useful to lazy people like me who do not want to write their own GA optimizer.
Lots of code examples.
Go directly to the appendix if you already have an idea of what GA is for.
GA, genetic algorithms, basically is a way to speed up the brutal force process in searching best parameter pairs in a given solution space, also known as optimization.
All the problems are classical and introduced twinkling around solution space framing,mutate, crossover and fitness function formation.
great code base for simple GA optimizer.
Profile Image for Scott.
460 reviews11 followers
August 13, 2019
A really interesting read. It's kind of amazing to think that I stumbled across this idea on my own with very little programming experience at my first job.

This is far more robust than what I came up with, though. The much more reusable and flexible genetic engine is a great concept and one I plan to experiment with in the future. This is so great for NP problems like those slot machine reels were.
Displaying 1 - 5 of 5 reviews

Can't find what you're looking for?

Get help and learn more about the design.