Sub topic 16
Nature-Inspired Algorithms
Assume you know basic Python, probability, and the idea of optimization: searching for the best solution under stated constraints. Nature-inspired algorithms are computational methods that borrow patterns from living systems. They are usually heuristics, meaning they search efficiently for good solutions without guaranteeing the mathematical optimum.
The working model
A designer first represents a candidate solution in a form the algorithm can change. The algorithm then evaluates candidates with a fitness function, which assigns a score. It repeatedly creates new candidates, favors higher-scoring ones, and introduces variation.
| Biological pattern | Algorithmic idea | Typical use |
|---|---|---|
| Natural selection | Genetic algorithm | Scheduling, layout, parameter tuning |
| Ant pheromone trails | Ant colony optimization | Routing and network paths |
| Flocking or schooling | Particle swarm optimization | Continuous numerical search |
These methods are useful when the search space is large, irregular, or difficult to describe with a simple formula. They can also combine several objectives, such as low cost and short delivery time.
A small genetic algorithm
The following example evolves bit strings toward a target containing only 1 values. Each bit string is a candidate. Its fitness is the number of correct bits.
import random
TARGET_LENGTH = 20
POPULATION_SIZE = 30
GENERATIONS = 40
MUTATION_RATE = 0.02
def fitness(candidate):
return sum(candidate)
def make_candidate():
return [random.randint(0, 1) for _ in range(TARGET_LENGTH)]
def crossover(first, second):
point = random.randint(1, TARGET_LENGTH - 1)
return first[:point] + second[point:]
def mutate(candidate):
return [
1 - bit if random.random() < MUTATION_RATE else bit
for bit in candidate
]
population = [make_candidate() for _ in range(POPULATION_SIZE)]
for generation in range(GENERATIONS):
population.sort(key=fitness, reverse=True)
survivors = population[:POPULATION_SIZE // 2]
next_population = survivors[:]
while len(next_population) < POPULATION_SIZE:
parents = random.sample(survivors, 2)
child = mutate(crossover(*parents))
next_population.append(child)
population = next_population
best = max(population, key=fitness)
print("Best fitness:", fitness(best), "/", TARGET_LENGTH)
print("Best candidate:", "".join(map(str, best)))Selection supplies pressure toward better solutions. Crossover combines parts of two candidates. Mutation makes small random changes. The balance matters: too little mutation can cause premature convergence, where the population becomes similar before finding a good answer. Too much mutation turns the search into random guessing.
Common errors
- Choosing a biological metaphor before defining the engineering objective.
- Using a fitness function that rewards an unwanted side effect.
- Treating one successful run as proof of reliability.
- Ignoring constraints, such as invalid routes or unsafe designs.
- Assuming more generations always produce a better result.
Nature supplies useful search strategies, not ready-made specifications. The central design task is translating the biological pattern into a measurable objective and a valid representation.
Exercise
Why should mutation usually be neither zero nor extremely high in a genetic algorithm?
Show answer
Zero mutation can trap the population in a poor local solution, while very high mutation destroys useful structure and makes the search nearly random.