# Genetic algorithm fitness function [closed]

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.)

## closed as unclear what you're asking by Raphael♦Feb 18 '14 at 21:38

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

• What is the question? Note that there is Code Review for checking your code; what we can do is help you with the algorithmics. So what are you trying to accomplish? Have you tried many runs and different parameters? Any GA may get stuck in local optima. – Raphael Feb 18 '14 at 21:38