Added initilization functionality. Add necessary method to access,genes,chromosomes and the population.
This commit is contained in:
2
setup.py
2
setup.py
@ -5,7 +5,7 @@ with open("README.md", "r") as fh:
|
|||||||
|
|
||||||
setup(
|
setup(
|
||||||
name='EasyGA',
|
name='EasyGA',
|
||||||
version='0.0.6',
|
version='0.0.7',
|
||||||
description='A ubiquitous or general purpuse GA',
|
description='A ubiquitous or general purpuse GA',
|
||||||
py_modules=["EasyGA"],
|
py_modules=["EasyGA"],
|
||||||
package_dir={'':'src'},
|
package_dir={'':'src'},
|
||||||
|
|||||||
@ -1,8 +1,9 @@
|
|||||||
|
from initialization.random_initialization import random_initialization
|
||||||
|
|
||||||
def check_gene(value):
|
def check_gene(value):
|
||||||
assert value != "" , "Gene can not be empty"
|
assert value != "" , "Gene can not be empty"
|
||||||
return value
|
return value
|
||||||
|
|
||||||
|
|
||||||
## Your main structure
|
## Your main structure
|
||||||
class gene:
|
class gene:
|
||||||
# Defults
|
# Defults
|
||||||
@ -20,10 +21,6 @@ class gene:
|
|||||||
def get_value(self):
|
def get_value(self):
|
||||||
return self.value
|
return self.value
|
||||||
|
|
||||||
# Should the gene creation of the genes veriation be
|
|
||||||
# included in the gene class or should the use just
|
|
||||||
# assign value to the gene and we move on?
|
|
||||||
|
|
||||||
class chromosome:
|
class chromosome:
|
||||||
# Defults
|
# Defults
|
||||||
# fitness = double, genes = [gene,gene,gene,etc]
|
# fitness = double, genes = [gene,gene,gene,etc]
|
||||||
@ -37,10 +34,31 @@ class chromosome:
|
|||||||
def add_gene(self,gene):
|
def add_gene(self,gene):
|
||||||
self.genes.append(gene)
|
self.genes.append(gene)
|
||||||
|
|
||||||
|
def print_chromosome(self):
|
||||||
|
for i in range(len(self.genes)):
|
||||||
|
print(f"[{self.genes[i].get_value()}],", end = '')
|
||||||
|
|
||||||
|
|
||||||
class population:
|
class population:
|
||||||
# chromosomes = [chromosome,chromosome,etc]
|
# chromosomes = [chromosome,chromosome,etc]
|
||||||
def __init__(self, chromosome):
|
def __init__(self):
|
||||||
self.chromosomes = []
|
self.chromosomes = []
|
||||||
|
|
||||||
class ga:
|
def add_chromosome(self,chromosome):
|
||||||
pass
|
self.chromosomes.append(chromosome)
|
||||||
|
|
||||||
|
class GA:
|
||||||
|
def __init__(self, population_size, chromosome_length, user_gene_function):
|
||||||
|
# User defined variables
|
||||||
|
self.population_size = population_size
|
||||||
|
self.chromosome_length = chromosome_length
|
||||||
|
self.user_gene_function = user_gene_function
|
||||||
|
# setup required variables
|
||||||
|
self.population = []
|
||||||
|
# Setup ga implimentation structure
|
||||||
|
self.initialization_impl = random_initialization()
|
||||||
|
|
||||||
|
|
||||||
|
def initialize(self):
|
||||||
|
# Create the initial population
|
||||||
|
self.population = self.initialization_impl.initialize(self.population_size, self.chromosome_length, self.user_gene_function)
|
||||||
|
|||||||
15
src/example.py
Normal file
15
src/example.py
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
import random
|
||||||
|
import EasyGA
|
||||||
|
|
||||||
|
def user_gene_function():
|
||||||
|
return random.randint(1, 100)
|
||||||
|
|
||||||
|
Population_size = 10
|
||||||
|
Chromosome_length = 10
|
||||||
|
|
||||||
|
ga = EasyGA.GA(Population_size, Chromosome_length,user_gene_function)
|
||||||
|
|
||||||
|
# Setup the GA's population,chromosomes and genes
|
||||||
|
ga.initialize()
|
||||||
|
|
||||||
|
print(ga.population.chromosomes[0].print_chromosome())
|
||||||
3
src/initialization/initialization.py
Normal file
3
src/initialization/initialization.py
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
class initialization:
|
||||||
|
def initialize(self, population_size, chromosome_length,user_defined_function):
|
||||||
|
return [] # return an array of chromosomes for generation 0
|
||||||
20
src/initialization/random_initialization.py
Normal file
20
src/initialization/random_initialization.py
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
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):
|
||||||
|
# Create the population object
|
||||||
|
population = ga.population()
|
||||||
|
# Fill the population with chromosomes
|
||||||
|
for i in range(population_size):
|
||||||
|
#Create the chromosome object
|
||||||
|
chromosome = ga.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()))
|
||||||
|
# --------
|
||||||
|
population.add_chromosome(chromosome)
|
||||||
|
return population
|
||||||
Reference in New Issue
Block a user