Removed initialization methods
This commit is contained in:
@ -11,7 +11,6 @@ from structure import Gene as make_gene
|
|||||||
|
|
||||||
# Structure Methods
|
# Structure Methods
|
||||||
from fitness_function import Fitness_Examples
|
from fitness_function import Fitness_Examples
|
||||||
from initialization import Initialization_Methods
|
|
||||||
from termination_point import Termination_Methods
|
from termination_point import Termination_Methods
|
||||||
|
|
||||||
# Parent/Survivor Selection Methods
|
# Parent/Survivor Selection Methods
|
||||||
@ -239,7 +238,26 @@ class GA(Attributes):
|
|||||||
that is currently set.
|
that is currently set.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
self.population = self.initialization_impl(self)
|
if self.chromosome_impl is not None:
|
||||||
|
self.population = self.make_population(
|
||||||
|
self.chromosome_impl()
|
||||||
|
for _
|
||||||
|
in range(self.population_size)
|
||||||
|
)
|
||||||
|
|
||||||
|
elif self.gene_impl is not None:
|
||||||
|
self.population = self.make_population(
|
||||||
|
(
|
||||||
|
self.gene_impl()
|
||||||
|
for _
|
||||||
|
in range(self.chromosome_length)
|
||||||
|
)
|
||||||
|
for _
|
||||||
|
in range(self.population_size)
|
||||||
|
)
|
||||||
|
|
||||||
|
else:
|
||||||
|
raise ValueError("No chromosome or gene impl specified.")
|
||||||
|
|
||||||
|
|
||||||
def set_all_fitness(self):
|
def set_all_fitness(self):
|
||||||
|
|||||||
@ -12,7 +12,6 @@ from structure import Gene as make_gene
|
|||||||
|
|
||||||
# Structure Methods
|
# Structure Methods
|
||||||
from fitness_function import Fitness_Examples
|
from fitness_function import Fitness_Examples
|
||||||
from initialization import Initialization_Methods
|
|
||||||
from termination_point import Termination_Methods
|
from termination_point import Termination_Methods
|
||||||
|
|
||||||
# Parent/Survivor Selection Methods
|
# Parent/Survivor Selection Methods
|
||||||
@ -66,7 +65,6 @@ class Attributes:
|
|||||||
max_gene_mutation_rate = 0.15,
|
max_gene_mutation_rate = 0.15,
|
||||||
min_gene_mutation_rate = 0.01,
|
min_gene_mutation_rate = 0.01,
|
||||||
dist = None,
|
dist = None,
|
||||||
initialization_impl = Initialization_Methods.random_initialization,
|
|
||||||
fitness_function_impl = Fitness_Examples.is_it_5,
|
fitness_function_impl = Fitness_Examples.is_it_5,
|
||||||
make_population = make_population,
|
make_population = make_population,
|
||||||
make_chromosome = make_chromosome,
|
make_chromosome = make_chromosome,
|
||||||
@ -133,7 +131,6 @@ class Attributes:
|
|||||||
self.dist = dist
|
self.dist = dist
|
||||||
|
|
||||||
# Default EasyGA implimentation structure
|
# Default EasyGA implimentation structure
|
||||||
self.initialization_impl = initialization_impl
|
|
||||||
self.fitness_function_impl = fitness_function_impl
|
self.fitness_function_impl = fitness_function_impl
|
||||||
self.make_population = make_population
|
self.make_population = make_population
|
||||||
self.make_chromosome = make_chromosome
|
self.make_chromosome = make_chromosome
|
||||||
|
|||||||
@ -1,11 +0,0 @@
|
|||||||
# Initialization Functions
|
|
||||||
|
|
||||||
## random_initialization
|
|
||||||
random_initialization class
|
|
||||||
|
|
||||||
Takes in
|
|
||||||
```
|
|
||||||
population_size, chromosome_length, user_defined_function
|
|
||||||
```
|
|
||||||
Creates a chromosome of random genes based on the user user_defined_function
|
|
||||||
that its supplied.
|
|
||||||
@ -1,2 +0,0 @@
|
|||||||
# FROM (. means local) file_name IMPORT function_name
|
|
||||||
from .initialization_methods import Initialization_Methods
|
|
||||||
@ -1,38 +0,0 @@
|
|||||||
def _chromosomes_to_population(initialize):
|
|
||||||
"""Makes a population from chromosomes."""
|
|
||||||
return lambda ga:\
|
|
||||||
ga.make_population(
|
|
||||||
(
|
|
||||||
initialize(ga)
|
|
||||||
for _
|
|
||||||
in range(ga.population_size)
|
|
||||||
)
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
class Initialization_Methods:
|
|
||||||
"""Initialization examples that are used as defaults and examples"""
|
|
||||||
|
|
||||||
# Private method decorators, see above.
|
|
||||||
_chromosomes_to_population = _chromosomes_to_population
|
|
||||||
|
|
||||||
|
|
||||||
@_chromosomes_to_population
|
|
||||||
def random_initialization(ga):
|
|
||||||
"""Takes the initialization inputs and returns a collection of values.
|
|
||||||
Method decorators convert them to a GA population object.
|
|
||||||
"""
|
|
||||||
|
|
||||||
# Using the chromosome_impl to set every index inside of the chromosome
|
|
||||||
if ga.chromosome_impl is not None:
|
|
||||||
for value in ga.chromosome_impl():
|
|
||||||
yield value
|
|
||||||
|
|
||||||
# Using the gene_impl to set every gene to be the same
|
|
||||||
elif ga.gene_impl is not None:
|
|
||||||
for _ in range(ga.chromosome_length):
|
|
||||||
yield ga.gene_impl()
|
|
||||||
|
|
||||||
# Exit because no gene creation method specified
|
|
||||||
else:
|
|
||||||
raise Exception("Did not specify any initialization constraints.")
|
|
||||||
Reference in New Issue
Block a user