testing
This commit is contained in:
@ -1,36 +1,38 @@
|
||||
# Defult packages for GA functionality
|
||||
from initialization.random_initialization import random_initialization
|
||||
# Import all the data structure onjects
|
||||
from initialization.gene import gene
|
||||
from initialization.chromosome import chromosome
|
||||
from initialization.population import population
|
||||
|
||||
import random
|
||||
from defaults.defaults import defaults
|
||||
from gene.gene import gene
|
||||
|
||||
from initialization.random_gene import random_gene
|
||||
from initialization.random_initialization import random_initialization
|
||||
|
||||
class GA:
|
||||
def __init__(self):
|
||||
# Default variables
|
||||
self.gene = gene(defaults.default_gene_function())
|
||||
self.chromosome_length = defaults.chromosome_length
|
||||
# self.population_size = defaults.generations
|
||||
# self.chromosome_length = defaults.chromosome_length
|
||||
# self.generations = defaults.generations
|
||||
# # Defualt ga implimentation structure
|
||||
|
||||
# self.initialization = defaults.default_initialize()
|
||||
# self.mutation = defaults.default_mutations_function()
|
||||
# self.selection = defaults.default_selection_function()
|
||||
# self.crossover = defaults.default_crossover_function()
|
||||
# self.termination = defaults.default_termination_function(self.generations)
|
||||
# self.fitness_function = defaults.default_fitness_function()
|
||||
#
|
||||
# def initialize(self):
|
||||
# # Create the initial population
|
||||
# self.population = self.initialization.initialize(self.population_size,
|
||||
# self.chromosome_length,
|
||||
# self.user_gene_function)
|
||||
#
|
||||
# def evolve(self):
|
||||
# # Evolve will run all the functions
|
||||
# initialize()
|
||||
# Default variables
|
||||
self.population = None
|
||||
self.generations = 3
|
||||
self.chromosome_length = 4
|
||||
self.population_size = 5
|
||||
self.mutation_rate = 0.03
|
||||
# Defualt EastGA implimentation structure
|
||||
self.gene_function_impl = random_gene(1,100)
|
||||
# Set the GA Configuration
|
||||
self.initialization_impl = random_initialization()
|
||||
#self.mutation_impl = PerGeneMutation(Mutation_rate)
|
||||
#self.selection_impl = TournamentSelection()
|
||||
#self.crossover_impl = FastSinglePointCrossover()
|
||||
#self.termination_impl = GenerationTermination(Total_generations)
|
||||
#self.evaluation_impl = TestEvaluation()
|
||||
|
||||
|
||||
def initialize(self):
|
||||
# Create the first population
|
||||
self.population = self.initialization_impl.initialize(
|
||||
self.population_size,
|
||||
self.chromosome_length,
|
||||
self.gene_function_impl)
|
||||
|
||||
def evolve():
|
||||
# If you just want to evolve through all generations
|
||||
|
||||
44
src/defaults.py
Normal file
44
src/defaults.py
Normal file
@ -0,0 +1,44 @@
|
||||
|
||||
# Defult packages for GA functionality
|
||||
from create_gene.random_gene import random_gene
|
||||
from initialization.random_initialization import random_initialization
|
||||
|
||||
|
||||
|
||||
class defaults:
|
||||
# Defult values so that the user doesnt have to explicidly
|
||||
# state every feature of the genetic algorithm.
|
||||
def __init__(self):
|
||||
|
||||
|
||||
def gene_function(self):
|
||||
return random_gene(1,10)
|
||||
|
||||
def fitness_function(self):
|
||||
pass
|
||||
|
||||
def initialize_function(self):
|
||||
return random_initialization(population,chromosome,gene,
|
||||
chromosome_length,population_size,gene_function)
|
||||
|
||||
def selection_function(self):
|
||||
return tournament_selection()
|
||||
|
||||
def crossover_function(self):
|
||||
return fast_single_point_crossover()
|
||||
|
||||
def mutations_function(self):
|
||||
return per_gene_mutation()
|
||||
|
||||
def termination_point_function(self,amount):
|
||||
# The default termination point is based on how
|
||||
# many generations the user wants to run.
|
||||
return generation_termination(amount)
|
||||
|
||||
def get_highest_fitness(self):
|
||||
# Get the highest fitness of the current generation
|
||||
pass
|
||||
|
||||
def get_lowest_fitness(self):
|
||||
# Get the lowest fitness of the current generation
|
||||
pass
|
||||
@ -1,41 +0,0 @@
|
||||
import random
|
||||
|
||||
class defaults:
|
||||
# Defult values so that the user doesnt have to explicidly
|
||||
# state every feature of the genetic algorithm.
|
||||
def __init__(self):
|
||||
self.generations = 3
|
||||
self.chromosome_length = 4
|
||||
self.population_size = 5
|
||||
self.mutation_rate = 0.03
|
||||
|
||||
def default_gene_function():
|
||||
return random.randint(1, 100)
|
||||
|
||||
def default_fitness_function():
|
||||
pass
|
||||
|
||||
def default_initialize_function():
|
||||
return random_initialization()
|
||||
|
||||
def default_selection_function():
|
||||
return tournament_selection()
|
||||
|
||||
def default_crossover_function():
|
||||
return fast_single_point_crossover()
|
||||
|
||||
def default_mutations_function():
|
||||
return per_gene_mutation()
|
||||
|
||||
def default_termination_point_function(amount):
|
||||
# The default termination point is based on how
|
||||
# many generations the user wants to run.
|
||||
return generation_termination(amount)
|
||||
|
||||
def defult_get_highest_fitness():
|
||||
# Get the highest fitness of the current generation
|
||||
pass
|
||||
|
||||
def default_get_lowest_fitness():
|
||||
# Get the lowest fitness of the current generation
|
||||
pass
|
||||
@ -1,3 +1,3 @@
|
||||
class initialization:
|
||||
def initialize(self, population_size, chromosome_length,user_defined_function):
|
||||
def initialize(self,chromosome_length,population_size,gene_function):
|
||||
return [] # return an array of chromosomes for generation 0
|
||||
|
||||
15
src/initialization/random_gene.py
Normal file
15
src/initialization/random_gene.py
Normal file
@ -0,0 +1,15 @@
|
||||
# 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(low,high):
|
||||
# Check the values so that they follow the rules.
|
||||
check_values(low,high)
|
||||
return random.randint(1,100)
|
||||
@ -1,20 +1,20 @@
|
||||
from initialization.population import population
|
||||
from initialization.chromosome import chromosome
|
||||
from initialization.gene import gene
|
||||
|
||||
from initialization.initialization import initialization
|
||||
import EasyGA as ga
|
||||
import random
|
||||
|
||||
class random_initialization(initialization):
|
||||
def initialize(self, population_size, chromosome_length,user_defined_function):
|
||||
def initialize(self,chromosome_length,population_size,gene_function):
|
||||
# Create the population object
|
||||
population = ga.population()
|
||||
population = population.population()
|
||||
# Fill the population with chromosomes
|
||||
for i in range(population_size):
|
||||
#Create the chromosome object
|
||||
chromosome = ga.chromosome()
|
||||
chromosome = chromosome.chromosome()
|
||||
#Fill the Chromosome with genes
|
||||
for j in range(chromosome_length):
|
||||
# File the gene object with a value
|
||||
# Where the user function is being implimented ---
|
||||
chromosome.add_gene(ga.gene(user_defined_function()))
|
||||
# --------
|
||||
gene = gene.gene(gene_function)
|
||||
chromosome.add_gene(gene)
|
||||
|
||||
population.add_chromosome(chromosome)
|
||||
return population
|
||||
|
||||
14
src/initialization/test_population.py
Normal file
14
src/initialization/test_population.py
Normal file
@ -0,0 +1,14 @@
|
||||
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)
|
||||
@ -4,5 +4,6 @@ import EasyGA
|
||||
# Create the Genetic algorithm
|
||||
ga = EasyGA.GA()
|
||||
|
||||
print(ga.gene.get_fitness())
|
||||
print(ga.gene.get_value())
|
||||
ga.initialize()
|
||||
|
||||
ga.population.chromosome[0].print_chromosome()
|
||||
|
||||
Reference in New Issue
Block a user