Skip to content

Commit

Permalink
some utterly unimportant experiments
Browse files Browse the repository at this point in the history
  • Loading branch information
stefan-k committed Jun 20, 2012
1 parent 1447ab2 commit 71ab060
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 8 deletions.
16 changes: 10 additions & 6 deletions evo_test.jl
Expand Up @@ -4,18 +4,22 @@

load("evolib.jl")

# define the objective function (in-place!)
function rosenbrock(chr::Chromosome)
x1 = chr[1].gene
x2 = chr[2].gene
chr.fitness = 100*(x2-x1^2)^2+(1-x1)^2-50*((x1+1)^2+(x2-1)^2)-10*((x1-1.5)^2+(x2-2.5)^2)+exp(2*x2-5)
end
srand(123)

# in case someone wants to calculate the objective function for a population
function rosenbrock(pop::Population)
for i=1:length(pop)
rosenbrock(pop[i])
end
#pop = Population(pmap((x)->rosenbrock(x), pop.chromosomes))
end

# define the objective function (in-place!)
function rosenbrock(chr::Chromosome)
x1 = chr[1].gene
x2 = chr[2].gene
chr.fitness = 100*(x2-x1^2)^2+(1-x1)^2-50*((x1+1)^2+(x2-1)^2)-10*((x1-1.5)^2+(x2-2.5)^2)+exp(2*x2-5)
chr
end

# randomly initialize a population with 100 chromosomes
Expand Down
3 changes: 2 additions & 1 deletion evolib.jl
Expand Up @@ -269,7 +269,8 @@ rand(T::Type{Chromosome}, num::Int64, x...) = Chromosome([rand(Gene, x...) for i

function rand(T::Type{Chromosome}, num::Int64, obj_func::Function, x...)
chr = rand(T, num, x...)
chr.fitness = obj_func(chr)
#chr.fitness = obj_func(chr)
chr = obj_func(chr)
return chr
end

Expand Down
5 changes: 4 additions & 1 deletion genetic_test.jl
Expand Up @@ -9,6 +9,7 @@ function rosenbrock(chr::Chromosome)
x1 = chr[1].gene
x2 = chr[2].gene
chr.fitness = 100*(x2-x1^2)^2+(1-x1)^2-50*((x1+1)^2+(x2-1)^2)-10*((x1-1.5)^2+(x2-2.5)^2)+exp(2*x2-5)
return chr
end

# in case someone wants to calculate the objective function for a population
Expand All @@ -26,4 +27,6 @@ probs = GeneticProbabilities(0.2, 0.499, 0.3, 0.001)
popul = rand(Population, 100, 2, rosenbrock, 0.1, 6.0, -6.0 )

# launch genetic algorithm
@time genetic(popul, probs, 1000, rosenbrock)
@time best, conve = genetic(popul, probs, 1000, rosenbrock)
print(best)
println()

0 comments on commit 71ab060

Please sign in to comment.