Understanding the Flow of Genetic Algorithms

Genetic Algorithms in Elixir — by Sean Moriarity (13 / 101)

The Pragmatic Programmers
The Pragmatic Programmers

--

👈 Initializing the Population | TOC | Selecting Parents 👉

You’re now ready to start writing your algorithm. But, before you begin, what do you notice about the structure of the genetic algorithm previously described? Specifically, what happens after children are mutated?

The answer: the process repeats itself. Genetic algorithms are recursive meaning the algorithm repeats itself over and over until it hits a termination point.

Below the initial population, add the following:

​ algorithm =
​ ​fn​ population, algorithm ->
​ ​# Algorithm here​
​ ​end​

The algorithm is an anonymous function that takes two parameters: population represents the current generation’s population, and algorithm is a reference to itself. This is a trick used to implement a recursive anonymous function. It’s not essential to understand why this works or why this is necessary. All you need to know is that algorithm is a reference to your algorithm function.

In other languages, you’ll likely see genetic algorithms implemented using loops instead of recursion. You need to use recursion because Elixir doesn’t support loops. But remember, a recursive function is just as powerful as a loop.

Recursive functions usually have two branches: a base-case and a recursive case. The base case…

--

--

The Pragmatic Programmers
The Pragmatic Programmers

We create timely, practical books and learning resources on classic and cutting-edge topics to help you practice your craft and accelerate your career.