Updated gene input checks

Updated the check of incoming data to ensure validity - if the user enters a single digit, say "5", it will automatically be converted to a list like [5,5]. This already worked before with range, but it now works with domain as well.
This commit is contained in:
RyleyGG
2020-09-25 11:24:47 -04:00
parent 129925bbdd
commit ed1b2bbe03
2 changed files with 4 additions and 3 deletions

View File

@ -38,6 +38,9 @@ class GA:
#assuming domain if string (strings can never be range) #assuming domain if string (strings can never be range)
for x in range(len(self.gene_input)): for x in range(len(self.gene_input)):
if isinstance(gene_input[x], int):
self.gene_input[x] = [self.gene_input[x], self.gene_input[x]]
if self.gene_input_type[x] == None: if self.gene_input_type[x] == None:
if (isinstance(self.gene_input[x], list)): if (isinstance(self.gene_input[x], list)):
for y in range(len(self.gene_input[x])): for y in range(len(self.gene_input[x])):
@ -50,8 +53,6 @@ class GA:
if isinstance(gene_input[x], str): if isinstance(gene_input[x], str):
self.gene_input_type[x] = "domain" self.gene_input_type[x] = "domain"
else: else:
if isinstance(gene_input[x], int):
self.gene_input[x] = [self.gene_input[x], self.gene_input[x]]
self.gene_input_type[x] = "range" self.gene_input_type[x] = "range"

View File

@ -4,7 +4,7 @@ import random
# Create the Genetic algorithm # Create the Genetic algorithm
ga = EasyGA.GA() ga = EasyGA.GA()
test_range_two = [["left", "right"],[22,35],5,[22,"up"]] test_range_two = [["left", "right"],[22,35],5,[22,"up"]]
ga.gene_input_type[1] = "domain" ga.gene_input_type[2] = "domain"
ga.initialize(test_range_two) ga.initialize(test_range_two)
ga.population.print_all() ga.population.print_all()