Genetic Algorithm

Genetic Algorithm

class Genetic_algorithm(search_space, f_calls, pop_size=10, generation=1000, elitism=0.5, filename='', verbose=True)[source]

Bases: zellij.core.metaheuristic.Metaheuristic

Genetic_algorithm (GA) implements a classic genetic algorithm.

It uses DEAP. See Metaheuristic for more info.

pop_size

Population size of the GA. In a distributed environment (e.g. MPILoss), it has an influence on the parallelization quality. It must be tuned according the available hardware.

Type

int

generation

Generation number of the GA.

Type

int

elitism

Percentage of the best parents to keep in the next population by replacing the worst children. Default 50%.

Type

float, default=0.5

filename

If a file containing initial solutions. GA will initialize the population with it.

Type

str, optional

See also

Metaheuristic

Parent class defining what a Metaheuristic is in Zellij.

Loss Function

Describes what a loss function is in Zellij.

Search space

Describes what a search space is in Zellij.

Examples

>>> from zellij.core import Loss
>>> from zellij.core import ContinuousSearchspace
>>> from zellij.core import FloatVar, ArrayVar
>>> from zellij.utils.neighborhoods import FloatInterval, ArrayInterval, Intervals
>>> from zellij.strategies.genetic_algorithm import Genetic_algorithm
>>> from zellij.utils.operators import NeighborMutation, DeapTournament, DeapOnePoint
>>> from zellij.utils.benchmark import himmelblau
...
>>> lf = Loss()(himmelblau)
>>> sp = ContinuousSearchspace(ArrayVar(
...                           FloatVar("a",-5,5, neighbor=FloatInterval(0.5)),
...                           FloatVar("b",-5,5,neighbor=FloatInterval(0.5)),
...                           neighbor=ArrayInterval())
...                         ,lf, neighbor=Intervals(),
...                         mutation = NeighborMutation(0.5),
...                         selection = DeapTournament(3),
...                         crossover = DeapOnePoint())
...
>>> ga = Genetic_algorithm(sp, 1000, pop_size=25, generation=40,elitism=0.5)
>>> ga.run()
define_individual(self)[source]

Describe how an individual should be initialized. Here a random point from SearchSpace is sampled.

initIndividual(self, icls, content)[source]

Initialize an individual to DEAP.

initPopulation(self, pcls, ind_init, filename)[source]

Initialize a population of individual, from a file, to DEAP.

run(H=None, n_process=1)[source]

Runs GA

Parameters
  • H (Fractal, optional) – When used by Decomposition Based Algorithm, a fractal corresponding to the current subspace is given

  • n_process (int, default=1) – Determine the number of best solution found to return.

Returns

  • best_sol (list[float]) – Returns a list of the n_process best found points to the continuous format

  • best_scores (list[float]) – Returns a list of the n_process best found scores associated to best_sol

Addons

Here is the list of Addons linked to Genetic Algorithm. These are implemented by using Search space kwargs.

Mutation

class NeighborMutation(probability, search_space=None)[source]

Bases: zellij.core.addons.Mutator

Based on DEAP. It is a Search space Addon which defines a mutation. The mutation itself is based on the neighbor Addons defined for each Variables of the Search space. See Neighborhood.

Parameters
  • probability (float) – Probaility for an individual to be mutated.

  • search_space (Search space) – Targeted Search space.

probability
property target

Crossover

class DeapOnePoint(search_space=None)[source]

Bases: zellij.core.addons.Crossover

Based on DEAP cxOnePoint. Search space Addon defining a crossover method.

Parameters

search_space (Search space) – Targeted Search space.

property target

Selection

class DeapTournament(size, search_space=None)[source]

Bases: zellij.core.addons.Selector

Based on DEAP tournament. Search space Addon defining a selection method.

Parameters
size
property target