I'm working on implementing a genetic algorithm in Python currently and am doing so based off the book, "Computation Intelligence - An Introduction, Second Edition" by Andries P. Engelbrecht. My understanding is each generation you perform fitness calc, selection, crosover and mutation
.
My overall strategy currently is:
while not stopping_criteria():
next_population = []
# Step 1. Calculate Fitness
for each individual in population:
calculate_fitness()
# Step 2. Grab some number of top performers to copy over
top_performers = ellitism(population, 2) # Grab top 2 for instance
next_population += top_performers
# Step 3. Selection/Crossover/Mutation
# While the next_population has fewer individuals than the current population
while length(next_population) < length(population):
# Step 3.1 tournament selection of tournament size 4, top 2
parent1, parent2 = tournament_selection(population)
# Step 3.2 Crossover using SBX
child1, child2 = simulated_binary_crossover(parent1, parent2)
# Step 3.3 Mutation of children?
gaussian_mutation(child1, 0.05)
gaussian_mutation(child2, 0.05)
next_population += [child1, child2]
I believe I'm doing steps 1 - 3.1
correctly. My question is really:
Is the crossover correct? Is it a good approach to do tournament selection? I want to make sure that the overall population still has some diversity so I can avoid local optima. This is why I'm trying to only take the top two performers and copy them over (although, maybe that is also too many).
Regarding crossover, is it okay to just give each child a small probability to mutate each gene? Would it be better to give a variable mutation rate based on fitness of the current population?
I have tested my approach on numerous equations but find sometimes I still get stuck in local maxima. With f(x,y) = -| 2x^2 - 1.05x^4 + ((x^6)/6) + xy + y^2 |
(Three-hump camel function from https://en.wikipedia.org/w/index.php?title=Test_functions_for_optimization&oldid=787014841) I find (0, 0)
the majority of the time, but still get stuck near it on occasion. This is mainly why I wonder if my approach to crossover/mutation is off.