@ -1,10 +1,9 @@
|
||||
import random
|
||||
# Import all the data prebuilt modules
|
||||
from initialization.population_structure.population import population as create_population
|
||||
from initialization.chromosome_structure.chromosome import chromosome as create_chromosome
|
||||
from initialization.gene_structure.gene import gene as create_gene
|
||||
|
||||
# Import functions for defaults
|
||||
from initialization.gene_function.gene_random import random_gene
|
||||
# Import functionality defaults
|
||||
from initialization.random_initialization import random_initialization
|
||||
|
||||
@ -12,14 +11,14 @@ from initialization.random_initialization import random_initialization
|
||||
class GA:
|
||||
def __init__(self):
|
||||
# Default variables
|
||||
self.domain = None
|
||||
self.new_range = None
|
||||
self.population = None
|
||||
self.generations = 3
|
||||
self.chromosome_length = 4
|
||||
self.chromosome_length = 3
|
||||
self.population_size = 5
|
||||
self.mutation_rate = 0.03
|
||||
# Defualt EastGA implimentation structure
|
||||
self.gene_function_impl = random_gene
|
||||
# Set the GA Configuration
|
||||
self.initialization_impl = random_initialization
|
||||
#self.mutation_impl = PerGeneMutation(Mutation_rate)
|
||||
#self.selection_impl = TournamentSelection()
|
||||
@ -27,13 +26,12 @@ class GA:
|
||||
#self.termination_impl = GenerationTermination(Total_generations)
|
||||
#self.evaluation_impl = TestEvaluation()
|
||||
|
||||
|
||||
def initialize(self):
|
||||
# Create the first population
|
||||
self.population = self.initialization_impl(
|
||||
self.population_size,
|
||||
self.chromosome_length,
|
||||
self.gene_function_impl)
|
||||
self.domain,
|
||||
self.new_range)
|
||||
|
||||
def evolve():
|
||||
# If you just want to evolve through all generations
|
||||
|
||||
@ -1,13 +0,0 @@
|
||||
# Imported library
|
||||
import random
|
||||
|
||||
def check_values(low,high):
|
||||
#Check to make sure its not less then zero
|
||||
assert low > 0 , "The random gene low can not be less then zero"
|
||||
# Check to make sure the high value is not
|
||||
# lower than or equal to low and not 0.
|
||||
assert high > low , "High value can not be smaller then low value"
|
||||
assert high != 0, "High value can not be zero"
|
||||
|
||||
def random_gene():
|
||||
return random.randint(1,100)
|
||||
@ -36,8 +36,7 @@ class population:
|
||||
self.fitness = fitness
|
||||
|
||||
def __repr__(self):
|
||||
for index in range(len(self.chromosomes)):
|
||||
return f'{self.chromosomes[index]}'
|
||||
return ''.join([chromosome.__repr__() for chromosome in self.chromosomes])
|
||||
|
||||
def print_all(self):
|
||||
# Ex .Current population
|
||||
|
||||
@ -3,7 +3,7 @@ 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
|
||||
|
||||
def random_initialization(chromosome_length,population_size,gene_function):
|
||||
def random_initialization(population_size, chromosome_length, domain, new_range):
|
||||
# Create the population object
|
||||
population = create_population()
|
||||
# Fill the population with chromosomes
|
||||
@ -11,6 +11,16 @@ def random_initialization(chromosome_length,population_size,gene_function):
|
||||
chromosome = create_chromosome()
|
||||
#Fill the Chromosome with genes
|
||||
for j in range(chromosome_length):
|
||||
chromosome.add_gene(create_gene(gene_function()))
|
||||
if domain != None:
|
||||
# Each chromosome location is specified with its own function
|
||||
chromosome.add_gene(create_gene(domain(j)))
|
||||
# Will break if chromosome_length != lists in domain
|
||||
elif new_range != None:
|
||||
# new_rnage = [range function,lowerbound,upperbound]
|
||||
function = new_range[0]
|
||||
chromosome.add_gene(create_gene(function(new_range[1],new_range[2])))
|
||||
else:
|
||||
#Exit because either were not specified
|
||||
print("Your domain or range were not specified")
|
||||
population.add_chromosome(chromosome)
|
||||
return population
|
||||
|
||||
@ -4,13 +4,23 @@ import random
|
||||
# Create the Genetic algorithm
|
||||
ga = EasyGA.GA()
|
||||
|
||||
def user_defined_gene():
|
||||
return random.choice(["left","right","up","down"])
|
||||
def user_gene_domain(gene_index):
|
||||
"""Each gene index is assosiated to its index in the chromosome"""
|
||||
domain = [
|
||||
random.randrange(1,100,5),
|
||||
random.uniform(10,5),
|
||||
random.choice(["up","down"])
|
||||
]
|
||||
return domain[gene_index]
|
||||
|
||||
ga.gene_function_impl = user_defined_gene
|
||||
print(user_gene_domain(0))
|
||||
|
||||
# If the user wants to use a domain
|
||||
ga.domain = user_gene_domain
|
||||
# If the user wants to use a custom range
|
||||
#ga.new_range = [random.randrange,1,100,None]
|
||||
|
||||
# Creating population
|
||||
ga.initialize()
|
||||
|
||||
# Print the current population
|
||||
ga.population.print_all()
|
||||
#ga.population.print_all()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user