How do I define a fitness function?

Cheeseman picture Cheeseman · Sep 7, 2011 · Viewed 16.9k times · Source

I'm working on a project which will have a selected set of data and each data will have different attributes. I will need to use a fitness function to choose the data that best matches my selected scenario using the attributes.

However, I don't really find any sites explaining how to define my own fitness function. All I've got is that it's part of genetic algorithm, and this is as far as I got. So, can I be given some pointers here?

Answer

andrew cooke picture andrew cooke · Sep 7, 2011

This is the hard part of GAs (well, that and data representation) and really you can only learn by experience.

Stating the obvious, the function has to be something that measures how good the results are. In particular, it has to be smooth across a wide range of data - whatever the data, your fitness function has to show the right way to improve.

So, for example, a fitness function that is zero unless the answer is right is no good, because it doesn't help you get close to the right answer when you are starting.

And a fitness function that increases as things get better, but doesn't identify the very best solution is not so good either, because your population will improve to a certain point and then get stuck.

So you need to sit down, write out some examples of your data, and then think about what kind of function you can use. You want something that gives low values for bad data and high values for good data. And that adjusts nicely between the two.

Try any crazy idea you can think of at first, and then see how you might put that into a nice mathematical form. Just brainstorm and keep trying and iterating... you will probably find that your first choice isn't so good, and once you run the GA you'll be able to look at what is happening in more detail and improve it.