# stl08007 / JuliaGA

Fixed GA deepcopy issues, added plotting

Stephen committed Feb 10, 2018
1 parent 1bb94ac commit 53b7a9e94668dbbe21497463230493ac5dd01c5f
BIN +15.6 KB BestFitness.png
Binary file not shown.
108 GA.jl
 @@ -1,4 +1,5 @@ using StatsBase using PyPlot #= Parents <— {randomly generated population} While not (termination criterion) @@ -40,7 +41,7 @@ function initialize_population(populationSize::Int, chromosomeSize::Int, bounds: row = [] # Create member elements based on bounds for bound in bounds push!(row, rand(bound[1]:0.10:bound[2])) push!(row, rand(bound[1]:0.01:bound[2])) end # Add another element to the end to keep score push!(row, 1.0) @@ -122,7 +123,7 @@ function mutate(population::Array{Array{Float64}}, mutateRate::Float64, bounds:: for member in population for i = 1:length(member)-1 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 @@ -150,28 +151,103 @@ function GA(populationSize::Int, chromosomeSize::Int, fitness_function::Function # Set recombination and mutation rate, lower and upper bound recombRate = 0.7 mutateRate = 0.05 maxIterations = 1000 maxIterations = 100 runs = 20 # First initialize the population population = initialize_population(populationSize, chromosomeSize, bounds) # 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 while i < maxIterations # Score and sort the population population = score_sort_population(population, fitness_function) push!(bestScores,population[1][end]) population = create_next_generation(population, recombRate) population = mutate(population, mutateRate, bounds) i += 1 run = 0 bestGenerations = [[[0.0,0.0,Inf]]] generations = Array{Array{Array{Float64}}}(0) # For each run while run < runs 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 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 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) end bounds = [(-2.0,2.0), (-2.0,2.0)] GA(25,2,ackley,bounds) GA(25,2,fitness_function,bounds)
BIN +255 KB contour_gen1.png
Binary file not shown.
BIN +168 KB contour_gen10.png
Binary file not shown.
BIN +80 KB contour_gen100.png
Binary file not shown.
BIN +141 KB contour_gen20.png
Binary file not shown.
BIN +129 KB contour_gen30.png
Binary file not shown.
BIN +125 KB contour_gen40.png
Binary file not shown.
BIN +122 KB contour_gen50.png
Binary file not shown.
BIN +109 KB contour_gen60.png
Binary file not shown.
BIN +102 KB contour_gen70.png
Binary file not shown.
BIN +101 KB contour_gen80.png
Binary file not shown.
BIN +84.6 KB contour_gen90.png
Binary file not shown.