Major structural changes
This commit is contained in:
@ -1,15 +1,11 @@
|
|||||||
# Import all the data structure onjects
|
# Import all the data prebuilt modules
|
||||||
from initialization.gene import gene
|
from initialization.gene_creation.gene_random import random_gene
|
||||||
from initialization.chromosome import chromosome
|
|
||||||
from initialization.population import population
|
|
||||||
|
|
||||||
|
|
||||||
from initialization.random_gene import random_gene
|
|
||||||
from initialization.random_initialization import random_initialization
|
from initialization.random_initialization import random_initialization
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
class GA:
|
class GA:
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
|
|
||||||
# Default variables
|
# Default variables
|
||||||
self.population = None
|
self.population = None
|
||||||
self.generations = 3
|
self.generations = 3
|
||||||
@ -17,7 +13,7 @@ class GA:
|
|||||||
self.population_size = 5
|
self.population_size = 5
|
||||||
self.mutation_rate = 0.03
|
self.mutation_rate = 0.03
|
||||||
# Defualt EastGA implimentation structure
|
# Defualt EastGA implimentation structure
|
||||||
self.gene_function_impl = random_gene(1,100)
|
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)
|
||||||
|
|||||||
@ -9,7 +9,5 @@ def check_values(low,high):
|
|||||||
assert high > low , "High value can not be smaller then low value"
|
assert high > low , "High value can not be smaller then low value"
|
||||||
assert high != 0, "High value can not be zero"
|
assert high != 0, "High value can not be zero"
|
||||||
|
|
||||||
def random_gene(low,high):
|
def random_gene():
|
||||||
# Check the values so that they follow the rules.
|
|
||||||
check_values(low,high)
|
|
||||||
return random.randint(1,100)
|
return random.randint(1,100)
|
||||||
@ -1,3 +0,0 @@
|
|||||||
class initialization:
|
|
||||||
def initialize(self,chromosome_length,population_size,gene_function):
|
|
||||||
return [] # return an array of chromosomes for generation 0
|
|
||||||
@ -1,8 +1,8 @@
|
|||||||
class population:
|
class population:
|
||||||
# population = [chromosome,chromosome,etc]
|
# population = [chromosome,chromosome,etc]
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.chromosomes = []
|
|
||||||
self.fitness = None
|
self.fitness = None
|
||||||
|
self.chromosomes = []
|
||||||
|
|
||||||
def add_chromosome(self,chromosome):
|
def add_chromosome(self,chromosome):
|
||||||
self.chromosomes.append(chromosome)
|
self.chromosomes.append(chromosome)
|
||||||
@ -1,20 +1,23 @@
|
|||||||
from initialization.population import population
|
# Import the data structure
|
||||||
from initialization.chromosome import chromosome
|
from .population_structure.population import population as create_population
|
||||||
from initialization.gene import gene
|
from .chromosome_structure.chromosome import chromosome as create_chromosome
|
||||||
|
from .gene_structure.gene import gene as create_gene
|
||||||
|
|
||||||
from initialization.initialization import initialization
|
from .initialization import initialization
|
||||||
|
|
||||||
class random_initialization(initialization):
|
class random_initialization:
|
||||||
def initialize(self,chromosome_length,population_size,gene_function):
|
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
|
# Create the population object
|
||||||
population = population.population()
|
population = create_population()
|
||||||
# Fill the population with chromosomes
|
# Fill the population with chromosomes
|
||||||
for i in range(population_size):
|
for i in range(population_size):
|
||||||
chromosome = chromosome.chromosome()
|
chromosome = create_chromosome()
|
||||||
#Fill the Chromosome with genes
|
#Fill the Chromosome with genes
|
||||||
for j in range(chromosome_length):
|
for j in range(chromosome_length):
|
||||||
gene = gene.gene(gene_function)
|
chromosome.add_gene(create_gene(gene_function()))
|
||||||
chromosome.add_gene(gene)
|
|
||||||
|
|
||||||
population.add_chromosome(chromosome)
|
population.add_chromosome(chromosome)
|
||||||
return population
|
return population
|
||||||
|
|||||||
@ -4,6 +4,8 @@ import EasyGA
|
|||||||
# Create the Genetic algorithm
|
# Create the Genetic algorithm
|
||||||
ga = EasyGA.GA()
|
ga = EasyGA.GA()
|
||||||
|
|
||||||
|
# Start the population
|
||||||
ga.initialize()
|
ga.initialize()
|
||||||
|
|
||||||
ga.population.chromosome[0].print_chromosome()
|
for chromosome in ga.population.chromosomes:
|
||||||
|
print(chromosome.genes[0].__dict__)
|
||||||
|
|||||||
Reference in New Issue
Block a user