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(
|
||||
name='EasyGA',
|
||||
version='0.0.6',
|
||||
version='0.0.7',
|
||||
description='A ubiquitous or general purpuse GA',
|
||||
py_modules=["EasyGA"],
|
||||
package_dir={'':'src'},
|
||||
|
||||
@ -1,8 +1,9 @@
|
||||
from initialization.random_initialization import random_initialization
|
||||
|
||||
def check_gene(value):
|
||||
assert value != "" , "Gene can not be empty"
|
||||
return value
|
||||
|
||||
|
||||
## Your main structure
|
||||
class gene:
|
||||
# Defults
|
||||
@ -20,10 +21,6 @@ class gene:
|
||||
def get_value(self):
|
||||
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:
|
||||
# Defults
|
||||
# fitness = double, genes = [gene,gene,gene,etc]
|
||||
@ -37,10 +34,31 @@ class chromosome:
|
||||
def add_gene(self,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:
|
||||
# chromosomes = [chromosome,chromosome,etc]
|
||||
def __init__(self, chromosome):
|
||||
def __init__(self):
|
||||
self.chromosomes = []
|
||||
|
||||
class ga:
|
||||
pass
|
||||
def add_chromosome(self,chromosome):
|
||||
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