Chromosome - danielwilczak101/EasyGA GitHub Wiki

A chromosome is a collection of data into a single object in a genetic algorithm. Similar to objects, a chromosome can be thought of as something which holds the attributes of something. By default, all data is stored as a list of genes in each chromosome. Chromosomes also store a fitness attribute representing the performance of the chromosome. Additionally, other built-in methods for chromosomes allow for more usage, such as printing.

Initialization

A chromosome may be initialized using chromosome = ga.make_chromosome(genes). This creates a chromosome holding the specified genes. How the genes are stored does not matter, but it must be an iterable object, such as a list of genes or another chromosome. The genes are then stored in the chromosome as chromosome.gene_list, a new list of genes.

Note: It is not necessary for the genes to be provided as gene objects. Either way, they will be cast to genes upon initialization.

>>> chromosome_1 = ga.make_chromosome(range(3))
>>> print(chromosome_1)
[0][1][2]
>>> chromosome_2 = ga.make_chromosome(chromosome_1)
>>> print(chromosome_2)
[0][1][2]
>>> chromosome_3 = ga.make_chromosome([1, 4, 5])
>>> print(chromosome_3)
[1][4][5]
>>> chromosome_4 = ga.make_chromosome([ga.make_gene(6)])
>>> print(chromosome_4)
[6]

Attributes

Chromosomes contain two attributes. The first is their gene list, given by chromosome.gene_list. The second is their fitness, given by chromosome.fitness. The fitness is initialized as None and must be set based on a fitness function.

Note: Attempting to print a list will result in printing the repr of the elements, which differs from the str representation shown above.

>>> print(chromosome_1.gene_list)
[EasyGA.make_gene(0), EasyGA.make_gene(1), EasyGA.make_gene(2)]
>>> print(chromosome_1.fitness)
None

Additional Methods

Chromosomes contain many methods that allow for easier usage. We recommend using these methods instead of interacting with the gene list whenever convenient.

Add Gene

The add gene method allows a gene to be added to the chromosome at a specified index. If no index is given, the gene is appended to the end of the gene list.

>>> chromosome = ga.make_chromosome([1, 2, 3])
>>> chromosome.add_gene(5)
>>> print(chromosome)
[1][2][3][5]

Remove Gene

The remove gene method allows a user to remove a gene at a specified index. The removed gene is then returned.

>>> chromosome = ga.make_chromosome([0, 1, 4, 9, 16])
>>> chromosome.remove_gene(3)
EasyGA.make_gene(9)
>>> print(chromosome)
[0][1][4][16]

Gene Value List/Iter

This method allows the user to directly get the values inside of the genes without going through the genes. Use chromosome.gene_value_list to get a list of values, or chromosome.gene_value_iter to iterate without creating a list.

Note: This is not an attribute, so every use creates a new list based on the current gene list. To modify the values in the chromosome, use setitem.

>>> chromosome = ga.make_chromosome([0, 1, 4, 9, 16])
>>> print(chromosome.gene_value_list)
[0, 1, 4, 9, 16]
>>> for value in chromosome.gene_value_iter:
...     print(value)
... 
0
1
4
9
16

Iter

The iter method allows directly iterating over chromosomes or casting them to other types, such as lists.

>>> chromosome = ga.make_chromosome([0, 1, 4, 9, 16])
>>> gene_list = list(chromosome)
>>> print(gene_list)
[EasyGA.make_gene(0), EasyGA.make_gene(1), EasyGA.make_gene(4), EasyGA.make_gene(9), EasyGA.make_gene(16)]
>>> for gene in chromosome:
...     print(gene.value)
... 
0
1
4
9
16

Get Item

The getitem method allows random access to genes in a chromosome, as though it were a list.

>>> chromosome = ga.make_chromosome([0, 1, 4, 9, 16])
>>> print(chromosome[1:4])
[EasyGA.make_gene(1), EasyGA.make_gene(4), EasyGA.make_gene(9)]
>>> for i in range(len(chromosome)):
...     print(chromosome[i])
... 
[0]
[1]
[4]
[9]
[16]

Set Item

The setitem method allows genes to be set in the chromosome directly, as though it were a list.

>>> chromosome = ga.make_chromosome([0, 1, 4, 9, 16])
>>> chromosome[1:4] = [1, 2, 3]
>>> print(chromosome)
[0][1][2][3][16]

Del Item

The delitem method allows genes to be deleted in the chromosome directly, as though it were a list. Unlike remove gene, the removed gene is not returned, but a slice of genes can be deleted all at once.

>>> chromosome = ga.make_chromosome([0, 1, 4, 9, 16])
>>> del chromosome[1:4]
>>> print(chromosome)
[0][16]

Len

The len method returns the length of the chromosome i.e. how many genes it contains.

>>> chromosome = ga.make_chromosome([0, 1, 4, 9, 16])
>>> len(chromosome)
5

Contains

The contains method returns True or False, depending on whether or not a given gene is in the chromosome or not.

>>> chromosome = ga.make_chromosome([0, 1, 4, 9, 16])
>>> 1 in chromosome
False
>>> 4 in chromosome
True

Index Of

The index-of method finds the index of a gene in a chromosome. A guess for the index of the gene may be given to speed up the search. If there are multiple occurrences of the gene, only the first instance found is returned.

>>> chromosome = ga.make_chromosome([0, 1, 4, 9, 16])
>>> chromosome.index_of(9)
3
>>> chromosome.index_of(9, guess = 4)
3

Eq

The eq method allows comparison of chromosomes by comparing their genes.

>>> chromosome_1 = ga.make_chromosome([0, 1, 4, 9, 16])
>>> chromosome_2 = ga.make_chromosome(n*n for n in range(5))
>>> chromosome_1 == chromosome_2
True

Str/Repr

The str and repr methods allow for conversion of chromosomes to strings.

>>> chromosome = ga.make_chromosome([0, 1, 4, 9, 16])
>>> print(chromosome)
[0][1][4][9][16]
>>> str(chromosome)
'[0][1][4][9][16]'
>>> chromosome
EasyGA.make_chromosome([EasyGA.make_gene(0), EasyGA.make_gene(1), EasyGA.make_gene(4), EasyGA.make_gene(9), EasyGA.make_gene(16)])
>>> repr(chromosome)
'EasyGA.make_chromosome([EasyGA.make_gene(0), EasyGA.make_gene(1), EasyGA.make_gene(4), EasyGA.make_gene(9), EasyGA.make_gene(16)])'