Merge branch 'master' of https://github.com/danielwilczak101/EasyGA
This commit is contained in:
30
README.md
30
README.md
@ -25,37 +25,7 @@ ga.evolve()
|
||||
Put the out here
|
||||
```
|
||||
|
||||
## Customized:
|
||||
```python
|
||||
import random
|
||||
import EasyGA
|
||||
|
||||
# Create the Genetic algorithm
|
||||
ga = EasyGA.GA()
|
||||
|
||||
# Makes a new gene
|
||||
new_gene = ga.make_gene("HelloWorld")
|
||||
# Makes a chromosome to store genes in
|
||||
new_chromosome = ga.make_chromosome()
|
||||
# Makes a Population to store chromosomes in
|
||||
new_population = ga.make_population()
|
||||
|
||||
ga.initialize()
|
||||
|
||||
print(ga.population)
|
||||
|
||||
for chromosome in ga.population.chromosomes:
|
||||
print(chromosome.genes[0].__dict__)
|
||||
```
|
||||
|
||||
### Output:
|
||||
```python
|
||||
<initialization.population_structure.population.population object at 0x7f993002fdf0>
|
||||
{'fitness': None, 'value': 47}
|
||||
{'fitness': None, 'value': 4}
|
||||
{'fitness': None, 'value': 68}
|
||||
{'fitness': None, 'value': 57}
|
||||
```
|
||||
|
||||
# How Testing works
|
||||
|
||||
|
||||
@ -1,4 +1,5 @@
|
||||
import random
|
||||
|
||||
# Import all the data prebuilt modules
|
||||
from initialization.population_structure.population import population as create_population
|
||||
from initialization.chromosome_structure.chromosome import chromosome as create_chromosome
|
||||
@ -7,9 +8,10 @@ from initialization.gene_structure.gene import gene as create_gene
|
||||
# Import functionality defaults
|
||||
from initialization.random_initialization import random_initialization
|
||||
|
||||
|
||||
class GA:
|
||||
def __init__(self):
|
||||
"""Initialize the GA."""
|
||||
|
||||
# Default variables
|
||||
self.chromosome_impl = None
|
||||
self.gene_impl = None
|
||||
@ -19,6 +21,7 @@ class GA:
|
||||
self.chromosome_length = 3
|
||||
self.population_size = 5
|
||||
self.mutation_rate = 0.03
|
||||
|
||||
# Defualt EastGA implimentation structure
|
||||
self.initialization_impl = random_initialization
|
||||
self.update_fitness = True
|
||||
@ -28,7 +31,8 @@ class GA:
|
||||
#self.termination_impl = GenerationTermination(Total_generations)
|
||||
#self.evaluation_impl = TestEvaluation()
|
||||
|
||||
def initialize(self):
|
||||
def initialize_population(self):
|
||||
"""Initialize the population"""
|
||||
self.population = self.initialization_impl(
|
||||
self.population_size,
|
||||
self.chromosome_length,
|
||||
@ -40,27 +44,41 @@ class GA:
|
||||
pass
|
||||
|
||||
def evolve(self):
|
||||
"""Updates the ga to the next generation.
|
||||
If update_fitness is set then all fitness values are updated.
|
||||
Otherwise only fitness values set to None (i.e. uninitialized fitness values) are updated."""
|
||||
for chromosome in self.population.get_all_chromosomes():
|
||||
if self.update_fitness or chromosome.get_fitness() is None:
|
||||
chromosome.set_fitness(self.fitness_impl(chromosome))
|
||||
"""Runs the ga until the ga is no longer active."""
|
||||
|
||||
# run one iteration while the ga is active
|
||||
while self.active():
|
||||
self.evolve_generation(1)
|
||||
|
||||
def active(self):
|
||||
"""Returns if the ga should terminate or not"""
|
||||
return self.current_generation < self.generations
|
||||
|
||||
def evolve_generation(self, number_of_generations):
|
||||
# If you want to evolve through a number of generations
|
||||
# and be able to pause and output data based on that generation run.
|
||||
pass
|
||||
"""Evolves the ga the specified number of generations.
|
||||
If update_fitness is set then all fitness values are updated.
|
||||
Otherwise only fitness values set to None (i.e. uninitialized fitness values) are updated."""
|
||||
|
||||
# run the specified number of times
|
||||
for n in range(number_of_generations):
|
||||
|
||||
# for each chromosome in the population
|
||||
for chromosome in self.population.get_all_chromosomes():
|
||||
|
||||
# if the fitness should be updated, update it
|
||||
if self.update_fitness or chromosome.get_fitness() is None:
|
||||
chromosome.set_fitness(self.fitness_impl(chromosome))
|
||||
|
||||
# apply selection, crossover, and mutation
|
||||
|
||||
def make_gene(self,value):
|
||||
"""Let's the user create a gene."""
|
||||
return create_gene(value)
|
||||
|
||||
def make_chromosome(self):
|
||||
"""Let's the user create a chromosome."""
|
||||
return create_chromosome()
|
||||
|
||||
def make_population(self):
|
||||
"""Let's the user create a population."""
|
||||
return create_population()
|
||||
|
||||
@ -4,6 +4,8 @@ from .chromosome_structure.chromosome import chromosome as create_chromosome
|
||||
from .gene_structure.gene import gene as create_gene
|
||||
|
||||
def random_initialization(population_size, chromosome_length, chromosome_impl, gene_impl):
|
||||
"""Takes the initialization inputs and choregraphs them to output the type of population
|
||||
with the given parameters."""
|
||||
# Create the population object
|
||||
population = create_population()
|
||||
# Fill the population with chromosomes
|
||||
@ -11,10 +13,11 @@ def random_initialization(population_size, chromosome_length, chromosome_impl, g
|
||||
chromosome = create_chromosome()
|
||||
#Fill the Chromosome with genes
|
||||
for j in range(chromosome_length):
|
||||
# Using the chromosome_impl to set every index inside of the chromosome
|
||||
if chromosome_impl != None:
|
||||
# Each chromosome location is specified with its own function
|
||||
chromosome.add_gene(create_gene(chromosome_impl(j)))
|
||||
# Will break if chromosome_length != lists in domain
|
||||
# Will break if chromosome_length != len(lists) in domain
|
||||
elif gene_impl != None:
|
||||
# gene_impl = [range function,lowerbound,upperbound]
|
||||
function = gene_impl[0]
|
||||
|
||||
@ -15,6 +15,6 @@ def user_gene_domain(gene_index):
|
||||
# If the user wants to use a domain
|
||||
ga.chromosome_impl = user_gene_domain
|
||||
|
||||
ga.initialize()
|
||||
ga.initialize_population()
|
||||
|
||||
ga.population.print_all()
|
||||
|
||||
Reference in New Issue
Block a user