Genetic Algorithm

With these definitions, one way to define a Genetic Algorithm is as follows:

proc GA(fitness, theta, n, r, m)
    ; fitness is the fitness function
    ; theta is the fitness threshold, which is used to determine
    ;   when to halt
    ; n is the population size in each generation
    ; r is the fraction of the population generated by crossover
    ; m is the mutation rate

    P := generate n individuals at random
    ; initial generation is generated randomly

    while max fitness(hi) < theta do
	   i
      ; define the next generation S (also of size n)

      Reproduction step: Probabilistically select
      (1-r)/n individuals of P and add them to S, where
      Prob(hi) = fitness(hi) / sum fitness(hj)
			        j

      Crossover step: Probabilistically select rn/2 pairs
      of individuals from P according to Prob(h)

      foreach pair (h1, h2), produce two offspring by applying
	 the crossover operator and add these offspring to S
      
      Mutate step: Choose m% of S and randomly invert one
	 bit in each

      P := S

    end_while

    Find b such that fitness(b) = max fitness(h)
				   h
    return(b)

end_proc