Skip to content

Latest commit

 

History

History
200 lines (117 loc) · 9.02 KB

File metadata and controls

200 lines (117 loc) · 9.02 KB

Mathematical Foundations & Complexity Analysis

Evolutionary Knapsack Optimizer — Theoretical Deep Dive

This document provides a rigorous treatment of the theoretical underpinnings behind the Genetic Algorithm (GA) approach to the 0/1 Knapsack Problem implemented in this repository.


1. The 0/1 Knapsack Problem: Formal Definition

1.1 Decision Problem

The 0/1 Knapsack Problem belongs to the class of NP-complete problems, as established by Richard Karp in his seminal 1972 paper "Reducibility Among Combinatorial Problems."

Instance: A finite set $U$ of items, a size function $s: U \rightarrow \mathbb{Z}^+$, a value function $v: U \rightarrow \mathbb{Z}^+$, and positive integers $B$ (budget/capacity) and $K$ (target value).

Question: Is there a subset $U' \subseteq U$ such that:

$$\sum_{u \in U'} s(u) \leq B \quad \text{and} \quad \sum_{u \in U'} v(u) \geq K$$

1.2 Optimization Variant

The optimization variant — used in this implementation — seeks the subset that maximizes total value subject to the capacity constraint:

$$\max_{x \in {0,1}^n} \sum_{i=1}^{n} v_i x_i \quad \text{s.t.} \quad \sum_{i=1}^{n} w_i x_i \leq W$$

1.3 Complexity Landscape

Aspect Complexity
Brute force enumeration $O(2^n)$
Dynamic programming $O(nW)$ — pseudo-polynomial
Fully polynomial-time approximation scheme (FPTAS) $O(n^2 / \epsilon)$
This GA implementation $O(g \cdot p \cdot n)$ per run

The DP approach is pseudo-polynomial because $W$ is encoded in $\log W$ bits, making the true complexity exponential in input size. This is precisely why metaheuristic approaches remain relevant for large instances.


2. Genetic Algorithm Theory

2.1 The Schema Theorem (Holland, 1975)

The theoretical justification for GAs rests on John Holland's Schema Theorem, which provides a lower bound on the expected number of instances of a schema in the next generation:

$$E[m(H, t+1)] \geq m(H, t) \cdot \frac{f(H)}{{\bar{f}}} \cdot \left[1 - p_c \cdot \frac{\delta(H)}{l-1}\right] \cdot (1 - p_m)^{o(H)}$$

Where:

  • $m(H, t)$ — number of instances of schema $H$ at generation $t$
  • $f(H)$ — average fitness of strings matching schema $H$
  • $\bar{f}$ — average fitness of the entire population
  • $p_c$ — crossover probability
  • $\delta(H)$ — defining length of the schema
  • $l$ — chromosome length
  • $p_m$ — mutation probability per bit
  • $o(H)$ — order of the schema (number of fixed positions)

Implication: Short, low-order, above-average schemata receive exponentially increasing trials in subsequent generations. This is the Building Block Hypothesis — GAs work by assembling short, high-fitness building blocks into complete solutions.

2.2 Convergence Under Elitism

Rudolph (1994) proved that a GA with elitist selection converges to the global optimum with probability 1, given:

  1. The mutation operator can reach any point in the search space from any other point (ergodicity)
  2. The best solution found so far is always preserved (elitism)

This implementation satisfies both conditions:

  • Bit-flip mutation can transform any binary string into any other
  • $(\mu + \lambda)$ selection ensures the best individual always survives

Therefore, this algorithm provably converges to the optimal knapsack packing given sufficient generations.


3. Operator Analysis

3.1 Single-Point Crossover

The crossover operator in this implementation uses a fixed midpoint cut:

$$\text{child} = [p_1[0], \ldots, p_1[\lfloor n/2 \rfloor - 1], \ p_2[\lfloor n/2 \rfloor], \ldots, p_2[n-1]]$$

Positional bias: Fixed-point crossover introduces positional bias — genes near the crossover point have higher linkage disruption. For the knapsack problem (where gene order is arbitrary), this is acceptable. For problems with epistatic linkage, uniform crossover would be preferred.

Exploration capacity: Each crossover produces 2 offspring (symmetric cuts), doubling the population before survivor selection. This creates a $(\mu + 2\mu)$ strategy, producing $3\mu$ candidates from which the top $\mu$ survive.

3.2 Adaptive Mutation

The mutation operator is triggered by three conditions:

  1. Duplicate detection — if the offspring already exists in the population
  2. Infeasibility — if the offspring violates the weight constraint
  3. Stochastic trigger — with probability $p_m / 100$

This creates an adaptive mutation pressure that increases when the population is losing diversity (many duplicates) or when crossover produces infeasible solutions. This is more sophisticated than standard fixed-rate mutation.

