Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Fixed GA deepcopy issues, added plotting
  • Loading branch information
Stephen committed Feb 10, 2018
1 parent 1bb94ac commit 53b7a9e
Show file tree
Hide file tree
Showing 13 changed files with 92 additions and 16 deletions.
Binary file added BestFitness.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
108 changes: 92 additions & 16 deletions GA.jl
@@ -1,4 +1,5 @@
using StatsBase using StatsBase
using PyPlot
#= #=
Parents <— {randomly generated population} Parents <— {randomly generated population}
While not (termination criterion) While not (termination criterion)
Expand Down Expand Up @@ -40,7 +41,7 @@ function initialize_population(populationSize::Int, chromosomeSize::Int, bounds:
row = [] row = []
# Create member elements based on bounds # Create member elements based on bounds
for bound in bounds for bound in bounds
push!(row, rand(bound[1]:0.10:bound[2])) push!(row, rand(bound[1]:0.01:bound[2]))
end end
# Add another element to the end to keep score # Add another element to the end to keep score
push!(row, 1.0) push!(row, 1.0)
Expand Down Expand Up @@ -122,7 +123,7 @@ function mutate(population::Array{Array{Float64}}, mutateRate::Float64, bounds::
for member in population for member in population
for i = 1:length(member)-1 for i = 1:length(member)-1
if rand() < mutateRate if rand() < mutateRate
member[i] = rand(bounds[i][1]:0.10:bounds[i][2]) member[i] = rand(bounds[i][1]:0.01:bounds[i][2])
end end
end end
end end
Expand Down Expand Up @@ -150,28 +151,103 @@ function GA(populationSize::Int, chromosomeSize::Int, fitness_function::Function
# Set recombination and mutation rate, lower and upper bound # Set recombination and mutation rate, lower and upper bound
recombRate = 0.7 recombRate = 0.7
mutateRate = 0.05 mutateRate = 0.05
maxIterations = 1000 maxIterations = 100
runs = 20
# First initialize the population # First initialize the population
population = initialize_population(populationSize, chromosomeSize, bounds)


# Then loop for the required iterations # Then loop for the required iterations
bestScores = Array{Float64}(0) # Initialize iteration counter, run counter, and array to hold generations of best population
i = 0 i = 0
while i < maxIterations run = 0
# Score and sort the population bestGenerations = [[[0.0,0.0,Inf]]]
population = score_sort_population(population, fitness_function) generations = Array{Array{Array{Float64}}}(0)
push!(bestScores,population[1][end])
population = create_next_generation(population, recombRate) # For each run
population = mutate(population, mutateRate, bounds) while run < runs
i += 1 nextPopulation = initialize_population(populationSize, chromosomeSize, bounds)
# Iterate through max amount of generations
while i < maxIterations
# Score and sort the population
population = score_sort_population(nextPopulation, fitness_function)
# Push current population to generations array
push!(generations, deepcopy(population))
# Create children
nextPopulation = create_next_generation(population, recombRate)
# Mutate children
nextPopulation = mutate(nextPopulation, mutateRate, bounds)
i += 1
end
#=
At the end of the run, if the last generations best solution
# is better than that of the best of all runs so far
set new best run so we can graph later
=#
if generations[end][1][end] < bestGenerations[end][1][end]
bestGenerations = deepcopy(generations)
end
clear!(:generations)
clear!(:population)
clear!(:children)
run += 1
end

# Reporting and plotting
# Report best solution and value
bestVariables = bestGenerations[end][1][1:end-1]
bestScore = bestGenerations[end][1][end]
println("Best solution\nVariables: $bestVariables\nScore: $bestScore")

# Generate best fitness vs. # generation
# Init score array
y = Array{Float64}(0)
# Get scores, push to array
for g in bestGenerations
push!(y,g[1][end])
end
# Plot and label
PyPlot.plot(y)
xlabel("Generation #")
ylabel("Fitness")
# Save and close
PyPlot.savefig("BestFitness.png")
PyPlot.close()

# Contour
# Check to make sure we only have two variables, otherwise exit
if length(bestGenerations[1][1]) > 3
println("Plotting the 4th dimension currently disabled to avoid tearing the space-time continuum")
quit()
end end
println(bestScores)
println(population) # Init x and y z for contour plots
x = y = z = Array{Float64}(0)
for (index, gen) in enumerate(bestGenerations)
if (index == 1) || (mod(index,10) == 0)
for m in gen
push!(x,m[1])
push!(y,m[2])
end
size = length(x)
z = rand(size,size)
for i in 1:size
for j in 1:size
z[i,j] = fitness_function([x[i],y[j]])
end
end
contour(x,y,z)
savefig("contour_gen$index.png")
close()
clear!(:x)
clear!(:y)
clear!(:z)
end
end

end end


function ackley(X) function fitness_function(X)
return e - 20*exp(-0.2*sqrt((X[1]^2 + X[2]^2)/2)) - exp((cos(2*pi*X[1]) + cos(2*pi*X[2]))/2) return e - 20*exp(-0.2*sqrt((X[1]^2 + X[2]^2)/2)) - exp((cos(2*pi*X[1]) + cos(2*pi*X[2]))/2)
end end


bounds = [(-2.0,2.0), (-2.0,2.0)] bounds = [(-2.0,2.0), (-2.0,2.0)]
GA(25,2,ackley,bounds) GA(25,2,fitness_function,bounds)
Binary file added contour_gen1.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added contour_gen10.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added contour_gen100.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added contour_gen20.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added contour_gen30.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added contour_gen40.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added contour_gen50.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added contour_gen60.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added contour_gen70.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added contour_gen80.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added contour_gen90.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 53b7a9e

Please sign in to comment.