Change domain feature
This commit is contained in:
@ -1,3 +1,4 @@
|
|||||||
|
import random
|
||||||
# Import all the data prebuilt modules
|
# Import all the data prebuilt modules
|
||||||
from initialization.population_structure.population import population as create_population
|
from initialization.population_structure.population import population as create_population
|
||||||
from initialization.chromosome_structure.chromosome import chromosome as create_chromosome
|
from initialization.chromosome_structure.chromosome import chromosome as create_chromosome
|
||||||
@ -12,14 +13,15 @@ from initialization.random_initialization import random_initialization
|
|||||||
class GA:
|
class GA:
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
# Default variables
|
# Default variables
|
||||||
self.domain = range(1, 100)
|
self.domain = None
|
||||||
|
self.new_range = None
|
||||||
self.population = None
|
self.population = None
|
||||||
self.generations = 3
|
self.generations = 3
|
||||||
self.chromosome_length = 4
|
self.chromosome_length = 3
|
||||||
self.population_size = 5
|
self.population_size = 5
|
||||||
self.mutation_rate = 0.03
|
self.mutation_rate = 0.03
|
||||||
# Defualt EastGA implimentation structure
|
# Defualt EastGA implimentation structure
|
||||||
self.gene_function_impl = random_gene
|
|
||||||
# Set the GA Configuration
|
# Set the GA Configuration
|
||||||
self.initialization_impl = random_initialization
|
self.initialization_impl = random_initialization
|
||||||
#self.mutation_impl = PerGeneMutation(Mutation_rate)
|
#self.mutation_impl = PerGeneMutation(Mutation_rate)
|
||||||
@ -29,14 +31,11 @@ class GA:
|
|||||||
#self.evaluation_impl = TestEvaluation()
|
#self.evaluation_impl = TestEvaluation()
|
||||||
|
|
||||||
def initialize(self):
|
def initialize(self):
|
||||||
if isinstance(self.domain, range):
|
|
||||||
self.domain = [x/float(100) for x in range(int(min(self.domain)*100), int(max(self.domain)*100))]
|
|
||||||
# Create the first population
|
|
||||||
self.population = self.initialization_impl(
|
self.population = self.initialization_impl(
|
||||||
self.population_size,
|
self.population_size,
|
||||||
self.chromosome_length,
|
self.chromosome_length,
|
||||||
self.gene_function_impl,
|
self.domain,
|
||||||
self.domain)
|
self.new_range)
|
||||||
|
|
||||||
def evolve():
|
def evolve():
|
||||||
# If you just want to evolve through all generations
|
# If you just want to evolve through all generations
|
||||||
|
|||||||
@ -3,7 +3,7 @@ from .population_structure.population import population as create_population
|
|||||||
from .chromosome_structure.chromosome import chromosome as create_chromosome
|
from .chromosome_structure.chromosome import chromosome as create_chromosome
|
||||||
from .gene_structure.gene import gene as create_gene
|
from .gene_structure.gene import gene as create_gene
|
||||||
|
|
||||||
def random_initialization(chromosome_length, population_size, gene_function, domain):
|
def random_initialization(population_size, chromosome_length, domain, new_range):
|
||||||
# Create the population object
|
# Create the population object
|
||||||
population = create_population()
|
population = create_population()
|
||||||
# Fill the population with chromosomes
|
# Fill the population with chromosomes
|
||||||
@ -11,6 +11,16 @@ def random_initialization(chromosome_length, population_size, gene_function, dom
|
|||||||
chromosome = create_chromosome()
|
chromosome = create_chromosome()
|
||||||
#Fill the Chromosome with genes
|
#Fill the Chromosome with genes
|
||||||
for j in range(chromosome_length):
|
for j in range(chromosome_length):
|
||||||
chromosome.add_gene(create_gene(gene_function(domain)))
|
if domain != None:
|
||||||
|
# Each chromosome location is specified with its own function
|
||||||
|
chromosome.add_gene(create_gene(domain(j)))
|
||||||
|
# Will break if chromosome_length != lists in domain
|
||||||
|
elif new_range != None:
|
||||||
|
# new_rnage = [range function,lowerbound,upperbound]
|
||||||
|
function = new_range[0]
|
||||||
|
chromosome.add_gene(create_gene(function(new_range[1],new_range[2])))
|
||||||
|
else:
|
||||||
|
#Exit because either were not specified
|
||||||
|
print("Your domain or range were not specified")
|
||||||
population.add_chromosome(chromosome)
|
population.add_chromosome(chromosome)
|
||||||
return population
|
return population
|
||||||
|
|||||||
@ -1,14 +1,22 @@
|
|||||||
import EasyGA
|
import EasyGA
|
||||||
|
import random
|
||||||
# Create the Genetic algorithm
|
# Create the Genetic algorithm
|
||||||
ga = EasyGA.GA()
|
ga = EasyGA.GA()
|
||||||
|
|
||||||
# input domain
|
def user_gene_domain(gene_index):
|
||||||
#ga.domain = range(3, 10)
|
"""Each gene index is assosiated to its index in the chromosome"""
|
||||||
ga.domain = ['left', 'right']
|
domain = [
|
||||||
|
random.randrange(1,100),
|
||||||
|
random.uniform(10,5),
|
||||||
|
random.choice(["up","down"])
|
||||||
|
]
|
||||||
|
return domain[gene_index]
|
||||||
|
|
||||||
|
# If the user wants to use a domain
|
||||||
|
ga.domain = user_gene_domain
|
||||||
|
# If the user wants to use a custom range
|
||||||
|
#ga.new_range = [random.randrange,1,100]
|
||||||
|
|
||||||
# initialize random population
|
|
||||||
ga.initialize()
|
ga.initialize()
|
||||||
|
|
||||||
# Print population
|
|
||||||
ga.population.print_all()
|
ga.population.print_all()
|
||||||
|
|||||||
Reference in New Issue
Block a user