Genetic Algorithm (Knapsack) Theory Guide

Try the Genetic Algorithm (Knapsack) Solver →
Intermediate12 min read
4.8/5
322 students studied this today

Genetic Algorithm, Knapsack Problem, Constrained Optimization, Penalty Function, Fitness, Chromosomes

The Genetic Knapsack problem takes the basic Genetic Algorithm and applies it to a real-world scenario where you have to make the best possible choices under a strict limit. A military logistics officer packing a supply helicopter faces the same fundamental problem — maximum mission value, strict weight ceiling, and far too many possible combinations to evaluate exhaustively. The Genetic Algorithm encodes each candidate packing decision as a binary chromosome where each bit represents whether an item is included or excluded, then evolves the population through selection, crossover, and mutation while enforcing the weight constraint as a fitness penalty — producing near-optimal solutions to a problem that belongs to the computationally intractable class of NP-hard combinatorics.

The Fitness & Penalty Function

Fitness={Biif WiWmax0if Wi>Wmax\text{Fitness} = \begin{cases} \sum B_i & \text{if } \sum W_i \le W_{max} \\[0.5em] 0 & \text{if } \sum W_i > W_{max} \end{cases}

What do these variables mean?

  • Bi\sum B_iTotal Benefit. The sum of the benefits of all the items you actually packed (where the bit is 1).
  • Wi\sum W_iTotal Weight. The sum of the weights of all the items you packed.
  • WmaxW_{max}The strict weight limit of your bag.
  • The FormulaThis is a 'Piecewise' function. It checks a condition first: If your bag is under the weight limit, your score is your Total Benefit. But if your bag goes even 1 gram over the limit, your score instantly drops to 0 (a Penalty).
  • The Penalty RuleWe give overweight bags a 0 instead of deleting them so our population size stays perfectly consistent for our code loops!

How Does it Work?

1

Define the Constraint: Establish your bag's Maximum Weight and list out your items with their specific (Benefit, Weight) values.

2

Decode the Chromosome: Look at your binary string. A '1' means the item goes in the bag; a '0' means it stays out.

3

Evaluate Weight & Benefit: Add up the weights and benefits of the packed items.

4

The Penalty Check: If Total Weight > Max Weight, the fitness score is 0. Otherwise, the fitness score is the Total Benefit.

5

Selection: Sort the population by fitness score from highest to lowest. Overweight bags (score 0) sink to the bottom and won't be chosen as parents.

6

Crossover & Mutation: Breed the top-scoring bags by swapping tails and randomly flipping bits to discover even better packing combinations.

7

Repeat: Keep evaluating new generations, applying the penalty to any overweight children, until you find the ultimate packing list.

Solved Example: The Weekend Camping Trip

You have a backpack with a Max Weight of 10kg. Item 1: Tent (Benefit 10, Weight 7kg). Item 2: Sleeping Bag (Benefit 8, Weight 4kg). Item 3: Flashlight (Benefit 5, Weight 1kg). Your current chromosome is '101'.

Step 1:

Step 1: Decode the chromosome '101'. You packed the Tent and the Flashlight.

Step 2:

Step 2: Calculate Total Weight: 7kg (Tent) + 1kg (Flashlight) = 8kg.

Step 3:

Step 3: Perform Penalty Check: Is 8kg <= 10kg? Yes. The bag is valid.

Step 4:

Step 4: Calculate Fitness: Benefit 10 + Benefit 5 = 15. Your fitness score is 15.

Step 5:

Scenario 2: If the chromosome was '110' (Tent + Sleeping Bag), the weight would be 7+4 = 11kg. Because 11 > 10, the Fitness would be 0.

Student Tip: You can verify these exact manual calculations using our interactive Genetic Algorithm (Knapsack) step-by-step solver. Simply plug in the values from the table above to see the logic in action.

Implementation Pseudocode

function runKnapsackGA(items, maxWeight, population):
    // 1. Evaluate Population
    evaluatedPop = []
    for each chromosome in population:
        totalWeight = 0
        totalBenefit = 0
        
        for i = 0 to length(chromosome) - 1:
            if chromosome[i] == '1':
                totalWeight = totalWeight + items[i].weight
                totalBenefit = totalBenefit + items[i].benefit
                
        // The Penalty Check
        if totalWeight <= maxWeight:
            fitness = totalBenefit
        else:
            fitness = 0
            
        evaluatedPop.push({ chromosome, fitness })
        
    // 2. Selection (Sort Descending)
    sortedPop = sort(evaluatedPop) by fitness descending
    nextGen = getChromosomes(sortedPop)
    
    // 3. Apply Crossovers & Mutations
    applyCrossoverRules(nextGen)
    applyMutationRules(nextGen)
    
    // 4. Evaluate the New Generation
    return runKnapsackGA(items, maxWeight, nextGen) // Recursive Loop

Rules & Common Mistakes

⚠️

Exam Trap: Always calculate the Weight *first*. If a chromosome is overweight, don't even waste your time calculating the Benefit! Just write 'Fitness = 0' and move to the next one to save massive amounts of time during manual exams.

💡

A chromosome of all 0s (0000) is technically valid (Weight = 0), but its Fitness is 0. A chromosome of all 1s (1111) is almost always overweight, so its Fitness is also 0. The optimal answer is always a careful mix of 1s and 0s.

💡

Make a table with columns for 'Binary String', 'Total Weight', 'Total Benefit', 'Valid?', and 'Final Fitness'. It prevents silly addition mistakes when evaluating large populations.

Advantages

  • Perfect for solving NP-Hard problems. Checking every possible combination of 50 items would take computers years; a GA finds a great answer in seconds.
  • Beautifully handles strict, unbending real-world constraints simply by adding a math penalty.
  • Highly adaptable. You can easily change the constraints (e.g., adding a 'Max Volume' limit alongside the 'Max Weight' limit).

Disadvantages

  • × Can suffer from 'Mass Extinction'. If you have a tiny bag and heavy items, randomly generating starting chromosomes might result in a population where *everyone* is overweight (Fitness = 0), giving the algorithm no 'good parents' to start with.
  • × Just like One-Max, it is an estimation algorithm. It guarantees a 'really good' answer, but not always the mathematically perfect answer.

Algorithm Complexity

ScenarioTime ComplexitySpace ComplexityNotes
Evaluation Time (Per Pop)O(P×I)O(P \times I)O(1)O(1)Where PP is Population Size and II is the number of Items (chromosome length). Must read every bit to sum weights and benefits.
Sorting TimeO(PlogP)O(P \log P)O(1)O(1)Sorting the evaluated bags from highest to lowest fitness score.
Overall Space-O(P×I+M)O(P \times I + M)Where MM is the memory required to store the static item list (weights/benefits) alongside the population arrays.

Genetic Knapsack vs. One-Max Genetic Algorithm

The Knapsack problem is the One-Max problem with the lights turned off and obstacles added. The evolutionary machinery is identical — you still Sort, Crossover, and Mutate — but the evaluation of each chromosome is completely different because you must now decode it against a list of real items and enforce a hard weight limit.

  • One-Max fitness = count the 1s (one arithmetic step); Knapsack fitness requires three sequential steps: decode the chromosome against the item list, sum the weights to check the constraint, and only if valid, sum the benefits — failure at the constraint check produces a 0, short-circuiting all further calculation.
  • In One-Max, every chromosome has a fitness score greater than or equal to zero (all-zeros string has fitness 0, but is never penalized); in Knapsack, an entire generation can theoretically consist of all-zeros chromosomes if the population is initialized randomly with very heavy items and a tight weight limit — 'Mass Extinction'.
  • One-Max has a single, globally optimal answer ('1111...1') that is always achievable; Knapsack may have multiple locally optimal packing lists, and the GA is only guaranteed to find a very good answer — not necessarily the mathematically perfect one.

Summary

The Genetic Knapsack problem is where evolutionary computing proves its real-world value. The constraint — a hard weight limit backed by a zero-fitness penalty — forces the population to evolve toward solutions that are both valuable and physically feasible, mirroring thousands of real logistics, scheduling, and resource allocation problems. The core lesson is that the GA framework itself doesn't change; only the fitness function changes. Swap the fitness function, and the same Selection-Crossover-Mutation loop can solve an entirely different class of problem.

Common Exam Questions & FAQ

+ Why assign a fitness of 0 to overweight chromosomes instead of deleting them?

Deleting chromosomes mid-generation would shrink the population size, breaking the fixed-size array that the Selection, Crossover, and Mutation loops depend on. Assigning a 0 fitness achieves the same biological result — overweight bags sink to the bottom of the ranking and are never chosen as parents — while keeping the data structure perfectly stable throughout every iteration.

+ What is the difference between 0/1 Knapsack and Fractional Knapsack?

In 0/1 Knapsack (which this GA solves), each item is binary — you either pack the whole item or leave it entirely. In Fractional Knapsack, you can take a partial amount of any item (e.g., 0.7kg of rice). Fractional Knapsack can be solved optimally and instantly using a greedy algorithm (sort by benefit-to-weight ratio, take as much as possible of each). 0/1 Knapsack is NP-Hard, which is exactly why we use Genetic Algorithms for it.

+ How does 'Mass Extinction' happen and how can it be avoided?

Mass Extinction occurs when the initial randomly generated population happens to consist entirely of overweight chromosomes — every individual receives a fitness of 0, giving the algorithm no 'good parents' to breed from. The fix is smarter initialization: generate starting chromosomes using a repair heuristic (randomly add items one by one until the next item would exceed the limit, then stop), ensuring at least some valid solutions exist from the very first generation.

🎓 Core University Curriculum

This algorithm and its manual calculation methods are foundational requirements in leading Computer Science and Software Engineering programs worldwide. You will find this topic heavily featured in the syllabi of these standard AI courses:

Explore Related Algorithms