diff --git a/EasyGA/structure/chromosome.py b/EasyGA/structure/chromosome.py index bed2c49..7806821 100644 --- a/EasyGA/structure/chromosome.py +++ b/EasyGA/structure/chromosome.py @@ -107,6 +107,17 @@ class Chromosome(): return (to_gene(gene) in self.gene_list) + def __hash__(self): + """ + Returns hash(self). + Allows the user to use + {chromosome} + {chromosome: x} + or any other thing requiring hashes with chromosomes. + """ + return hash(tuple(self)) + + def __eq__(self, chromosome): """Returns self == chromosome, True if all genes match.""" return self.gene_list == chromosome.gene_list diff --git a/EasyGA/structure/population.py b/EasyGA/structure/population.py index 5369ca4..abb2822 100644 --- a/EasyGA/structure/population.py +++ b/EasyGA/structure/population.py @@ -159,6 +159,17 @@ class Population: return (to_chromosome(chromosome) in self.chromosome_list) + def __hash__(self): + """ + Returns hash(self). + Allows the user to use + {population} + {population: x} + or any other thing requiring hashes with populations. + """ + return hash(tuple(self)) + + def __eq__(self, population): """Returns self == population, True if all chromosomes match.""" return self.chromosome_list == population.chromosome_list