Domain update
Can set the domain to either a range or a list of values.
This commit is contained in:
@ -1,13 +1,5 @@
|
||||
# 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)
|
||||
def random_gene(domain):
|
||||
return domain[random.randint(0, len(domain)-1)]
|
||||
|
||||
@ -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(chromosome_length, population_size, gene_function, domain):
|
||||
# Create the population object
|
||||
population = create_population()
|
||||
# Fill the population with chromosomes
|
||||
@ -11,6 +11,6 @@ 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()))
|
||||
chromosome.add_gene(create_gene(gene_function(domain)))
|
||||
population.add_chromosome(chromosome)
|
||||
return population
|
||||
|
||||
Reference in New Issue
Block a user