Domain update

Can set the domain to either a range or a list of values.
This commit is contained in:
SimpleArt
2020-09-24 23:51:21 -04:00
parent 4daec6574d
commit 5c5d6920b2
5 changed files with 18 additions and 30 deletions

View File

@ -12,6 +12,7 @@ from initialization.random_initialization import random_initialization
class GA: class GA:
def __init__(self): def __init__(self):
# Default variables # Default variables
self.domain = range(1, 100)
self.population = None self.population = None
self.generations = 3 self.generations = 3
self.chromosome_length = 4 self.chromosome_length = 4
@ -27,13 +28,15 @@ class GA:
#self.termination_impl = GenerationTermination(Total_generations) #self.termination_impl = GenerationTermination(Total_generations)
#self.evaluation_impl = TestEvaluation() #self.evaluation_impl = TestEvaluation()
def initialize(self): def initialize(self):
if isinstance(self.domain, range):
self.domain = [x/float(100) for x in range(int(min(self.domain)*100), int(max(self.domain)*100))]
# Create the first population # Create the first population
self.population = self.initialization_impl( 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,
self.domain)
def evolve(): def evolve():
# If you just want to evolve through all generations # If you just want to evolve through all generations

View File

@ -1,13 +1,5 @@
# Imported library # Imported library
import random import random
def check_values(low,high): def random_gene(domain):
#Check to make sure its not less then zero return domain[random.randint(0, len(domain)-1)]
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)

View File

@ -36,8 +36,7 @@ class population:
self.fitness = fitness self.fitness = fitness
def __repr__(self): def __repr__(self):
for index in range(len(self.chromosomes)): return ''.join([chromosome.__repr__() for chromosome in self.chromosomes])
return f'{self.chromosomes[index]}'
def print_all(self): def print_all(self):
# Ex .Current population # Ex .Current population

View File

@ -3,7 +3,7 @@ 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
def random_initialization(chromosome_length,population_size,gene_function): def random_initialization(chromosome_length, population_size, gene_function, domain):
# Create the population object # Create the population object
population = create_population() population = create_population()
# Fill the population with chromosomes # Fill the population with chromosomes
@ -11,6 +11,6 @@ def random_initialization(chromosome_length,population_size,gene_function):
chromosome = create_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):
chromosome.add_gene(create_gene(gene_function())) chromosome.add_gene(create_gene(gene_function(domain)))
population.add_chromosome(chromosome) population.add_chromosome(chromosome)
return population return population

View File

@ -3,18 +3,12 @@ import EasyGA
# Create the Genetic algorithm # Create the Genetic algorithm
ga = EasyGA.GA() ga = EasyGA.GA()
#Creating a gene with no fitness # input domain
gene1 = ga.make_gene("Im a gene") #ga.domain = range(3, 10)
gene2 = ga.make_gene("Im also a gene") ga.domain = ['left', 'right']
#Creating a Chromosome with no genes
chromosome = ga.make_chromosome()
chromosome.add_gene(gene1)
chromosome.add_gene(gene2)
# Creating a populaiton
populaiton = ga.make_population()
populaiton.add_chromosome(chromosome)
print(gene1) # initialize random population
print(chromosome) ga.initialize()
print(populaiton)
populaiton.print_all() # Print population
ga.population.print_all()