Files
EasyGA/src/initialization/population_structure/population.py
RyleyGG e05aa7f62b Changed implementation framework
Instead of a nested approach, selection/crossover/mutation are all called separately and directly by the GA. selection_impl was also separated into parent_selection_impl and survivor_selection_impl, as both are needed separately.
2020-10-04 17:59:59 -04:00

60 lines
2.1 KiB
Python

class Population:
# fitness = Empty; population = [chromosome, chromosome, etc.]
def __init__(self, chromosomes = None):
if chromosomes is None:
self.chromosomes = []
else:
self.chromosomes = chromosomes
self.fitness = None
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 = -1):
if index == -1:
index = len(self.chromosomes)
self.chromosomes.insert(index, chromosome)
def remove_chromosome(self, index):
del self.chromosomes[index]
def get_all_chromosomes(self):
"""returns all chromosomes in the population"""
return self.chromosomes
def get_fitness(self):
return self.fitness
def set_all_chromosomes(self, chromosomes):
self.chromosomes = chromosomes
def set_chromosome(self, chromosome, index = -1):
if index == -1:
index = len(self.chromosomes)-1
self.chromosomes[index] = chromosome
def set_fitness(self, fitness):
self.fitness = fitness
def __repr__(self):
for index in range(len(self.chromosomes)):
return f'{self.chromosomes[index]}'
def print_all(self):
# 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)