2. Evolutionary Toolbox

The toolbox module is intended to contain the operators that you need in your evolutionary algorithms, from initialisation to evaluation. It is always possible to use directly the operators from this module but the toolbox does also contain the default values of the different parameters for each method. More over, it makes your algorithms easier to understand and modify, since once an oprerator is set, it can be reused with a simple keyword that conatins all its arguments. Plus, every keyword or argument can be overriden at all time.

The toolbox is also used in predefined algorithms from the algorithms module.

class eap.toolbox.Toolbox

A toolbox for evolution that contains the evolutionary operators. At first this toolbox is empty, you can populate it by using the method register().

register(methodname, method[, content_init[, size_init]], ...)

Register a method in the toolbox under the name method name. You may provide default arguments that will be passed automaticaly when calling the registered method.

Keyworded arguments content_init and size_init may be used to simulate iterable initializers. For example, when building objects deriving from list, the content argument will provide to the built list its initial content. Depending on what is given to content_init and size the initialization is different. If content_init is an iterable, then the iterable is consumed enterily to intialize each object, in that case size_init is not used. Otherwise, content_init may be a simple function that will be repeated size_init times in order to fill the object.

unregister(methodname)

Unregister method name from the toolbox.

2.1. Operators

This module contains the operators an evolutionary algorithm. They are used to modify, select and move the individuals in their environment. A good set of operators should allow to move from an initial population of good solutions, equivalent to random sampling, to excellent configurations optimizing the studied problem.

Note

Operators that affects the individual’s constitution (attributes) are responsible of invalidating the fitness and make sure that the new individual(s) is (are) independent of the original individual(s).

2.1.1. Crossover

eap.toolbox.cxTwoPoints(ind1, ind2)

Execute a two points crossover on the input individuals. The two children produced are returned as a tuple, the two parents are left intact. This operation apply on an Individual composed of a list of attributes and act as follow

>>> ind1 = [A(1), ..., A(i), ..., A(j), ..., A(m)]
>>> ind2 = [B(1), ..., B(i), ..., B(j), ..., B(k)]
>>> # Crossover with mating points 1 < i < j <= min(m, k) + 1
>>> child1, child2 = twoPointsCx(ind1, ind2)
>>> print child1
[A(1), ..., B(i), ..., B(j-1), A(j), ..., A(m)]
>>> print child2
[B(1), ..., A(i), ..., A(j-1), B(j), ..., B(k)]

This function use the randint() function from the python base random module.

eap.toolbox.cxOnePoint(ind1, ind2)

Execute a one point crossover on the input individuals. The two children produced are returned as a tuple, the two parents are left intact. This operation apply on an Individual composed of a list of attributes and act as follow

>>> ind1 = [A(1), ..., A(n), ..., A(m)]
>>> ind2 = [B(1), ..., B(n), ..., B(k)]
>>> # Crossover with mating point i, 1 < i <= min(m, k)
>>> child1, child2 = twoPointsCx(ind1, ind2)
>>> print child1
[A(1), ..., B(i), ..., B(k)]
>>> print child2
[B(1), ..., A(i), ..., A(m)]

This function use the randint() function from the python base random module.

eap.toolbox.cxUniform(ind1, ind2, indpb)

Uniform crossover

eap.toolbox.cxPartialyMatched(ind1, ind2)

Execute a partialy matched crossover (PMX) on the input indviduals. The two children produced are returned as a tuple, the two parents are left intact. This crossover expect iterable individuals of indices, the result for any other type of individuals is unpredictable.

Moreover, this crossover consists of generating two children by matching pairs of values in a certain range of the two parents and swaping the values of those indexes. For more details see Goldberg and Lingel, “Alleles, loci, and the traveling salesman problem”, 1985.

For example, the following parents will produce the two following children when mated with crossover points a = 2 and b = 3.

>>> ind1 = [0, 1, 2, 3, 4]
>>> ind2 = [1, 2, 3, 4, 0]
>>> child1, child2 = pmxCx(ind1, ind2)
>>> print child1
[0, 2, 3, 1, 4]
>>> print child2
[2, 3, 1, 4, 0]

This function use the randint() function from the python base random module.

eap.toolbox.cxUniformPartialyMatched(ind1, ind2, indpb)

Execute a uniform partialy matched crossover (UPMX) on the input indviduals. The two children produced are returned as a tuple, the two parents are left intact. This crossover expect iterable individuals of indices, the result for any other type of individuals is unpredictable.

Moreover, this crossover consists of generating two children by matching pairs of values chosen at random with a probability of indpb in the two parents and swaping the values of those indexes. For more details see Cicirello and Smith, “Modeling GA performance for control parameter optimization”, 2000.

For example, the following parents will produce the two following children when mated with the chosen points [0, 1, 0, 0, 1].

>>> ind1 = [0, 1, 2, 3, 4]
>>> ind2 = [1, 2, 3, 4, 0]
>>> child1, child2 = pmxCx(ind1, ind2)
>>> print child1
[4, 2, 1, 3, 0]
>>> print child2
[2, 1, 3, 0, 4]

