Building a Framework for Genetic Algorithms

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

The Pragmatic Programmers
The Pragmatic Programmers

--

👈 Using Mix to Write Genetic Algorithms | TOC | Understanding Hyperparameters 👉

You now have an empty project and an idea of what your genetic algorithm framework should look like. It’s time to start implementing each step.

Start by opening the genetic.ex file. The file is populated with some default code and will look something like this:

​ ​defmodule​ Genetic ​do​
​ ...documentation...
​ ​def​ hello ​do​
​ ​:world​
​ ​end​

​ ​end​

You can delete the default documentation and Hello World function. This module will contain the most basic parts of your genetic algorithm. This is where you’ll define each step of the algorithm based on the rules you determined earlier.

Creating an Initial Outline

In genetic.ex, start by creating a function named run, like this:

​ ​def​ run(...) ​do​
​ population = initialize()
​ population
​ |> evolve()
​ ​end​

This function calls the initialize function, which is responsible for creating the initial population. You might be wondering why the parameters of run are left blank. You’ll worry about them later. For now, concentrate on the overall structure of the code.

After you initialize the population, you pass the population into a function…

--

--

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.