Genetic Algorithms in Elixir: Chapter 2

Now working through chapter 2.

An underscore makes a better placeholder than …

Intialize is added above the two rum functions. We only have one so far…

The first evolve does not need opts.

Most of the new functions should start as defp

run does not need the population variable.

By Hyperparameters initialize already takes genotype so opts is an additional Param.

2 Likes

I’ll adjust the placeholder. As far as the reference to two run functions, I initially named run and evolve the same thing, I must have missed that reference. Most of the stuff in regards to passing hyperparameters via opts is necessary for playing with some configurations later on. For example, opts is used to specify :population_size in initialize. I believe there’s a mistake in the transcription of the signature of initialize where it says:

def initialize(opts \\ []) do
   # ...omitted...
end

It should say:

def initialize(genotype, opts \\ []) do
   # ...omitted...
end

Thank you for the feedback :slight_smile: !

3 Likes

Good suggestions, but I don’t agree that an underscore is better than “…” as a placeholder. The underscore is used frequently in Elixir code, so it doesn’t stand out as something needed to be replace is “…” does to me.

I am really enjoying the book (B2) so far.

The version of evolve/4 on page 27 takes genotype as an argument, but it doesn’t seem to need it for anything other than to pass it to itself again recursively. The version on page 28 looks like it was corrected in this regard.

If that isn’t needed, it seems like a slightly prettier way to write run/3 would be:

def run(genotype, fitness_function, max_fitness) do
  genotype
  |> initialize() # maybe call this `initialize_population`
  |> evolve(fitness_function, max_fitness)
end

An even more pedantic suggestion - use verbs for all the function names in the pipeline, i.e., mutate instead of mutation and maybe cross instead of crossover.

2 Likes