Fixed all jacks code
This commit is contained in:
@ -25,7 +25,7 @@ class GA:
|
||||
# Defualt EastGA implimentation structure
|
||||
self.gene_function_impl = random_gene
|
||||
# Set the GA Configuration
|
||||
self.initialization_impl = random_initialization()
|
||||
self.initialization_impl = random_initialization
|
||||
#self.mutation_impl = PerGeneMutation(Mutation_rate)
|
||||
#self.selection_impl = TournamentSelection()
|
||||
#self.crossover_impl = FastSinglePointCrossover()
|
||||
@ -35,7 +35,7 @@ class GA:
|
||||
|
||||
def initialize(self):
|
||||
# Create the first population
|
||||
self.population = self.initialization_impl.initialize(
|
||||
self.population = self.initialization_impl(
|
||||
self.population_size,
|
||||
self.chromosome_length,
|
||||
self.gene_function_impl)
|
||||
|
||||
@ -1,22 +1,31 @@
|
||||
class chromosome:
|
||||
# fitness = Empty, genes = [gene,gene,gene,etc]
|
||||
def __init__(self):
|
||||
def __init__(self, genes = []):
|
||||
self.genes = genes
|
||||
self.fitness = None
|
||||
self.genes = []
|
||||
|
||||
def add_gene(self,gene):
|
||||
self.genes.append(gene)
|
||||
def add_gene(self, gene, index = -1):
|
||||
if index == -1:
|
||||
index = len(self.genes) - 1
|
||||
self.genes.insert(index, gene)
|
||||
|
||||
def remove_gene(self, index):
|
||||
del self.genes[index]
|
||||
|
||||
def get_genes(self):
|
||||
return self.genes
|
||||
|
||||
def get_fitness(self):
|
||||
return self.fitness
|
||||
|
||||
def get_chromosome(self):
|
||||
return self.genes
|
||||
def set_gene(self, gene, index):
|
||||
self.genes[index] = gene
|
||||
|
||||
def print_chromosome(self):
|
||||
for i in range(len(self.genes)):
|
||||
# Print the gene one by one.
|
||||
if(i == len(self.genes) - 1):
|
||||
print(f"[{self.genes[i].get_value()}]")
|
||||
else:
|
||||
print(f"[{self.genes[i].get_value()}],", end = '')
|
||||
def set_genes(self, genes):
|
||||
self.genes = genes
|
||||
|
||||
def set_fitness(self, fitness):
|
||||
self.fitness = fitness
|
||||
|
||||
def __repr__(self):
|
||||
return f"chromosome({self.genes.__repr__()})"
|
||||
|
||||
@ -4,22 +4,21 @@ def check_gene(value):
|
||||
return value
|
||||
|
||||
class gene:
|
||||
# fitness = Empty, value = Define by gene function
|
||||
def __init__(self, value):
|
||||
self.fitness = None
|
||||
self.value = check_gene(value)
|
||||
|
||||
def set_fitness(self,fitness):
|
||||
self.fitness = fitness
|
||||
|
||||
def get_fitness(self):
|
||||
return self.fitness
|
||||
|
||||
def get_value(self):
|
||||
return self.value
|
||||
|
||||
def print_value(self):
|
||||
print(self.value)
|
||||
def set_fitness(self, fitness):
|
||||
self.fitness = fitness
|
||||
|
||||
def print_fitness(self):
|
||||
print(self.fitness)
|
||||
def set_value(self):
|
||||
self.value = value
|
||||
|
||||
def __repr__(self):
|
||||
return f"Gene({self.value} - Fitness{self.fitness})"
|
||||
|
||||
@ -1,8 +1,43 @@
|
||||
class population:
|
||||
# population = [chromosome,chromosome,etc]
|
||||
def __init__(self):
|
||||
def __init__(self, chromosomes = []):
|
||||
self.chromosomes = chromosomes
|
||||
self.fitness = None
|
||||
self.chromosomes = []
|
||||
|
||||
def add_chromosome(self,chromosome):
|
||||
self.chromosomes.append(chromosome)
|
||||
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) - 1
|
||||
self.chromosomes.insert(index, chromosome)
|
||||
|
||||
def remove_chromosome(self, index):
|
||||
del self.chromosomes[index]
|
||||
|
||||
def get_all_chromosomes(self):
|
||||
return chromosomes
|
||||
|
||||
def get_fitness(self):
|
||||
return self.fitness
|
||||
|
||||
def set_all_chromosomes(self, chromosomes):
|
||||
self.chromosomes = chromosomes
|
||||
|
||||
def set_chromosome(self, chromosomes, index):
|
||||
self.chromosome[index] = chromosome
|
||||
|
||||
def set_fitness(self, fitness):
|
||||
self.fitness = fitness
|
||||
|
||||
def __repr__(self):
|
||||
return f"population({self.chromosomes.__repr__()})"
|
||||
|
||||
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)
|
||||
|
||||
@ -1,14 +0,0 @@
|
||||
from population import population
|
||||
from chromosome import chromosome
|
||||
from gene import gene
|
||||
|
||||
population = population()
|
||||
# Fill the population with chromosomes
|
||||
for i in range(population_size):
|
||||
chromosome = chromosome()
|
||||
#Fill the Chromosome with genes
|
||||
for j in range(chromosome_length):
|
||||
gene = gene(gene_function)
|
||||
chromosome.add_gene(gene)
|
||||
|
||||
population.add_chromosome(chromosome)
|
||||
|
||||
@ -3,19 +3,18 @@ from .population_structure.population import population as create_population
|
||||
from .chromosome_structure.chromosome import chromosome as create_chromosome
|
||||
from .gene_structure.gene import gene as create_gene
|
||||
|
||||
class random_initialization:
|
||||
def initialize(self,chromosome_length,population_size,gene_function):
|
||||
# I dont understand why python needs this in its scope but it does.
|
||||
global population
|
||||
global chromosome
|
||||
global gene
|
||||
# Create the population object
|
||||
population = create_population()
|
||||
# Fill the population with chromosomes
|
||||
for i in range(population_size):
|
||||
chromosome = create_chromosome()
|
||||
#Fill the Chromosome with genes
|
||||
for j in range(chromosome_length):
|
||||
chromosome.add_gene(create_gene(gene_function()))
|
||||
population.add_chromosome(chromosome)
|
||||
return population
|
||||
def random_initialization(chromosome_length,population_size,gene_function):
|
||||
# I dont understand why python needs this in its scope but it does.
|
||||
global population
|
||||
global chromosome
|
||||
global gene
|
||||
# Create the population object
|
||||
population = create_population()
|
||||
# Fill the population with chromosomes
|
||||
for i in range(population_size):
|
||||
chromosome = create_chromosome()
|
||||
#Fill the Chromosome with genes
|
||||
for j in range(chromosome_length):
|
||||
chromosome.add_gene(create_gene(gene_function()))
|
||||
population.add_chromosome(chromosome)
|
||||
return population
|
||||
|
||||
@ -11,6 +11,7 @@ new_chromosome = ga.make_chromosome()
|
||||
# Makes a Population to store chromosomes in
|
||||
new_population = ga.make_population()
|
||||
|
||||
# Creating population
|
||||
ga.initialize()
|
||||
|
||||
print(ga.population)
|
||||
|
||||
Reference in New Issue
Block a user