Writing the Fitness Function

<< Click to Display Table of Contents >>

Navigation:  Chapter 6 - Programming Your Genetic Algorithms Using the DLL >

Writing the Fitness Function

Previous pageReturn to chapter overviewNext page

The formula that is used to calculate the fitness on the basis of chromosome values is called the fitness function.  When using the GeneHunter DLL, you must write this function as a formula in Visual Basic because this function is unique to a specific problem.  The function will take as inputs the variables of the problem (the chromosomes) and the function result will be the fitness computed with those variables.  For example, suppose we wanted to implement the Tin Can example of Chapter 2 .  The VB fitness function might be coded like this:

 

Function Can_Area!(height!, radius!)

Can_Area = 2 * (3.14159 * radius ^ 2) + 2 * 3.14159 * radius * height

End Function

 

Note: GeneHunter always maximizes the fitness function.  To minimize the fitness function, just maximize the negative of the fitness function.  In other words, take the negative of the fitness function before passing it back to the DLL.