Gene - danielwilczak101/EasyGA GitHub Wiki

A gene is the smallest unit of data in the genetic algorithm. It represents a single value and is meant to hold the data of the genetic algorithm. Aside from this, genes also have special printing formats.

Initialization

A gene may be initialized using gene = ga.make_gene(value). This creates a gene holding the specified value. It is also possible to copy genes by using gene_copy = ga.make_gene(gene). This does not create a gene that holds a gene but rather a gene that holds the value from the original gene. To avoid duplication or similar issues, the data is always deepcopied, which helps to prevent accidentally changing a gene without interacting with it.

>>> gene_1 = ga.make_gene(5)
>>> print(gene_1)
[5]
>>> gene_2 = ga.make_gene(gene_1)
>>> print(gene_2)
[5]
>>> gene_3 = ga.make_gene("7")
>>> print(gene_3)
[7]

Attributes

The only built-in attribute that the gene has is its value. This may be referenced using gene.value.

>>> gene_1.value
5

Additional Methods

As shown above, genes have a few additional methods. When printed, they wrap their values in square brackets. This is due to the str method. A repr method also exists, mostly for database purposes. The eq method is also defined by comparing gene values.

>>> gene_1 = ga.make_gene(5)
>>> gene_2 = ga.make_gene(gene_1)
>>> str(gene_1)
'[5]'
>>> repr(gene_1)
'EasyGA.make_gene(5)'
>>> gene_1 == gene_2
True
>>> gene_1 == 5
True