3.3 Constraint Handling

Infeasible solutions are handled through a repair + penalize hybrid:

  • Repair: Mutation iteratively flips bits until a feasible solution is found
  • Penalize: The fitness function's capacity-proximity term naturally penalizes solutions far from the capacity limit

This avoids the need for explicit penalty functions with tunable coefficients — a common source of brittleness in constrained evolutionary optimization.


4. Fitness Landscape Analysis

4.1 Custom Fitness Function

The absolute worth function:

$$f(x) = \frac{V \cdot W_{used} - V \cdot |W_{max} - W_{used}|}{W_{used}}$$

Can be simplified to:

$$f(x) = V \cdot \left(1 - \frac{|W_{max} - W_{used}|}{W_{used}}\right) = V \cdot \frac{W_{used} - |W_{max} - W_{used}|}{W_{used}}$$

Case 1: $W_{used} \leq W_{max}$ (feasible):

$$f(x) = V \cdot \frac{W_{used} - W_{max} + W_{used}}{W_{used}} = V \cdot \frac{2W_{used} - W_{max}}{W_{used}} = V \cdot \left(2 - \frac{W_{max}}{W_{used}}\right)$$

This shows that fitness increases as $W_{used} \rightarrow W_{max}$, reaching maximum $f = V$ when the knapsack is exactly full.

Case 2: $W_{used} > W_{max}$ (infeasible):

$$f(x) = V \cdot \frac{2W_{used} - 2W_{max} + W_{max} - W_{used}}{W_{used}} = V \cdot \frac{W_{used} - W_{max}}{W_{used} + W_{max} - W_{used}}$$

These solutions are filtered out by the constraint check, so this case doesn't affect selection.

4.2 Selection Pressure Dynamics

The ratio $f(H) / \bar{f}$ determines selection intensity. As the population converges:

  • Early generations: high variance in fitness → strong selection pressure → rapid improvement
  • Late generations: low variance → weak selection pressure → fine-tuning

This natural annealing of selection pressure is visible in the convergence plots: steep improvement in generations 0–20, gradual refinement in generations 20–100.


5. Connections to Other Metaheuristics

5.1 Simulated Annealing (SA)

SA explores via random perturbation with a temperature-controlled acceptance function. The GA's mutation operator serves a similar role, but operates on a population rather than a single solution. This implicit parallelism gives GAs better exploration of multimodal landscapes.

5.2 Particle Swarm Optimization (PSO)

PSO uses velocity vectors and social/cognitive components for search. GAs achieve similar behavior through crossover (social information sharing) and mutation (individual exploration). For binary problems like knapsack, GA's binary encoding is more natural than PSO's continuous velocity model.

5.3 Ant Colony Optimization (ACO)

ACO constructs solutions incrementally using pheromone trails. GAs operate on complete solutions, making them better suited for problems where partial solutions don't have meaningful fitness values.


6. Empirical Complexity of This Implementation

6.1 Per-Generation Cost

Operation Complexity
Fitness evaluation $O(n)$ per chromosome
Crossover $O(n)$ per pair
Mutation $O(n \cdot k)$ where $k$ = retries
Duplicate check $O(p \cdot n)$ per chromosome
Sorting $O(p \log p)$
Total per generation $O(p^2 \cdot n + p \log p)$

6.2 Total Runtime

$$T_{total} = O(g \cdot (p^2 \cdot n + p \log p))$$

For the default configuration ($g=100, p=10, n=20$):

$$T \approx 100 \cdot (100 \cdot 20 + 10 \cdot \log 10) \approx 200{,}000 \ \text{operations}$$

Compare to brute force: $2^{20} = 1{,}048{,}576$ — a 5× reduction with near-optimal solution quality.


7. References

  1. Karp, R. M. (1972). Reducibility Among Combinatorial Problems. In: Complexity of Computer Computations, pp. 85–103.
  2. Holland, J. H. (1975). Adaptation in Natural and Artificial Systems. University of Michigan Press.
  3. Goldberg, D. E. (1989). Genetic Algorithms in Search, Optimization, and Machine Learning. Addison-Wesley.
  4. Rudolph, G. (1994). Convergence Analysis of Canonical Genetic Algorithms. IEEE Transactions on Neural Networks, 5(1), 96–101.
  5. Michalewicz, Z. (1996). Genetic Algorithms + Data Structures = Evolution Programs. Springer-Verlag.
  6. Martello, S. & Toth, P. (1990). Knapsack Problems: Algorithms and Computer Implementations. John Wiley & Sons.
  7. Eiben, A. E. & Smith, J. E. (2015). Introduction to Evolutionary Computing. Springer, 2nd Edition.

"The measure of intelligence is the ability to change." — Charles Darwin