Skip to content

Commit

Permalink
datastructure for probabilites, roulette on probabilities, removed so…
Browse files Browse the repository at this point in the history
…rt from roulette
  • Loading branch information
stefan-k committed Apr 2, 2012
1 parent a39f14d commit 6ee1076
Showing 1 changed file with 40 additions and 10 deletions.
50 changes: 40 additions & 10 deletions evolib.jl
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -35,10 +35,11 @@


# I have no idea what I'm doing # I have no idea what I'm doing
abstract AbstractEvolutionary abstract AbstractEvolutionary
abstract AbstractGene <: AbstractEvolutionary abstract AbstractGene <: AbstractEvolutionary
abstract AbstractChromosome <: AbstractEvolutionary abstract AbstractChromosome <: AbstractEvolutionary
abstract AbstractPopulation <: AbstractEvolutionary abstract AbstractPopulation <: AbstractEvolutionary
abstract AbstractGenerations <: AbstractEvolutionary abstract AbstractGenerations <: AbstractEvolutionary
abstract AbstractGeneticProbabilities <: AbstractEvolutionary


################################################################################ ################################################################################
## GENE TYPE ## ## GENE TYPE ##
Expand Down Expand Up @@ -333,12 +334,36 @@ function push(generations::Generations, population::Population)
generations.generations += 1 generations.generations += 1
end end


################################################################################
## DATASTRUCTURE FOR GENETIC ALGORITHM PROBABILITIES ##
################################################################################

type GeneticProbabilities <: AbstractGeneticProbabilities
mutation::Float64
recombination::Float64
reproduction::Float64
immigration::Float64

function GeneticProbabilities(mutation::Float64,
recombination::Float64,
reproduction::Float64,
immigration::Float64)
sum = mutation + recombination + reproduction + immigration
new(mutation/sum, recombination/sum, reproduction/sum, immigration/sum)
end
end

get_vector(gp::GeneticProbabilities) = [gp.mutation, gp.recombination, gp.reproduction, gp.immigration]


################################################################################ ################################################################################
## FUNCTIONS ## ## FUNCTIONS ##
################################################################################ ################################################################################


# Roulette Wheel Selection on Population
function roulette(pop::Population) function roulette(pop::Population)
sort!(pop) #sort!(pop) # I don't think sorting is necessary and might even lead to
# problems... gotta check that
f_sum = inv_fitness_sum(pop) f_sum = inv_fitness_sum(pop)
idx = rand()*f_sum idx = rand()*f_sum
x = 0 x = 0
Expand All @@ -353,8 +378,14 @@ function roulette(pop::Population)
error("weird error that should not happen. You probably didn't define a fitness.") error("weird error that should not happen. You probably didn't define a fitness.")
end end


# return several indices determined by roulette
roulette(pop::Population, num::Int64) = [ roulette(pop) | i = 1:num ]

# Roulette Wheel Selection on general Vectors (in case probabilies are passed)
function roulette(p::Vector{Float64}) function roulette(p::Vector{Float64})
prop = sortr(p) # do not sort in-place! # Am I thinking wrong? Is sorting even necessary?
#prop = sortr(p) # do not sort in-place!
prop = copy(p)
prop_sum = sum(prop) # In case it doesn't sum up to 1 prop_sum = sum(prop) # In case it doesn't sum up to 1
idx = rand()*prop_sum idx = rand()*prop_sum
x = 0 x = 0
Expand All @@ -369,9 +400,7 @@ function roulette(p::Vector{Float64})
error("dafuq?") error("dafuq?")
end end



roulette(gp::GeneticProbabilities) = roulette(get_vector(gp))
# return several indices determined by roulette
roulette(pop::Population, num::Int64) = [ roulette(pop) | i = 1:num ]


# make sure the gene doesn't exceed it's limits # make sure the gene doesn't exceed it's limits
function assess_limits(g::Gene) function assess_limits(g::Gene)
Expand Down Expand Up @@ -445,5 +474,6 @@ crossover(chr1::Chromosome, chr2::Chromosome) = crossover(chr1, chr2, 2)
## GENETIC ALGORITHM ## ## GENETIC ALGORITHM ##
################################################################################ ################################################################################



function genetic(pop::Population, probabilities::GeneticProbabilities)
end


0 comments on commit 6ee1076

Please sign in to comment.