commit 50ec3f9878802f499faa601773377bdc5c11d7ef parent fbf139f3fbbb9e4e280d7e353905616832c331f2 Author: dracuxan <[email protected]> Date: Wed, 22 Apr 2026 21:18:32 +0530 change dir structure Diffstat:
28 files changed, 85 insertions(+), 147 deletions(-)
diff --git a/genetic/.formatter.exs b/.formatter.exs diff --git a/.gitignore b/.gitignore @@ -1 +1,29 @@ -*.dump +# The directory Mix will write compiled artifacts to. +/_build/ + +# If you run "mix test --cover", coverage assets end up here. +/cover/ + +# The directory Mix downloads your dependencies sources to. +/deps/ + +# Where third-party dependencies like ExDoc output generated docs. +/doc/ + +# If the VM crashes, it generates a dump, let's ignore it too. +erl_crash.dump + +# Also ignore archive artifacts (built via "mix archive.build"). +*.ez + +# Ignore package tarball (built via "mix hex.build"). +genetic-*.tar + +# Temporary files, for example, from tests. +/tmp/ + +.codex + +games/ + +*.dot diff --git a/genetic/README.md b/README.md diff --git a/genetic/.gitignore b/genetic/.gitignore @@ -1,29 +0,0 @@ -# The directory Mix will write compiled artifacts to. -/_build/ - -# If you run "mix test --cover", coverage assets end up here. -/cover/ - -# The directory Mix downloads your dependencies sources to. -/deps/ - -# Where third-party dependencies like ExDoc output generated docs. -/doc/ - -# If the VM crashes, it generates a dump, let's ignore it too. -erl_crash.dump - -# Also ignore archive artifacts (built via "mix archive.build"). -*.ez - -# Ignore package tarball (built via "mix hex.build"). -genetic-*.tar - -# Temporary files, for example, from tests. -/tmp/ - -.codex - -games/ - -*.dot diff --git a/genetic/scripts/one_max.exs b/genetic/scripts/one_max.exs @@ -1,67 +0,0 @@ -defmodule OneMax do - alias Types.Chromosome - @behaviour Problem - @max_n 1000 - - @impl true - def genotype do - genes = Enum.map(1..@max_n, fn _ -> Enum.random(0..1) end) - %Chromosome{genes: genes, size: @max_n} - end - - @impl true - def fitness_function(chromosome) do - Enum.sum(chromosome.genes) - end - - @impl true - def terminate?(population, _generation) do - best = Enum.max_by(population, &OneMax.fitness_function/1) - best.fitness == @max_n - - # best = Enum.min_by(population, &OneMax.fitness_function/1) - # best.fitness == 0 - - # avg = - # population - # |> Enum.map(&Enum.sum(&1.genes)) - # |> Enum.sum() - # |> Kernel./(length(population)) - # - # avg >= 15 - - # generation == 100 - end -end - -{soln, generation} = - Genetic.run( - OneMax, - population_size: 100, - selection_rate: 0.8, - mutation_rate: 0.01, - survival_rate: 0.2, - selection_type: &Toolbox.Selection.elite/2, - crossover_type: &Toolbox.Crossover.uniform/3, - mutation_type: &Toolbox.Mutation.scramble/2 - ) - -IO.puts("\nfinal answer: #{inspect(soln)}") -IO.puts("generations passed: #{generation}") - -# {_, tgs} = Utilities.Statistics.lookup(generation) -# IO.inspect(tgs) - -stats = - :ets.tab2list(:statistics) - |> Enum.sort_by(fn {gen, _stat} -> gen end) - |> Enum.map(fn {gen, stats} -> [gen, stats.mean_fitness] end) - -{:ok, _} = - Gnuplot.plot( - [ - [:set, :title, "mean fitness versus generation (one_max)"], - [:plot, "-", :with, :line] - ], - [stats] - ) diff --git a/genetic/lib/application.ex b/lib/application.ex diff --git a/genetic/lib/genetic.ex b/lib/genetic.ex diff --git a/genetic/lib/problem.ex b/lib/problem.ex diff --git a/genetic/lib/toolbox/crossover.ex b/lib/toolbox/crossover.ex diff --git a/genetic/lib/toolbox/mutation.ex b/lib/toolbox/mutation.ex diff --git a/genetic/lib/toolbox/reinsertion.ex b/lib/toolbox/reinsertion.ex diff --git a/genetic/lib/toolbox/selection.ex b/lib/toolbox/selection.ex diff --git a/genetic/lib/types/chromosome.ex b/lib/types/chromosome.ex diff --git a/genetic/lib/utilities/genealogy.ex b/lib/utilities/genealogy.ex diff --git a/genetic/lib/utilities/statistics.ex b/lib/utilities/statistics.ex diff --git a/genetic/mix.exs b/mix.exs diff --git a/genetic/mix.lock b/mix.lock diff --git a/genetic/scripts/cargo.exs b/scripts/cargo.exs diff --git a/genetic/scripts/codebreaker.exs b/scripts/codebreaker.exs diff --git a/genetic/scripts/n_queens.exs b/scripts/n_queens.exs diff --git a/scripts/one_max.exs b/scripts/one_max.exs @@ -1,61 +1,67 @@ -population = - for _ <- 1..100 do - for _ <- 1..1000 do - Enum.random([1, 0]) - end - end +defmodule OneMax do + alias Types.Chromosome + @behaviour Problem + @max_n 1000 -evaluate = - fn population -> - Enum.sort_by(population, &Enum.sum/1) + @impl true + def genotype do + genes = Enum.map(1..@max_n, fn _ -> Enum.random(0..1) end) + %Chromosome{genes: genes, size: @max_n} end -selection = - fn population -> - population - |> Enum.chunk_every(2) - |> Enum.map(&List.to_tuple/1) + @impl true + def fitness_function(chromosome) do + Enum.sum(chromosome.genes) end -crossover = - fn population -> - Enum.reduce(population, [], fn {p1, p2}, acc -> - cx_split = :rand.uniform(1000) + @impl true + def terminate?(population, _generation) do + best = Enum.max_by(population, &OneMax.fitness_function/1) + best.fitness == @max_n - {{h1, t1}, {h2, t2}} = - {Enum.split(p1, cx_split), Enum.split(p2, cx_split)} + # best = Enum.min_by(population, &OneMax.fitness_function/1) + # best.fitness == 0 - [h1 ++ t2, h2 ++ t1 | acc] - end) - end + # avg = + # population + # |> Enum.map(&Enum.sum(&1.genes)) + # |> Enum.sum() + # |> Kernel./(length(population)) + # + # avg >= 15 -mutation = - fn population -> - Enum.map(population, fn chromosome -> - if :rand.uniform() < 0.05 do - Enum.shuffle(chromosome) - else - chromosome - end - end) + # generation == 100 end +end -algorithm = - fn population, algorithm -> - best = Enum.max_by(population, &Enum.sum/1) - IO.write("\rcurrent best: " <> Integer.to_string(Enum.sum(best))) - - if Enum.sum(best) == 1000 do - best - else - population - |> evaluate.() - |> selection.() - |> crossover.() - |> mutation.() - |> algorithm.(algorithm) - end - end +{soln, generation} = + Genetic.run( + OneMax, + population_size: 100, + selection_rate: 0.8, + mutation_rate: 0.01, + survival_rate: 0.2, + selection_type: &Toolbox.Selection.elite/2, + crossover_type: &Toolbox.Crossover.uniform/3, + mutation_type: &Toolbox.Mutation.scramble/2 + ) + +IO.puts("\nfinal answer: #{inspect(soln)}") +IO.puts("generations passed: #{generation}") + +# {_, tgs} = Utilities.Statistics.lookup(generation) +# IO.inspect(tgs) + +stats = + :ets.tab2list(:statistics) + |> Enum.sort_by(fn {gen, _stat} -> gen end) + |> Enum.map(fn {gen, stats} -> [gen, stats.mean_fitness] end) -sol = algorithm.(population, algorithm) -IO.puts("\nfinal answer: #{inspect(sol)}") +{:ok, _} = + Gnuplot.plot( + [ + [:set, :title, "mean fitness versus generation (one_max)"], + [:plot, "-", :with, :line] + ], + [stats] + ) diff --git a/genetic/scripts/portfolio.exs b/scripts/portfolio.exs diff --git a/genetic/scripts/schedule.exs b/scripts/schedule.exs diff --git a/genetic/scripts/speller.exs b/scripts/speller.exs diff --git a/genetic/scripts/tetris.exs b/scripts/tetris.exs diff --git a/genetic/scripts/tiger_sim.exs b/scripts/tiger_sim.exs diff --git a/genetic/test/genetic_test.exs b/test/genetic_test.exs diff --git a/genetic/test/test_helper.exs b/test/test_helper.exs