This commit is contained in:
danielwilczak101
2020-09-23 13:23:07 -04:00
parent c2ebec6be2
commit cd15a85b71
15 changed files with 117 additions and 82 deletions

View File

@ -0,0 +1,22 @@
class chromosome:
# fitness = Empty, genes = [gene,gene,gene,etc]
def __init__(self):
self.fitness = None
self.genes = []
def add_gene(self,gene):
self.genes.append(gene)
def get_fitness(self):
return self.fitness
def get_chromosome(self):
return self.genes
def print_chromosome(self):
for i in range(len(self.genes)):
# Print the gene one by one.
if(i == len(self.genes) - 1):
print(f"[{self.genes[i].get_value()}]")
else:
print(f"[{self.genes[i].get_value()}],", end = '')

View File

@ -0,0 +1,25 @@
def check_gene(value):
#Check to make sure the gene is not empty
assert value != "" , "Gene can not be empty"
return value
class gene:
# fitness = Empty, value = Define by gene function
def __init__(self, value):
self.fitness = None
self.value = check_gene(value)
def set_fitness(self,fitness):
self.fitness = fitness
def get_fitness(self):
return self.fitness
def get_value(self):
return self.value
def print_value(self):
print(self.value)
def print_fitness(self):
print(self.fitness)

View File

@ -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

View File

@ -0,0 +1,8 @@
class population:
# population = [chromosome,chromosome,etc]
def __init__(self):
self.chromosomes = []
self.fitness = None
def add_chromosome(self,chromosome):
self.chromosomes.append(chromosome)

View 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)

View File

@ -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

View File

View File

@ -0,0 +1,2 @@
def test_pass():
assert 1 + 1 == 2

View 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)