commit 2eecb3f32236433e558c3e451bd0d5437b1a838c
parent 0606211a1e8e0a5011f0213e982e6d9fac285285
Author: dracuxan <[email protected]>
Date: Thu, 30 Apr 2026 12:11:07 +0530
separate initialization functions in toolbox
Diffstat:
7 files changed, 29 insertions(+), 5 deletions(-)
diff --git a/.gitignore b/.gitignore
@@ -28,4 +28,6 @@ games/
*.dot
+notes/
+
priv/
diff --git a/lib/genetic.ex b/lib/genetic.ex
@@ -9,8 +9,8 @@ defmodule Genetic do
end
def initialize(genotype, opts \\ []) do
- population_size = Keyword.get(opts, :population_size, 100)
- population = pmap(1..population_size, fn _ -> genotype.() end)
+ init_fn = Keyword.get(opts, :initialization_type, &Toolbox.Initialize.random/2)
+ population = apply(init_fn, [genotype, opts])
Utilities.Genealogy.add_chromosomes(population)
population
end
diff --git a/lib/toolbox/initialize.ex b/lib/toolbox/initialize.ex
@@ -0,0 +1,6 @@
+defmodule Toolbox.Initialize do
+ def random(genotype, opts \\ []) do
+ population_size = Keyword.get(opts, :population_size, 100)
+ Enum.map(1..population_size, fn _ -> genotype.() end)
+ end
+end
diff --git a/mix.exs b/mix.exs
@@ -28,7 +28,8 @@ defmodule Genetic.MixProject do
{:benchee, "~> 1.5.0"},
{:exprof, "~> 0.2.4"},
{:alex, "~> 0.3.2"},
- {:rustler, "~> 0.31.0"}
+ {:rustler, "~> 0.31.0"},
+ {:math, "~> 0.6.0"}
]
end
end
diff --git a/mix.lock b/mix.lock
@@ -12,6 +12,7 @@
"makeup": {:hex, :makeup, "1.2.1", "e90ac1c65589ef354378def3ba19d401e739ee7ee06fb47f94c687016e3713d1", [:mix], [{:nimble_parsec, "~> 1.4", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm", "d36484867b0bae0fea568d10131197a4c2e47056a6fbe84922bf6ba71c8d17ce"},
"makeup_elixir": {:hex, :makeup_elixir, "1.0.1", "e928a4f984e795e41e3abd27bfc09f51db16ab8ba1aebdba2b3a575437efafc2", [:mix], [{:makeup, "~> 1.0", [hex: :makeup, repo: "hexpm", optional: false]}, {:nimble_parsec, "~> 1.2.3 or ~> 1.3", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm", "7284900d412a3e5cfd97fdaed4f5ed389b8f2b4cb49efc0eb3bd10e2febf9507"},
"makeup_erlang": {:hex, :makeup_erlang, "1.0.3", "4252d5d4098da7415c390e847c814bad3764c94a814a0b4245176215615e1035", [:mix], [{:makeup, "~> 1.0", [hex: :makeup, repo: "hexpm", optional: false]}], "hexpm", "953297c02582a33411ac6208f2c6e55f0e870df7f80da724ed613f10e6706afd"},
+ "math": {:hex, :math, "0.6.0", "69325af99e600123f6d994833502f6d063a2fa8f2786a3c0461fe6c6123a5166", [:mix], [], "hexpm", "2c73a64d64f719ee1f2821d382f3ed63e8e9564e5176d1c8aa777aac49b41bf7"},
"nimble_parsec": {:hex, :nimble_parsec, "1.4.2", "8efba0122db06df95bfaa78f791344a89352ba04baedd3849593bfce4d0dc1c6", [:mix], [], "hexpm", "4b21398942dda052b403bbe1da991ccd03a053668d147d53fb8c4e0efe09c973"},
"rustler": {:hex, :rustler, "0.31.0", "7e5eefe61e6e6f8901e5aa3de60073d360c6320d9ec363027b0197297b80c46a", [:mix], [{:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: false]}, {:toml, "~> 0.6", [hex: :toml, repo: "hexpm", optional: false]}], "hexpm", "99e378459bfb9c3bda6d3548b2b3bc6f9ad97f728f76bdbae7bf5c770a4f8abd"},
"statistex": {:hex, :statistex, "1.1.0", "7fec1eb2f580a0d2c1a05ed27396a084ab064a40cfc84246dbfb0c72a5c761e5", [:mix], [], "hexpm", "f5950ea26ad43246ba2cce54324ac394a4e7408fdcf98b8e230f503a0cba9cf5"},
diff --git a/scripts/cargo.exs b/scripts/cargo.exs
@@ -38,7 +38,7 @@ defmodule Cargo do
end
{soln, _} = Genetic.run(Cargo, crossover_type: &Toolbox.Crossover.single_point/3)
-IO.puts("\nsolution: #{inspect(soln)}")
+IO.puts("\nsolution: #{inspect(soln.genes)}")
weight =
soln.genes
diff --git a/scripts/schedule.exs b/scripts/schedule.exs
@@ -29,7 +29,7 @@ defmodule Schedule do
end
def terminate?(_population, generation) do
- generation == 1000
+ generation == 350
end
defp credit_hours, do: [3.0, 3.0, 3.0, 4.5, 3.0, 3.0, 3.0, 3.0, 4.5, 1.5]
@@ -45,3 +45,17 @@ end
)
IO.puts("\nschedule: #{inspect(soln)}")
+
+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 (schedule)"],
+ [:plot, "-", :with, :points]
+ ],
+ [stats]
+ )