liveBook · Manning

Fitness

As explained in Grokking AI Algorithms, Second Edition

A fitness function evaluates candidate solutions and assigns numerical values representing their performance according to an optimization objective.

Fitness is a score for how well a candidate solution meets a goal. Think of judging a packed knapsack: its contents may be valuable, but the packing fails if the bag is too heavy. A fitness function turns that judgment into a number an optimization algorithm can use to compare candidates. Depending on the goal, a better score may be higher or lower. section 4.7 section 4.2

Scoring a knapsack

Suppose the bag holds at most 9 kg. Pearls weigh 3 kg and are worth $4; a crown weighs 4 kg and is worth $5; a ring weighs 2 kg and is worth $5. Packing all three weighs 9 kg and scores 14—the sum of their values. Gold and a crown weigh 11 kg, so that packing breaks the limit. One simple fitness rule gives an overweight bag a score of 0, however valuable its contents look. section 4.2 section 4.7

To score a population of candidate knapsacks:

  • Represent each packing as a chromosome: a string of genes, with 1 for an included item and 0 for an excluded item. section 4.4
  • For each chromosome, add the weights and values of its included items. Return 0 if the weight exceeds the limit; otherwise, return the total value. section 4.7
  • Use the scores to favor stronger candidates when selecting parents, while still allowing less-fit candidates a chance. Repeat scoring as new generations appear. section 4.8 section 4.3
def calculate_individual_fitness(individual, maximum_weight):
    total_individual_weight = 0
    total_individual_value = 0
    for gene_index in range(len(individual)): #A
        gene_switch = individual[gene_index]
        if gene_switch == '1': #B
            total_individual_weight
            ➥+= knapsack_items[gene_index][KNAPSACK_ITEM_WEIGHT_INDEX] #C
            total_individual_value
            ➥+= knapsack_items[gene_index][KNAPSACK_ITEM_VALUE_INDEX] #C
    if total_individual_weight > maximum_weight: #D
        return 0
    return total_individual_value #E

The Python function shows the rule directly: inspect each gene, accumulate weight and value for included items, then return either 0 or the total value. section 4.7

The rule must match the real objective. Maximizing packed value is different from minimizing unused space, and an inaccurate score steers selection toward the wrong solutions. A refinery problem needs a more involved rule: it must account for extraction time as well as item value to measure value per hour. Fitness measures solution quality, not how quickly the computer found the solution. section 4.7 section 5.12 section 4.2

Fitness also helps decide when to stop: an algorithm can stop at a desired score or when its best score barely changes across generations. Outside genetic algorithms, particle swarm optimization scores each particle after it moves and tracks its best result. In the drone-material example, lower scores are better because the search minimizes a function associated with drag and wobble. The practical question is always the same: does the number reward what you actually want? section 4.19 section 7.7 section 7.3

How Grokking AI Algorithms, Second Edition covers it

How deep it goes
Works through knapsack scoring and Python implementation, then extends the idea to particle scoring without deriving general fitness-design rules.
Examples
Uses packed-item values and weight limits, a refinery’s value per hour, and a drone-material scoring exercise.
What it assumes
Introduces binary chromosome encoding and illustrates scoring in Python without relying on advanced mathematical proofs.
What you can do after
Build it from scratch for a knapsack chromosome and evaluate whether its score respects the problem’s constraints.
How it connects
Links fitness to objective functions, heuristics, constraint penalties, parent selection, and particle swarm optimization.

Also explained in

Compare how 6 books explain fitness →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Grokking AI Algorithms, Second Edition. The section links point to the parts of the book it draws on.