I'm trying to write some little code (POC for the selection/mutation operators) that uses a genetic algorithm to solve a global maximum for a function.
f(x_1...x_n) = M - (x_1 - a_1)^2 - (x_2 - a_2)^2 - ... - (x_n - a_n)^2
M a_i are constants. I have to find x_i such that f(x_i) = max(f) = M
My selection method is truncation (I select the top 100 fittest of a population of 500). My crossover method is average. there is a 80% chance for crossover, other wise one of the parents is passed on. My elite count is 5 (1% of the population) There is a 3% chance for a mutation for an individual, the range of the mutation is [-0.3, 0.3]
My fitness function is f it self and my stopping condition is ABS(previous best fitness - current best fitness) <= 10^(-21)
You can find the code I wrote here.
The problem is that it converges before it reaches even an approximate solution.
What can I change in the solution approach so that the algorithm would converge on the maximum(f)? (This is not my algorithm, it's a reduction of a problem I have at work.)