From 31f5f25c36b2c193ee506234d873786a7d26e77e Mon Sep 17 00:00:00 2001 From: RyleyGG Date: Sun, 27 Sep 2020 17:46:17 -0400 Subject: [PATCH] Comment updates --- .../chromosome_structure/chromosome.py | 10 ++++++- src/initialization/gene_structure/gene.py | 7 +++++ .../population_structure/population.py | 29 ++++++++++--------- 3 files changed, 31 insertions(+), 15 deletions(-) diff --git a/src/initialization/chromosome_structure/chromosome.py b/src/initialization/chromosome_structure/chromosome.py index fecc278..e83c9fc 100644 --- a/src/initialization/chromosome_structure/chromosome.py +++ b/src/initialization/chromosome_structure/chromosome.py @@ -1,7 +1,7 @@ class chromosome: - # fitness = Empty; genes = [gene, gene, gene, etc.] def __init__(self, genes = None): + """Initialize the chromosome based on input gene list, defaulted to an empty list""" if genes is None: self.genes = [] else: @@ -9,29 +9,37 @@ class chromosome: self.fitness = None def add_gene(self, gene, index = -1): + """Add a gene to the chromosome at the specified index, defaulted to end of the chromosome""" if index == -1: index = len(self.genes) self.genes.insert(index, gene) def remove_gene(self, index): + """Remove a gene from the chromosome at the specified index""" del self.genes[index] def get_genes(self): + """Return all genes in the chromosome""" return self.genes def get_fitness(self): + """Return the fitness of the chromosome""" return self.fitness def set_gene(self, gene, index): + """Set a gene at a specific index""" self.genes[index] = gene def set_genes(self, genes): + """Set the entire gene set of the chromosome""" self.genes = genes def set_fitness(self, fitness): + """Set the fitness value of the chromosome""" self.fitness = fitness def __repr__(self): + """Format the repr() output for the chromosome""" output_str = '' for gene in self.genes: output_str += gene.__repr__() diff --git a/src/initialization/gene_structure/gene.py b/src/initialization/gene_structure/gene.py index ea3b547..598b6dc 100644 --- a/src/initialization/gene_structure/gene.py +++ b/src/initialization/gene_structure/gene.py @@ -4,21 +4,28 @@ def check_gene(value): return value class gene: + def __init__(self, value): + """Initialize a gene with fitness of value None and the input value""" self.fitness = None self.value = check_gene(value) def get_fitness(self): + """Return fitness of the gene""" return self.fitness def get_value(self): + """Return value of the gene""" return self.value def set_fitness(self, fitness): + """Set fitness of the gene""" self.fitness = fitness def set_value(self): + """Set value of the gene""" self.value = value def __repr__(self): + """Format the repr() output value""" return f'[{self.value}]' diff --git a/src/initialization/population_structure/population.py b/src/initialization/population_structure/population.py index f2167bc..18c7e38 100644 --- a/src/initialization/population_structure/population.py +++ b/src/initialization/population_structure/population.py @@ -1,7 +1,7 @@ class population: - # fitness = Empty; population = [chromosome, chromosome, etc.] def __init__(self, chromosomes = None): + """Intiialize the population with fitness of value None, and a set of chromosomes dependant on user-passed parameter""" if chromosomes is None: self.chromosomes = [] else: @@ -9,47 +9,48 @@ class population: self.fitness = None def get_closet_fitness(self,value): - # Get the chomosome that has the closets fitness to the value defined + """Get the chomosome that has the closets fitness to the value defined""" pass def add_chromosome(self, chromosome, index = -1): + """Adds a chromosome to the population at the input index, defaulted to the end of the chromosome set""" if index == -1: index = len(self.chromosomes) self.chromosomes.insert(index, chromosome) def remove_chromosome(self, index): + """removes a chromosome from the indicated index""" del self.chromosomes[index] def get_all_chromosomes(self): + """returns all chromosomes in the population""" return chromosomes def get_fitness(self): + """returns the population's fitness""" return self.fitness def set_all_chromosomes(self, chromosomes): + """sets the chromosome set of the population""" self.chromosomes = chromosomes - def set_chromosome(self, chromosomes, index): - self.chromosome[index] = chromosome + def set_chromosome(self, chromosome, index): + """sets a specific chromosome at a specific index""" + self.chromosomes[index] = chromosome def set_fitness(self, fitness): + """Sets the fitness value of the population""" self.fitness = fitness def __repr__(self): + """Sets the repr() output format""" return ''.join([chromosome.__repr__() for chromosome in self.chromosomes]) def print_all(self): - # Ex .Current population - # Chromosome 1 - [gene][gene][gene][.etc] / Chromosome fitness - # + """Prints information about the population in the following format:""" + """Ex .Current population""" + """Chromosome 1 - [gene][gene][gene][.etc] / Chromosome fitness - """ print("Current population:") for index in range(len(self.chromosomes)): print(f'Chromosome - {index} {self.chromosomes[index]}', end = "") print(f' / Fitness = {self.chromosomes[index].fitness}') - - def generate_first_chromosomes(self, chromosome_count, chromosome_length, gene_lower_bound, gene_upper_bound): - #Creating the chromosomes with Genes of random size - for x in range(chromosome_count): - chromosome = Chromosome(chromosome_length) - for y in range(chromosome_length): - chromosome.gene_set[y] = Gene(random.randint(gene_lower_bound[y], gene_upper_bound[y])) - self.chromosome_set.append(chromosome)