Genetic Algorithm Knapsack: A Solved Numerical Example

Scenario: Disaster Relief Supply Packing

The Objective: Simulate packing emergency supplies by evolving binary combinations to maximize the total survival benefit without exceeding the strict 10kg weight limit.

Core Mechanics
  • The Binary Blueprint: Every candidate solution is just a string of 11s and 00s. A 11 means the item goes in the bag; a 00 means it stays behind. The algorithm’s entire job is to breed the perfect string.
  • The Overweight Death Penalty: A solution's fitness score is the total value of its packed items. However, if the total weight exceeds the bag's capacity, the solution is illegal! It must instantly receive a score of 00 or a massive penalty.
  • Targeted Crossover: In textbook theory, parents are split randomly. On an exam, you are given the exact bit to slice at. Simply chop both parent strings at that specified point and swap their tails to create the two children.
  • Deterministic Mutation: Real mutation is a random wildcard. But here, you will be told exactly which bit to target. Find that specific position in your new child string and flip it (a 00 becomes a 11, or a 11 becomes a 00).

Step 1: The Knapsack Items & Evolution Rules

The Genetic Knapsack problem aims to maximize the total benefit of selected items without exceeding the strict weight limit. A 11 in the chromosome means the item is packed; a 00 means it is left behind.

Data PointItem NameBenefit (Score)Weight (Cost)
P1Medical Kit94
P2Water Supplies73
P3Food Rations85
P4Blankets42
P5Radio51
Knapsack Constraints & SetupMax Weight Limit: 10 kg
Initial Chromosomes (Starting Population):
P111011
P210101
P301110
P411100
Crossover Rules: Cross New #1 & New #2 after bit 2 Cross New #3 & New #4 after bit 2
Mutation Rules: Mutate Child C1 at bit 4

Step 2 & 3: Decode & Fitness Evaluation

We decode each binary string to see which items are inside the bag. If the bag exceeds 10kg, the constraints are violated and it receives a penalty (Fitness = 0).

Parent IDChromosomeItems InsideWeightBenefitConstraintFitness f(x)f(x)
P1110111, 2, 4, 51025Valid25
P2101011, 3, 51022Valid22
P3011102, 3, 41019Valid19
P4111001, 2, 31224Overweight0

Step 4: Selection Pool (Descending)

Parents are sorted by their fitness score. Notice how any invalid chromosomes with a fitness of 00 naturally sink to the bottom of the pool, meaning they will likely die off in this generation.

New RankChromosomeWeightBenefitFitness f(x)f(x)
New #1 (P1)11011102525
New #2 (P2)10101102222
New #3 (P3)01110101919
New #4 (P4)1110012240

Step 5: Genetic Operations (Crossover & Mutation)

Parents swap packed items based on the crossover rules. If a parent had a fitness of 0 (invalid), it is struck out and does not reproduce. Then, target children undergo mutation.

1. Apply Crossover

Child IDChromosome
C1
11101
C2
10011
C3
01100
C4
11110

2. Apply Mutation

Child IDChromosome
C1 (mutated)11111
C210011
C301100
C411110

Step 6: Final Generation Evaluation

We evaluate the fitness of the new children to see if they exceeded the weight limit. If the stopping criteria is not met, this new generation becomes the parent pool for the next iteration.

Child IDNew ChromosomeGenetics StatusWeightConstraintFinal Fitness f(x)f(x)
C111111Mutated15Overweight0
C210011Crossover7Valid18
C301100Crossover8Valid15
C411110Crossover14Overweight0

Final Takeaway

Notice how Parent P4 and Child C1 are instantly slapped with a fitness score of 0, despite packing highly valuable items! This perfectly demonstrates the most crucial exam concept for the Knapsack problem: constraint violations must be penalized heavily so overweight solutions die off and cannot pass on their invalid genes.