Cleaned up to allow any iterable input

This commit is contained in:
SimpleArt
2020-11-30 14:12:36 -05:00
parent 27ca73711e
commit 8e0437bf4e
2 changed files with 7 additions and 4 deletions

View File

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

View File

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