testing
This commit is contained in:
22
src/initialization/chromosome.py
Normal file
22
src/initialization/chromosome.py
Normal 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 = '')
|
||||
25
src/initialization/gene.py
Normal file
25
src/initialization/gene.py
Normal 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)
|
||||
@ -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
|
||||
|
||||
8
src/initialization/population.py
Normal file
8
src/initialization/population.py
Normal 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)
|
||||
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
|
||||
|
||||
0
src/initialization/test_chromosome.py
Normal file
0
src/initialization/test_chromosome.py
Normal file
2
src/initialization/test_gene.py
Normal file
2
src/initialization/test_gene.py
Normal file
@ -0,0 +1,2 @@
|
||||
def test_pass():
|
||||
assert 1 + 1 == 2
|
||||
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)
|
||||
Reference in New Issue
Block a user