More changes
This commit is contained in:
50
scripts/cargo.exs
Normal file
50
scripts/cargo.exs
Normal file
@@ -0,0 +1,50 @@
|
||||
defmodule Cargo do
|
||||
@behaviour Problem
|
||||
alias Types.Chromosome
|
||||
|
||||
@impl true
|
||||
def genotype do
|
||||
genes = for _ <- 1..10, do: Enum.random(0..1)
|
||||
%Chromosome{genes: genes, size: 10}
|
||||
end
|
||||
|
||||
@impl true
|
||||
def fitness_function(chromosome) do
|
||||
profits = [6, 5, 8, 9, 6, 7, 3, 1, 2, 6]
|
||||
weights = [10, 6, 8, 7, 10, 9, 7, 11, 6, 8]
|
||||
weight_limit = 40
|
||||
|
||||
potential_profits =
|
||||
chromosome.genes
|
||||
|> Enum.zip(profits)
|
||||
|> Enum.map(fn {g, p} -> g * p end)
|
||||
|> Enum.sum()
|
||||
|
||||
over_limit? =
|
||||
chromosome.genes
|
||||
|> Enum.zip(weights)
|
||||
|> Enum.map(fn {c, w} -> c * w end)
|
||||
|> Enum.sum()
|
||||
|> Kernel.>(weight_limit)
|
||||
|
||||
profits = if over_limit?, do: 0, else: potential_profits
|
||||
profits
|
||||
end
|
||||
|
||||
@impl true
|
||||
def terminate?(population, _generation) do
|
||||
Enum.max_by(population, &Cargo.fitness_function/1).fitness == 53
|
||||
end
|
||||
end
|
||||
|
||||
soln = Genetic.run(Cargo, population_size: 50)
|
||||
|
||||
IO.write("\n")
|
||||
IO.inspect(soln)
|
||||
|
||||
weight = soln.genes
|
||||
|> Enum.zip([10, 6, 8, 7, 10, 9, 7, 11, 6, 8])
|
||||
|> Enum.map(fn {g, w} -> w*g end)
|
||||
|> Enum.sum()
|
||||
|
||||
IO.write("\nWeight is: #{weight}\n")
|
@@ -1,23 +1,24 @@
|
||||
genotype = fn -> for _ <- 1..1000, do: Enum.random(0..1) end
|
||||
|
||||
fitness_function = fn chromosome -> Enum.sum(chromosome) end
|
||||
max_fitness = 1000
|
||||
|
||||
defmodule OneMax do
|
||||
alias Types.Chromosome
|
||||
|
||||
@behaviour Problem
|
||||
|
||||
@impl Problem
|
||||
@impl true
|
||||
def genotype() do
|
||||
for _ <- 1..1000, do: Enum.random(0..
|
||||
genes = for _ <- 1..1000, do: Enum.random(0..1)
|
||||
%Chromosome{genes: genes}
|
||||
end
|
||||
|
||||
@impl Problem
|
||||
@impl true
|
||||
def fitness_function(chromosome) do
|
||||
Enum.sum(chromosome.genes)
|
||||
end
|
||||
|
||||
@impl true
|
||||
def terminate?(_population, generation), do: generation == 100
|
||||
end
|
||||
|
||||
soln = Genetic.run(fitness_function, genotype, max_fitness)
|
||||
soln = Genetic.run(OneMax)
|
||||
|
||||
IO.write("\n")
|
||||
IO.inspect(soln)
|
||||
IO.inspect(soln.genes)
|
||||
|
31
scripts/portfolio.exs
Normal file
31
scripts/portfolio.exs
Normal 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)
|
25
scripts/speller.exs
Normal file
25
scripts/speller.exs
Normal file
@@ -0,0 +1,25 @@
|
||||
defmodule Speller do
|
||||
@behaviour Problem
|
||||
alias Types.Chromosome
|
||||
|
||||
def genotype do
|
||||
genes =
|
||||
Stream.repeatedly(fn -> Enum.random(?a..?z) end)
|
||||
|> Enum.take(34)
|
||||
%Chromosome{genes: genes, size: 34}
|
||||
end
|
||||
|
||||
def fitness_function(chromosome) do
|
||||
target = "supercalifragilisticexpialidocious"
|
||||
guess = List.to_string(chromosome.genes)
|
||||
String.jaro_distance(target, guess)
|
||||
end
|
||||
|
||||
def terminate?([best | _]), do: best.fitness == 1
|
||||
|
||||
end
|
||||
|
||||
soln = Genetic.run(Speller)
|
||||
|
||||
IO.write("\n")
|
||||
IO.inspect(soln)
|
Reference in New Issue
Block a user