speller.exs (747B)
1 defmodule Speller do 2 alias Types.Chromosome 3 @behaviour Problem 4 5 @impl true 6 def genotype do 7 genes = 8 Stream.repeatedly(fn -> Enum.random(?a..?z) end) 9 |> Enum.take(8) 10 11 %Chromosome{genes: genes, size: 8} 12 end 13 14 @impl true 15 def fitness_function(chromosome) do 16 target = "dracuxan" 17 guess = List.to_string(chromosome.genes) 18 String.jaro_distance(target, guess) 19 end 20 21 @impl true 22 def terminate?(population, _generation) do 23 [best | _] = population 24 best.fitness == 1 25 end 26 end 27 28 {soln, _} = 29 Genetic.run(Speller, 30 selection_type: &Toolbox.Selection.elite/2, 31 mutation_type: &Toolbox.Mutation.flip/2, 32 crossover_type: &Toolbox.Crossover.uniform/3 33 ) 34 35 IO.puts("\nfinal answer: #{inspect(soln)}")