Customizing Mutation in Your Framework

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

The Pragmatic Programmers
The Pragmatic Programmers

--

👈 Understanding Mutation | TOC | Implementing Common Mutation Strategies 👉

Just like in Chapter 5, Selecting the Best, and Chapter 6, Generating New Solutions, you’ll need to slightly modify your framework to allow you to customize mutation hyperparameters. These hyperparameters are mutation strategy and mutation rate.

In this section, you’ll create a mutation toolbox and modify your framework to allow you to easily customize mutation in your algorithms.

Creating the Toolbox

First, you need to create a mutation toolbox, just like you created a selection and crossover toolbox. Create a new file in toolbox called mutation.ex.

Next, create the Toolbox.Mutation module, like this:

​ ​defmodule​ Toolbox.Mutation ​do​
​ alias Types.Chromosome

​ ​# ...​
​ ​end​

You’ll be working a lot with the Chromosome struct in this module, so you’ll want to create an alias. Now, whenever you implement a new mutation strategy, you’ll add it to your mutation toolbox.

Changing Mutation Strategy

Open up genetic.ex and navigate to the mutation/2 function. It looks like this:

​ ​def​ mutation(population, opts \\ []) ​do​
​ population
​ |> Enum.map(
​ ​fn​ chromosome ->…

--

--

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.