The evolve
signature in this code sample suddenly loses the population
argument which I think isn’t intended, the code goes on to use it a little later without an alternate source for it.
Edit: In this same code block, the variable best
gets assigned an integer whereas previous it’d been a Chromosome. Other code (like our printing the results) expects best to contain a Chromosome, as it gets returned from evolve().
My amended evolve function looks like this instead:
def evolve(population, problem, generation, last_max_fitness, temperature, opts \\ []) do
population = evaluate(population, &problem.fitness_function/1, opts)
best = Enum.max_by(population, &problem.fitness_function/1)
best_fitness = best.fitness
temperature = 0.8 * (temperature + (best_fitness - last_max_fitness))
IO.write("\rCurrent Best: #{List.to_string(best.genes)} (#{best.fitness})")
if problem.terminate?(population, generation, temperature) do
best
else
generation = generation + 1
population
|> select(opts)
|> crossover(opts)
|> mutation(opts)
|> evolve(problem, generation, best_fitness, temperature, opts)
end
end
(Disregard the IO.write, I like to see what’s going on :))