More changes

This commit is contained in:
2022-06-28 08:57:51 +02:00
parent 0b318ac0e4
commit e367229253
6 changed files with 129 additions and 20 deletions

31
scripts/portfolio.exs Normal file
View File

@@ -0,0 +1,31 @@
defmodule Portfolio do
@behaviour Problem
alias Types.Chromosome
@target_fitness 180
@impl true
def genotype do
genes =
for _ <- 1..10, do:
{:rand.uniform(10), :rand.uniform(10)}
%Chromosome{genes: genes, size: 10}
end
@impl true
def fitness_function(chromosome) do
chromosome.genes
|> Enum.map(fn {roi, risk} -> 2 * roi - risk end)
|> Enum.sum()
end
@impl true
def terminate?(population, _generation) do
max_value = Enum.max_by(population, &Portfolio.fitness_function/1)
max_value > @target_fitness
end
end
soln = Genetic.run(Portfolio)