Removed get/set

This commit is contained in:
SimpleArt
2020-11-22 15:58:11 -05:00
parent 8ed3cbe0ee
commit cdc2b6bdfb
3 changed files with 16 additions and 135 deletions

View File

@ -13,40 +13,13 @@ class Chromosome:
def add_gene(self, gene, index = None): def add_gene(self, gene, index = None):
"""Add a gene to the chromosome at the specified index, defaulted to end of the chromosome""" """Add a gene to the chromosome at the specified index, defaulted to end of the chromosome"""
if index is None: if index is None:
index = self.size() index = len(self)
self.gene_list.insert(index, gene) self.gene_list.insert(index, gene)
def remove_gene(self, index): def remove_gene(self, index):
"""Removes the gene at the given index""" """Removes the gene at the given index"""
del self.gene_list[index] return self.gene_list.pop(index)
def get_gene(self, index):
"""Returns the gene at the given index"""
return self.gene_list[index]
def get_gene_list(self):
return self.gene_list
def get_fitness(self):
"""Return the fitness of the chromosome"""
return self.fitness
def set_gene(self, gene, index):
self.gene_list[index] = gene
def set_gene_list(self, gene_list):
self.gene_list = gene_list
def set_fitness(self, fitness):
"""Set the fitness value of the chromosome"""
self.fitness = fitness
@property @property

View File

@ -8,26 +8,6 @@ class Gene:
self.fitness = None self.fitness = None
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, value):
"""Set value of the gene"""
self.value = value
def __repr__(self): def __repr__(self):
""" """
Allows the user to use Allows the user to use

View File

@ -15,11 +15,21 @@ class Population:
def update(self): def update(self):
"""Sets all the population variables to what they should be at """Sets all the population variables to what they should be at
the end of the generation """ the end of the generation """
self.set_chromosome_list(self.next_population) self.chromosome_list = self.next_population
self.reset_mating_pool() self.reset_mating_pool()
self.reset_next_population() self.reset_next_population()
def reset_mating_pool(self):
"""Clears the mating pool"""
self.mating_pool = []
def reset_next_population(self):
"""Clears the next population"""
self.next_population = []
def remove_chromosome(self, index): def remove_chromosome(self, index):
"""Removes and returns a chromosome from the indicated index from the population""" """Removes and returns a chromosome from the indicated index from the population"""
return self.chromosome_list.pop(index) return self.chromosome_list.pop(index)
@ -35,16 +45,6 @@ class Population:
return self.next_population.pop(index) return self.next_population.pop(index)
def reset_mating_pool(self):
"""Clears the mating pool"""
self.mating_pool = []
def reset_next_population(self):
"""Clears the next population"""
self.next_population = []
def append_children(self, chromosome_list): def append_children(self, chromosome_list):
"""Appends a list of chromosomes to the next population""" """Appends a list of chromosomes to the next population"""
self.next_population += chromosome_list self.next_population += chromosome_list
@ -52,24 +52,7 @@ class Population:
def sort_by_best_fitness(self, ga): def sort_by_best_fitness(self, ga):
"""Sorts the population by fitness""" """Sorts the population by fitness"""
self.set_chromosome_list(ga.sort_by_best_fitness(self.chromosome_list)) self.chromosome_list = ga.sort_by_best_fitness(self.chromosome_list)
@property
def total_children(self):
"""Returns the size of the next population"""
return len(self.next_population)
@property
def total_parents(self):
"""Returns the size of the mating pool"""
return len(self.mating_pool)
def get_closet_fitness(self, value):
"""Get the chomosome that has the closets fitness to the value defined"""
pass
def add_chromosome(self, chromosome, index = None): def add_chromosome(self, chromosome, index = None):
@ -77,7 +60,7 @@ class Population:
defaulted to the end of the chromosome set""" defaulted to the end of the chromosome set"""
if index is None: if index is None:
index = self.size() index = len(self)
self.chromosome_list.insert(index, chromosome) self.chromosome_list.insert(index, chromosome)
@ -91,64 +74,9 @@ class Population:
self.next_population.append(chromosome) self.next_population.append(chromosome)
def get_chromosome(self, index):
"""Returns the chromosome at the given index in the population"""
return self.chromosome_list[index]
def get_parent(self, index):
"""Returns the parent at the given index in the mating pool"""
return self.mating_pool[index]
def get_child(self, index):
"""Returns the child at the given index in the next population"""
return self.next_population[index]
def get_chromosome_list(self):
"""Returns all chromosomes in the population"""
return self.chromosome_list
def get_mating_pool(self):
"""Returns chromosomes in the mating pool"""
return self.mating_pool
def get_next_population(self):
"""Returns chromosomes in the next population"""
return self.next_population
def get_fitness(self):
"""Returns the population's fitness"""
return self.fitness
def set_chromosome_list(self, chromosome_list):
"""Sets the chromosome list"""
self.chromosome_list = chromosome_list
def set_mating_pool(self, chromosome_list):
"""Sets entire mating pool"""
self.mating_pool = chromosome_list
def set_chromosome(self, chromosome, index):
"""Sets the chromosome at the given index"""
self.chromosome_list[index] = chromosome
def set_parent(self, index): def set_parent(self, index):
"""Sets the indexed chromosome from the population as a parent""" """Sets the indexed chromosome from the population as a parent"""
self.add_parent(self.get_chromosome(index)) self.add_parent(self[index])
def set_fitness(self, fitness):
"""Sets the fitness value of the population"""
self.fitness = fitness
def __iter__(self): def __iter__(self):