Merge branch 'master' of https://github.com/danielwilczak101/EasyGA
This commit is contained in:
@ -4,7 +4,7 @@ from initialization.chromosome_structure.chromosome import chromosome as create_
|
|||||||
from initialization.gene_structure.gene import gene as create_gene
|
from initialization.gene_structure.gene import gene as create_gene
|
||||||
|
|
||||||
# Import functions for defaults
|
# Import functions for defaults
|
||||||
from initialization.gene_creation.gene_random import random_gene
|
from initialization.gene_function.gene_random import random_gene
|
||||||
# Import functionality defaults
|
# Import functionality defaults
|
||||||
from initialization.random_initialization import random_initialization
|
from initialization.random_initialization import random_initialization
|
||||||
|
|
||||||
@ -25,7 +25,7 @@ class GA:
|
|||||||
# Defualt EastGA implimentation structure
|
# Defualt EastGA implimentation structure
|
||||||
self.gene_function_impl = random_gene
|
self.gene_function_impl = random_gene
|
||||||
# Set the GA Configuration
|
# Set the GA Configuration
|
||||||
self.initialization_impl = random_initialization()
|
self.initialization_impl = random_initialization
|
||||||
#self.mutation_impl = PerGeneMutation(Mutation_rate)
|
#self.mutation_impl = PerGeneMutation(Mutation_rate)
|
||||||
#self.selection_impl = TournamentSelection()
|
#self.selection_impl = TournamentSelection()
|
||||||
#self.crossover_impl = FastSinglePointCrossover()
|
#self.crossover_impl = FastSinglePointCrossover()
|
||||||
@ -35,7 +35,7 @@ class GA:
|
|||||||
|
|
||||||
def initialize(self):
|
def initialize(self):
|
||||||
# Create the first population
|
# Create the first population
|
||||||
self.population = self.initialization_impl.initialize(
|
self.population = self.initialization_impl(
|
||||||
self.population_size,
|
self.population_size,
|
||||||
self.chromosome_length,
|
self.chromosome_length,
|
||||||
self.gene_function_impl)
|
self.gene_function_impl)
|
||||||
|
|||||||
@ -1,22 +1,38 @@
|
|||||||
class chromosome:
|
class chromosome:
|
||||||
# fitness = Empty, genes = [gene,gene,gene,etc]
|
|
||||||
def __init__(self):
|
# fitness = Empty; genes = [gene, gene, gene, etc.]
|
||||||
|
def __init__(self, genes = None):
|
||||||
|
if genes is None:
|
||||||
|
self.genes = []
|
||||||
|
else:
|
||||||
|
self.genes = genes
|
||||||
self.fitness = None
|
self.fitness = None
|
||||||
self.genes = []
|
|
||||||
|
|
||||||
def add_gene(self,gene):
|
def add_gene(self, gene, index = -1):
|
||||||
self.genes.append(gene)
|
if index == -1:
|
||||||
|
index = len(self.genes)
|
||||||
|
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):
|
def get_fitness(self):
|
||||||
return self.fitness
|
return self.fitness
|
||||||
|
|
||||||
def get_chromosome(self):
|
def set_gene(self, gene, index):
|
||||||
return self.genes
|
self.genes[index] = gene
|
||||||
|
|
||||||
def print_chromosome(self):
|
def set_genes(self, genes):
|
||||||
for i in range(len(self.genes)):
|
self.genes = genes
|
||||||
# Print the gene one by one.
|
|
||||||
if(i == len(self.genes) - 1):
|
def set_fitness(self, fitness):
|
||||||
print(f"[{self.genes[i].get_value()}]")
|
self.fitness = fitness
|
||||||
else:
|
|
||||||
print(f"[{self.genes[i].get_value()}],", end = '')
|
def __repr__(self):
|
||||||
|
output_str = ''
|
||||||
|
for gene in self.genes:
|
||||||
|
output_str += gene.__repr__()
|
||||||
|
return output_str
|
||||||
|
|||||||
@ -4,22 +4,21 @@ def check_gene(value):
|
|||||||
return value
|
return value
|
||||||
|
|
||||||
class gene:
|
class gene:
|
||||||
# fitness = Empty, value = Define by gene function
|
|
||||||
def __init__(self, value):
|
def __init__(self, value):
|
||||||
self.fitness = None
|
self.fitness = None
|
||||||
self.value = check_gene(value)
|
self.value = check_gene(value)
|
||||||
|
|
||||||
def set_fitness(self,fitness):
|
|
||||||
self.fitness = fitness
|
|
||||||
|
|
||||||
def get_fitness(self):
|
def get_fitness(self):
|
||||||
return self.fitness
|
return self.fitness
|
||||||
|
|
||||||
def get_value(self):
|
def get_value(self):
|
||||||
return self.value
|
return self.value
|
||||||
|
|
||||||
def print_value(self):
|
def set_fitness(self, fitness):
|
||||||
print(self.value)
|
self.fitness = fitness
|
||||||
|
|
||||||
def print_fitness(self):
|
def set_value(self):
|
||||||
print(self.fitness)
|
self.value = value
|
||||||
|
|
||||||
|
def __repr__(self):
|
||||||
|
return f'[{self.value}]'
|
||||||
|
|||||||
@ -1,8 +1,55 @@
|
|||||||
class population:
|
class population:
|
||||||
# population = [chromosome,chromosome,etc]
|
|
||||||
def __init__(self):
|
# fitness = Empty; population = [chromosome, chromosome, etc.]
|
||||||
|
def __init__(self, chromosomes = None):
|
||||||
|
if chromosomes is None:
|
||||||
|
self.chromosomes = []
|
||||||
|
else:
|
||||||
|
self.chromosomes = chromosomes
|
||||||
self.fitness = None
|
self.fitness = None
|
||||||
self.chromosomes = []
|
|
||||||
|
def get_closet_fitness(self,value):
|
||||||
def add_chromosome(self,chromosome):
|
# Get the chomosome that has the closets fitness to the value defined
|
||||||
self.chromosomes.append(chromosome)
|
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):
|
||||||
|
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):
|
||||||
|
pass
|
||||||
|
|
||||||
|
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)
|
||||||
|
|||||||
@ -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,14 @@ from .population_structure.population import population as create_population
|
|||||||
from .chromosome_structure.chromosome import chromosome as create_chromosome
|
from .chromosome_structure.chromosome import chromosome as create_chromosome
|
||||||
from .gene_structure.gene import gene as create_gene
|
from .gene_structure.gene import gene as create_gene
|
||||||
|
|
||||||
class random_initialization:
|
def random_initialization(chromosome_length,population_size,gene_function):
|
||||||
def initialize(self,chromosome_length,population_size,gene_function):
|
# Create the population object
|
||||||
# I dont understand why python needs this in its scope but it does.
|
population = create_population()
|
||||||
global population
|
# Fill the population with chromosomes
|
||||||
global chromosome
|
for i in range(population_size):
|
||||||
global gene
|
chromosome = create_chromosome()
|
||||||
# Create the population object
|
#Fill the Chromosome with genes
|
||||||
population = create_population()
|
for j in range(chromosome_length):
|
||||||
# Fill the population with chromosomes
|
chromosome.add_gene(create_gene(gene_function()))
|
||||||
for i in range(population_size):
|
population.add_chromosome(chromosome)
|
||||||
chromosome = create_chromosome()
|
return population
|
||||||
#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,9 +11,9 @@ new_chromosome = ga.make_chromosome()
|
|||||||
# Makes a Population to store chromosomes in
|
# Makes a Population to store chromosomes in
|
||||||
new_population = ga.make_population()
|
new_population = ga.make_population()
|
||||||
|
|
||||||
|
# Creating population
|
||||||
ga.initialize()
|
ga.initialize()
|
||||||
|
|
||||||
print(ga.population)
|
ga.population.print_all()
|
||||||
|
print("")
|
||||||
for chromosome in ga.population.chromosomes:
|
print(ga.population.chromosomes[0].__repr__())
|
||||||
print(chromosome.genes[0].__dict__)
|
|
||||||
|
|||||||
Reference in New Issue
Block a user