Table Of ContentEarly Praise for Genetic Algorithms and Machine Learning for Programmers
I really like the book; it’s pitched nicely at the interested beginner and doesn’t
make undue assumptions about background knowledge.
➤ Burkhard Kloss
Director, Applied Numerical Research Labs
A unique take on the subject and should very much appeal to programmers
looking to get started with various machine learning techniques.
➤ Christopher L. Simons
Senior Lecturer, University of the West of England, Bristol, UK
Turtles, paper bags, escape, AI, fun whilst learning: it’s turtles all the way out.
➤ Russel Winder
Retired Consultant, Self-Employed
This book lifts the veil on the complexity and magic of machine learning techniques
for ordinary programmers. Simple examples and interactive programs really show
you not just how these algorithms work, but bring real-world problems to life.
➤ Steve Love
Programmer, Freelance
We've left this page blank to
make the page numbers the
same in the electronic and
paper books.
We tried just leaving it out,
but then people wrote us to
ask about the missing pages.
Anyway, Eddy the Gerbil
wanted to say “hello.”
Genetic Algorithms and Machine
Learning for Programmers
Create AI Models and Evolve Solutions
Frances Buontempo
The Pragmatic Bookshelf
Raleigh, North Carolina
Many of the designations used by manufacturers and sellers to distinguish their products
are claimed as trademarks. Where those designations appear in this book, and The Pragmatic
Programmers, LLC was aware of a trademark claim, the designations have been printed in
initial capital letters or in all capitals. The Pragmatic Starter Kit, The Pragmatic Programmer,
Pragmatic Programming, Pragmatic Bookshelf, PragProg and the linking g device are trade-
marks of The Pragmatic Programmers, LLC.
Every precaution was taken in the preparation of this book. However, the publisher assumes
no responsibility for errors or omissions, or for damages that may result from the use of
information (including program listings) contained herein.
Our Pragmatic books, screencasts, and audio books can help you and your team create
better software and have more fun. Visit us at https://pragprog.com.
The team that produced this book includes:
Publisher: Andy Hunt
VP of Operations: Janet Furlow
Managing Editor: Susan Conant
Development Editor: Tammy Coron
Copy Editor: Jasmine Kwityn
Indexing: Potomac Indexing, LLC
Layout: Gilson Graphics
For sales, volume licensing, and support, please contact support@pragprog.com.
For international rights, please contact rights@pragprog.com.
Copyright © 2019 The Pragmatic Programmers, LLC.
All rights reserved. No part of this publication may be reproduced, stored in a retrieval system,
or transmitted, in any form, or by any means, electronic, mechanical, photocopying, recording,
or otherwise, without the prior consent of the publisher.
ISBN-13: 978-1-68050-620-4
Book version: P1.0—January 2019
Contents
Preface . . . . . . . . . . . . . . ix
1. Escape! Code Your Way Out of a Paper Bag . . . . . 1
Let’s Begin 3
Your Mission: Find a Way Out 4
How to Help the Turtle Escape 6
Let’s Save the Turtle 7
Did It Work? 11
Over to You 13
2. Decide! Find the Paper Bag . . . . . . . . . 15
Your Mission: Learn from Data 16
How to Grow a Decision Tree 18
Let’s Find That Paper Bag 23
Did It Work? 27
Over to You 31
3. Boom! Create a Genetic Algorithm . . . . . . . 33
Your Mission: Fire Cannonballs 35
How to Breed Solutions 37
Let’s Fire Some Cannons 40
Did It Work? 48
Over to You 53
4. Swarm! Build a Nature-Inspired Swarm . . . . . . 57
Your Mission: Crowd Control 58
How to Form a Swarm 66
Let’s Make a Swarm 69
Did It Work? 76
Over to You 78
Contents • vi
5. Colonize! Discover Pathways . . . . . . . . 79
Your Mission: Lay Pheromones 80
How to Create Pathways 83
Let’s March Some Ants 85
Did It Work? 93
Over to You 97
6. Diffuse! Employ a Stochastic Model . . . . . . . 99
Your Mission: Make Small Random Steps 100
How to Cause Diffusion 109
Let’s Diffuse Some Particles 111
Did It Work? 119
Over to You 125
7. Buzz! Converge on One Solution . . . . . . . 127
Your Mission: Beekeeping 128
How to Feed the Bees 131
Let’s Make Some Bees Swarm 133
Did It Work? 143
Over to You 145
8. Alive! Create Artificial Life . . . . . . . . . 147
Your Mission: Make Cells Come Alive 149
How to Create Artificial Life 152
Let’s Make Cellular Automata 154
Did It Work? 160
Over to You 161
9. Dream! Explore CA with GA . . . . . . . . 163
Your Mission: Find the Best 164
How to Explore a CA 167
Let’s Find the Best Starting Row 169
Did It Work? 181
Over to You 185
10. Optimize! Find the Best . . . . . . . . . 187
Your Mission: Move Turtles 188
How to Get a Turtle into a Paper Bag 189
Let’s Find the Bottom of the Bag 193
Did It Work? 198
Extension to More Dimensions 203
Over to You 205
Contents • vii
Bibliography . . . . . . . . . . . . 207
Index . . . . . . . . . . . . . . 209
Preface
Have you ever heard the phrase “Coding your way out of a paper bag”? In
this book, you’ll do exactly that. In each chapter, you’ll examine different
machine learning techniques that you can use to programmatically get parti-
cles, ants, bees, and even turtles out of a paper bag. While the metaphor itself
may be silly, it’s a great way to demonstrate how algorithms find solutions
over time.
Who Is This Book For?
If you’re a beginner to intermediate programmer keen to understand machine
learning, this book is for you. Inside its pages, you’ll create genetic algorithms,
nature-inspired swarms, Monte Carlo simulations, cellular automata, and
clusters. You’ll also learn how to test your code as you dive into even more
advanced topics.
Experts in machine learning may still enjoy the “programming out of a paper
bag” metaphor, though they are unlikely to learn new things.
What’s in This Book?
In this book, you will:
• Use heuristics and design fitness functions
• Build genetic algorithms
• Make nature-inspired swarms with ants, bees, and particles
• Create Monte Carlo simulations
• Investigate cellular automata
• Find minima and maxima using hill climbing and simulated annealing
• Try selection methods, including tournament and roulette wheels
• Learn about heuristics, fitness functions, metrics, and clusters
You’ll also test your code, get inspired to solve new problems, and work
through scenarios to code your way out of a paper bag—an important skill
for any competent programmer. Beyond that, you’ll see how the algorithms
report erratum • discuss