# Genetic Algorithm¶

This class represents a basic ($$\mu+\lambda$$) genetic algorithm for single-objective problems. The figure below shows the flow of a genetic algorithm in general. In the following, it is explained how pymoo does allow to customize the modules.

1. Initial Population:: A starting population is sampled in the beginning. In this framework, this can be either a Sampling object, which defines different initial sampling strategies, or Population where the X and F values are set, or a simple numpy array (pop_size x n_var).

2. Evaluation: It is executed using the problem defined to be solved.

3. Survival: It is very often the core of the genetic algorithm that is used. For simple single-objective genetic algorithm, the individuals can be sorted by their fitness and survival of the fittest can be applied.

4. Selection: In the beginning of the recombination process individuals need to be slected to participate in mating. Depending on the recombination crossover a different number of parents is is used to select some individual to be the parents of a new offspring. Different kind of selections can increase the convergence of the algorithm.

5. Crossover: When the parents are selected the actual mating is done. A crossover operator combines parents into one or several offspring. Commonly, problem information, such as the variable bounds, are needed to perform the mating. For more customized problems even more information might be necessary (e.g. current generation, diversity meausre of the population, …)

6. Mutation: It is performed after the offsprings are created through crossover. Usually, the mutation is executed with a predefined probability. This operator helps to increase the diversity in the population.

## Example¶

[1]:

from pymoo.algorithms.so_genetic_algorithm import ga
from pymoo.optimize import minimize
from pymop.factory import get_problem

method = ga(
pop_size=100,
eliminate_duplicates=True)

res = minimize(get_problem("g01"),
method,
termination=('n_gen', 50),
disp=False)

print("Best solution found: \nX = %s\nF = %s" % (res.X, res.F))


Best solution found:
X = [0.99318015 0.99805751 0.99841552 0.99154133 0.99398002 0.98832975
0.99559111 0.9838085  0.99401594 2.86412984 2.84443849 2.77771475
0.99492776]
F = [-14.34353041]


## API¶

pymoo.algorithms.so_genetic_algorithm.ga(pop_size=100, sampling=RandomSampling(), selection=TournamentSelection(func_comp=comp_by_cv_and_fitness), crossover=SimulatedBinaryCrossover(prob=0.9, eta=3), mutation=PolynomialMutation(prob=None, eta=5), eliminate_duplicates=True, n_offsprings=None, **kwargs)
Parameters
pop_sizeint

The population sized used by the algorithm.

sampling

The sampling process defines the initial set of solutions which are the starting point of the optimization algorithm. Here, you have three different options by passing

(i) A Sampling implementation which is an implementation of a random sampling method.

(ii) A Population object containing the variables to be evaluated initially OR already evaluated solutions (F needs to be set in this case).

(iii) Pass a two dimensional numpy.array with (n_individuals, n_var) which contains the variable space values for each individual.

selectionSelection

This object defines the mating selection to be used. In an evolutionary algorithm each generation parents need to be selected to produce new offsprings using different recombination and mutation operators. Different strategies for selecting parents are possible e.g. selecting them just randomly, only in the neighbourhood, using a tournament selection to introduce some seletion pressure, …

crossoverCrossover

The crossover has the purpose of create offsprings during the evolution. After the mating selection the parents are passed to the crossover operator which will dependent on the implementation create a different number of offsprings.

mutationMutation

Some genetic algorithms rely only on the mutation operation. However, it has shown that increases the performance to perform a mutation after creating the offsprings through crossover as well. Usually the mutation operator needs to be initialized with a probability to be executed. Having a high probability of mutation will most of the time increase the diversity in the population.

eliminate_duplicatesbool

The genetic algorithm implementation has a built in feature that eliminates duplicates after merging the parent and the offspring population. If there are duplicates with respect to the current population or in the offsprings itself they are removed and the mating process is repeated to fill up the offsprings until the desired number of unique offsprings is met.

n_offspringsint (default: None)

Number of offspring that are created through mating. By default n_offsprings=None which sets the number of offsprings equal to the population size. By setting n_offsprings=1 a, so called, steady-state version of an algorithm can be achieved.

Returns
gaAlgorithm

Returns an SingleObjectiveGeneticAlgorithm algorithm object.