From 129925bbdd8f77103535fb6fa45f4e7a647959ba Mon Sep 17 00:00:00 2001 From: RyleyGG Date: Fri, 25 Sep 2020 11:14:09 -0400 Subject: [PATCH] Cleaned up backend & user interaction with EasyGA In the initial commit, string inputs would implicitly be seen as domain, and all integer inputs would be seen as range. If the user wanted to assign any integer inputs as domain, they would have to call the entire gene_input_type, even if only to change a single element to domain. It has now been updated to where the user can specifically call the element they want to update. The testing file new_initialization_method_testing.py reflects this. --- src/EasyGA.py | 19 ++++++++++--------- src/new_initialization_method_testing.py | 5 +++-- 2 files changed, 13 insertions(+), 11 deletions(-) diff --git a/src/EasyGA.py b/src/EasyGA.py index 75b7986..9f367b8 100644 --- a/src/EasyGA.py +++ b/src/EasyGA.py @@ -21,6 +21,9 @@ class GA: self.gene_function_impl = random_gene self.gene_input = [] self.gene_input_type = [] #What if user gives two numbers (i.e. [1,100]) but wants to pick between the two (domain)? + while len(self.gene_input_type) != self.chromosome_length: + self.gene_input_type.append(None) + # Set the GA Configuration self.initialization_impl = random_initialization #self.mutation_impl = PerGeneMutation(Mutation_rate) @@ -34,26 +37,24 @@ class GA: self.gene_input = gene_input #assuming domain if string (strings can never be range) - if self.gene_input_type == []: - for x in range(len(self.gene_input)): + for x in range(len(self.gene_input)): + if self.gene_input_type[x] == None: if (isinstance(self.gene_input[x], list)): for y in range(len(self.gene_input[x])): if isinstance(gene_input[x][y], str): - self.gene_input_type.append("domain") + self.gene_input_type[x] = "domain" break elif y == (len(self.gene_input[x]) -1): - self.gene_input_type.append("range") + self.gene_input_type[x] = "range" else: if isinstance(gene_input[x], str): - self.gene_input_type.append("domain") + self.gene_input_type[x] = "domain" else: if isinstance(gene_input[x], int): self.gene_input[x] = [self.gene_input[x], self.gene_input[x]] - self.gene_input_type.append("range") + self.gene_input_type[x] = "range" - #If length doesn't correspond to chromosome, update here - while len(self.gene_input_type) != self.chromosome_length: - self.gene_input_type.append(self.gene_input_type[len(self.gene_input_type)-1]) + # Create the first population self.population = self.initialization_impl( diff --git a/src/new_initialization_method_testing.py b/src/new_initialization_method_testing.py index 8687dd0..2cbe4d2 100644 --- a/src/new_initialization_method_testing.py +++ b/src/new_initialization_method_testing.py @@ -3,9 +3,10 @@ import random # Create the Genetic algorithm ga = EasyGA.GA() -test_range_two = [["left", "right"],[22,88],5,[22,"up"]] +test_range_two = [["left", "right"],[22,35],5,[22,"up"]] +ga.gene_input_type[1] = "domain" ga.initialize(test_range_two) -ga.population.print_all() +ga.population.print_all()