diff --git a/EasyGA/structure/chromosome.py b/EasyGA/structure/chromosome.py index a0cbc38..bed2c49 100644 --- a/EasyGA/structure/chromosome.py +++ b/EasyGA/structure/chromosome.py @@ -112,6 +112,11 @@ class Chromosome(): return self.gene_list == chromosome.gene_list + def __hash__(self): + """Hash chromosomes by genes so they can be used in sets/dictionaries.""" + return hash(tuple(self)) + + def __add__(self, chromosome): """Return self + chromosome, a chromosome made by concatenating the genes.""" return Chromosome(chain(self, chromosome)) diff --git a/EasyGA/structure/gene.py b/EasyGA/structure/gene.py index cf67e70..1e44b59 100644 --- a/EasyGA/structure/gene.py +++ b/EasyGA/structure/gene.py @@ -19,6 +19,11 @@ class Gene: return self.value == Gene(other_gene).value + def __hash__(self): + """Hash genes by value so that they can be used in sets/dictionaries.""" + return hash(self.value) + + def __repr__(self): """ Allows the user to use