From e84483cf8b1d0e7da2ef0e9434704fdd96e027f6 Mon Sep 17 00:00:00 2001 From: danielwilczak101 <44122838+danielwilczak101@users.noreply.github.com> Date: Mon, 21 Sep 2020 20:26:21 -0400 Subject: [PATCH] Major chnages to structure --- README.md | 47 ++++++++++++++++++++++++++++------------ src/EasyGA.py | 60 ++++++++++++++++++++++++++++++++++++++++----------- 2 files changed, 81 insertions(+), 26 deletions(-) diff --git a/README.md b/README.md index f8b7f17..1b55c95 100644 --- a/README.md +++ b/README.md @@ -14,36 +14,55 @@ pip3 install EasyGA ```Python import EasyGA -# Setup the defult genetic algorithm +# Setup the default genetic algorithm ga = EasyGA.GA() -# Run the defult genetic algorithm +# Run the default genetic algorithm ga.evolve() ``` -### Output: -```Python -print("Output HERE") + +# Output: +```python +Put the out here ``` -## How to use EasyGA: +## Different version that is more customized: ```python import random 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 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): pass -# Standard user size requirements -Population_size = 10 -Chromosome_length = 10 +# The user defined initialization function +def user_initialization_function(): + pass -# Create the Genetic algorithm -ga = EasyGA.GA(Population_size, Chromosome_length,user_gene_function,user_fitness_function) -ga.initialize() +# User sets the gene function +ga.gene = user_gene_function +# 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: diff --git a/src/EasyGA.py b/src/EasyGA.py index f3c0672..2d4d519 100644 --- a/src/EasyGA.py +++ b/src/EasyGA.py @@ -62,20 +62,56 @@ class population: self.chromosomes.append(chromosome) class GA: - def __init__(self, population_size, chromosome_length, - user_gene_function,user_fitness_function): - # User defined variables - self.population_size = population_size - self.chromosome_length = chromosome_length - self.user_gene_function = user_gene_function - # setup required variables - self.population = [] - # Setup ga implimentation structure - self.initialization_impl = random_initialization() - + def __init__(self): + # Default variables + self.population_size = defaults.generations + self.chromosome_length = defaults.chromosome_length + self.generations = defaults.generations + # Defualt ga implimentation structure + self.create_gene = defaults.default_gene_function() + self.initialization = defaults.default_initialize() + self.mutation = defaults.default_mutations_function() + 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): # 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.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)