Implemented __hash__

This commit is contained in:
SimpleArt
2021-06-10 21:56:21 -04:00
parent 2a26a4cc82
commit cd0054b3d0
2 changed files with 22 additions and 0 deletions

View File

@ -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

View File

@ -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