Split the process into function decorators
This commit is contained in:
@ -1,6 +1,27 @@
|
|||||||
|
def chromosomes_to_population(initialize):
|
||||||
|
return lambda ga: ga.make_population([initialize(ga) for _ in range(ga.population_size)])
|
||||||
|
|
||||||
|
def genes_to_chromosome(initialize):
|
||||||
|
return lambda ga: ga.make_chromosome([genes for genes in initialize(ga)])
|
||||||
|
|
||||||
|
def value_to_gene(initialize):
|
||||||
|
return lambda ga: (ga.make_gene(value) for value in initialize(ga))
|
||||||
|
|
||||||
|
|
||||||
class Initialization_Methods:
|
class Initialization_Methods:
|
||||||
"""Initialization examples that are used as defaults and examples"""
|
"""Initialization examples that are used as defaults and examples"""
|
||||||
|
|
||||||
|
def __chromosomes_to_population(initialize):
|
||||||
|
return chromosomes_to_population(initialize)
|
||||||
|
def __genes_to_chromosome(initialize):
|
||||||
|
return genes_to_chromosome(initialize)
|
||||||
|
def __value_to_gene(initialize):
|
||||||
|
return value_to_gene(initialize)
|
||||||
|
|
||||||
|
|
||||||
|
@chromosomes_to_population
|
||||||
|
@genes_to_chromosome
|
||||||
|
@value_to_gene
|
||||||
def random_initialization(ga):
|
def random_initialization(ga):
|
||||||
"""Takes the initialization inputs and
|
"""Takes the initialization inputs and
|
||||||
- return a new population
|
- return a new population
|
||||||
@ -10,21 +31,14 @@ class Initialization_Methods:
|
|||||||
|
|
||||||
# Using the chromosome_impl to set every index inside of the chromosome
|
# Using the chromosome_impl to set every index inside of the chromosome
|
||||||
if ga.chromosome_impl is not None:
|
if ga.chromosome_impl is not None:
|
||||||
return ga.make_population([
|
for value in ga.chromosome_impl():
|
||||||
ga.make_chromosome([
|
yield value
|
||||||
ga.make_gene(value)
|
|
||||||
for value in ga.chromosome_impl()])
|
|
||||||
for i in range(ga.population_size)])
|
|
||||||
|
|
||||||
# Using the gene_impl to set every gene to be the same
|
# Using the gene_impl to set every gene to be the same
|
||||||
elif ga.gene_impl is not None:
|
elif ga.gene_impl is not None:
|
||||||
return ga.make_population([
|
for _ in range(ga.population_size):
|
||||||
ga.make_chromosome([
|
yield ga.gene_impl()
|
||||||
ga.make_gene(ga.gene_impl())
|
|
||||||
for j in range(ga.chromosome_length)])
|
|
||||||
for i in range(ga.population_size)])
|
|
||||||
|
|
||||||
# Exit because no gene creation method specified
|
# Exit because no gene creation method specified
|
||||||
else:
|
else:
|
||||||
print("You did not specify any initialization constraints.")
|
raise Exception("Did not specify any initialization constraints.")
|
||||||
return None
|
|
||||||
|
|||||||
Reference in New Issue
Block a user