Genome

Submodules

pygenalgo.genome.chromosome module

class pygenalgo.genome.chromosome.Chromosome(genome: list[Gene], fitness: float = nan, valid: bool = True)[source]

Bases: object

Description:

Implements a dataclass for the Chromosome entity. This class is responsible for holding the individual solution(s), of the optimization problem, during the evolution process.

clone() Chromosome[source]

Makes a duplicate of the self object by deep-coping only the genome field.

Returns:

a “deep-copy” of the object.

property fitness: float

Accessor of the fitness value of the chromosome.

Returns:

the fitness (float) of the genome.

property genome: list[Gene]

Accessor of the genome list of the chromosome.

Returns:

the list (of Genes) of the chromosome.

hamming_distance(other) int[source]

Compute the Hamming distance of the “self” object, with the “other” chromosome. In practice, it’s the number of positions at which the corresponding genes are different.

Parameters:

other – (Chromosome) to compare the Hamming distance.

Returns:

(int) the number of dissimilarities between the two input chromosomes.

has_valid_genome() bool[source]

Checks the validity of the whole chromosome, by calling individually all genes is_valid method.

Returns:

True if ALL genes are valid, else False.

invalidate_fitness() None[source]

Invalidates the fitness of the chromosome by setting the value to NaN. This is used during the evolution process (mutation).

Returns:

None.

property valid: bool

Accessor (getter) of the validity parameter.

Returns:

the valid value.

values() list[source]

Returns the gene values of the chromosome as list.

Returns:

the list values of the genome.

pygenalgo.genome.gene module

class pygenalgo.genome.gene.Gene(datum: Any, func: Callable, valid: bool = True)[source]

Bases: object

Description:

This is the main class that encodes the data of a single Gene in the chromosome. The class encapsulates not only the data, but also the way that this gene can be mutated using a random function. This Gene can be from a single ‘bit’ to a whole image. This way provides us with flexibility to parameterize the chromosome with different “kinds of genes” each one responsible for a specific function.

clone() Gene[source]

Makes a duplicate of the self object by deep-coping only the datum field.

Returns:

a “deep-copy” of the object.

flip() None[source]

This method flips the value of the gene data. It is used only by the FlipMutator operator for problems where the chromosome is represented by a list of bits.

  1. 1 -> 0

  2. 0 -> 1

Returns:

None.

property func: Callable

Accessor (getter) of the gene function reference.

Returns:

the _func reference.

property is_valid: bool

Accessor (getter) of the validity parameter.

Returns:

the valid value.

random() None[source]

This method should be different for each type of Gene. It describes how a specific type of Gene creates a random version of itself. The main idea is that inside the Chromosome, each Gene can represent a very different concept of the problem solution, so its Gene should have its own way to perform random mutation.

This way by calling on the random() method, each Gene will know how to mutate itself without breaking any rules/constraints.

Returns:

None.

property value: Any

Accessor (getter) of the data reference.

Returns:

the datum value.

Module contents