From cd0054b3d0ae76ae9a1cd8eb66ac52c875a8ec56 Mon Sep 17 00:00:00 2001 From: SimpleArt <71458112+SimpleArt@users.noreply.github.com> Date: Thu, 10 Jun 2021 21:56:21 -0400 Subject: [PATCH] Implemented __hash__ --- EasyGA/structure/chromosome.py | 11 +++++++++++ EasyGA/structure/population.py | 11 +++++++++++ 2 files changed, 22 insertions(+) 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