Merge branch 'master' into ryley_beta
This commit is contained in:
@ -1,5 +1,3 @@
|
||||
# FROM (. means local) file_name IMPORT function_name
|
||||
from .initialization_methods import Initialization_Methods
|
||||
from .population_structure.population import Population
|
||||
from .chromosome_structure.chromosome import Chromosome
|
||||
from .gene_structure.gene import Gene
|
||||
|
||||
|
||||
0
src/initialization/chromosome_structure/__init__.py
Normal file
0
src/initialization/chromosome_structure/__init__.py
Normal file
@ -1,13 +1,16 @@
|
||||
class Chromosome:
|
||||
|
||||
def __init__(self, genes = None):
|
||||
if genes is None:
|
||||
def __init__(self, gene_list = None):
|
||||
if gene_list is None:
|
||||
self.gene_list = []
|
||||
else:
|
||||
self.gene_list = genes
|
||||
|
||||
self.fitness = None
|
||||
# If the chromosome has been selected then the flag would switch to true
|
||||
self.selected = False
|
||||
|
||||
def add_gene(self, gene, index = -1):
|
||||
"""Add a gene to the chromosome at the specified index, defaulted to end of the chromosome"""
|
||||
if index == -1:
|
||||
index = len(self.gene_list)
|
||||
self.gene_list.insert(index, gene)
|
||||
@ -19,6 +22,7 @@ class Chromosome:
|
||||
return self.gene_list
|
||||
|
||||
def get_fitness(self):
|
||||
"""Return the fitness of the chromosome"""
|
||||
return self.fitness
|
||||
|
||||
def set_gene(self, gene, index):
|
||||
@ -28,9 +32,11 @@ class Chromosome:
|
||||
self.gene_list = genes
|
||||
|
||||
def set_fitness(self, fitness):
|
||||
"""Set the fitness value of the chromosome"""
|
||||
self.fitness = fitness
|
||||
|
||||
def __repr__(self):
|
||||
"""Format the repr() output for the chromosome"""
|
||||
output_str = ''
|
||||
for gene in self.gene_list:
|
||||
output_str += gene.__repr__()
|
||||
|
||||
0
src/initialization/gene_structure/__init__.py
Normal file
0
src/initialization/gene_structure/__init__.py
Normal file
@ -6,16 +6,20 @@ def check_gene(value):
|
||||
class Gene:
|
||||
|
||||
def __init__(self, value):
|
||||
"""Initialize a gene with fitness of value None and the input value"""
|
||||
self.fitness = None
|
||||
self.value = check_gene(value)
|
||||
|
||||
def get_fitness(self):
|
||||
"""Return fitness of the gene"""
|
||||
return self.fitness
|
||||
|
||||
def get_value(self):
|
||||
"""Return value of the gene"""
|
||||
return self.value
|
||||
|
||||
def set_fitness(self, fitness):
|
||||
"""Set fitness of the gene"""
|
||||
self.fitness = fitness
|
||||
|
||||
def set_value(self, value):
|
||||
@ -23,4 +27,5 @@ class Gene:
|
||||
self.value = value
|
||||
|
||||
def __repr__(self):
|
||||
"""Format the repr() output value"""
|
||||
return f'[{self.value}]'
|
||||
|
||||
33
src/initialization/methods.py
Normal file
33
src/initialization/methods.py
Normal file
@ -0,0 +1,33 @@
|
||||
# Import the data structure
|
||||
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
|
||||
|
||||
class Initialization_methods:
|
||||
"""Initialization examples that are used as defaults and examples"""
|
||||
|
||||
def random_initialization(ga):
|
||||
"""Takes the initialization inputs and choregraphs them to output the type of population
|
||||
with the given parameters."""
|
||||
# Create the population object
|
||||
population = create_population()
|
||||
|
||||
# Fill the population with chromosomes
|
||||
for i in range(ga.population_size):
|
||||
chromosome = create_chromosome()
|
||||
#Fill the Chromosome with genes
|
||||
for j in range(ga.chromosome_length):
|
||||
# Using the chromosome_impl to set every index inside of the chromosome
|
||||
if ga.chromosome_impl != None:
|
||||
# Each chromosome location is specified with its own function
|
||||
chromosome.add_gene(create_gene(ga.chromosome_impl(j)))
|
||||
# Will break if chromosome_length != len(lists) in domain
|
||||
elif ga.gene_impl != None:
|
||||
# gene_impl = [range function,lowerbound,upperbound]
|
||||
function = ga.gene_impl[0]
|
||||
chromosome.add_gene(create_gene(function(*ga.gene_impl[1:])))
|
||||
else:
|
||||
#Exit because either were not specified
|
||||
print("Your domain or range were not specified")
|
||||
population.add_chromosome(chromosome)
|
||||
return population
|
||||
0
src/initialization/population_structure/__init__.py
Normal file
0
src/initialization/population_structure/__init__.py
Normal file
@ -1,24 +1,25 @@
|
||||
class Population:
|
||||
|
||||
# fitness = Empty; population = [chromosome, chromosome, etc.]
|
||||
def __init__(self, chromosomes = None):
|
||||
if chromosomes is None:
|
||||
def __init__(self, chromosome_list = None):
|
||||
"""Intiialize the population with fitness of value None, and a set of chromosomes dependant on user-passed parameter"""
|
||||
if chromosome_list is None:
|
||||
self.chromosome_list = []
|
||||
else:
|
||||
self.chromosome_list = chromosomes
|
||||
self.chromosome_list = chromosome_list
|
||||
self.fitness = None
|
||||
self.mating_pool = []
|
||||
|
||||
def get_closet_fitness(self,value):
|
||||
# Get the chomosome that has the closets fitness to the value defined
|
||||
"""Get the chomosome that has the closets fitness to the value defined"""
|
||||
pass
|
||||
|
||||
def add_chromosome(self, chromosome, index = -1):
|
||||
"""Adds a chromosome to the population at the input index, defaulted to the end of the chromosome set"""
|
||||
if index == -1:
|
||||
index = len(self.chromosome_list)
|
||||
self.chromosome_list.insert(index, chromosome)
|
||||
|
||||
def remove_chromosome(self, index):
|
||||
"""removes a chromosome from the indicated index"""
|
||||
del self.chromosome_list[index]
|
||||
|
||||
def get_all_chromosomes(self):
|
||||
@ -26,6 +27,7 @@ class Population:
|
||||
return self.chromosome_list
|
||||
|
||||
def get_fitness(self):
|
||||
"""returns the population's fitness"""
|
||||
return self.fitness
|
||||
|
||||
def set_all_chromosomes(self, chromosomes):
|
||||
@ -37,6 +39,7 @@ class Population:
|
||||
self.chromosome_list[index] = chromosome
|
||||
|
||||
def set_fitness(self, fitness):
|
||||
"""Sets the fitness value of the population"""
|
||||
self.fitness = fitness
|
||||
|
||||
def __repr__(self):
|
||||
@ -44,9 +47,10 @@ class Population:
|
||||
return f'{self.chromosome_list[index]}'
|
||||
|
||||
def print_all(self):
|
||||
# Ex .Current population
|
||||
# Chromosome 1 - [gene][gene][gene][.etc] / Chromosome fitness - #
|
||||
"""Prints information about the population in the following format:"""
|
||||
"""Ex .Current population"""
|
||||
"""Chromosome 1 - [gene][gene][gene][.etc] / Chromosome fitness - """
|
||||
print("Current population:")
|
||||
for index in range(len(self.chromosome_list)):
|
||||
print(f'Chromosome - {index} {self.chromosome_list[index]}', end = "")
|
||||
print(f' / Fitness = {self.chromosome_list[index].fitness}')
|
||||
print(f' / Fitness = {self.chromosome_list[index].fitness}')
|
||||
|
||||
0
src/initialization/test_methods.py
Normal file
0
src/initialization/test_methods.py
Normal file
Reference in New Issue
Block a user