Skip to content

Genetic Algorithm (GA)

The classic evolutionary method, with crossover and mutation on a population. It works on continuous problems and, with space="native", directly on permutations, selections and binary variables (see Native operators).

GA

GA(*, crossover_prob=0.7, crossover_inflation=0.5, mutation_prob=0.3, mutation_rate=0.1, mutation_step_size=0.1, mutation_step_size_damp=0.99, selection='roulette', selection_pressure=5.0, tournament_size=3, space='encoded', operators=None, crossover=None, mutation=None, **kwargs)

Genetic Algorithm with (mu + lambda) survival.

Each iteration makes crossover_prob * pop_size children by crossover and mutation_prob * pop_size mutants. Parents, children and mutants are merged, and the best pop_size survive. Roulette wheel selection uses ranks, so it does not depend on the scale or sign of the objective.

Parameters

  • crossover_prob: fraction of the population size produced by crossover
  • crossover_inflation: gamma of blend crossover. Each child component is a*x1 + (1 - a)*x2 with a drawn from [-gamma, 1 + gamma] (BLX alpha), and the second child is the mirror a*x2 + (1 - a)*x1 with the same a. The default 0.5 is BLX-0.5.
  • mutation_prob: fraction of the population size produced by mutation
  • mutation_rate: fraction of components changed in a mutant. 0 turns mutation off, also in the native space
  • mutation_step_size: standard deviation of the Gaussian mutation (in code units)
  • mutation_step_size_damp: the step size is multiplied by this after every iteration
  • selection: "roulette" (rank-based roulette wheel, also "roulettewheel" or "rw"), "tournament" or "random"
  • selection_pressure: roulette wheel probabilities are proportional to exp(-selection_pressure * r), with the rank r from 0 (best) to 1 (worst). Larger values favor the best solutions more.
  • tournament_size: number of competitors in tournament selection
  • space: "encoded" (the default) works on the [0, 1] codes. "native" decodes each variable, applies its native operators (ypea.operators), and encodes the children back. Types without native operators use the code operators.
  • operators (dict, optional): Maps a variable name to an object with crossover(x1, x2, rng) and/or mutate(x, rng) methods, or a tuple (crossover, mutate), used in place of the defaults with space="native". A missing or None entry keeps the default.
  • crossover (ypea.operators.Crossover or None): An operator in place of blend crossover, for example ypea.operators.SBX(). A code operator needs space="encoded" and works on the whole codes. A native one (NativeCrossover) needs space="native" and replaces the default of the variable types it lists (operators entries still come first). None (the default) keeps blend crossover, or the native defaults.
  • mutation (ypea.operators.Mutation or None): An operator in place of the Gaussian mutation, for example ypea.operators.PolynomialMutation(), with the same rules as crossover. mutation_prob still sets the number of mutants, and mutation_rate=0 still turns mutation off.

References: D. E. Goldberg, "Genetic Algorithms in Search, Optimization, and Machine Learning", Addison-Wesley, 1989. L. J. Eshelman and J. D. Schaffer, "Real-coded genetic algorithms and interval-schemata", Foundations of Genetic Algorithms 2, Morgan Kaufmann, 1993 (blend crossover).

Long runs and combinatorial problems

The mutation step shrinks by mutation_step_size_damp (0.99 by default) every iteration, so after 500 iterations it is below 1% of its start value. On continuous problems this gives the final precision. On routes, assignments and knapsacks the search stops moving, and more evaluations do not help. For those problems, and for long runs, set mutation_step_size_damp=1. It does worse on continuous problems, so the default stays 0.99.

Example

import ypea

problem = ypea.Problem(
    ypea.Real("x", shape=20, lb=-10, ub=10),
    lambda sol: ypea.benchmarks.sphere(sol.x),
    vectorized=True,
    name="sphere",
)

alg = ypea.GA(max_nfe=20_000)
result = alg.solve(problem, seed=1)
print(result.summary())
GA on sphere
  best objective value: 8.49517e-12
  feasible: True (violation 0)
  evaluations: 20000, iterations: 199, time: 0.043 s, stopped by: max_nfe

Differs from Python: MATLAB draws its own random numbers, so the best value differs from Python's.

problem = ypea.Problem( ...
    vars=ypea.Real("x", shape=20, lb=-10, ub=10), ...
    objective=@(sol) ypea.benchmarks.sphere(sol.x), ...
    vectorized=true, ...
    name="sphere");

alg = ypea.GA(max_nfe=20000);
result = alg.solve(problem, seed=1);
disp(result.summary())
GA on sphere
  best objective value: 5.54002e-08
  feasible: true (violation 0)
  evaluations: 20000, iterations: 199, time: 0.138 s, stopped by: max_nfe

Search on Rastrigin

GA solutions on Rastrigin at four iterations, and its convergence
GA with default settings, 50 iterations on Rastrigin in two dimensions (seed 1). The red points are the current population. Once they share one spot, the curve still shows how close they are. The open diamond is the best solution so far, and the plus sign the global minimum.