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.
This commit is contained in:
RyleyGG
2020-09-25 11:14:09 -04:00
parent 5821e709a3
commit 129925bbdd
2 changed files with 13 additions and 11 deletions

View File

@ -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(

View File

@ -3,7 +3,8 @@ 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()