5.0 Step 3: Evaluating Solutions – The Fitness Function
The fitness function is the mechanism that quantifies the quality of a candidate solution. It serves as the guide for natural selection, taking a solution as input and producing a numerical score—its “fitness”—as output. Because this function is called repeatedly for every individual in every generation, its computational speed is critical. A slow fitness function can render a GA impractical, regardless of how well other components are designed.
An effective fitness function must have the following characteristics:
- It must be fast to compute.
- It must quantitatively measure how fit a given solution is in the context of the problem’s objective.
The fitness function is often the same as the problem’s objective function (e.g., if the goal is to minimize cost, the fitness function could be the calculated cost). However, in more complex problems with multiple objectives or constraints, the fitness function may be designed differently to incorporate penalties for constraint violations or balance competing goals.
For example, in the 0/1 Knapsack problem, a simple fitness function would be to sum the profit values of all items picked (represented by a 1 in the chromosome), ensuring the total weight does not exceed the knapsack’s capacity.
Once each individual’s fitness has been calculated, this value is used to drive the selection of parents for creating the next generation of solutions.