From 8e0437bf4e80879312a16d17cc024dbdf5f19fc5 Mon Sep 17 00:00:00 2001 From: SimpleArt <71458112+SimpleArt@users.noreply.github.com> Date: Mon, 30 Nov 2020 14:12:36 -0500 Subject: [PATCH] Cleaned up to allow any iterable input --- src/structure/chromosome.py | 4 ++-- src/structure/population.py | 7 +++++-- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/src/structure/chromosome.py b/src/structure/chromosome.py index dcf8dbf..fba5a75 100644 --- a/src/structure/chromosome.py +++ b/src/structure/chromosome.py @@ -2,11 +2,11 @@ from copy import deepcopy class Chromosome: - def __init__(self, gene_list = []): + def __init__(self, gene_list): """Initialize the chromosome with fitness value of None, and a set of genes dependent on user-passed parameter.""" - self.gene_list = deepcopy(gene_list) + self.gene_list = [deepcopy(gene) for gene in gene_list] self.fitness = None diff --git a/src/structure/population.py b/src/structure/population.py index 6ab2155..a89d5b1 100644 --- a/src/structure/population.py +++ b/src/structure/population.py @@ -2,11 +2,11 @@ from copy import deepcopy class Population: - def __init__(self, chromosome_list = []): + def __init__(self, chromosome_list): """Initialize the population with fitness of value None, and a set of chromosomes dependant on user-passed parameter.""" - self.chromosome_list = deepcopy(chromosome_list) + self.chromosome_list = [deepcopy(chromosome) for chromosome in chromosome_list] self.fitness = None self.mating_pool = [] self.next_population = [] @@ -50,6 +50,9 @@ class Population: Appends to the front so that chromosomes with fitness values already will stay sorted. """ + + if not isinstance(chromosome_list, list): + chromosome_list = list(chromosome_list) self.next_population = chromosome_list + self.next_population