diff --git a/src/structure/chromosome.py b/src/structure/chromosome.py index 9d82d59..89b0b22 100644 --- a/src/structure/chromosome.py +++ b/src/structure/chromosome.py @@ -13,40 +13,13 @@ class Chromosome: def add_gene(self, gene, index = None): """Add a gene to the chromosome at the specified index, defaulted to end of the chromosome""" if index is None: - index = self.size() + index = len(self) self.gene_list.insert(index, gene) def remove_gene(self, index): """Removes the gene at the given index""" - del self.gene_list[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 + return self.gene_list.pop(index) @property diff --git a/src/structure/gene.py b/src/structure/gene.py index 5a6600d..cc5ee81 100644 --- a/src/structure/gene.py +++ b/src/structure/gene.py @@ -8,26 +8,6 @@ class Gene: 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): """ Allows the user to use diff --git a/src/structure/population.py b/src/structure/population.py index f5172e4..82759cb 100644 --- a/src/structure/population.py +++ b/src/structure/population.py @@ -15,11 +15,21 @@ class Population: def update(self): """Sets all the population variables to what they should be at the end of the generation """ - self.set_chromosome_list(self.next_population) + self.chromosome_list = self.next_population self.reset_mating_pool() 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): """Removes and returns a chromosome from the indicated index from the population""" return self.chromosome_list.pop(index) @@ -35,16 +45,6 @@ class Population: 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): """Appends a list of chromosomes to the next population""" self.next_population += chromosome_list @@ -52,24 +52,7 @@ class Population: def sort_by_best_fitness(self, ga): """Sorts the population by fitness""" - self.set_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 + self.chromosome_list = ga.sort_by_best_fitness(self.chromosome_list) def add_chromosome(self, chromosome, index = None): @@ -77,7 +60,7 @@ class Population: defaulted to the end of the chromosome set""" if index is None: - index = self.size() + index = len(self) self.chromosome_list.insert(index, chromosome) @@ -91,64 +74,9 @@ class Population: 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): """Sets the indexed chromosome from the population as a parent""" - self.add_parent(self.get_chromosome(index)) - - - def set_fitness(self, fitness): - """Sets the fitness value of the population""" - self.fitness = fitness + self.add_parent(self[index]) def __iter__(self):