Search⌘ K

Creating the Problem Behaviour

Explore how to create a Problem behaviour in Elixir for genetic algorithms. Learn to define essential callbacks like genotype, fitness function, and termination criteria, and adapt your framework to work with a custom chromosome struct to effectively manage evolution processes.

We'll cover the following...

Behaviour creation

To start writing your behaviour, we will first create a new file within the lib directory named problem.ex.

Then, we will open this problem.ex file and add a new module named Problem, like this:

C++
defmodule Problem do
alias Types.Chromosome
end

This is a barebones module that contains an alias for the Chromosome module we created at the beginning of this chapter. The alias makes accessing the Chromosome module easier later on.

At this point, it’s time to start thinking about what a problem consists of? Remember, we need to define callbacks that can be implemented by modules that adopt the Problem behaviour. In the previous chapter, the problem-specific functions were a fitness function and a genotype function, which is a good place to start.

Think about what each of these functions took as input and what they needed to do. The genotype function didn’t require any input. All it needed to do was return an enumerable which represented a single chromosome. In this chapter, we created a chromosome type that works in place of the original representation of a ...