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 crossovercrossover_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 mutationmutation_rate: fraction of components changed in a mutant. 0 turns mutation off, also in the native spacemutation_step_size: standard deviation of the Gaussian mutation (in code units)mutation_step_size_damp: the step size is multiplied by this after every iterationselection: "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 selectionspace: "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 withcrossover(x1, x2, rng)and/ormutate(x, rng)methods, or a tuple(crossover, mutate), used in place of the defaults withspace="native". A missing or None entry keeps the default.crossover(ypea.operators.Crossover or None): An operator in place of blend crossover, for exampleypea.operators.SBX(). A code operator needsspace="encoded"and works on the whole codes. A native one (NativeCrossover) needsspace="native"and replaces the default of the variable types it lists (operatorsentries 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 exampleypea.operators.PolynomialMutation(), with the same rules ascrossover.mutation_probstill sets the number of mutants, andmutation_rate=0still 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¶