pynetics.list package

Submodules

pynetics.list.alphabet module

TODO TBD…

class pynetics.list.alphabet.Alphabet(*, genes: Iterable[Any])

Bases: object

Alleles based on a list of equiprobable elements.

Although it can be initialized by repeated genes, only one of each will be stored.

genes

The genes that conform this alphabet.

get(n: int = 1, rep: bool = True) → Union[Any, Sequence[Any]]

Returns 1-to-n random values among all the possible values.

The method will return either a random allele from the whole genetic pool or a sequence of alleles, with or without repetition.

Parameters:
  • n – The number of genes to return. Defaults to 1.
  • rep – If n > 1, True means repeated alleles may be returned, whereas False means no repeated alleles will be returned. Defaults to True.
Returns:

An allele in case n = 1 or a sequence of alleles in case n > 1.

Raises:

MoreGenesRequiredThanExisting – If n > len(genetic pool) and rep is False (if we don’t allow repetition, it is impossible to return more values than there are.

pynetics.list.bin module

Specific implementations for binary alphabet based algorithms.

pynetics.list.bin.generalised_crossover(parent1: pynetics.list.genotype.ListGenotype, parent2: pynetics.list.genotype.ListGenotype) → Tuple[pynetics.list.genotype.ListGenotype, pynetics.list.genotype.ListGenotype]

Progeny obtained by crossing genotypes as if they where integers.

This recombination algorithm is expected to be used with binary genotypes of the same length. It may work with other kind of individuals, but the outcome is unpredictable.

Parameters:
  • parent1 – One of the genotypes.
  • parent2 – The other.
Returns:

A tuple with the progeny.

pynetics.list.diversity module

Implementations of different diversity algorithms for genotypes represented by lists.

class pynetics.list.diversity.DifferentGenesInPopulation(alphabet: pynetics.list.alphabet.Alphabet)

Bases: object

Diversity based on the appearance of different genes regardless their position.

The value is computed as follows. For each gene position, a value of M (the different appearing genes) is computed. Then, all the values are sum added and the divided by Y = N * L where L is the length of the individual and N the number of possible alleles. The value then is expected to belong to the interval [0, 1], where 0 is no diversity at all and 1 a completely diverse population.

It is expected for the genotypes to have the same length.

pynetics.list.diversity.average_hamming(genotypes: Sequence[pynetics.list.genotype.ListGenotype]) → float

Average of each hamming loci distances.

The diversity will be computed as follows:

  1. Calculate all the hamming distances between each pair of
    genotypes.
  2. Divide between the length of the genotypes.

It is assumed that all the genotypes have the same length; if not, the diversity will be computed as if all the genotypes had the same length (the minimum among them).

Parameters:genotypes – A sequence of individuals from which obtain the diversity.
Returns:A float value representing the diversity.

pynetics.list.exception module

Definition specific errors for list based genetic algorithms.

exception pynetics.list.exception.AlphabetError

Bases: pynetics.exception.PyneticsError

Errors related to the Alphabet classes.

exception pynetics.list.exception.EmptyListGenotype

Bases: pynetics.exception.RecombinationError

A genotype with at least one gene was needed, but it was zero.

exception pynetics.list.exception.NotEnoughSymbolsInAlphabet(*, expected: int, real: int)

Bases: pynetics.list.exception.AlphabetError

The alphabet hasn’t as many symbols as needed.

exception pynetics.list.exception.TooFewGenes(*, genes: Iterable[Any])

Bases: pynetics.list.exception.AlphabetError

Very few genes to fill up the alphabet.

pynetics.list.genotype module

Implementations for list based genotypes.

class pynetics.list.genotype.ListGenotype(*, genes: Optional[Iterable[Any]] = None)

Bases: pynetics.api.Genotype, collections.abc.MutableSequence

A list based genotype, used for various implementations.

It also behaves as a MutableSequence (provides __init__, __len__, __getitem__, __setitem__, __delitem__, and insert), so be used too almost like a drop-in replacement for a list.

insert(index: int, value: Any)

Inserts an element in the given index.

If the given index is greater or equal to the length of the genotype, the value is inserted at the end of it, regardless the length of it.

The elements to the right are shifted one position.

Parameters:
  • index – The position to insert the value.
  • value – Which value insert.
phenotype() → List[Any]

Returns the list of genes conforming this genotype.

Returns:An ordered list with the elements conforming this genotype.

pynetics.list.initializer module

API implementations specific of list based genotypes problems.

class pynetics.list.initializer.AlphabetInitializer(*, size: int, alphabet: pynetics.list.alphabet.Alphabet, cls: Optional[Type[pynetics.list.genotype.ListGenotype]] = None)

Bases: pynetics.list.initializer.ListGenotypeInitializer

Initializer for ListGenotype based on an arbitrary alphabet.

create() → pynetics.list.genotype.ListGenotype

Generates a new random genotype.

Returns:A new ListGenotype instance.
class pynetics.list.initializer.IntervalInitializer(*, size: int, lower: Union[int, float], upper: Union[int, float], cls: Optional[Type[pynetics.list.genotype.ListGenotype]] = None)

Bases: pynetics.list.initializer.ListGenotypeInitializer

Initializer for genotypes whose genes belong to an interval.

This is an abstract class that comprises the common behavior for genotypes of either integer or real genes.

create() → pynetics.list.genotype.ListGenotype

Generates a new random genotype.

Returns:A new genotype instance.
get_value_from_interval() → Union[int, float]

Generates a new value that belongs to the interval.

Returns:A value for the specified interval at initialization.
class pynetics.list.initializer.ListGenotypeInitializer(*, size: int, cls: abc.ABCMeta)

Bases: pynetics.api.Initializer

Common behaviour between initializers.

class pynetics.list.initializer.PermutationInitializer(*, size: int, alphabet: pynetics.list.alphabet.Alphabet, cls: Optional[Type[pynetics.list.genotype.ListGenotype]] = None)

Bases: pynetics.list.initializer.ListGenotypeInitializer

TODO TBD…

create() → pynetics.list.genotype.ListGenotype

Generates a new random genotype.

Returns:A new ListGenotype instance.

pynetics.list.int module

Operators for specific int representation of list-based genotypes.

class pynetics.list.int.Creep(*, amount: int, fixed: Optional[bool] = True, lower: Optional[int] = None, upper: Optional[int] = None)

Bases: pynetics.list.mutation.PerGeneMutation

Mutates the genotype by adding a small value to some genes.

This value may be positive or negative, and the resulting value may belong to a closer or upper interval.

compute_amount() → int

Compute the amount to add or subtract base on the arguments.

Returns:An integer value to add to the gene.
do(genotype: pynetics.list.genotype.ListGenotype, index: int)

Performs the specific mutation

Parameters:
  • genotype – the genotype to mutate.
  • index – which gene is affected.
class pynetics.list.int.IntegerIntervalInitializer(*, size: int, lower: Union[int, float], upper: Union[int, float], cls: Optional[Type[pynetics.list.genotype.ListGenotype]] = None)

Bases: pynetics.list.initializer.IntervalInitializer

Initializer for int based ListGenotype instances.

get_value_from_interval() → int

Generates a new value that belongs to the interval.

This value will be an integer value.

Returns:A value for the specified interval at initialization.
class pynetics.list.int.RangeCrossover(*, lower: int, upper: int)

Bases: object

Offspring is obtained by crossing individuals gene by gene.

For each gene, the interval of their values is calculated. Then, the difference of the interval is used for calculating the new interval from where to pick the values of the new genes. First, a value is taken from the new interval. Second, the other value is calculated by taking the symmetrical by the center of the range.

It is expected for the genotypes to have the same length. If not, the operation works over the first common genes, leaving the rest untouched.

pynetics.list.mutation module

Generic mutation schemas for list-based genotypes.

class pynetics.list.mutation.PerGeneMutation

Bases: object

General behaviour for a mutation applied per gene.

do(genotype: pynetics.list.genotype.ListGenotype, index: int)

Performs the specific mutation

Parameters:
  • genotype – the genotype to mutate.
  • index – which gene is affected.
class pynetics.list.mutation.RandomGene(alphabet: pynetics.list.alphabet.Alphabet, same: bool = False)

Bases: pynetics.list.mutation.PerGeneMutation

Mutates the genotype by changing some genes values.

For each gene the mutation probability will be check and, if a mutation is requested, that gene will be replaced by a random one extracted from the alphabet.

genotype : aacgaata alphabet : (a, c, t, g) mutate in : 2, 7 ———– mutated : abcdaaba

do(genotype: pynetics.list.genotype.ListGenotype, index: int)

Performs the specific mutation

Parameters:
  • genotype – the genotype to mutate.
  • index – which gene is affected.
class pynetics.list.mutation.SwapGenes

Bases: pynetics.list.mutation.PerGeneMutation

Mutates the genotype by swapping two random genes.

For each gene, a random value will be compared against the mutation probability and, if the value is lower, the mutation will take place and its value will be swapped with the value of another random gene in the genotype.

For example, if the mutation occurs in the gene located in position 3 and it has to be swapped with the one in position 5, then:

genotype : 12345678 positions : 3, 5 ———– mutated : 12365478

do(genotype: pynetics.list.genotype.ListGenotype, index: int)

Performs the specific mutation

Parameters:
  • genotype – the genotype to mutate.
  • index – which gene is affected.

pynetics.list.real module

TODO TBD…

class pynetics.list.real.FlexibleRecombination(*, lower: float, upper: float, alpha: float)

Bases: object

TODO TBD…

class pynetics.list.real.RealIntervalInitializer(*, size: int, lower: Union[int, float], upper: Union[int, float], cls: Optional[Type[pynetics.list.genotype.ListGenotype]] = None)

Bases: pynetics.list.initializer.IntervalInitializer

Initializer for int based ListGenotype instances.

get_value_from_interval() → float

Generates a new value that belongs to the interval.

This value will be an integer value.

Returns:A value for the specified interval at initialization.
pynetics.list.real.plain_recombination(parent1: pynetics.list.genotype.ListGenotype, parent2: pynetics.list.genotype.ListGenotype) → Tuple[pynetics.list.genotype.ListGenotype, pynetics.list.genotype.ListGenotype]

TODO TBD…

Parameters:
  • parent1 – One of the genotypes.
  • parent2 – The other.
Returns:

A tuple with the progeny.

pynetics.list.recombination module

Recombination algorithms for list-based genotypes.

class pynetics.list.recombination.NPivot(*, num_pivots: int)

Bases: object

Progeny obtained by mixing parents with N random pivot points.

The process is as follows. N random points belonging to the [1, L-1] interval (being L the size of the genotypes) is selected. Then, one by one, the contents of both individuals are interchanged each time a new pivot point is reached. For example:

pivots : 3, 5 parents : XXXXXXXX, OOOOOOOO ———– children : XXXOOXXX, OOOXXOOO

The method implementation has been slightly modified to allow the recombination of different length genotypes. In this case, L will be the length of the shortest genotype.

Also, if the specified pivots number is greater than L-1, then L-1 will be used as the new N.

pynetics.list.recombination.pmx(parent1: pynetics.list.genotype.ListGenotype, parent2: pynetics.list.genotype.ListGenotype) → Tuple[pynetics.list.genotype.ListGenotype, pynetics.list.genotype.ListGenotype]

TODO TBD…

Parameters:
  • parent1 – One of the genotypes.
  • parent2 – The other.
Returns:

A tuple with the progeny.

pynetics.list.recombination.random_mask(parent1: pynetics.list.genotype.ListGenotype, parent2: pynetics.list.genotype.ListGenotype) → Tuple[pynetics.list.genotype.ListGenotype, pynetics.list.genotype.ListGenotype]

Progeny is created by using a random mask.

The random mask is generated to determine which genes are switched between genotypes. For example:

random mask : 00100110 parents : XXXXXXXX, OOOOOOOO ———– children : XXOXXOOX, OOXOOXXO

The method implementation has been slightly modified to allow the recombination of different length genotypes. In this case, the mask will only affect to the minimum length, leaving the rest of the genes untouched.

Parameters:
  • parent1 – One of the genotypes.
  • parent2 – The other.
Returns:

A tuple with the progeny.

Module contents