Major chnages to structure

This commit is contained in:
danielwilczak101
2020-09-21 20:26:21 -04:00
parent 5c4dd96427
commit e84483cf8b
2 changed files with 81 additions and 26 deletions

View File

@ -14,36 +14,55 @@ pip3 install EasyGA
```Python ```Python
import EasyGA import EasyGA
# Setup the defult genetic algorithm # Setup the default genetic algorithm
ga = EasyGA.GA() ga = EasyGA.GA()
# Run the defult genetic algorithm # Run the default genetic algorithm
ga.evolve() ga.evolve()
``` ```
### Output:
```Python # Output:
print("Output HERE") ```python
Put the out here
``` ```
## How to use EasyGA: ## Different version that is more customized:
```python ```python
import random import random
import EasyGA import EasyGA
# Setup the default genetic algorithm
ga = EasyGA.GA()
# User set sizes
ga.population_size = 10
ga.chromosome_length = 10
ga.generations = 10
# The user defined gene function # The user defined gene function
def user_gene_function(): def user_gene_function():
return random.randint(1, 100) pass
# The user defined Fitness Function # The user defined Fitness function that gives the chromosome some kind of fitness
def user_fitness_function(chromosome): def user_fitness_function(chromosome):
pass pass
# Standard user size requirements # The user defined initialization function
Population_size = 10 def user_initialization_function():
Chromosome_length = 10 pass
# Create the Genetic algorithm # User sets the gene function
ga = EasyGA.GA(Population_size, Chromosome_length,user_gene_function,user_fitness_function) ga.gene = user_gene_function
ga.initialize() # Set the fitness functions
ga.fitness = user_fitness_function
# Changing the initialization function.
ga.initialization = user_initialization_function
# Run the customized genetic algorithm
ga.eveolve()
```
# Output:
```python
Put the out here
``` ```
### Getting your Genes and Chromosomes from the population: ### Getting your Genes and Chromosomes from the population:

View File

@ -62,20 +62,56 @@ class population:
self.chromosomes.append(chromosome) self.chromosomes.append(chromosome)
class GA: class GA:
def __init__(self, population_size, chromosome_length, def __init__(self):
user_gene_function,user_fitness_function): # Default variables
# User defined variables self.population_size = defaults.generations
self.population_size = population_size self.chromosome_length = defaults.chromosome_length
self.chromosome_length = chromosome_length self.generations = defaults.generations
self.user_gene_function = user_gene_function # Defualt ga implimentation structure
# setup required variables self.create_gene = defaults.default_gene_function()
self.population = [] self.initialization = defaults.default_initialize()
# Setup ga implimentation structure self.mutation = defaults.default_mutations_function()
self.initialization_impl = random_initialization() self.selection = defaults.default_selection_function()
self.crossover = defaults.default_crossover_function()
self.termination = defaults.default_termination_function(self.generations)
self.fitness_function = defaults.default_fitness_function()
def initialize(self): def initialize(self):
# Create the initial population # Create the initial population
self.population = self.initialization_impl.initialize(self.population_size, self.population = self.initialization.initialize(self.population_size,
self.chromosome_length, self.chromosome_length,
self.user_gene_function) self.user_gene_function)
def evolve(self):
# Evolve will run all the functions
initialize()
class defaults(self):
def __init__(self):
self.generations = 3
self.chromosome_length = 4
self.population_size = 5
self.mutation_rate = 0.03
def default_gene_function():
return random.randint(1, 100)
def default_fitness_function():
pass
def default_initialize_functio():
return random_initialization()
def default_selection_function():
return tournament_selection()
def default_crossover_function():
return fast_single_point_crossover()
def default_mutations_function():
return per_gene_mutation()
def default_termination_function(generations):
return generation_termination(generations)