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:
@ -21,6 +21,9 @@ class GA:
|
|||||||
self.gene_function_impl = random_gene
|
self.gene_function_impl = random_gene
|
||||||
self.gene_input = []
|
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)?
|
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
|
# Set the GA Configuration
|
||||||
self.initialization_impl = random_initialization
|
self.initialization_impl = random_initialization
|
||||||
#self.mutation_impl = PerGeneMutation(Mutation_rate)
|
#self.mutation_impl = PerGeneMutation(Mutation_rate)
|
||||||
@ -34,26 +37,24 @@ class GA:
|
|||||||
self.gene_input = gene_input
|
self.gene_input = gene_input
|
||||||
|
|
||||||
#assuming domain if string (strings can never be range)
|
#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)):
|
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])):
|
||||||
if isinstance(gene_input[x][y], str):
|
if isinstance(gene_input[x][y], str):
|
||||||
self.gene_input_type.append("domain")
|
self.gene_input_type[x] = "domain"
|
||||||
break
|
break
|
||||||
elif y == (len(self.gene_input[x]) -1):
|
elif y == (len(self.gene_input[x]) -1):
|
||||||
self.gene_input_type.append("range")
|
self.gene_input_type[x] = "range"
|
||||||
else:
|
else:
|
||||||
if isinstance(gene_input[x], str):
|
if isinstance(gene_input[x], str):
|
||||||
self.gene_input_type.append("domain")
|
self.gene_input_type[x] = "domain"
|
||||||
else:
|
else:
|
||||||
if isinstance(gene_input[x], int):
|
if isinstance(gene_input[x], int):
|
||||||
self.gene_input[x] = [self.gene_input[x], self.gene_input[x]]
|
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
|
# Create the first population
|
||||||
self.population = self.initialization_impl(
|
self.population = self.initialization_impl(
|
||||||
|
|||||||
@ -3,9 +3,10 @@ import random
|
|||||||
|
|
||||||
# Create the Genetic algorithm
|
# Create the Genetic algorithm
|
||||||
ga = EasyGA.GA()
|
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.initialize(test_range_two)
|
||||||
ga.population.print_all()
|
ga.population.print_all()
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user