This function use the random() and randint() functions from the python base random module.

eap.toolbox.cxBlend(ind1, ind2, alpha)

Blend crossover

eap.toolbox.cxESBlend(ind1, ind2, alpha, minstrategy=None)

Execute a blend crossover on both, the individual and the strategy.

eap.toolbox.cxESTwoPoints(ind1, ind2)

Execute a classical two points crossover on both the individual and its strategy. The crossover points for the individual and the strategy are the same.

eap.toolbox.cxSimulatedBinary(ind1, ind2, nu)

Simulated binary crossover

eap.toolbox.cxMessyOnePoint(ind1, ind2)

Execute a one point crossover will mostly change the individuals size.

eap.toolbox.cxTreeUniformOnePoint(ind1, ind2)

Randomly select in each individual and exchange each subtree with the point as root between each individual.

eap.toolbox.cxTypedTreeOnePoint(ind1, ind2)

Randomly select in each individual and exchange each subtree with the point as root between each individual. Since the node are strongly typed, the operator then make sure the the type of second node correspond to the type of the first node. It it doesn’t it randomly select another point in the second individual and try again. It tries up to 5 times before returning the unmodified individuals.

2.1.2. Mutation

eap.toolbox.mutGaussian(individual, mu, sigma, indpb)

This function applies a gaussian mutation of mean mu and standard deviation sigma on the input individual and returns the mutant. The individual is left intact and the mutant is an independant copy. This mutation expects an iterable individual composed of real valued attributes. The mutIndxPb argument is the probability of each attribute to be mutated.

Note

The mutation is not responsible for constraints checking, the reason for this is that there is too many possibilities for resetting the values. For example, if a value exceed the maximum, it may be set to the maximum, to the maximum minus (the value minus the maximum), it may be cycled to the minimum or even cycled to the minimum plus (the value minus the maximum). Wich way is closer to the representation used is up to you.

One easy way to add cronstraint checking to an operator is to simply wrap the operator in a second function. See the multi-objective example (moga_kursawefct.py) for an explicit example.

This function uses the random() and gauss() functions from the python base random module.

eap.toolbox.mutShuffleIndexes(individual, indpb)

Shuffle the attributes of the input individual and return the mutant. The individual is left intact and the mutant is an independant copy. The individual is expected to be iterable. The shuffleIndxPb argument is the probability of each attribute to be moved.

This function uses the random() and randint() functions from the python base random module.

eap.toolbox.mutFlipBit(individual, indpb)

Flip the value of the attributes of the input individual and return the mutant. The individual is left intact and the mutant is an independant copy. The individual is expected to be iterable and the values of the attributes shall stay valid after the not operator is called on them. The flipIndxPb argument is the probability of each attribute to be flipped.

This function uses the random() function from the python base random module.

eap.toolbox.mutES(individual, indpb, minstrategy=None)

Mutate an evolution strategy according to its strategy attribute. The strategy shall be teh same size as the individual. This is subject to change.

eap.toolbox.mutTreeUniform(ind, expr)

Randomly select a point in the Tree, then replace the subtree with the point as a root by a randomly generated expression. The expression is generated using the method expr.

eap.toolbox.mutTypedTreeUniform(ind, expr)

The mutation of strongly typed GP expression is pretty easy. First, it finds a subtree. Second, it has to identify the return type of the root of this subtree. Third, it generates a new subtree with a root’s type corresponding to the original subtree root’s type. Finally, the old subtree is replaced by the new subtree, and the mutant is returned.

2.1.3. Selection

eap.toolbox.selTournament(individuals, n, tournsize)

Select n individuals from the input individuals using n tournaments of tournSize individuals. The list returned contains shallow copies of the input individuals.

This function uses the choice() function from the python base random module.

eap.toolbox.selRandom(individuals, n)

Select n individuals at random from the input individuals. The list returned contains shallow copies of the input individuals.

Changed in version 0.3.1a: Removed random sample without replacement as this is simply a call to python”s sample() function

This function uses the choice() function from the python base random module.

eap.toolbox.selBest(individuals, n)

Select the n best individuals among the input individuals. The list returned contains shallow copies of the input individuals.

eap.toolbox.selWorst(individuals, n)

Select the n worst individuals among the input individuals. The list returned contains shallow copies of the input individuals.

2.1.4. Migration

eap.toolbox.migRing(populations, n, selection[, replacement][, migarray][, sel_kargs][, repl_kargs])

Perform a ring migration between the populations. The migration first select n emigrants from each population using the specified selection operator and then replace n individuals from the associated population in the migarray by the emigrants. If no replacement operator is specified, the immigrants will replace the emigrants of the population, otherwise, the immigrants will replace the individuals selected by the replacement operator. The migration array, if provided, shall contain each population’s index once and only once. If no migration array is provided, it defaults to a serial ring migration (1 – 2 – ... – n – 1). You may pass keyworded arguments to the two selection operators by giving a dictionary to sel_kargs and repl_kargs.

Table Of Contents

Previous topic

1. Evolutionary Algorithm Bases

Next topic

3. Algorithms

This Page