From e12287cdcd004daf76c8aa203bb71dfec0fa1846 Mon Sep 17 00:00:00 2001 From: SimpleArt <71458112+SimpleArt@users.noreply.github.com> Date: Sun, 7 Mar 2021 14:18:13 -0500 Subject: [PATCH] Added hashing methods for sets/dictionaries. --- EasyGA/structure/chromosome.py | 5 +++++ EasyGA/structure/gene.py | 5 +++++ 2 files changed, 10 insertions(+) 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