Code optimizations, float-range implementation
Random gene initialization now supports float ranges (assumed by default if gene input includes float). Backend was also optimized and cleaned up greatly.
This commit is contained in:
@ -38,21 +38,17 @@ class GA:
|
||||
|
||||
#assuming domain if string (strings can never be range)
|
||||
for x in range(len(self.gene_input)):
|
||||
if isinstance(gene_input[x], int):
|
||||
if isinstance(gene_input[x], list) == False:
|
||||
self.gene_input[x] = [self.gene_input[x], self.gene_input[x]]
|
||||
|
||||
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[x] = "domain"
|
||||
break
|
||||
elif y == (len(self.gene_input[x]) -1):
|
||||
self.gene_input_type[x] = "range"
|
||||
else:
|
||||
if isinstance(gene_input[x], str):
|
||||
if self.gene_input_type[x] == None: #If it hasn't been hard-set by the user
|
||||
for y in range(len(self.gene_input[x])):
|
||||
if isinstance(gene_input[x][y], str):
|
||||
self.gene_input_type[x] = "domain"
|
||||
else:
|
||||
break
|
||||
elif isinstance(gene_input[x][y], float):
|
||||
self.gene_input_type[x] = "float-range"
|
||||
elif y == (len(self.gene_input[x]) -1 and self.gene_input_type[x] != "float-range"):
|
||||
self.gene_input_type[x] = "range"
|
||||
|
||||
|
||||
|
||||
@ -10,18 +10,13 @@ def check_values(low,high):
|
||||
assert high != 0, "High value can not be zero"
|
||||
|
||||
def random_gene(gene_input, gene_input_type, gene_index):
|
||||
|
||||
created_gene = None
|
||||
#Determining if single range/domain or index-dependent
|
||||
if isinstance(gene_input[0], list):
|
||||
if gene_input_type[gene_index] == "range":
|
||||
created_gene = random.randint(gene_input[gene_index][0], gene_input[gene_index][1])
|
||||
elif gene_input_type[gene_index] == "domain":
|
||||
created_gene = random.choice(gene_input[gene_index])
|
||||
else:
|
||||
if gene_input_type[gene_index] == "range":
|
||||
created_gene = random.randint(gene_input[0], gene_input[1])
|
||||
elif gene_input_type[gene_index] == "domain":
|
||||
created_gene = random.choice(gene_input)
|
||||
if gene_input_type[gene_index] == "range":
|
||||
created_gene = random.randint(gene_input[gene_index][0], gene_input[gene_index][1])
|
||||
elif gene_input_type[gene_index] == "domain":
|
||||
created_gene = random.choice(gene_input[gene_index])
|
||||
elif gene_input_type[gene_index] == "float-range":
|
||||
created_gene = random.uniform(gene_input[gene_index][0], gene_input[gene_index][1])
|
||||
|
||||
return created_gene
|
||||
|
||||
@ -1,11 +1,19 @@
|
||||
import EasyGA
|
||||
import random
|
||||
|
||||
#1. GA should take in range for gene input
|
||||
#2. GA should take in index-dependent range for gene input
|
||||
#3. GA should take in domain input
|
||||
#4. GA should take in index-dependent domain for gene input
|
||||
#5. GA should accept mix of range and domain for gene input
|
||||
|
||||
|
||||
# Create the Genetic algorithm
|
||||
ga = EasyGA.GA()
|
||||
test_range_two = [["left", "right"],[22,35],5,[22,"up"]]
|
||||
ga.gene_input_type[2] = "domain"
|
||||
ga.initialize(test_range_two)
|
||||
test_gene_input = [["left", "right"],[1,100],[5.0,10],[22,"up"]]
|
||||
#ga.gene_input_type[1] = "domain"
|
||||
#ga.gene_input_type[1] = "float-range"
|
||||
ga.initialize(test_gene_input)
|
||||
ga.population.print_all()
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user