commit f1a0f1b8963db3f578f815a962fd80a7089ca660
parent c3c36069005cf599e8e8aa4a47613d7421738c66
Author: dracuxan <[email protected]>
Date: Tue, 7 Apr 2026 18:08:44 +0530
upd: change final return value to tupple of best solution and generation
Diffstat:
5 files changed, 8 insertions(+), 8 deletions(-)
diff --git a/genetic/lib/genetic.ex b/genetic/lib/genetic.ex
@@ -83,7 +83,7 @@ defmodule Genetic do
IO.write("\rcurrent best: #{best.fitness}")
if problem.terminate?(population, generation) do
- best
+ {best, generation}
else
{parents, leftover} = select(population)
children = crossover(parents, opts)
diff --git a/genetic/scripts/cargo.exs b/genetic/scripts/cargo.exs
@@ -37,7 +37,7 @@ defmodule Cargo do
end
end
-soln = Genetic.run(Cargo)
+{soln, _} = Genetic.run(Cargo)
IO.puts("\nsolution: #{inspect(soln)}")
weight =
diff --git a/genetic/scripts/one_max.exs b/genetic/scripts/one_max.exs
@@ -33,5 +33,5 @@ defmodule OneMax do
end
end
-soln = Genetic.run(OneMax, selection_type: &Toolbox.Selection.elite/2)
+{soln, _} = Genetic.run(OneMax, selection_type: &Toolbox.Selection.elite/2)
IO.puts("\nfinal answer: #{inspect(soln.genes)}")
diff --git a/genetic/scripts/portfolio.exs b/genetic/scripts/portfolio.exs
@@ -25,5 +25,5 @@ defmodule Portfolio do
end
end
-soln = Genetic.run(Portfolio)
+{soln, _} = Genetic.run(Portfolio)
IO.puts("\nfinal answer: #{inspect(soln)}")
diff --git a/genetic/scripts/speller.exs b/genetic/scripts/speller.exs
@@ -6,14 +6,14 @@ defmodule Speller do
def genotype do
genes =
Stream.repeatedly(fn -> Enum.random(?a..?z) end)
- |> Enum.take(10)
+ |> Enum.take(8)
- %Chromosome{genes: genes, size: 10}
+ %Chromosome{genes: genes, size: 8}
end
@impl true
def fitness_function(chromosome) do
- target = "abcdefghij"
+ target = "dracuxan"
guess = List.to_string(chromosome.genes)
String.jaro_distance(target, guess)
end
@@ -25,5 +25,5 @@ defmodule Speller do
end
end
-soln = Genetic.run(Speller, selection_type: &Toolbox.Selection.roulette/2)
+{soln, _} = Genetic.run(Speller, selection_type: &Toolbox.Selection.roulette/2)
IO.puts("\nfinal answer: #{inspect(soln)}")