...

/

Creating the Problem Behaviour

Creating the Problem Behaviour

Learn how to create a behaviour and integrate it into the genetic algorithm.

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:

Press + to interact
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 ...