Note

Starting genetic algorithms slideshow, slide 11

Genetic algorithms

Evolution

Evolution = reproduction with inheritance + variability + selection

Main branches of EC

  • genetic algorithms (GA)
  • genetic programming (GP)
  • evolution strategies (ES)
    • evolving evolution
  • evolutionary programming (EP)
    • simulation of adaptive behaviour in evolution
    • emphasizes the development of behavioural models and not genetic models
  • evolutionary computation
    • reproduction
    • random variation
    • competition
    • selection

Swarm Intelligence

  • motivated by the study of colonies, or swarms of social organisms
  • Ant colony optimization: modelling of social ants behaviour
  • Particle swarm optimization: a global optimization approach modelled on the social behaviour of bird flocks or fish schooling
    • individuals (particles) grouped a swarms
  • Bees algorithms: foraging behaviour of swarms of bees

What are genetic algorithms?

  • a search technique that incorporates a simulation of evolution as a search heuristic when finding a good solution
Natural termGenetic Algorithm term
chromosomestring, “chromosome” (solution to a particular problem, feasible or infeasible)
genefeature, character, detector (**)
allelefeature value
locusstring position
genotypestructure, population
phenotypeparameter set, alternative solution, decoded structure

Genetic algorithms

  • formally introduced in the US in the 70s by John Holland
  • Holland’s original GA is usually referred to as the simple genetic algorithm (SGA)

before we can apply a genetic algorithm to a problem, we need to answer:

  • how is an individual represented?
  • what is the fitness function?
  • how are individuals selected?
  • how do individuals reproduce?

Elitism: global variable saving the best chromosome/fitness (crossover can possibly get rid of the best chromosome)

Main components of SGA reproduction cycle

  • select parents for mating pool (equal to population size)
  • apply crossover with probability , otherwise, copy parents
  • for each offspring apply mutation (bit flip with probability independently for each bit)
  • replace the whole population with the resulting offspring (generational population model)

A general SGA

BEGIN
    i = 0 # set generation number to zero
    initpopulation P(0) # INITIALIZE usually random population of individuals representing candidate solution
 
    evaluate P(0) # EVALUATE fitness of all initial individuals of population
 
    while (not done) do # test for termination criterion (time, fitness, etc)
        i = i + 1 # increase the generation number
        select P(i) from P(i-1) # SELECT a sub-population for offspring reproduction
        recombine P(i) # RECOMBINE the genes of pairs of selected parents
        mutate P(i) # MUTATE (perturb) the mated population (offspring)
        evaluate P(i) # EVALUATE the new individuals
END
# PYTHON EXAMPLE
import random
 
# Parameters
POP_SIZE = 10
GENES = "01"  # binary string chromosome
CHROMO_LENGTH = 8
MAX_GENERATIONS = 20
 
# Generate random chromosome
def random_chromosome():
    return "".join(random.choice(GENES) for _ in range(CHROMO_LENGTH))
 
# Fitness function (example: maximize number of '1's)
def fitness(chromosome):
    return chromosome.count("1")
 
# Selection (tournament)
def select(population):
    return max(random.sample(population, 2), key=lambda x: fitness(x))
 
# Crossover (single point)
def crossover(parent1, parent2):
    point = random.randint(1, CHROMO_LENGTH - 1)
    return parent1[:point] + parent2[point:]
 
# Mutation (flip bit with small probability)
def mutate(chromosome, mutation_rate=0.1):
    return "".join(
        (random.choice(GENES) if random.random() < mutation_rate else gene)
        for gene in chromosome
    )
 
# Genetic Algorithm
def genetic_algorithm():
    # Initialize population
    population = [random_chromosome() for _ in range(POP_SIZE)]
    generation = 0
 
    while generation < MAX_GENERATIONS:
        generation += 1
        # Evaluate fitness
        population = sorted(population, key=fitness, reverse=True)
        print(f"Gen {generation}: Best = {population[0]} (Fitness: {fitness(population[0])})")
 
        # Stop if perfect solution found
        if fitness(population[0]) == CHROMO_LENGTH:
            break
 
        # Create new population
        new_pop = []
        for _ in range(POP_SIZE):
            parent1 = select(population)
            parent2 = select(population)
            child = crossover(parent1, parent2)
            child = mutate(child)
            new_pop.append(child)
 
        population = new_pop
 
genetic_algorithm()

Side-quest

use “empirically” in reports (smart way of saying “trial and error”)

Initial population

  • initialization sets the beginning population of individuals from which future generations are produced
  • concerns:
    • size of initial population
      • empirically determined for a given problem (trial and error)
    • genetic diversity of initial population
    • premature convergence on non-optimal solution
      • use convergence plots to establish premature convergence or not

Simple vs steady-state GA

  • Population creation: most commonly used, simple/steady-state
    • Simple: a generational algorithm in which the entire population is replaced at each generation
      • kill the parents and replace with the children
    • Steady-state: only a few individuals are replaced at each “generation”
      • e.g. replace worst, replace most similar (crowding)
      • will not be asked to implement in assignment 2, could be used as bonus

Note

up to slide 31

Important

only focus on tournament selection, will be only focus in 3p71 and is “the best” (for our scenarios)