genetic

genetic algorithm framework
git clone igris.git:dracuxan/genetic.git
Log | Files | Refs | README

commit 1f36b04ae4e0a9265ec327b1954af2adc0704b69
parent 50ec3f9878802f499faa601773377bdc5c11d7ef
Author: dracuxan <[email protected]>
Date:   Fri, 24 Apr 2026 11:25:14 +0530

add: benchmarks

Diffstat:
Abench/benchmark.exs | 33+++++++++++++++++++++++++++++++++
Abench/profile.exs | 39+++++++++++++++++++++++++++++++++++++++
2 files changed, 72 insertions(+), 0 deletions(-)

diff --git a/bench/benchmark.exs b/bench/benchmark.exs @@ -0,0 +1,33 @@ +defmodule DummyProblem do + alias Types.Chromosome + @behaviour Problem + + @impl true + def genotype do + genes = Enum.map(1..10000, fn _ -> Enum.random(0..1) end) + %Chromosome{genes: genes, size: 10000} + end + + @impl true + def fitness_function(chromosome), do: Enum.sum(chromosome.genes) + + @impl true + def terminate?(_pop, gen), do: gen == 1 +end + +dummy_pop = Genetic.initialize(&DummyProblem.genotype/0, population_size: 1000) +{dumm_selected_pop, _} = Genetic.select(dummy_pop, selection_rate: 1.0) + +Benchee.run( + %{ + "initialize" => fn -> Genetic.initialize(&DummyProblem.genotype/0) end, + "evaluate" => fn -> + Genetic.evaluate(dummy_pop, &DummyProblem.fitness_function/1) + end, + "select" => fn -> Genetic.select(dummy_pop) end, + "crossover" => fn -> Genetic.crossover(dumm_selected_pop) end, + "mutation" => fn -> Genetic.mutation(dummy_pop) end, + "evolve" => fn -> Genetic.evolve(dummy_pop, DummyProblem, 0) end + }, + memory_time: 2 +) diff --git a/bench/profile.exs b/bench/profile.exs @@ -0,0 +1,39 @@ +defmodule DummyProblem do + @behaviour Problem + alias Types.Chromosome + + @impl true + def genotype do + genes = Enum.map(1..100, fn _ -> Enum.random(0..1) end) + %Chromosome{genes: genes, size: 100} + end + + @impl true + def fitness_function(chromosome), do: Enum.sum(chromosome.genes) + + @impl true + def terminate?(_pop, gen), do: gen == 1 +end + +defmodule Profiler do + import ExProf.Macro + + def do_analyze do + profile do + Genetic.run(DummyProblem) + end + end + + def run do + {records, _block_result} = do_analyze() + + total_percent = + Enum.reduce(records, 0.0, fn + records, acc -> records.percent + acc + end) + + IO.inspect("total = #{total_percent}") + end +end + +Profiler.run()